[R] Odd error from mvrnorm?
peter dalgaard
pdalgd at gmail.com
Thu Oct 9 22:57:57 CEST 2014
On 09 Oct 2014, at 18:43 , John Hodgson <john at formby.plus.com> wrote:
> I have tried to generate multivariate normal samples with the following means and co-variances
>
> > mu
> [1] 1.4696642 6.3169666 -3.8702044 0.8411024 -2.6525455 6.1894152
> > Sigma
> [,1] [,2] [,3] [,4] [,5] [,6]
> [1,] 0.00015768570 0.002258112 -0.0007021312 0.007184825 -0.00009748966 -0.2297289
> [2,] 0.00225811184 0.456999600 -0.5582839996 -1.296776834 0.97284350454 -5.1210395
> [3,] -0.00070213121 -0.558284000 1.2288752207 -0.333759058 -2.54049486833 4.5709810
> [4,] 0.00718482510 -1.296776834 -0.3337590577 3443.775312311 0.25065256727 -11.5361060
> [5,] -0.00009748966 0.972843505 -2.5404948683 0.250652567 6.42422097236 -15.2357478
> [6,] -0.22972890073 -5.121039455 4.5709810480 -11.536106031 -15.23574783025 422.4494214
> >
> But get the following error message
>
> > mvrnorm(n=1,mu=mu,Sigma=Sigma)
> Error in drop(mu) + eS$vectors %*% diag(sqrt(pmax(ev, 0)), p) %*% t(X) :
> non-conformable arrays
>
> I can see that the values in Sigma are extremely variable in magnitude, but the matrix is not singular,
> and the error message does not seem to point in this direction.
>
> Can anyone see what I'm missing, or suggest a line of attack?
Well, you could set options(error=recover) and have a look the components of the failing expression.
For a workaround, I'd try something like generating variates from the correlation matrix and
scaling by the SD.
Given that the diagonal elements vary by a factor of ~5e-8, it is actually surprising that there is no protests about hitting the tolerance of 1e-6, but that only kicks in when checking for _negative_ eigenvalues.
I can't seem to reproduce this, so you might need to state your R version etc. and provide some reproducible code.
- Peter D.
>
> Thanks
>
> John Hodgson
>
> ______________________________________________
> 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.
--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
More information about the R-help
mailing list