R代码分享:使用PCA方法对于communality进行计算

该文介绍了使用PCA(主成分分析)计算communality的过程,包括数据矩阵化、计算四分相关系数、执行PCA生成特征向量、绘制特征值散点图以确定保留的组件数量,以及计算因子载荷的平方和作为communality的步骤。
摘要由CSDN通过智能技术生成

有关于communality的介绍在此不加展开,采用的计算思路是源于此篇文章的supplement materials:

Livingston G, Huntley J, Sommerlad A, et al. Dementia prevention, intervention, and care: 2020 report of the Lancet Commission. Lancet 2020; published online July 30. http://dx.doi.org/10.1016/S0140-6736(20)30367-6.

https://www.thelancet.com/cms/10.1016/S0140-6736(20)30367-6/attachment/cee43a30-904b-4a45-a4e5-afe48804398d/mmc1.pdf

以下为纯代码分享:

# step1. input data on all risk factors in the model
# my original dataset: pca_set

my_matrix<-as.matrix(pca_set)

# step2. calculate tetrachoric correlation to generate correlation coefficients and a correlation matrix
#install.packages("psych")
#install.packages("polycor")
library(psych)
library(polycor)
pca_cor<-hetcor(my_matrix,ML=TRUE)

# step3. conduct a PCA on the correlation matrix to generate eigenvectors
my_pca <- princomp(pca_cor, n.comp = sum(my_pca$sdev^2 >= 1))

# screen plot to find the elbow
eig_df <- data.frame(Comp = 1:length(my_pca$sdev), Eigenvalue = my_pca$sdev^2)
# Plot the scree plot
ggplot(eig_df, aes(x = Comp, y = Eigenvalue)) +
  geom_point(size = 3, color = "red") +
  geom_line(size = 1.2, color = "blue") +
  xlab("Component") +
  ylab("Eigenvalue") +
  ggtitle("Scree Plot")

# the 'elbow' in this example is 2

# step4. retain components with eighvalues ≥1 in the model. Then communality can be calculated as the sum of the square of all factor loadings
my_loadings<-my_pca$loadings
my_communality<-rowSums(my_loadings[,1:2]^2)
# my_communality<-rowSums(my_loadings[,1:my_elbow]^2)
my_communality

example of screen plot

原创内容,转载请注明出处;希望能对你有帮助:)

延伸:

有关PCA方法的一些介绍可参见:

https://www.datacamp.com/tutorial/pca-analysis-rhttps://rpubs.com/ranvirkumarsah/Intro2PCA_EFA

使用factor analysis方法来计算communality可参见:

Exploratory Factor Analysis -- Notes and R Code · Gaoping Huang's Blog

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值