问题描述
我目前正在运行主成分分析。对于解释,我想创建一个配置文件(模式)图来可视化每个主成分与原始变量之间的相关性。有没有人熟悉在 R 中创建它的包或代码?我在 R 中使用 prcomp() 函数。
查看示例:
https://canadianaudiologist.ca/predicting-speech-perception-from-the-audiogram-and-vice-versa/ https://blogs.sas.com/content/iml/2019/11/04/interpret-graphs-principal-components.html
这与我的数据库相似:
db <- structure(list(T025 = c(20,60,20,10,85,5,15,25,30,45,55,65,35,50,40,-5,80,10),T05 = c(0,70,-10,5),T1 = c(25,75,25),T2 = c(20,T4 = c(10,90,45),T8 = c(5,56,25)),row.names = c("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","177","191","200","205","208","212","231","236","240","246","250","259","263","264","275","276","282","293","303","304","307","309","315","316","320","322","324","327","333","338","343","356","365","377","379","393","395","399","405","411","426","428","439","448","451","459","490","495","498","513","515","521","524","528","532","550","552","559","566","570","577","583","587","595","624","638","641","645","647","650","660","668","677","683","688","691","702","704","710","719","730","732","748","752","758","766","772","780","782","790","810","828","830","836","853","862","880","889","896"
),class = "data.frame")
db.pca <- prcomp(db,center= TRUE,scale.=TRUE)
summary(db.pca)
str(db.pca)
ggbiplot(db.pca)
screeplot(db.pca,type="line")
解决方法
使用你的数据我做到了:
comp = prcomp(db,center=T,scale.=T)
b =matrix(ncol = 3)[-1,]
for(i in 1:ncol(comp$x)){
for(j in colnames(db)){
b = rbind(b,c(i,j,cor.test(comp$x[,i],db[,j])$estimate))
}
}
b= as.data.frame(b)
b$cor= as.numeric(b$cor)
ggplot(b,aes(x=V2,y=cor,group = V1,col= V1))+
geom_line()+
theme_classic()
我得到了这个:
有帮助吗?
,这是使用包 FactoMineR
获取相关性的一种方法。该图是一个基本的 R 图。
library(FactoMineR)
res.pca <- PCA(iris[-5],graph = FALSE)
cos2 <- res.pca$var$cos2
old_par <- par(xpd = TRUE)
matplot(
cos2,type = "l",xlab = "variable",ylab = "correlation",main = "Component Pattern Profiles",xaxt = "n"
)
axis(1,at = 1:nrow(cos2),labels = rownames(cos2))
legend(
x = "bottom",inset = c(0,-0.2),legend = colnames(cos2),col = 1:ncol(cos2),lty = 1:ncol(cos2),bty = "n",horiz = TRUE
)
par(old_par)