# Monte Carlo Simulation

8 messages
Open this post in threaded view
|
Report Content as Inappropriate

## Monte Carlo Simulation

 Hello, R friends... I am very new to R, and I need some help.  I am trying to construct a simulation for my dissertation. I need to create 1000 datasets of 1000 subjects with the following variables... Treatment variable - Drawn from a binomial distribution (1 run, prob=.13) Covariate 1 - Drawn from a normal distribution (mean=100, sd=16) Covariate 2 - Drawn from a normal distribution (mean=200, sd=9) Covariates 1 and 2 need to be correlated (say, r=.80) Covariate 3 - Drawn from a binomial distribution (1 run, prob=.5) Covariate 4 - Drawn from a distribution of discrete variables where 1 has an 80% chance of being selected, 2  - 10%, 3 - 5% and 4 - 5%.  This variable would need to be recoded into 4 binary variables. Covariate 5 - Drawn from a normal distribution (mean=84, sd=2) Covariate 6 - Drawn from a binomial distribution (1 run, prob=.15) Covariate 6 needs to correlate with Covariate 2 (r=.70, or so) I need each dataset saved as a new datafile with an iterative filename (e.g. sample1, sample2, etc.). Please help! Thanks! Shane ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Monte Carlo Simulation

 What have you tried so far? It is often helpful to begin with a much simpler problem, then add complexity incrementally until you've constructed the desired model. Best wishes. Charles Annis, P.E. [hidden email] 561-352-9699 http://www.StatisticalEngineering.com-----Original Message----- From: [hidden email] [mailto:[hidden email]] On Behalf Of Shane Phillips Sent: Friday, April 15, 2011 2:50 PM To: [hidden email] Subject: [R] Monte Carlo Simulation Hello, R friends... I am very new to R, and I need some help.  I am trying to construct a simulation for my dissertation. I need to create 1000 datasets of 1000 subjects with the following variables... Treatment variable - Drawn from a binomial distribution (1 run, prob=.13) Covariate 1 - Drawn from a normal distribution (mean=100, sd=16) Covariate 2 - Drawn from a normal distribution (mean=200, sd=9) Covariates 1 and 2 need to be correlated (say, r=.80) Covariate 3 - Drawn from a binomial distribution (1 run, prob=.5) Covariate 4 - Drawn from a distribution of discrete variables where 1 has an 80% chance of being selected, 2  - 10%, 3 - 5% and 4 - 5%.  This variable would need to be recoded into 4 binary variables. Covariate 5 - Drawn from a normal distribution (mean=84, sd=2) Covariate 6 - Drawn from a binomial distribution (1 run, prob=.15) Covariate 6 needs to correlate with Covariate 2 (r=.70, or so) I need each dataset saved as a new datafile with an iterative filename (e.g. sample1, sample2, etc.). Please help! Thanks! Shane ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Monte Carlo Simulation

 Here's a script of what I have so far.  I have a few problems.  First, the correlations.  Next, recoding that categorical variable into dichotomous variables.  Finally, the iterative filename thing. Thanks! S -----Original Message----- From: Charles Annis, P.E. [mailto:[hidden email]] Sent: Friday, April 15, 2011 3:01 PM To: Shane Phillips; [hidden email] Subject: RE: [R] Monte Carlo Simulation What have you tried so far? It is often helpful to begin with a much simpler problem, then add complexity incrementally until you've constructed the desired model. Best wishes. Charles Annis, P.E. [hidden email] 561-352-9699 http://www.StatisticalEngineering.com-----Original Message----- From: [hidden email] [mailto:[hidden email]] On Behalf Of Shane Phillips Sent: Friday, April 15, 2011 2:50 PM To: [hidden email] Subject: [R] Monte Carlo Simulation Hello, R friends... I am very new to R, and I need some help.  I am trying to construct a simulation for my dissertation. I need to create 1000 datasets of 1000 subjects with the following variables... Treatment variable - Drawn from a binomial distribution (1 run, prob=.13) Covariate 1 - Drawn from a normal distribution (mean=100, sd=16) Covariate 2 - Drawn from a normal distribution (mean=200, sd=9) Covariates 1 and 2 need to be correlated (say, r=.80) Covariate 3 - Drawn from a binomial distribution (1 run, prob=.5) Covariate 4 - Drawn from a distribution of discrete variables where 1 has an 80% chance of being selected, 2  - 10%, 3 - 5% and 4 - 5%.  This variable would need to be recoded into 4 binary variables. Covariate 5 - Drawn from a normal distribution (mean=84, sd=2) Covariate 6 - Drawn from a binomial distribution (1 run, prob=.15) Covariate 6 needs to correlate with Covariate 2 (r=.70, or so) I need each dataset saved as a new datafile with an iterative filename (e.g. sample1, sample2, etc.). Please help! Thanks! Shane ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Monte Carlo Simulation

 In reply to this post by Shane Phillips Hi Shane, See ?rbinom, ?rnorm, ?mvrnorm (in the MASS package), ?sample, ?for and ?write.table Best, Ista On Fri, Apr 15, 2011 at 2:49 PM, Shane Phillips <[hidden email]> wrote: > Hello, R friends... > > I am very new to R, and I need some help.  I am trying to construct a simulation for my dissertation. > > I need to create 1000 datasets of 1000 subjects with the following variables... > > Treatment variable - Drawn from a binomial distribution (1 run, prob=.13) > Covariate 1 - Drawn from a normal distribution (mean=100, sd=16) > Covariate 2 - Drawn from a normal distribution (mean=200, sd=9) > Covariates 1 and 2 need to be correlated (say, r=.80) > Covariate 3 - Drawn from a binomial distribution (1 run, prob=.5) > Covariate 4 - Drawn from a distribution of discrete variables where 1 has an 80% chance of being selected, 2  - 10%, 3 - 5% and 4 - 5%.  This variable would need to be recoded into 4 binary variables. > Covariate 5 - Drawn from a normal distribution (mean=84, sd=2) > Covariate 6 - Drawn from a binomial distribution (1 run, prob=.15) > Covariate 6 needs to correlate with Covariate 2 (r=.70, or so) > > I need each dataset saved as a new datafile with an iterative filename (e.g. sample1, sample2, etc.). > > Please help! > > Thanks! > > Shane > > ______________________________________________ > [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. > -- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Monte Carlo Simulation

 In reply to this post by Shane Phillips On 15 April 2011 12:03, Shane Phillips <[hidden email]> wrote: > Here's a script of what I have so far.  I have a few problems.  First, the correlations.  Next, recoding that categorical variable into dichotomous variables.  Finally, the iterative filename thing. >  Where? Perhaps give the list one question at a time? Here's a start for one of the questions, selected (almost) at random. Give 80% of people a score of 1 on a x1, and 20% of people zero. tempVar <- runif(1000) x1 <- ifelse(tempVar < 0.8, 1, 0) rm(tempVar) Tell us how far you've got.  Do you need to know about write.table() for the saving the files, or is the problem with splitting a large file, or generating the 1000 names ... Jeremy -- Jeremy Miles Psychology Research Methods Wiki: www.researchmethodsinpsychology.com ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Monte Carlo Simulation

 In reply to this post by Charles Annis, P.E. I apologize for my last post.  here is the script I forgot to paste! subject=1:1000 treat=rbinom(1*1000,1,.13) gender=rbinom(1*1000,1,.5) eth=runif(1*1000, min=1, max=4) cogat=rnorm(1*1000, 100, 16) map=rnorm(1*1000, 200, 9) growth=0 simtest=data.frame (subject=subject, treat=treat, gender=gender, eth=round(eth,digits=0), cogat=round(cogat,digits=0),map=round(map,digits=0),growth) simtest<-transform(simtest, growth=rnorm(1000,m=ifelse(treat==0,0.1,0.5),s=0.03)) simtest Once again the issues are the correlations, the recoding of the categorical variable and the iterations. Thanks to all who have helped so far!  You are all so smart! S ________________________________________ From: Charles Annis, P.E. [[hidden email]] Sent: Friday, April 15, 2011 3:00 PM To: Shane Phillips; [hidden email] Subject: RE: [R] Monte Carlo Simulation What have you tried so far? It is often helpful to begin with a much simpler problem, then add complexity incrementally until you've constructed the desired model. Best wishes. Charles Annis, P.E. [hidden email] 561-352-9699 http://www.StatisticalEngineering.com-----Original Message----- From: [hidden email] [mailto:[hidden email]] On Behalf Of Shane Phillips Sent: Friday, April 15, 2011 2:50 PM To: [hidden email] Subject: [R] Monte Carlo Simulation Hello, R friends... I am very new to R, and I need some help.  I am trying to construct a simulation for my dissertation. I need to create 1000 datasets of 1000 subjects with the following variables... Treatment variable - Drawn from a binomial distribution (1 run, prob=.13) Covariate 1 - Drawn from a normal distribution (mean=100, sd=16) Covariate 2 - Drawn from a normal distribution (mean=200, sd=9) Covariates 1 and 2 need to be correlated (say, r=.80) Covariate 3 - Drawn from a binomial distribution (1 run, prob=.5) Covariate 4 - Drawn from a distribution of discrete variables where 1 has an 80% chance of being selected, 2  - 10%, 3 - 5% and 4 - 5%.  This variable would need to be recoded into 4 binary variables. Covariate 5 - Drawn from a normal distribution (mean=84, sd=2) Covariate 6 - Drawn from a binomial distribution (1 run, prob=.15) Covariate 6 needs to correlate with Covariate 2 (r=.70, or so) I need each dataset saved as a new datafile with an iterative filename (e.g. sample1, sample2, etc.). Please help! Thanks! Shane ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Monte Carlo Simulation

 In reply to this post by Jeremy Miles-2 See below: -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 -----Original Message----- From: Jeremy Miles <[hidden email]> Date: Fri, 15 Apr 2011 14:17:13 -0700 To: Shane Phillips <[hidden email]> Cc: "[hidden email]" <[hidden email]> Subject: Re: [R] Monte Carlo Simulation >On 15 April 2011 12:03, Shane Phillips <[hidden email]> wrote: >> Here's a script of what I have so far.  I have a few problems.  First, >>the correlations.  Next, recoding that categorical variable into >>dichotomous variables.  Finally, the iterative filename thing. >> > > Where? > >Perhaps give the list one question at a time? > >Here's a start for one of the questions, selected (almost) at random. >Give 80% of people a score of 1 on a x1, and 20% of people zero. > >tempVar <- runif(1000) >x1 <- ifelse(tempVar < 0.8, 1, 0) >rm(tempVar) x1 <- rbinom(1000,1,0.8) > > >Tell us how far you've got.  Do you need to know about write.table() >for the saving the files, or is the problem with splitting a large >file, or generating the 1000 names ... > > >Jeremy > > > >-- >Jeremy Miles >Psychology Research Methods Wiki: www.researchmethodsinpsychology.com > >______________________________________________ >[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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Monte Carlo Simulation

 In reply to this post by Shane Phillips The filenames can be done within a loop, like this: for (id in 1:1000) {   ## the filename   fname <- paste('sample', formatC(id,width=4,flag='0'),'.tsv',sep='')    ## more stuff } -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 -----Original Message----- From: Shane Phillips <[hidden email]> Date: Fri, 15 Apr 2011 11:49:48 -0700 To: "[hidden email]" <[hidden email]> Subject: [R] Monte Carlo Simulation >Hello, R friends... > >I am very new to R, and I need some help.  I am trying to construct a >simulation for my dissertation. > >I need to create 1000 datasets of 1000 subjects with the following >variables... > >Treatment variable - Drawn from a binomial distribution (1 run, prob=.13) >Covariate 1 - Drawn from a normal distribution (mean=100, sd=16) >Covariate 2 - Drawn from a normal distribution (mean=200, sd=9) >Covariates 1 and 2 need to be correlated (say, r=.80) >Covariate 3 - Drawn from a binomial distribution (1 run, prob=.5) >Covariate 4 - Drawn from a distribution of discrete variables where 1 has >an 80% chance of being selected, 2  - 10%, 3 - 5% and 4 - 5%.  This >variable would need to be recoded into 4 binary variables. >Covariate 5 - Drawn from a normal distribution (mean=84, sd=2) >Covariate 6 - Drawn from a binomial distribution (1 run, prob=.15) >Covariate 6 needs to correlate with Covariate 2 (r=.70, or so) > >I need each dataset saved as a new datafile with an iterative filename >(e.g. sample1, sample2, etc.). > >Please help! > >Thanks! > >Shane > >______________________________________________ >[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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.