An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20040202/14c190d7/attachment.pl
mvrnorm problem
5 messages · Stuart V Jordan, Peter Dalgaard, (Ted Harding) +2 more
Stuart V Jordan <sjordan at princeton.edu> writes:
mvrnorm(n = 1000,B,V)
Error in mu + eS$vectors %*% diag(sqrt(pmax(ev, 0)), p) %*% t(X) :
non-conformable arrays
mvrnorm(n = 1000,t(B),V)
Error in mu + eS$vectors %*% diag(sqrt(pmax(ev, 0)), p) %*% t(X) :
non-conformable arrays
You might, for at least two good reasons, have said that this is from library(MASS). The point is that
mvrnorm(n=10,matrix(c(1,1),1,2),diag(2))
Error in mu + eS$vectors %*% diag(sqrt(pmax(ev, 0)), p) %*% t(X) :
non-conformable arrays
mvrnorm(n=10,matrix(c(1,1),2,1),diag(2))
Error in mu + eS$vectors %*% diag(sqrt(pmax(ev, 0)), p) %*% t(X) :
non-conformable arrays
mvrnorm(n=10,c(1,1),diag(2))
[,1] [,2] [1,] 0.5005327 1.1919216 [2,] 2.8273925 2.7004788 [3,] 2.6493970 1.1304274 .... and the docs quite clearly say that mu wants to be a vector, not a matrix. Curiously enough, this works with rmvnorm from the mvtnorm package by Genz, Bretz, and Hothorn, the difference being that this version adds in the means with a sweep() operation, whereas mvrnorm just adds mu (to the transpose of the ultimate result) and relies on recycling rules. I.e. the point is that
x <- matrix(1:2,1,2) M <- matrix(1:4,2) x+M
Error in x + M : non-conformable arrays
t(x)+M
Error in t(x) + M : non-conformable arrays
c(x)+M
[,1] [,2] [1,] 2 4 [2,] 4 6
sweep(M,1,x,"+")
[,1] [,2] [1,] 2 4 [2,] 4 6
O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
On 02-Feb-04 Stuart V Jordan wrote:
I am trying to simulate draws from a multivariate normal using mvrnorm,
and
am getting the following error message:
Error in mu + eS$vectors %*% diag(sqrt(pmax(ev, 0)), p) %*% t(X) :
non-conformable arrays
[...] Hmmm ... using the same B and V as giben by Stuart Jordan, I get:
mvrnorm(1,B,V)
[1] -179.8332342 0.9632282 2.5687489 -2.2337125 47.2717626 [6] -3745.3844310 0.9632282 0.2839965 -0.1500585 0.5804460 [11] 5.1919420 -4.7381677 -0.2382119
mvrnorm(2,B,V)
Error in mu + eS$vectors %*% diag(sqrt(pmax(ev, 0)), p) %*% t(X) :
non-conformable arrays
?????
Ted.
PS By the way, [near] singularity seems to have nothing to do
with it here:
If I brutally make V non-singular:
diag(V) <- diag(V)+10000000
it goes the same way.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 167 1972
Date: 02-Feb-04 Time: 22:17:13
------------------------------ XFMail ------------------------------
Stuart V Jordan <sjordan at princeton.edu> writes:
mvrnorm(n = 1000,B,V)
Error in mu + eS$vectors %*% diag(sqrt(pmax(ev, 0)), p) %*% t(X) :
non-conformable arrays
mvrnorm(n = 1000,t(B),V)
Error in mu + eS$vectors %*% diag(sqrt(pmax(ev, 0)), p) %*% t(X) :
non-conformable arrays
You might, for at least two good reasons, have said that this is from library(MASS). The point is that
mvrnorm(n=10,matrix(c(1,1),1,2),diag(2))
Error in mu + eS$vectors %*% diag(sqrt(pmax(ev, 0)), p) %*% t(X) :
non-conformable arrays
mvrnorm(n=10,matrix(c(1,1),2,1),diag(2))
Error in mu + eS$vectors %*% diag(sqrt(pmax(ev, 0)), p) %*% t(X) :
non-conformable arrays
mvrnorm(n=10,c(1,1),diag(2))
[,1] [,2] [1,] 0.5005327 1.1919216 [2,] 2.8273925 2.7004788 [3,] 2.6493970 1.1304274 .... and the docs quite clearly say that mu wants to be a vector, not a matrix. Curiously enough, this works with rmvnorm from the mvtnorm package by Genz, Bretz, and Hothorn, the difference being that this version adds in the means with a sweep() operation, whereas mvrnorm just adds mu (to the transpose of the ultimate result) and relies on recycling rules.
Credits to Fritz: {rd}mvnorm moved from `e1071' to `mvtnorm' for
obvious reasons some time ago.
Best,
Torsten
I.e. the point is that
x <- matrix(1:2,1,2) M <- matrix(1:4,2) x+M
Error in x + M : non-conformable arrays
t(x)+M
Error in t(x) + M : non-conformable arrays
c(x)+M
[,1] [,2] [1,] 2 4 [2,] 4 6
sweep(M,1,x,"+")
[,1] [,2] [1,] 2 4 [2,] 4 6 -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
As Peter Dalgaard has already pointed out, you both need to read the help page. If you supply a matrix where the help page asks for a vector, you are likely to get troubles. I will coerce it for the next version, but I would like to point out that in about 12 years of providing mvrnorm (often as here without credit), this it the first time anyone has made this error in public. Anticipating what people will try is not easy.
On Mon, 2 Feb 2004 Ted.Harding at nessie.mcc.ac.uk wrote:
On 02-Feb-04 Stuart V Jordan wrote:
I am trying to simulate draws from a multivariate normal using mvrnorm,
and
am getting the following error message:
Error in mu + eS$vectors %*% diag(sqrt(pmax(ev, 0)), p) %*% t(X) :
non-conformable arrays
[...] Hmmm ... using the same B and V as giben by Stuart Jordan, I get:
mvrnorm(1,B,V)
[1] -179.8332342 0.9632282 2.5687489 -2.2337125 47.2717626 [6] -3745.3844310 0.9632282 0.2839965 -0.1500585 0.5804460 [11] 5.1919420 -4.7381677 -0.2382119
mvrnorm(2,B,V)
Error in mu + eS$vectors %*% diag(sqrt(pmax(ev, 0)), p) %*% t(X) :
non-conformable arrays
?????
Ted.
PS By the way, [near] singularity seems to have nothing to do
with it here:
If I brutally make V non-singular:
diag(V) <- diag(V)+10000000
it goes the same way.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 167 1972
Date: 02-Feb-04 Time: 22:17:13
------------------------------ XFMail ------------------------------
______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595