[R] Using napredict in prcomp

Alain Paquette alain.paquette at gmail.com
Wed Mar 11 14:22:11 CET 2009


Thanks for the answer.
Yes indeed, napredict only puts NAs in the right places (ie where 
na.action was used). Anyway I realize that I'm much better off dealing 
with the missing values prior to the PCA, and R offers many options for 
that.
Thanks
Alain


Prof Brian Ripley a écrit :
> On Tue, 10 Mar 2009, Alain Paquette wrote:
>
>> Hello all,
>>
>> I wish to compute site scores using PCA (prcomp) on a matrix with 
>> missing values, for example:
>>
>>   Drain    Slope    OrgL
>> a    4    1    NA
>> b    2.5    39    6
>> c    6    8    45
>> d    3    9    12
>> e    3    16    4
>> ...
>>
>> Where a,b... are sites.
>> The command
>>> pca<-prcomp(~ Drain + Slope + OrgL, data = t, center = TRUE, scale = 
>>> TRUE, 
>> na.action=na.exclude)
>> works great, and from
>>> pca$x
>> I can get site scores, e.g.
>>
>>                      PC1          PC2          PC3
>> a          NA           NA           NA
>> b -2.10475208 -2.315128625 -0.885197753
>> c  5.01177388 -1.778786252 -0.193285051
>> d  0.28638602  0.298315086  0.386113799
>> e -0.58861254  0.089498632 -0.434951813
>> ...
>>
>> Easy enough...
>> But how do I use the "napredict" argument? Is it intended as an 
>> argument to be used in the prcomp line (as suggested in ?prcomp), or 
>> is it to be used by itself, to replace NAs in the above site score 
>> matrix (which is what I really want to do).
>
> I don't see an 'napredict' argument.  The help says
>
>        x: if 'retx' is true the value of the rotated data (the centred
>           (and scaled if requested) data multiplied by the 'rotation'
>           matrix) is returned.  Hence, 'cov(x)' is the diagonal matrix
>           'diag(sdev^2)'.  For the formula method, 'napredict' is
>           applied to handle the treatment of values omitted by the
>           'na.action'.
>
> 'napredict' is a function (so please look up its help), and it says R 
> applies it, not that you need to.  It is napredict which gives you the 
> NAs in the correct places.
>
>>
>> Thank you,
>> Alain
>>
>> -- 
>> Alain Paquette, Ph.D.
>> alain.paquette at gmail.com
>>
>> Centre d'étude de la forêt (CEF)
>> Université du Québec à Montréal
>> www.cef-cfr.ca
>>
>> Projet TRIADE
>> www.projettriade.ca
>> alain.paquette at projettriade.ca
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide 
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>

-- 
Alain Paquette, Ph.D.
alain.paquette at gmail.com

Centre d'étude de la forêt (CEF)
Université du Québec à Montréal
www.cef-cfr.ca

Projet TRIADE
www.projettriade.ca
alain.paquette at projettriade.ca




More information about the R-help mailing list