Skip to content

Mapping actual to expected columns for princomp object

2 messages · Liaw, Andy, Brian Ripley

#
[Re-directing to R-devel, as I think this needs changes to the code.]

Can I suggest a modification to stats:predict.princomp so that it will check
for column (variable) names?

In src/library/stats/R/princomp-add.R, insert the following after line 4:

    if (!is.null(cn <- names(object$center))) newdata <- newdata[, cn]

Now Dana's example looks like:
Error in "[.data.frame"(newdata, , names(object$center)) : 
        undefined columns selected
Comp.1      Comp.2
1  -3.29329963 -1.24675774
2   0.15760569  0.09364550
3   1.90206906  0.06292855
4  -0.92968723  0.64356801
5  -1.15298669  0.25451588
6   0.48466884 -0.87611668
7   0.98602646 -0.52156549
8  -1.53126034 -0.96259529
9  -0.79112984 -1.50831648
10  0.02997392 -0.18888807
Comp.1      Comp.2
1   2.49603051 -2.42516162
2  -0.15633499  0.15754735
3  -1.77400454  0.81118427
4   1.05941012  0.23869214
5   1.11286213 -0.20669206
6  -0.83645436 -0.60720531
7  -1.15932677 -0.08488413
8   0.98526969 -1.47482877
9   0.09070675 -1.68781215
10 -0.14930067 -0.15239717

Best,
Andy
#
I am currently working on this (and on the predict method for prcomp, 
which does exist, BTW).  It needs a bit more in the way of sanity 
checks.

Note that the predict method for lm is for a formula-driven fit, whereas 
that for princomp is not, hence some of the differences.  It is not 
reasonable to apply the docs for predict.lm to predict.princomp, and they 
do not work the same way.
On Thu, 24 Mar 2005, Liaw, Andy wrote: