[R] Matrix multiplication problem

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Fri Mar 8 15:18:26 CET 2002


Gavin Simpson <gavin.simpson at ucl.ac.uk> writes:

> Results in the following error:
> Error in df %*% t(Yhat) : non-conformable arguments
...
> df is a column matrix of the form:
> 
>           [,1]
> [1,] 0.3333333
> [2,] 0.3333333
> [3,] 0.3333333
> [4,] 0.3333333
> 
> With Yhat' and Yhat both being matrices of the form:
> Yhat'
>           [,1]       [,2]       [,3]       [,4]
> [1,]  2.1462687 -0.5641791 -0.9761194 -0.6059701
> [2,] -1.9487562  1.5880597  0.8587065 -0.4980100
> [3,]  0.2646766  0.9791045 -0.1472637 -1.0965174
> [4,]  0.2701493 -0.6134328 -0.1089552  0.4522388
> 
> And Yhat:
>            [,1]       [,2]       [,3]       [,4]
> [1,]  2.1462687 -1.9487562  0.2646766  0.2701493
> [2,] -0.5641791  1.5880597  0.9791045 -0.6134328
> [3,] -0.9761194  0.8587065 -0.1472637 -0.1089552
> [4,] -0.6059701 -0.4980100 -1.0965174  0.4522388
> 
> I am stumped as to what I am doing wrong and how to go about solving it.

You're multiplying a 4x1 matrix with a 4x4 and they don't conform. 

...

>      df <- as.matrix(rep((1/(n-1)), times=n))
>     
>      S <- df %*% t(Yhat) %*% Yhat
...


Either

a) use S <- t(df) %*%...
b) use df <- matrix(rep((1/(n-1)), times=n), 1)

or

c) just forget about df and use  S <- (t(Yhat) %*% Yhat)/(n-1)

-- 
   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 mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list