PSA: R’s rnorm() and mvrnorm() use different spreads
Quick public service announcement for my fellow R nerds:
R has two commonly-used random-Normal generators:
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!
offers daily e-mail updates
news and tutorials
on topics such as: Data science
, Big Data, R jobs
, visualization (ggplot2
), programming (RStudio
, Web Scraping
) statistics (regression
, time series
) and more...
If you got this far, why not subscribe for updates
from the site? Choose your flavor: e-mail
, or facebook