(This article was first published on R – Civil Statistician, and kindly contributed to R-bloggers)
Quick public service announcement for my fellow R nerds:
R has two commonly-used random-Normal generators: rnorm and MASS::mvrnorm. I was foolish and assumed that their parameterizations were equivalent when you’re generating univariate data. But nope:
- Base R can generate univariate draws with rnorm(n, mean, sd), which uses the standard deviation for the spread.
- The MASS package has a multivariate equivalent, mvrnorm(n, mu, Sigma), which uses the variance-covariance matrix for the spread. In the univariate case, Sigma is the variance.
I was using mvrnorm to generate a univariate random variable, but giving it the standard deviation instead of the variance. It took me two weeks of debugging to find this problem.
Dear reader, I hope this cautionary tale reminds you to check R function arguments carefully!