1
$\begingroup$

I am teaching a multivariate stats class and wanted to give people some simulated datasets with mediation, path modeling, and other features. I know it's possible to run monte carlo studies in MPlus, but does anyone know any good ways to save a single such dataset to a CSV file? Or how to do it in R? I was hoping to specify a set of multivariate relationships, means, and SDs, and then create the CSV file.

$\endgroup$

closed as off-topic by Nick Cox, usεr11852, Xi'an, mpiktas, Andy May 5 '15 at 10:34

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – Nick Cox, Xi'an, mpiktas, Andy
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Although this is asked in terms of R, if you understand the underlying statistical ideas, the problem (& the code) would be trivial. Hence, I think we could treat this as a statistical (ie on-topic). $\endgroup$ – gung May 5 '15 at 0:55
1
$\begingroup$

Use the mvrnorm function in the MASS package to get a multivariate normal distribution (it comes with base R).

Here's some example code:

library(MASS)
library(ggplot2) # used for plotting, otherwise, it's optional
Sigma <- matrix(c(10,3,3,2),2,2)
var(mvrnorm(n = 1000, rep(0, 2), Sigma))
example <- mvrnorm(n = 1000, rep(0, 2), Sigma)

examplePlot <- qplot(example[,1], example[,2]) +
    xlab(expression(x[1])) +
    ylab(expression(x[2])) 

ggsave("examplePlot.jpg", examplePlot, width = 4, height = 4)
write.csv(x =  example, file = "example.csv",  row.names = FALSE)

That produces this figure: enter image description here

To learn more aboutmvrnorm, type ?mvrnorm to view its help file. Also, many functions in include examples. So, when you find your multivariate analysis functions, type: example(...) and often R will show you how to use it.

Good with your course! Also, if you include the specific types of analysis you are trying to do, we could provide better tips.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.