Marietta Suarez
2016-Jul-01 17:28 UTC
[R] trouble double looping to generate data for a meta-analysis
i'm trying to generate data for a meta analysis. 1- generate data following a normal distribution, 2- generate data following a skewed distribution, 3- generate data following a logistic distribution. i need to loop this because the # of studies in each meta will be either 10 or 15. k or total number of studies in the meta will be 5. i need to loop twice to repeat this process 10 times. database should be 3 columns (distributions) by 65 rows x 10 reps here's my code, not sure what's not working: library(fGarch) #n reps =10 rep=10 #begin function here, need to vary n and k, when k=2 n=10, when k3 n=15 fun=function(n, k){ #prepare to store data data=matrix(0,nrow=10*k, ncol=3) db=matrix(0,nrow=650, ncol=3) for (j in 1:rep) { for (i in 1:k) { #generate data under normal, skewed, and logistic distributions here data[,1]=rnorm(n, 100, 15) data[,2]=rsnorm(n, 100, 15, 1) data[,3]=rlogis(n, 100, 15) } [j]=db } } save=fun(10,2) Please help!!! [[alternative HTML version deleted]]
Federman, Douglas
2016-Jul-01 19:06 UTC
[R] trouble double looping to generate data for a meta-analysis
You might look at the package Wakefield For data generation -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Marietta Suarez Sent: Friday, July 01, 2016 1:28 PM To: r-help at r-project.org Subject: [R] trouble double looping to generate data for a meta-analysis i'm trying to generate data for a meta analysis. 1- generate data following a normal distribution, 2- generate data following a skewed distribution, 3- generate data following a logistic distribution. i need to loop this because the # of studies in each meta will be either 10 or 15. k or total number of studies in the meta will be 5. i need to loop twice to repeat this process 10 times. database should be 3 columns (distributions) by 65 rows x 10 reps here's my code, not sure what's not working: library(fGarch) #n reps =10 rep=10 #begin function here, need to vary n and k, when k=2 n=10, when k3 n=15 fun=function(n, k){ #prepare to store data data=matrix(0,nrow=10*k, ncol=3) db=matrix(0,nrow=650, ncol=3) for (j in 1:rep) { for (i in 1:k) { #generate data under normal, skewed, and logistic distributions here data[,1]=rnorm(n, 100, 15) data[,2]=rsnorm(n, 100, 15, 1) data[,3]=rlogis(n, 100, 15) } [j]=db } } save=fun(10,2) Please help!!! [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Bert Gunter
2016-Jul-01 22:11 UTC
[R] trouble double looping to generate data for a meta-analysis
Hint: It's much more efficient not to loop and generate random data in a single call only once -- then make your samples. (This can even often be done with different distribution parameters, as in many cases these can also be vactorized) Example: ## 1000 random samples of size 100> set.seed(1122) > samps.norm <- matrix(rnorm(1e5),nrow = 100 ) > dim(samps.norm)[1] 100 1000 ## This was instantaneous on my machine. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Fri, Jul 1, 2016 at 10:28 AM, Marietta Suarez <marietta0423 at gmail.com> wrote:> i'm trying to generate data for a meta analysis. 1- generate data following > a normal distribution, 2- generate data following a skewed distribution, 3- > generate data following a logistic distribution. i need to loop this > because the # of studies in each meta will be either 10 or 15. k or total > number of studies in the meta will be 5. i need to loop twice to repeat > this process 10 times. database should be 3 columns (distributions) by 65 > rows x 10 reps > > > here's my code, not sure what's not working: > library(fGarch) > > #n reps =10 > rep=10 > > #begin function here, need to vary n and k, when k=2 n=10, when k3 n=15 > fun=function(n, k){ > > #prepare to store data > data=matrix(0,nrow=10*k, ncol=3) > db=matrix(0,nrow=650, ncol=3) > > for (j in 1:rep) > { > for (i in 1:k) > { > #generate data under normal, skewed, and logistic distributions here > > data[,1]=rnorm(n, 100, 15) > data[,2]=rsnorm(n, 100, 15, 1) > data[,3]=rlogis(n, 100, 15) > } > [j]=db > } > } > > save=fun(10,2) > > Please help!!! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Michael Dewey
2016-Jul-02 14:04 UTC
[R] trouble double looping to generate data for a meta-analysis
Dear Marietta Comments in-line On 01/07/2016 18:28, Marietta Suarez wrote:> i'm trying to generate data for a meta analysis. 1- generate data following > a normal distribution, 2- generate data following a skewed distribution, 3- > generate data following a logistic distribution. i need to loop this > because the # of studies in each meta will be either 10 or 15. k or total > number of studies in the meta will be 5. i need to loop twice to repeat > this process 10 times. database should be 3 columns (distributions) by 65 > rows x 10 reps > > > here's my code, not sure what's not working: > library(fGarch) > > #n reps =10 > rep=10 > > #begin function here, need to vary n and k, when k=2 n=10, when k3 n=15So you define fun> fun=function(n, k){ > > #prepare to store data > data=matrix(0,nrow=10*k, ncol=3) > db=matrix(0,nrow=650, ncol=3) > > for (j in 1:rep) > { > for (i in 1:k) > { > #generate data under normal, skewed, and logistic distributions here > > data[,1]=rnorm(n, 100, 15) > data[,2]=rsnorm(n, 100, 15, 1) > data[,3]=rlogis(n, 100, 15) > } > [j]=dbAt this point have you actually put anything in db?> } > } >Assuming that is where fun ends I think you meant to precede it with data It is not a good idea to use data as the name of your data as it is used by R itself, db would have been fine though.> save=fun(10,2) > > Please help!!! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >-- Michael http://www.dewey.myzen.co.uk/home.html
Jim Lemon
2016-Jul-03 11:01 UTC
[R] trouble double looping to generate data for a meta-analysis
Hi Marietta, You may not be aware that the variable k is doing nothing in your example except running the random variable generation 2 or 3 times for each cycle of the outer loop as each successive run just overwrites the one before. If you want to include all two or three lots of values you will have to do something like this: db<-list() gen_sample<-function(n,k) { for(m in 1:length(n)) { for(j in 1:n[m]) { for(i in 1:k[m]) { dbindx<-i+(j-1)*k[m]+(m-1)*(n[m]+k[1]+k[2]) db[[dbindx]]<- matrix(c(rnorm(n[m], 100, 15), rsnorm(n[m], 100, 15), rlogis(n[m], 100, 15)),ncol=3) } } } return(db) } gen_sample(c(10,15),c(2,3)) Jim On Sat, Jul 2, 2016 at 3:28 AM, Marietta Suarez <marietta0423 at gmail.com> wrote:> i'm trying to generate data for a meta analysis. 1- generate data following > a normal distribution, 2- generate data following a skewed distribution, 3- > generate data following a logistic distribution. i need to loop this > because the # of studies in each meta will be either 10 or 15. k or total > number of studies in the meta will be 5. i need to loop twice to repeat > this process 10 times. database should be 3 columns (distributions) by 65 > rows x 10 reps > > > here's my code, not sure what's not working: > library(fGarch) > > #n reps =10 > rep=10 > > #begin function here, need to vary n and k, when k=2 n=10, when k3 n=15 > fun=function(n, k){ > > #prepare to store data > data=matrix(0,nrow=10*k, ncol=3) > db=matrix(0,nrow=650, ncol=3) > > for (j in 1:rep) > { > for (i in 1:k) > { > #generate data under normal, skewed, and logistic distributions here > > data[,1]=rnorm(n, 100, 15) > data[,2]=rsnorm(n, 100, 15, 1) > data[,3]=rlogis(n, 100, 15) > } > [j]=db > } > } > > save=fun(10,2) > > Please help!!! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.