R之回归分析

来源:互联网 发布:雷锋站长源码 编辑:程序博客网 时间:2024/04/19 12:46
²  
数据挖掘系列之
  回归分析
²导论
²相关概念
²回归分析(regressionanalysis)是确定两种或两种以上变量间相互依赖的定量关系的一种统计分析方法,运用十分广泛。
²回归分析按照涉及的自变量的多少,可分为一元回归分析和多元回归分析。
²按照自变量和因变量之间的关系类型,可分为线性回归分析和非线性回归分析。
²如果在回归分析中,只包括一个自变量和一个因变量,且二者的关系可用一条直线近似表示,这种回归分析称为一元线性回归分析。
²如果回归分析中包括两个或两个以上的自变量,且因变量和自变量之间是线性关系,则称为多元线性回归分析。
²导论
²研究的问题

      1、确定Y与                间的定量关系表达式,这种表达式是回归方程;

²

     2、对求得的回归方程的可信度进行检验;

²

    3、判断自变量                       对Y有无影响;

²

    4、利用所求的回归方程进行预测和控制。

²
²导论
²建立回归模型的步骤

1、确定研究对象,明确哪个变量是解释变量,哪个变量是预报变量。

²2、画出解释变量和预报变量之间的散点图,观察它们之间的关系。
²3、确定回归方程的类型,如y =ax + b。
²4、按一定规则估计回归方程中的参数,如最小二乘法。
²5、得出结果后分析残差是否有异常,若存在异常则检查数据是否有误,或模型是否合适。
²
²主要内容

1、 一元线性回归

2 、多元线性回归

3、 广义线性回归之logistic回归

²1.一元线性回归

引例:

例1 由专业知识知道,合金的强度Y(N/mm2)与合金中碳含量X(%)有关。为了解他们之间的关系,从生产中收集了一批数据(xi,yi)(i=1,2,…,n),具体数据见表1。
 

 表1 合金的强度和合金中碳的含量数据量

²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.1一元线性回归模型
² 1.2参数β0,β1 的最小二乘估计:
² 1.2参数β0,β1 的最小二乘估计:
² 1.2参数β0,β1 的最小二乘估计:
² 1.2参数β0,β1 的最小二乘估计:
²1.3 t检验:
²1.4相关系数检验法:
²例1.5

求例1的回归方程,并对相应的方程做检验.

²1.6参数的区间估计

对于给定的置信水平1-α,则有:

²1.7 预测

1.根据自变量x 的取值预测 y 的取值;

2.预测可分两种类型:

⑴.点预测:

⑵.区间预测:

²例1.8

求例1中X=x0=0.16时相应Y的概率为0.95的预测区间.

²2  多元线性回归分析
²2  多元线性回归分析
²2.2回归系数的估计
²2.3 显著性检验

1.回归系数的显著性检验:

²2.4典例分析
²根据经验,在人的身高相等的情况下,血压的收缩压Y与体重X1(kg)、年龄X2(岁数)有关。现收集了13个男子数据,见表,试建立Y关于X1,X2的线性回归方程。
²
²2.4典例分析(Code)
²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))
²lm.sol=lm(y~x1+x2,data=blood)
²summary(lm.sol)
²Call:
²lm(formula = y ~ x1 + x2,data = blood)                            求得表达式为:y=2.14X1+0.40X2-62.96
²Residuals:
²    Min     1Q  Median      3Q    Max
²-4.0404 -1.0183  0.4640 0.6908  4.3274
²Coefficients:
²             Estimate Std. Error t valuePr(>|t|)   
²(Intercept) -62.96336   16.99976 -3.704 0.004083 **
²x1            2.13656    0.17534 12.185 2.53e-07 ***
²x2            0.40022    0.08321  4.810 0.000713 ***
²---
²Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’1
²Residual standard error:2.854 on 10 degrees of freedom
²Multiple R-squared:  0.9461,   Adjusted R-squared:  0.9354
²F-statistic: 87.84 on 2 and10 DF,  p-value: 4.531e-07
²
²2.4典例分析--参数β的区间估计
²2.5 预测
²2.6修正拟合模型

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)

²2.7  逐步回归方法

首先,求出一个基本回归方程,然后,逐步添加解释变量,根据添加解释变量对拟合优度的改进和对其它回归系数的影响等决定是否保留添加的解释变量。

      1.如果新添加的解释变量改进拟合优度,并且其它回归系数在统计上仍是显著的,那么,保留添加的解释变量。新添加解释变量不引起多重共线性;

      2.新添加的解释变量未能改进拟合优度,且对其它回归系数也没有影响,则新添加解释变量不予保留,不产生多重共线性;

      3.如果新添加的解释变量,不仅影响拟合优度,而且显著地影响其它回归系数的显著性、符号、数值,甚至使参数符号反号到不能接受的地步,则认为新添加的解释变量可能是引起多重共线性的原因。

²例2.7

某种水泥在凝固时放出的热量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

²Step处理:

lm.step=step(lm.sol)

²2.8  回归诊断

主要内容:

误差项是否满足独立性、等方差性、正态性;
选择线性模型是否合适;
是否存在异常样本;
回归分析的结果是否对某些样本的依赖过重,即回归模型是否具备稳定性;
变量之间是否存在高度相关,即是否有多重共线性问题存在;
²3.  广义线性回归模型

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.

²3.2  二项分布族

R使用:

fm=glm(formula,family=gaussian(link=identity),data=data.frame)

广义线性模型与线性模型相同,但效率低很多。

²例  3.2

为研究高压电线对牲畜的影响,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

²题目
²Eg:50位急性林巴细胞性白血病病人,在入院治疗时取得了外辕血中的细胞数(X1,千个/mm3)、林巴结浸润等级(X2,分为0,1,2,3级)、出院后有无巩固治疗(X3,“1”表示有巩固治疗,“0”表示无巩固治疗).通过数访取得病人的生存时间,并以变量Y=0表示生存时间在1年以内,Y=1表示生存时间在1年或1年以上.关于Xl,X2,X3和Y的观测数据,如表所示.试用Logistic回归模型分析病人生存时间长短的概率与X1,X2,X3的关系.
²
²
0 0
原创粉丝点击