R语言主成分回归

2020/09/29 11:19

R语言主成分回归

R学习-主成分分析和主成分回归#主成分分析和主成分回归Pearson 1901年提出 Hotelling 1933进一步发展一 princomp() 函数princomp(x, cor = FALSE, scores = TRUE, covmat = NULL,subset = rep(TRUE, nrow(as.matrix(x))), ...)# 分析用数据# cor 是否用样本的协方差矩阵作主成分分析prcomp()二 summary()函数三 loadings()函数四 predict() 函数五 screeplot() 函数六 biplot() 函数实例某中学随机抽取某年级30名学生,测量其身高,体重,胸围,坐高,针对这30名中学生身体四项指标数据做主成分分析。student<-data.frame(X1=c(148, 139, 160, 149, 159, 142, 153, 150, 151, 139,140, 161, 158, 140, 137, 152, 149, 145, 160, 156,151, 147, 157, 147, 157, 151, 144, 141, 139, 148),X2=c(41, 34, 49, 36, 45, 31, 43, 43, 42, 31,29, 47, 49, 33, 31, 35, 47, 35, 47, 44,42, 38, 39, 30, 48, 36, 36, 30, 32, 38),X3=c(72, 71, 77, 67, 80, 66, 76, 77, 77, 68,64, 78, 78, 67, 66, 73, 82, 70, 74, 78,73, 73, 68, 65, 80, 74, 68, 67, 68, 70),X4=c(78, 76, 86, 79, 86, 76, 83, 79, 80, 74,74, 84, 83, 77, 73, 79, 79, 77, 87, 85,82, 78, 80, 75, 88, 80, 76, 76, 73, 78))#主成分分析student.pr <- princomp(student, cor = TRUE)#显示结果summary(student.pr, loadings=TRUE)#预测,显示各样本主成分的值pre<-predict(student.pr)#显示碎石图screeplot(student.pr,type="lines")# 主成分分析散点图biplot(student.pr)例二对128个成年男子的身材进行测量,每人测得16项指标,身高,坐高,胸围,头高,裤长,下档,手长,领围,前胸,后背,肩厚,肩宽,袖长,肋围,腰围,腿肚,分别用X1-X16表示。16项指标的相关矩阵R。从相关矩阵出发进行主成分分析,随16项指标进行分类。命令x<-c(1.00,0.79, 1.00,0.36, 0.31, 1.00,0.96, 0.74, 0.38, 1.00,0.89, 0.58, 0.31, 0.90, 1.00,0.79, 0.58, 0.30, 0.78, 0.79, 1.00,0.76, 0.55, 0.35, 0.75, 0.74, 0.73, 1.00,0.26, 0.19, 0.58, 0.25, 0.25, 0.18, 0.24, 1.00,0.21, 0.07, 0.28, 0.20, 0.18, 0.18, 0.29,-0.04, 1.00,0.26, 0.16, 0.33, 0.22, 0.23, 0.23, 0.25, 0.49,-0.34, 1.00,0.07, 0.21, 0.38, 0.08,-0.02, 0.00, 0.10, 0.44,-0.16, 0.23, 1.00,0.52, 0.41, 0.35, 0.53, 0.48, 0.38, 0.44, 0.30,-0.05, 0.50, 0.24, 1.00,0.77, 0.47, 0.41, 0.79, 0.79, 0.69, 0.67, 0.32, 0.23, 0.31, 0.10, 0.62, 1.00,0.25, 0.17, 0.64, 0.27, 0.27, 0.14, 0.16, 0.51, 0.21, 0.15, 0.31, 0.17, 0.26, 1.00,0.51, 0.35, 0.58, 0.57, 0.51, 0.26, 0.38, 0.51, 0.15, 0.29, 0.28, 0.41, 0.50, 0.63, 1.00,0.21, 0.16, 0.51, 0.26, 0.23, 0.00, 0.12, 0.38, 0.18, 0.14, 0.31, 0.18, 0.24, 0.50, 0.65, 1.00)names<-c("X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9","X10", "X11", "X12", "X13", "X14", "X15", "X16")R<-matrix(0, nrow=16, ncol=16, dimnames=list(names, names))for (i in 1:16){for (j in 1:i){R<-x[(i-1)*i/2+j]; R[j,i]<-R}}#主成分分析pr<-princomp(covmat=R)load<-loadings(pr)#plot(load[,1:2])text(load[,1], load[,2], adj=c(-0.4, 0.3))主成分回归考虑进口总额Y与三个自变量:国内总产值,存储量,总消费量之间的关系。现收集了1949-1959共11年的数据,试做线性回归和主成分回归分析。conomy<-data.frame(x1=c(149.3, 161.2, 171.5, 175.5, 180.8, 190.7, 202.1, 212.4, 226.1, 231.9, 239.0),x2=c(4.2, 4.1, 3.1, 3.1, 1.1, 2.2, 2.1, 5.6, 5.0, 5.1, 0.7),x3=c(108.1, 114.8, 123.2, 126.9, 132.1, 137.7, 146.0, 154.1, 162.3, 164.3, 167.6),y=c(15.9, 16.4, 19.0, 19.1, 18.8, 20.4, 22.7, 26.5, 28.1, 27.6, 26.3))线性回归lm.sol<-lm(y~x1+x2+x3, data=conomy)summary(lm.sol)主成分回归# 主成分分析conomy.pr<-princomp(~x1+x2+x3, data=conomy, cor=T)summary(conomy.pr, loadings=TRUE)pre<-predict(conomy.pr)conomy$z1<-pre[,1]; conomy$z2<-pre[,2]lm.sol<-lm(y~z1+z2, data=conomy)summary(lm.sol)

免费直播

    精选课程 更多

    注册电脑版

    版权所有 2003-2020 广州环球青藤科技发展有限公司