[R] Simulation in R - Part 2

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[R] Simulation in R - Part 2

Alexander Geisler-3
Hello!

So, the simulation works (drawing 100 samples and then calculate the
model for each sample). Here is the code:

--snip--
# sample size n=200
ergebnisse200 <- rep(0, each=100)
stichproben200 <- vector(“list”, 100)
default200 <- rep(0, each=100)

for (i in seq(1:100)) {
n <- dim(daten)[1]  
ix <- sample(n,200)
samp_i <- daten[ix,]   # draw samples
y <- sum(samp_i$y)     # number of defaults
stichproben200[[i]] <- samp_i  # saving the samples
default200[i] <- y   # saving the number of defaults

# Modell berechnen:
posterior_i <- MCMClogit(y ~ fbl.ind + fekq3 + febitda4 + fuvs + fkru +
fzd + fur3, data=samp_i, b0=prior, B0=precision, tune=0.5) # calculation
ergebnisse200[i] <- summary(posterior_i)  # saving the results
}

# write out the solutions into an excel-file
write.csv2(ergebnisse200, "ergebnisse.csv")
--snip--

My solution has the following form:

http://img296.imageshack.us/my.php?image=ergebnissewa0.jpg

write.csv2 makes the right thing, but in the excel-file, if I open the
csv-file in excel, several objects are next to each other (I'm missing
the line break after each object of the array); look at
http://img67.imageshack.us/my.php?image=ergebnisseexcelbg7.jpg
The problem is that there is an error message by importing the csv in
excel, because there are to many columns needed to import the 100 objects.

So, my question:
Is it possible to write the 100 objects of the array among each other.
Like as it can be seen in R
(http://img296.imageshack.us/my.php?image=ergebnissewa0.jpg)?
Another way is to produce 50 samples in a first turn and then produce 50
samples again in another turn, but this can not be a "clean" solution
(and surely not the only one).

Hopefully you can help me and this is the last question for my simulation.

Thanks for your efforts
Alex

--
Alexander Geisler * Kaltenbach 151 * A-6272 Kaltenbach
email: [hidden email] | [hidden email]
phone: +43 650 / 811 61 90 | skpye: al1405ex

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: [R] Simulation in R - Part 2

PIKAL Petr
Hi

from write.csv help page:
x the object to be written, preferably a matrix or data frame. If
not, it is attempted to coerce x to a data frame.

So array is not a kind of object which can be saved as you want
without some complication. Basically it is a plain vector with dim
attributes and write.csv do its best to coerce it to data frame, but
here you definitely need to do the transformation on your own.

You either shall use list and then transfer it to data frame by

do.call(rbind, the.list) # or something similar

or try to reshape your array e.g. by using melt and cast from reshape
package to the form suitable for data frame transformation.

HTH
Petr



On 7 Dec 2006 at 22:33, Alexander Geisler wrote:

Date sent:       Thu, 07 Dec 2006 22:33:05 +0100
From:           Alexander Geisler <[hidden email]>
To:             [hidden email]
Subject:         [R] Simulation in R - Part 2

> Hello!
>
> So, the simulation works (drawing 100 samples and then calculate the
> model for each sample). Here is the code:
>
> --snip--
> # sample size n=200
> ergebnisse200 <- rep(0, each=100)
> stichproben200 <- vector(‶list※, 100)
> default200 <- rep(0, each=100)
>
> for (i in seq(1:100)) {
> n <- dim(daten)[1]  
> ix <- sample(n,200)
> samp_i <- daten[ix,]   # draw samples
> y <- sum(samp_i$y)     # number of defaults
> stichproben200[[i]] <- samp_i  # saving the samples
> default200[i] <- y   # saving the number of defaults
>
> # Modell berechnen:
> posterior_i <- MCMClogit(y ~ fbl.ind + fekq3 + febitda4 + fuvs + fkru
> + fzd + fur3, data=samp_i, b0=prior, B0=precision, tune=0.5) #
> calculation ergebnisse200[i] <- summary(posterior_i)  # saving the
> results }
>
> # write out the solutions into an excel-file
> write.csv2(ergebnisse200, "ergebnisse.csv")
> --snip--
>
> My solution has the following form:
>
> http://img296.imageshack.us/my.php?image=ergebnissewa0.jpg
>
> write.csv2 makes the right thing, but in the excel-file, if I open the
> csv-file in excel, several objects are next to each other (I'm missing
> the line break after each object of the array); look at
> http://img67.imageshack.us/my.php?image=ergebnisseexcelbg7.jpg The
> problem is that there is an error message by importing the csv in
> excel, because there are to many columns needed to import the 100
> objects.
>
> So, my question:
> Is it possible to write the 100 objects of the array among each other.
> Like as it can be seen in R
> (http://img296.imageshack.us/my.php?image=ergebnissewa0.jpg)? Another
> way is to produce 50 samples in a first turn and then produce 50
> samples again in another turn, but this can not be a "clean" solution
> (and surely not the only one).
>
> Hopefully you can help me and this is the last question for my
> simulation.
>
> Thanks for your efforts
> Alex
>
> --
> Alexander Geisler * Kaltenbach 151 * A-6272 Kaltenbach
> email: [hidden email] | [hidden email]
> phone: +43 650 / 811 61 90 | skpye: al1405ex
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html and provide commented,
> minimal, self-contained, reproducible code.

Petr Pikal
[hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.