R语言与概率统计(六) 主成分分析 因子分析
超高维度分析,N*P的矩阵,N为样本个数,P为指标,N<<P
PCA:抓住对y对重要的影响因素
主要有三种:PCA,因子分析,回归方程+惩罚函数(如LASSO)
为了降维,用更少的变量解决问题,如果是二维的,那么就是找到一条线,要使这些点再线上的投影最大,投影最大,就是越分散,就考虑方差最大。
> 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) Call:
lm(formula = y ~ x1 + x2 + x3, data = conomy) Residuals:
Min 1Q Median 3Q Max
-0.52367 -0.38953 0.05424 0.22644 0.78313 Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -10.12799 1.21216 -8.355 6.9e-05 ***
x1 -0.05140 0.07028 -0.731 0.488344
x2 0.58695 0.09462 6.203 0.000444 ***
x3 0.28685 0.10221 2.807 0.026277 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.4889 on 7 degrees of freedom
Multiple R-squared: 0.9919, Adjusted R-squared: 0.9884
F-statistic: 285.6 on 3 and 7 DF, p-value: 1.112e-07 > #### 作主成分分析
> conomy.pr<-princomp(~x1+x2+x3, data=conomy, cor=T)
> summary(conomy.pr, loadings=TRUE)
Importance of components:
Comp.1 Comp.2 Comp.3
Standard deviation 1.413915 0.9990767 0.0518737839
Proportion of Variance 0.666385 0.3327181 0.0008969632
Cumulative Proportion 0.666385 0.9991030 1.0000000000 Loadings:
Comp.1 Comp.2 Comp.3
x1 0.706 0.707
x2 -0.999
x3 0.707 -0.707
> #### 预测测样本主成分, 并作主成分分析
> pre<-predict(conomy.pr)
> conomy$z1<-pre[,1]
> conomy$z2<-pre[,2]
> lm.sol<-lm(y~z1+z2, data=conomy)
> summary(lm.sol) Call:
lm(formula = y ~ z1 + z2, data = conomy) Residuals:
Min 1Q Median 3Q Max
-0.89838 -0.26050 0.08435 0.35677 0.66863 Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 21.8909 0.1658 132.006 1.21e-14 ***
z1 2.9892 0.1173 25.486 6.02e-09 ***
z2 -0.8288 0.1660 -4.993 0.00106 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.55 on 8 degrees of freedom
Multiple R-squared: 0.9883, Adjusted R-squared: 0.9853
F-statistic: 337.2 on 2 and 8 DF, p-value: 1.888e-08 > #### 作变换, 得到原坐标下的关系表达式
> beta<-coef(lm.sol); A<-loadings(conomy.pr)
> x.bar<-conomy.pr$center; x.sd<-conomy.pr$scale
> coef<-(beta[2]*A[,1]+ beta[3]*A[,2])/x.sd
> beta0 <- beta[1]- sum(x.bar * coef)
> c(beta0, coef)
(Intercept) x1 x2 x3
-9.13010782 0.07277981 0.60922012 0.10625939
R语言与概率统计(六) 主成分分析 因子分析的更多相关文章
- R语言与概率统计(二) 假设检验
> ####################5.2 > X<-c(159, 280, 101, 212, 224, 379, 179, 264, + 222, 362, 168, 2 ...
- R语言结合概率统计的体系分析---数字特征
现在有一个人,如何对这个人怎么识别这个人?那么就对其存在的特征进行提取,比如,提取其身高,其相貌,其年龄,分析这些特征,从而确定了,这个人就是这个人,我们绝不会认错. 同理,对数据进行分析,也是提取出 ...
- R语言与概率统计(一) 描述性统计分析
#查看已安装的包,查看已载入的包,查看包的介绍 ########例题3.1 #向量的输入方法 w<-c(75.0, 64.0, 47.4, 66.9, 62.2, 62.2, 58.7, 6 ...
- R语言与概率统计(五) 聚类分析
#########################################0808聚类分析 X<-data.frame( x1=c(2959.19, 2459.77, 1495.63, ...
- R语言与概率统计(四) 判别分析(分类)
Fisher就是找一个线L使得组内方差小,组间距离大.即找一个直线使得d最大. ####################################1.判别分析,线性判别:2.分层抽样 #inst ...
- R语言与概率统计(三) 多元统计分析(下)广义线性回归
广义线性回归 > life<-data.frame( + X1=c(2.5, 173, 119, 10, 502, 4, 14.4, 2, 40, 6.6, + 21.4, 2.8, 2. ...
- R语言与概率统计(三) 多元统计分析(中)
模型修正 #但是,回归分析通常很难一步到位,需要不断修正模型 ###############################6.9通过牙膏销量模型学习模型修正 toothpaste<-data. ...
- R语言与概率统计(三) 多元统计分析(上)
> #############6.2一元线性回归分析 > x<-c(0.10,0.11,0.12,0.13,0.14,0.15,0.16,0.17,0.18,0.20,0.21,0. ...
- R语言与医学统计图形【1】par函数
张铁军,陈兴栋等 著 R语言基础绘图系统 基础绘图包之高级绘图函数--par函数 基础绘图包并非指单独某个包,而是由几个R包联合起来的一个联盟,比如graphics.grDevices等. 掌握par ...
随机推荐
- F - Star SPOJ - STARSBC
Fernando won a compass for his birthday, and now his favorite hobby is drawing stars: first, he marks ...
- 洛谷P1462 通往奥格瑞玛的道路(SPFA+二分答案)
题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡奥格瑞玛 题目描述 在艾泽拉斯, ...
- 实用:Java基础流计算
java的流不常用,每次学习完都懂,过了一段时间就全忘了... 记录下一点实用的东西... 需求: 截取文件的前250kb内容 public static void main(String[] arg ...
- nginx反向代理和负载均衡的简单部署
1. 安装 1) 从Nginx官网下载页面(http://nginx.org/en/download.html)下载Nginx最新版本(目前是1.5.13版本)安装包: 2) ...
- windows下如何打开.sketch的文件
1 .sketch的文件只能在苹果mac上支持的一种文件格式,现在越来越多的设计师喜欢用.sketch 2 windows下如果想打开.sketch文件,去Microsoft store 找一个Lun ...
- Redis持久化(三)
Redis持久化 Redis提供了哪些持久化机制 1. RDB持久化: 该机制是指在指定的时间间隔内将内存中的数据集快照写入磁盘. 2. AOF持久化: 该机制 ...
- UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 140: invalid continuation byte
web阅片系统,遇到dicom文件在文件夹不能正常读取的问题.解决方法如下: def rep7(request): file_path = os.path.dirname(__file__) + re ...
- Force git to overwrite local files on pull 使用pull强制覆盖本地文件 转载自:http://snowdream.blog.51cto.com/3027865/1102441
How do I force an overwrite of local files on a git pull? I think this is the right way: $ git fetch ...
- URAL 2052 Physical Education(数位DP)
题目链接:https://vjudge.net/contest/254142#problem/G 参考题解:https://blog.csdn.net/zearot/article/details/4 ...
- RDLC 传参 报表出现错误 (未解决)
经过测试 可以用的 带传参的 RDLC 在vs2019上 能正常运行 但在vs2019上剪切后,保存, 运行就会报An error 错误, ctr+z 全部 撤销后 保存 一样会报 这 ...