Dear Contributors, I am trying to perform a simulation over sample data, but I need to reproduce the same simulation over 4 groups of data. My ability with for loop is null, in particular related to dimensions as I always get, no matter what I try, "number of items to replace is not a multiple of replacement length" This is what I intend to do: replicate this operation for four times, where the index for the four groups is in the part of the code: datiPc[[1]][,2]. I have to replicate the following code 4 times, where the changing part is in the data from which I pick the sample, the data that are stored in datiPc[[1]][,2]. If I had to use data for the four samples, I would substitute the 1 with a j and replicate a loop four times, but it never worked. My desired final outcome is a matrix with 10000 observations for each couple of extracted samples, i.e. 8 columns of 10000 observations of means. db<-c() # Estrazione dei campioni dai dati di PGG e TRUST estr1 <- c(); estr2 <- c(); m1<-c() m2<-c() tmp1<- data1[[1]][,2]; tmp2<- data2[[2]][,2]; for(i in 1:100){ estr1<-sample(tmp1, 1000, replace = TRUE) estr2<-sample(tmp2, 1000, replace = TRUE) m1[i]<-mean(estr1,na.rm=TRUE) m2[i]<-mean(estr2,na.rm=TRUE) } db<-data.frame(cbind(m1,m2)) Thanks for any help you can provide. Best Regards -- Francesca ------------------------------ [[alternative HTML version deleted]]
R. Michael Weylandt
2011-Nov-13 21:27 UTC
[R] Simulation over data repeatedly for four loops
Perhaps you might want to abstract your code a bit and try something like: X = rnorm(500) # Some Data replicate(1e4, mean(sample(X, 500, replace = T))) Obviously you can set up a loop over your data sets as needed. Michael On Sat, Nov 12, 2011 at 6:46 PM, Francesca <francesca.pancotto at gmail.com> wrote:> Dear Contributors, > > I am trying to perform a simulation over sample data, > > but I need to reproduce the same simulation over 4 groups of data. My > ability with for loop is null, in particular related > > to dimensions as I always get, no matter what I try, > > "number of items to replace is not a multiple of replacement length" > > > This is what I intend to do: replicate this operation for > > four times, where the index for the four groups is in the > > part of the code: datiPc[[1]][,2]. > > I have to replicate the following code 4 times, where the > > changing part is in the data from which I pick the sample, > > the data that are stored in datiPc[[1]][,2]. > > If I had to use data for the four samples, I would substitute the 1 with a > j and replicate a loop four times, but it never worked. > > > My desired final outcome is a matrix with 10000 observations for each > couple of extracted samples, i.e. 8 columns of 10000 observations of means. > > > > db<-c() > > # Estrazione dei campioni dai dati di PGG e TRUST > > estr1 <- c(); > > ? ?estr2 <- c(); > > ? ?m1<-c() > > ? ?m2<-c() > > ? ? ? tmp1<- data1[[1]][,2]; > > ? ? ?tmp2<- data2[[2]][,2]; > > ? ? ? ?for(i in 1:100){ > > estr1<-sample(tmp1, 1000, replace = TRUE) > > ? ? ? ?estr2<-sample(tmp2, 1000, replace = TRUE) > > > ? ? ? ?m1[i]<-mean(estr1,na.rm=TRUE) > > ? ? ? ?m2[i]<-mean(estr2,na.rm=TRUE) > > } > > db<-data.frame(cbind(m1,m2)) > Thanks for any help you can provide. > Best Regards > > -- > > Francesca > ------------------------------ > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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. >