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
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. Charles.Annis at StatisticalEngineering.com 561-352-9699 http://www.StatisticalEngineering.com -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Shane Phillips Sent: Friday, April 15, 2011 2:50 PM To: r-help at r-project.org 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 ______________________________________________ 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.
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 <SPhillips at lexington1.net> 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 > > ______________________________________________ > 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. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org
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 <SPhillips at Lexington1.net> Date: Fri, 15 Apr 2011 11:49:48 -0700 To: "r-help at r-project.org" <r-help at r-project.org> 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 > >______________________________________________ >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.
Maybe Matching Threads
- Converting a categorical variable to multiple dichotemous variables
- Help on calculating a variable using random numbers
- Random sample from a data frame where ID column values don't match the values in an ID column in a second data frame
- computing marginal values based on multiple columns?
- How to Store the executed values in a dataframe & rle function