pca - Extract Identifier from Principal Component Analysis with Missing Data in R -
i conducting principal component analysis in r on vectors missing data. want extract score principal component , match values observations not missing in original frame can't figure out how extract , match on right identifiers. example:
x1 <- c(1,2,3,na, 5,6,7) x2 <- c(7,na,6,na, 4,3,2) frame <- cbind(x1,x2) pca_ob<- princomp(~frame) pca_ob$score[,1]
this produces following output:
1 3 5 6 7 4.273146 2.104705 -0.715732 -2.125950 -3.536168
i bind pca_ob$score[,1] original frame based on identifiers , fill rest in nas such produces following matrix:
x1 x2 x3 1 1 7 4.273146 2 2 na na 3 3 6 2.104705 4 na na na 5 5 4 -0.715732 6 6 3 -2.125950 7 7 2 -3.536168
this takes output of first set of scores , matches them frame nas filling spots there isn't pca score , matching on variables there scores.any thoughts? thanks.
this feels bit of hack. there may better solution out there somewhere.
the method here create new object full of nas, , turn names of sparse data numeric indexes , assign using those.
> p1 <- pca_ob$scores[,1] > p1 1 3 5 6 7 4.273146 2.104705 -0.715732 -2.125950 -3.536168 > z<-rep(na, 7) > z[as.numeric(names(p1))]<-p1 > z [1] 4.273146 na 2.104705 na -0.715732 -2.125950 -3.536168
Comments
Post a Comment