问题
I am having issues with princomp
, specifically biplot
, when I want to use a covariance or correlation matrix not generated by princomp
itself. For simplicity I will use a much smaller dataset than the one I am dealing with.
cr <- cov.wt(USArrests)
biplot(princomp(data = USArrests, covmat = cr))
gives me the error
Error in biplot.princomp(princomp(data = USArrests, covmat = cr)) :
object 'princomp(data = USArrests, covmat = cr)' has no scores
Seems like something simple going on here, but google has thus far been unhelpful.
回答1:
The data
argument in princomp
can only be used by the "S3 method for class 'formula'". Thus, you need to specify your princomp
call in either of these two ways:
cr <- cov.wt(USArrests)
pr1 <- princomp(x = USArrests, covmat = cr)
pr2 <- princomp(formula = ~ ., data = USArrests, covmat = cr)
biplot(pr1)
biplot(pr2)
来源:https://stackoverflow.com/questions/18752899/scores-not-being-generated-from-princomp-unable-to-generate-biplot