1、加载数据做成分分析:
data9.2<-data.frame(
x1=c(82.9,88.0,99.9,105.3,117.7,131.0,148.2,161.8,174.2,184.7),
x2=c(92,93,96,94,100,101,105,112,112,112),
x3=c(17.1,21.3,25.1,29.0,34.0,40.0,44.0,49.0,51.0,53.0),
x4=c(94,96,97,97,100,101,104,109,111,111),
y=c(8.4,9.6,10.4,11.4,12.2,14.2,15.8,17.9,19.6,20.8)
)
data9.2.pr=princomp(~x1+x2+x3+x4,data=data9.2,cor=TRUE) #做主成分分析
summary(data9.2.pr,loadings=TRUE)
第一个主成分的方差贡献率为98.5%,已经足够选取1个主成分
2、现在生成主成分变量对主成分做回归:
data9.2.pre<-predict(data9.2.pr)
data9.2$Z1=data9.2.pre[,1]
data9.2$Z2=data9.2.pre[,2]
lm.data9.2<-lm(y~Z1,data=data9.2)#对主成分建立回归分析
summary(lm.data9.2)#模型检验
可以看到模型各项检验都良好
3、接下来做系数变换:
beta<-coef(lm.data9.2) #系数变换
A<-loadings(data9.2.pr)
x.bar<-data9.2.pr$center
x.sd<-data9.2.pr$scale
coef<-(beta[2]*A[,1])/x.sd
beta0<-beta[1]-sum(x.bar*coef)
c(beta0,coef)
回归方程:Y=-23.7777186+0.02992643X1+0.13365158 X2+0.08361156X3+0.16965187X4
源码:
data9.2<-data.frame(
x1=c(82.9,88.0,99.9,105.3,117.7,131.0,148.2,161.8,174.2,184.7),
x2=c(92,93,96,94,100,101,105,112,112,112),
x3=c(17.1,21.3,25.1,29.0,34.0,40.0,44.0,49.0,51.0,53.0),
x4=c(94,96,97,97,100,101,104,109,111,111),
y=c(8.4,9.6,10.4,11.4,12.2,14.2,15.8,17.9,19.6,20.8)
)
data9.2.pr=princomp(~x1+x2+x3+x4,data=data9.2,cor=TRUE) #做主成分分析
summary(data9.2.pr,loadings=TRUE)
data9.2.pre<-predict(data9.2.pr)
data9.2$Z1=data9.2.pre[,1]
data9.2$Z2=data9.2.pre[,2]
lm.data9.2<-lm(y~Z1,data=data9.2)#对主成分建立回归分析
summary(lm.data9.2)#模型检验
beta<-coef(lm.data9.2) #系数变换
A<-loadings(data9.2.pr)
x.bar<-data9.2.pr$center
x.sd<-data9.2.pr$scale
coef<-(beta[2]*A[,1])/x.sd
beta0<-beta[1]-sum(x.bar*coef)
c(beta0,coef)
/upload/1_14_o.png” alt=”undefined” />
1、加载数据做成分分析:
data9.2<-data.frame(
x1=c(82.9,88.0,99.9,105.3,117.7,131.0,148.2,161.8,174.2,184.7),
x2=c(92,93,96,94,100,101,105,112,112,112),
x3=c(17.1,21.3,25.1,29.0,34.0,40.0,44.0,49.0,51.0,53.0),
x4=c(94,96,97,97,100,101,104,109,111,111),
y=c(8.4,9.6,10.4,11.4,12.2,14.2,15.8,17.9,19.6,20.8)
)
data9.2.pr=princomp(~x1+x2+x3+x4,data=data9.2,cor=TRUE) #做主成分分析
summary(data9.2.pr,loadings=TRUE)
第一个主成分的方差贡献率为98.5%,已经足够选取1个主成分
2、现在生成主成分变量对主成分做回归:
data9.2.pre<-predict(data9.2.pr)
data9.2$Z1=data9.2.pre[,1]
data9.2$Z2=data9.2.pre[,2]
lm.data9.2<-lm(y~Z1,data=data9.2)#对主成分建立回归分析
summary(lm.data9.2)#模型检验
可以看到模型各项检验都良好
3、接下来做系数变换:
beta<-coef(lm.data9.2) #系数变换
A<-loadings(data9.2.pr)
x.bar<-data9.2.pr$center
x.sd<-data9.2.pr$scale
coef<-(beta[2]*A[,1])/x.sd
beta0<-beta[1]-sum(x.bar*coef)
c(beta0,coef)
回归方程:Y=-23.7777186+0.02992643X1+0.13365158 X2+0.08361156X3+0.16965187X4
源码:
data9.2<-data.frame(
x1=c(82.9,88.0,99.9,105.3,117.7,131.0,148.2,161.8,174.2,184.7),
x2=c(92,93,96,94,100,101,105,112,112,112),
x3=c(17.1,21.3,25.1,29.0,34.0,40.0,44.0,49.0,51.0,53.0),
x4=c(94,96,97,97,100,101,104,109,111,111),
y=c(8.4,9.6,10.4,11.4,12.2,14.2,15.8,17.9,19.6,20.8)
)
data9.2.pr=princomp(~x1+x2+x3+x4,data=data9.2,cor=TRUE) #做主成分分析
summary(data9.2.pr,loadings=TRUE)
data9.2.pre<-predict(data9.2.pr)
data9.2$Z1=data9.2.pre[,1]
data9.2$Z2=data9.2.pre[,2]
lm.data9.2<-lm(y~Z1,data=data9.2)#对主成分建立回归分析
summary(lm.data9.2)#模型检验
beta<-coef(lm.data9.2) #系数变换
A<-loadings(data9.2.pr)
x.bar<-data9.2.pr$center
x.sd<-data9.2.pr$scale
coef<-(beta[2]*A[,1])/x.sd
beta0<-beta[1]-sum(x.bar*coef)
c(beta0,coef)
评论