R语言进行主成分分析
导入数据
csvpath <- file.choose()
csvpath
读取数据
data <- read.csv(csvpath, header = T, rows.names = 1)
如下图示例所示:
图片 1.png
调用主成分分析函数princomp
df.pr <- princomp(data, cor = T)
summary(df.pr, loadings = T)
> summary(df.pr, loadings = T)
Importance of components:
Comp.1 Comp.2 Comp.3 Comp.4
Standard deviation 2.0513375 0.7302803 0.3937626 0.31643891
Proportion of Variance 0.8415971 0.1066619 0.0310098 0.02002672
Cumulative Proportion 0.8415971 0.9482589 0.9792687 0.99929545
Comp.5
Standard deviation 0.0593527515
Proportion of Variance 0.0007045498
Cumulative Proportion 1.0000000000
Loadings:
Comp.1 Comp.2 Comp.3 Comp.4 Comp.5
height 0.467 0.231 0.850
DBH 0.366 -0.897 0.166 -0.184
Length 0.458 -0.875 0.156
lantdh 0.463 0.334 0.372 -0.416 0.603
square 0.474 0.283 0.125 -0.265 -0.781
#Cumulative Proportion 表示各个变量的累积贡献率
绘制碎石图
screeplot(df.pr,type = "lines")
image.png