R之回归分析
来源:互联网 发布:雷锋站长源码 编辑:程序博客网 时间:2024/04/19 12:46
数据挖掘系列之
回归分析
1、确定Y与 间的定量关系表达式,这种表达式是回归方程;
2、对求得的回归方程的可信度进行检验;
3、判断自变量 对Y有无影响;
4、利用所求的回归方程进行预测和控制。
1、确定研究对象,明确哪个变量是解释变量,哪个变量是预报变量。
1、 一元线性回归
2 、多元线性回归
3、 广义线性回归之logistic回归
引例:
表1 合金的强度和合金中碳的含量数据量
Rcode:
x=c(0.1,0.11,0.12,0.13,0.14,0.15,0.16,0.17,0.18,0.20,0.21,0.23)
y=c(42.0,43.5,45.0,45.5,45.0,47.5,49.0,53.0,50.0,55.0,55.0,60.0)
plot(x,y);
l=lm(y~x);
plot(y~x);abline(l)
求例1的回归方程,并对相应的方程做检验.
对于给定的置信水平1-α,则有:
1.根据自变量x 的取值预测 y 的取值;
2.预测可分两种类型:
⑴.点预测:
⑵.区间预测:
求例1中X=x0=0.16时相应Y的概率为0.95的预测区间.
1.回归系数的显著性检验:
new.model=update(old.model,new.formula)
在new.formula中,其相应的名字用点“.”组成,可以被用作表示“旧模型公式中相应的部分”
例如:
fm5=lm(y~x1+x2+x3+x4+x5,data=production)
fm6=update(fm5,.~.+x6)
增加一个解释变量x6;
smf6=update(fm6,sqrt(.)~.)
将被解释变量改为根号y
blood=data.frame(
x1=c(76.0,91.5,85.5,82.5,79.0,80.5,74.5,79.0,85.0,76.5,82.0,95.0,92.5),
x2=c(50,20,20,30,30,50,60,50,40,55,40,40,20),
y=c(120,141,124,126,117,125,123,125,132,123,132,155,147))
fm5=lm(y~x1,data=blood)
fm6=update(fm5,.~.+x2)
fm7=lm(fm6,data=blood)
summary(fm7)
首先,求出一个基本回归方程,然后,逐步添加解释变量,根据添加解释变量对拟合优度的改进和对其它回归系数的影响等决定是否保留添加的解释变量。
1.如果新添加的解释变量改进拟合优度,并且其它回归系数在统计上仍是显著的,那么,保留添加的解释变量。新添加解释变量不引起多重共线性;
2.新添加的解释变量未能改进拟合优度,且对其它回归系数也没有影响,则新添加解释变量不予保留,不产生多重共线性;
3.如果新添加的解释变量,不仅影响拟合优度,而且显著地影响其它回归系数的显著性、符号、数值,甚至使参数符号反号到不能接受的地步,则认为新添加的解释变量可能是引起多重共线性的原因。
某种水泥在凝固时放出的热量Y与水泥中四种化学成分X1,X2,X3,X4有关,现测得13组数据,如表所示。希望从中选出主要的变量,建立Y关于他们的线性回归方程。
Call:
lm(formula= y ~ x1 + x2 + x3 + x4, data = cement)
Coefficients:
Estimate Std. Error t valuePr(>|t|)
(Intercept) 62.4054 70.0710 0.891 0.3991
x1 1.5511 0.7448 2.083 0.0708.
x2 0.5102 0.7238 0.705 0.5009
x3 0.1019 0.7547 0.135 0.8959
x4 -0.1441 0.7091 -0.203 0.8441
---
Signif.codes: 0 ‘***’0.001 ‘**’ 0.01‘*’ 0.05‘.’ 0.1 ‘ ’ 1
Residualstandard error: 2.446 on 8 degrees of freedom
MultipleR-squared: 0.9824, Adjusted R-squared: 0.9736
F-statistic:111.5 on 4 and 8 DF, p-value:4.756e-07
lm.step=step(lm.sol)
主要内容:
3.1 与广义线性线性模型有关的R函数
glm(formula,family = gaussian, data, weights, subset, na.action, start = NULL, etastart,mustart, offset, control = list(...), model = TRUE, method ="glm.fit", x = FALSE, y = TRUE, contrasts = NULL, ...)
formula:anobject of class "formula"(or one that can be coerced to that class): a symbolic description of the modelto be fitted. The details of model specification are given under ‘Details’.
family:adescription of the error distribution and link function to be used in themodel. This can be a character string naming a family function, a familyfunction or the result of a call to a family function. (Seefamily fordetails of family functions.)
data :anoptional data frame, list or environment (or object coercible by as.data.frame to adata frame) containing the variables in the model. If not found in data, thevariables are taken from environment(formula), typically the environment fromwhich glm is called.
weights:anoptional vector of ‘priorweights’ to be used in the fitting process.Should be NULL or a numeric vector.
Value
glmreturns an object of class inheriting from "glm" which inherits fromthe class "lm". See later in this section. If a non-standard methodis used, the object will also inherit from the class (if any) returned by thatfunction.
Thefunction summary(i.e., summary.glm) canbe used to obtain or print a summary of the results and the functionanova(i.e.,anova.glm) toproduce an analysis of variance table.
R使用:
fm=glm(formula,family=gaussian(link=identity),data=data.frame)
广义线性模型与线性模型相同,但效率低很多。
为研究高压电线对牲畜的影响,R.Norell研究小的电流对农场动物的影响。他在实验中,选择了7头,6种电击强度(0、1、2、3、4、5mA).每头牛被电击30下,每种强度5下,按随机的次序进行。然后重复整个实验,每头牛共被电击60下。对每次电击,响应变量—嘴巴运动,或者出现,或者不出现。表中的数据给出每种电击强度70次实验中响应的总次数。试分析电击对牛的影响。
Call:
glm(formula= y ~ x, family = binomial, data = norell)
DevianceResiduals:
1 2 3 4 5 6
-2.2507 0.3892 -0.1466 1.1080 0.3234 -1.6679
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -3.3010 0.3238 -10.20 <2e-16 ***
x 1.2459 0.1119 11.13 <2e-16 ***
---
Signif.codes: 0 ‘***’0.001 ‘**’ 0.01‘*’ 0.05‘.’ 0.1 ‘ ’ 1
(Dispersionparameter for binomial family taken to be 1)
Null deviance: 250.4866 on 5 degrees of freedom
Residualdeviance: 9.3526 on 4 degrees of freedom
AIC:34.093
Numberof Fisher Scoring iterations: 4
当电流强度为3.5时,求有相应的牛的概率
pre=predict(glm.sol,data.frame(x=3.5))
p=exp(pre)/(1+exp(pre))
输出结果:0.742642
控制:
有50%的牛有响应,求电流强度
x=glm.sol$coefficients[[1]]/glm.sol$coefficients[[2]]
结果:
2.649439
- R之回归分析
- R语言回归分析之影响分析
- R语言之回归分析篇
- R语言之回归分析篇
- 艾叶 R学习笔记之回归分析
- R语言-回归分析
- R语言回归分析
- R回归分析
- R-回归分析
- R语言数据分析系列之九 - 逻辑回归
- R语言_回归分析
- R语言-简单回归分析
- R语言中的回归分析
- R中的线性回归分析
- R语言做回归分析
- R语言回归分析实例
- 回归分析之线性回归
- R语言做如何做多元回归分析(逐步回归)之 提取回归结果参数-调整R方,标准化回归系数
- 母函数相关题目
- Sicily 2680. Sequence
- 学习开篇--计算机网络
- [C++学习]继承
- ActivityThead ActivityManagerService 和activity关系概述
- R之回归分析
- iOS——UIScrollView的适配问题
- boost asio timer,linux time and timer
- 《Effective C++》:条款46-条款47
- 工作总结第十天
- Java基础(包、多线程)
- 【java学习笔记】application&applet 示例
- MySql 基本操作语句整理
- Makefile浅谈