[R] help plsr function

Bjørn-Helge Mevik b.h.mevik at usit.uio.no
Tue Jun 24 10:58:59 CEST 2014


annie Zhang <annie.zhang2010 at gmail.com> writes:

> ## the predicted scores from the model
> (pred <- predict(data.cpls,n.comp=1:2,newdata=x.new,type="score"))
> ## the predicted scores using x%*%projection
> cbind(x.new.centered%*%data.cpls$projection[,1],x.new.centered%*%data.cpls$projection[,2])
>
> Can someone please tell me why the two predicted scores don't match?

If you look at the code that does the prediction:

> pls:::predict.mvr
function (object, newdata, ncomp = 1:object$ncomp, comps, type = c("response", 
    "scores"), na.action = na.pass, ...) 
{
[...]
           TT <- (newX - rep(object$Xmeans, each = nobs)) %*% 
                object$projection[, comps]

you will see that it subtracts the _old X_ coloumn means from the new X
matrix, not the _new X_ coloumn means.  So

sweep(x.new, 2, data.cpls$Xmeans, "-") %*% data.cpls$projection[,1:2]

will reproduce the values from predict().

-- 
Regards,
Bjørn-Helge Mevik



More information about the R-help mailing list