This question is motivated by my question on meta-analysismy question on meta-analysis. But I imagine that it would also be useful in teaching contexts where you want to create a dataset that exactly mirrors an existing published dataset.
I know how to generate random data from a given distribution. So for example, if I read about the results of a study that had:
- a mean of 102,
- a standard deviation of 5.2 , and
- a sample size of 72.
I could generate similar data using rnorm
in R. For example,
set.seed(1234)
x <- rnorm(n=72, mean=102, sd=5.2)
Of course the mean and SD would not be exactly equal to 102 and 5.2 respectively:
round(c(n=length(x), mean=mean(x), sd=sd(x)), 2)
## n mean sd
## 72.00 100.58 5.25
In general I'm interested in how to simulate data that satisfies a set of constraints. In the above case, the constaints are sample size, mean, and standard deviation. In other cases, there might be additional constraints. For example,
- a minimum and a maximum in either the data or the underlying variable might be known.
- the variable might be known to take on only integer values or only non-negative values.
- the data might include multiple variables with known inter-correlations.
Questions
- In general, how can I simulate data that exactly satisfies a set of constraints?
- Are there articles written about this? Are there any programs in R that do this?
- For the sake of example, how could and should I simulate a variable so that it has a specific mean and sd?