[R] Calculate SPE in PLS package

Bjørn-Helge Mevik b.h.mevik at usit.uio.no
Mon Oct 20 11:03:20 CEST 2008


"Stella Sim" <Stella.Sim at fonterra.com> writes:

> I want to calculate SPE (squared prediction error) in x-space, can
> someone help?
>
> Here are my codes:
>
> fit.pls<-
> plsr(Y~X,data=DAT,ncomp=3,scale=T,method='oscorespls',validation="CV",x=
> T) 
> actual<-fit.pls$model$X

(The x = TRUE is not needed as long as model = TRUE (default).  x=TRUE
returns the predictors as fit.pls$x, and is included for "compatibility"
with lm().)

> pred<-fit.pls$scores %*% t(fit.pls$loadings)
> SPE.x<-rowSums((actual-pred)^2)
>
> Am I missing something here? 

You are missing the mean X spectrum.  See
matplot(t(pred), type = "l", lty = 1) vs. matplot(t(actual), type = "l", lty = 1)

The Xmeans compontent of fit.pls contains this, so

pred <- sweep(fit.pls$scores %*% t(fit.pls$loadings), 2, fit.pls$Xmeans, "+")

would give you what you want.

Note, however, that this will calculate the _fitted_ SPE, not the
cross-validated SPE.  The crossvalidation implemented in the pls package
does not save the cross-validated scores/loadings -- that would consume
too much memory.  (Calculation of SPE withing the cross-validation
routines could have been implemented, but was not.)

-- 
Regards,
Bjørn-Helge Mevik



More information about the R-help mailing list