valentina colombo
2013-Oct-20 11:08 UTC
[R] Errore : requires numeric/complex matrix/vector arguments
Dear R users,I'm a new user of R. I'm trying to do a LM test an there is this type of error: Error in t(mX) %*% mX : requires numeric/complex matrix/vector arguments. To be clear I write down the code in which mY ( 126,1 ) mX (126,1) mZ(126,1) are matrix. LMTEST <- function(mY, mX, mZ)#mY, mX, mZ must be matrices!#returns the LM test statistic and the degree of freedom{iT = dim(mY)[1]ip = dim(mY)[2]iDF = dim(mZ)[2]*ipmE = mY - mX%*%solve(t(mX)%*%mX)%*%t(mX)%*%mY the error starts from the above step (t(mX)%*%mX)%*%t(mX)%*%mY RSS0 = t(mE)%*%mEmXX = cbind(mX, mZ)mK = mE - mXX%*%solve(t(mXX)%*%mXX)%*%t(mXX)%*%mERSS1 = t(mK)%*%mKdTR = sum(diag(solve(RSS0)%*%RSS1))LM = iT*(ip-dTR)pval = 1-pchisq(LM,df=iDF)return( c(pval, LM, iDF) )} Any suggestion? Where is the problem? I am getting craxy! Valentina [[alternative HTML version deleted]]
John Kane
2013-Oct-20 11:20 UTC
[R] Errore : requires numeric/complex matrix/vector arguments
It looks like you have posted in HTML and your code is basically unreadable. Please repost in text format. Also I'd suggest reading one or both of the following lists for some hints on how to create a good question. I think a key point here is to supply sample data using the dput() function as described in the links: https://github.com/hadley/devtools/wiki/Reproducibility http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Welcome to the R-help list John Kane Kingston ON Canada> -----Original Message----- > From: valentina81c at hotmail.it > Sent: Sun, 20 Oct 2013 11:08:08 +0000 > To: r-help at r-project.org > Subject: [R] Errore : requires numeric/complex matrix/vector arguments > > Dear R users,I'm a new user of R. I'm trying to do a LM test an there is > this type of error: Error in t(mX) %*% mX : requires numeric/complex > matrix/vector arguments. > To be clear I write down the code in which mY ( 126,1 ) mX (126,1) > mZ(126,1) are matrix. > > LMTEST <- function(mY, mX, mZ)#mY, mX, mZ must be matrices!#returns the > LM test statistic and the degree of freedom{iT = dim(mY)[1]ip > dim(mY)[2]iDF = dim(mZ)[2]*ipmE = mY - > mX%*%solve(t(mX)%*%mX)%*%t(mX)%*%mY > the error starts from the above step (t(mX)%*%mX)%*%t(mX)%*%mY > RSS0 = t(mE)%*%mEmXX = cbind(mX, mZ)mK = mE - > mXX%*%solve(t(mXX)%*%mXX)%*%t(mXX)%*%mERSS1 = t(mK)%*%mKdTR > sum(diag(solve(RSS0)%*%RSS1))LM = iT*(ip-dTR)pval > 1-pchisq(LM,df=iDF)return( c(pval, LM, iDF) )} > Any suggestion? Where is the problem? I am getting craxy! > Valentina > [[alternative HTML version deleted]] > > ______________________________________________ > 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.____________________________________________________________ FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!
Berend Hasselman
2013-Oct-20 11:29 UTC
[R] Errore : requires numeric/complex matrix/vector arguments
On 20-10-2013, at 13:08, valentina colombo <valentina81c at hotmail.it> wrote:> Dear R users,I'm a new user of R. I'm trying to do a LM test an there is this type of error: Error in t(mX) %*% mX : requires numeric/complex matrix/vector arguments. > To be clear I write down the code in which mY ( 126,1 ) mX (126,1) mZ(126,1) are matrix. > > LMTEST <- function(mY, mX, mZ)#mY, mX, mZ must be matrices!#returns the LM test statistic and the degree of freedom{iT = dim(mY)[1]ip = dim(mY)[2]iDF = dim(mZ)[2]*ipmE = mY - mX%*%solve(t(mX)%*%mX)%*%t(mX)%*%mY > the error starts from the above step (t(mX)%*%mX)%*%t(mX)%*%mY > RSS0 = t(mE)%*%mEmXX = cbind(mX, mZ)mK = mE - mXX%*%solve(t(mXX)%*%mXX)%*%t(mXX)%*%mERSS1 = t(mK)%*%mKdTR = sum(diag(solve(RSS0)%*%RSS1))LM = iT*(ip-dTR)pval = 1-pchisq(LM,df=iDF)return( c(pval, LM, iDF) )} > Any suggestion? Where is the problem? I am getting craxy!Your code is a complete mess and thus unreadable because you posted in HTML. Cleaning up and doing this LMTEST <- function(mY, mX, mZ)#mY, mX, mZ must be matrices! #returns the LM test statistic and the degree of freedom {iT = dim(mY)[1] ip = dim(mY)[2] iDF = dim(mZ)[2]*ip mE = mY - mX%*%solve(t(mX)%*%mX)%*%t(mX)%*%mY # the error starts from the above step (t(mX)%*%mX)%*%t(mX)%*%mY RSS0 = t(mE)%*%mE mXX = cbind(mX, mZ) mK = mE - mXX%*%solve(t(mXX)%*%mXX)%*%t(mXX)%*%mE RSS1 = t(mK)%*%mK dTR = sum(diag(solve(RSS0)%*%RSS1)) LM = iT*(ip-dTR) pval = 1-pchisq(LM,df=iDF) return( c(pval, LM, iDF) ) } set.seed(1) N <- 20 mX <- matrix(runif(N),ncol=1) mY <- matrix(runif(N),ncol=1) mZ <- matrix(runif(N),ncol=1) LMTEST(mY,mX,mZ) the answer I got was: [1] 0.004965514 7.891955826 1.000000000 So it must be your data. Are you sure they are numeric? Have you checked with str(mX) etc? Berend> Valentina > [[alternative HTML version deleted]]Please don't post in html but in plain text.> > ______________________________________________ > 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.
Berend Hasselman
2013-Oct-20 14:31 UTC
[R] Errore : requires numeric/complex matrix/vector arguments
On 20-10-2013, at 16:16, valentina colombo <valentina81c at hotmail.it> wrote:> Dear Mr. Hasselman, > I have attached my code to solve (hopefully) my problem in Error-require numeric/complex matrix/vector > Any suggestions? > Thanks >Please answer to the list and NOT only privately. I'm forwarding this message to R-help. You are not showing how you constructed mX and others. In the attachment you show how you read X. I do not get your error when I generate your matrices with these commands: set.seed(1) N <- 126 mX <- matrix(runif(N*4),ncol=4) mY <- matrix(runif(N*4),ncol=4) mZ <- matrix(runif(N*4),ncol=4) If you want to include data in a text-only mail use dput. Berend> > > > Subject: Re: [R] Errore : requires numeric/complex matrix/vector arguments > > From: bhh at xs4all.nl > > Date: Sun, 20 Oct 2013 13:29:37 +0200 > > CC: r-help at r-project.org > > To: valentina81c at hotmail.it > > > > > > On 20-10-2013, at 13:08, valentina colombo <valentina81c at hotmail.it> wrote: > > > > > Dear R users,I'm a new user of R. I'm trying to do a LM test an there is this type of error: Error in t(mX) %*% mX : requires numeric/complex matrix/vector arguments. > > > To be clear I write down the code in which mY ( 126,1 ) mX (126,1) mZ(126,1) are matrix. > > > > > > LMTEST <- function(mY, mX, mZ)#mY, mX, mZ must be matrices!#returns the LM test statistic and the degree of freedom{iT = dim(mY)[1]ip = dim(mY)[2]iDF = dim(mZ)[2]*ipmE = mY - mX%*%solve(t(mX)%*%mX)%*%t(mX)%*%mY > > > the error starts from the above step (t(mX)%*%mX)%*%t(mX)%*%mY > > > RSS0 = t(mE)%*%mEmXX = cbind(mX, mZ)mK = mE - mXX%*%solve(t(mXX)%*%mXX)%*%t(mXX)%*%mERSS1 = t(mK)%*%mKdTR = sum(diag(solve(RSS0)%*%RSS1))LM = iT*(ip-dTR)pval = 1-pchisq(LM,df=iDF)return( c(pval, LM, iDF) )} > > > Any suggestion? Where is the problem? I am getting craxy! > > > > Your code is a complete mess and thus unreadable because you posted in HTML. > > Cleaning up and doing this > > > > LMTEST <- function(mY, mX, mZ)#mY, mX, mZ must be matrices! > > #returns the LM test statistic and the degree of freedom > > {iT = dim(mY)[1] > > ip = dim(mY)[2] > > iDF = dim(mZ)[2]*ip > > mE = mY - mX%*%solve(t(mX)%*%mX)%*%t(mX)%*%mY > > # the error starts from the above step > > (t(mX)%*%mX)%*%t(mX)%*%mY > > RSS0 = t(mE)%*%mE > > mXX = cbind(mX, mZ) > > mK = mE - mXX%*%solve(t(mXX)%*%mXX)%*%t(mXX)%*%mE > > RSS1 = t(mK)%*%mK > > dTR = sum(diag(solve(RSS0)%*%RSS1)) > > LM = iT*(ip-dTR) > > pval = 1-pchisq(LM,df=iDF) > > return( c(pval, LM, iDF) ) > > } > > > > set.seed(1) > > > > N <- 20 > > mX <- matrix(runif(N),ncol=1) > > mY <- matrix(runif(N),ncol=1) > > mZ <- matrix(runif(N),ncol=1) > > > > LMTEST(mY,mX,mZ) > > > > the answer I got was: > > > > [1] 0.004965514 7.891955826 1.000000000 > > > > > > So it must be your data. > > Are you sure they are numeric? Have you checked with str(mX) etc? > > > > Berend > > > > > Valentina > > > [[alternative HTML version deleted]] > > > > > > Please don't post in html but in plain text. > > > > > > > > > > ______________________________________________ > > > 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. > > > <R error.txt>