有关于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.
以下为纯代码分享:
# 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
原创内容,转载请注明出处;希望能对你有帮助:)
延伸:
有关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