predict.mvr.Rd
Prediction for mvr (PCR, PLSR) models. New responses or scores are predicted using a fitted model and a new matrix of observations.
# S3 method for mvr predict(object, newdata, ncomp = 1:object$ncomp, comps, type = c("response", "scores"), na.action = na.pass, ...)
object | an |
---|---|
newdata | a data frame. The new data. If missing, the training data is used. |
ncomp, comps | vector of positive integers. The components to use in the prediction. See below. |
type | character. Whether to predict scores or response values |
na.action | function determining what should be done with missing
values in |
... | further arguments. Currently not used |
When type
is "response"
(default), predicted response
values are returned. If comps
is missing (or is NULL
),
predictions for length(ncomp)
models with ncomp[1]
components, ncomp[2]
components, etc., are returned.
Otherwise, predictions for a single model with the exact components in
comps
are returned. (Note that in both cases, the intercept is
always included in the predictions. It can be removed by subtracting
the Ymeans
component of the fitted model.)
When type
is "scores"
, predicted score values are
returned for the components given in comps
. If comps
is
missing or NULL
, ncomps
is used instead.
It is also possible to supply a matrix instead of a data frame as
newdata
, which is then assumed to be the \(X\) data matrix.
Note that the usual checks for the type of the data are then
omitted. Also note that this is
only possible with predict
; it will not work in
functions like predplot
, RMSEP
or
R2
, because they also need the response variable of the new
data.
When type
is "response"
, a three dimensional array of
predicted response values is returned. The dimensions correspond to
the observations, the response variables and the model sizes,
respectively.
When type
is "scores"
, a score matrix is returned.
A warning message like 'newdata' had 10 rows but variable(s)
found have 106 rows means that not all variables were found in the
newdata
data frame. This (usually) happens if the formula
contains terms like yarn$NIR
. Do not use such terms; use the
data
argument instead. See mvr
for details.
data(yarn) nir.mvr <- mvr(density ~ NIR, ncomp = 5, data = yarn[yarn$train,]) ## Predicted responses for models with 1, 2, 3 and 4 components pred.resp <- predict(nir.mvr, ncomp = 1:4, newdata = yarn[!yarn$train,]) ## Predicted responses for a single model with components 1, 2, 3, 4 predict(nir.mvr, comps = 1:4, newdata = yarn[!yarn$train,])#> density #> 110 51.04992 #> 22 50.72019 #> 31 32.01454 #> 41 34.29076 #> 51 30.35994 #> 61 20.57832 #> 71 19.07786#> Comp 1 Comp 2 Comp 3 #> 110 0.141826839 0.06103555 -0.1868236 #> 22 0.142264852 -0.03200534 -0.1335956 #> 31 0.003114755 0.14183105 -0.2645071 #> 41 0.027585394 0.02948577 -0.2574162 #> 51 -0.006228066 -0.11556152 -0.1281703 #> 61 -0.095932724 0.13546998 -0.1292624 #> 71 -0.082835344 -0.04981311 -0.2493806