R in action读书笔记(8)-第八章:回归(上)

8.1回归的多面性

8.2 OLS回归

OLS回归拟合模型形式:

为了能够恰当地解释oLs模型的系数,数据必须满足以下统计假设。

口正态性对于固定的自变量值,因变量值成正态分布。

口独立性Yi值之间相互独立。

口线性因变量与自变量之间为线性相关。

口同方差性因变量的方差不随自变量的水平不同而变化。也可称作不变方差,但是说同方差性感觉上更犀利。

8.2.1用lm()拟合回归模型

myfit<-lm(formula,data)

formula指要拟合的模型形式,data是一个数据框,包含了用于拟合模型的数据。

表达式(formula):Y~X1+X2+…+Xk

 

8.2.2简单线性回归

> fit<-lm(weight~height,data=women)

> summary(fit)

Call:

lm(formula = weight ~height, data = women)

Residuals:

Min 1Q Median 3Q Max

-1.7333 -1.1333-0.3833 0.7417 3.1167

Coefficients:

Estimate Std. Error t valuePr(>|t|)

(Intercept)-87.51667 5.93694 -14.74 1.71e-09 ***

height 3.45000 0.09114 37.85 1.09e-14 ***

---

Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’1

Residual standarderror: 1.525 on 13 degrees of freedom

MultipleR-squared: 0.991, Adjusted R-squared: 0.9903

F-statistic: 1433 on 1 and 13 DF, p-value: 1.091e-14

> plot(women$height,women$weight,xlab="h",ylab="w")

> abline(fit)

8.2.3多项式回归

> plot(women$height,women$weight,xlab="h",ylab="w")

> abline(fit)

> fit2<-lm(weight~height+I(height^2),data=women)

> plot(women$height,women$weight,xlab="height(ininches)",ylab="weight (in lbs)")

> lines(women$height,fitted(fit2))

8.2.4多元线性回归

> library(car)

> states<-as.data.frame(state.x77[,c("Murder","Population","Illiteracy","Income","Frost")])

> cor(states)

Murder PopulationIlliteracy Income

Murder 1.0000000 0.3436428 0.7029752 -0.2300776

Population 0.3436428 1.0000000 0.1076224 0.2082276

Illiteracy 0.7029752 0.1076224 1.0000000 -0.4370752

Income -0.2300776 0.2082276 -0.4370752 1.0000000

Frost -0.5388834 -0.3321525 -0.6719470 0.2262822

Frost

Murder -0.5388834

Population -0.3321525

Illiteracy -0.6719470

Income 0.2262822

Frost 1.0000000

> scatterplotMatrix(states,spread=FALSE,lty.smooth=2,main="spm")

8.2.5有交互项的多元线性回归

> fit<-lm(mpg~hp+wt+hp:wt,data=mtcars)

> summary(fit)

Call:

lm(formula = mpg ~ hp +wt + hp:wt, data = mtcars)

Residuals:

Min 1Q Median 3Q Max

-3.0632 -1.6491-0.7362 1.4211 4.5513

Coefficients:

Estimate Std. Error t valuePr(>|t|)

(Intercept)49.80842 3.60516 13.816 5.01e-14 ***

hp -0.12010 0.02470 -4.863 4.04e-05 ***

wt -8.21662 1.26971 -6.471 5.20e-07 ***

hp:wt 0.02785 0.00742 3.753 0.000811 ***

---

Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’1

 

Residual standarderror: 2.153 on 28 degrees of freedom

MultipleR-squared: 0.8848, Adjusted R-squared: 0.8724

F-statistic: 71.66 on 3and 28 DF, p-value: 2.981e-13

Effects包中的effect()函数,可以用图形展示交互项的结果

Plot(effect(term,mod,xlevels),multiline=TRUE)

term即模型要画的项,mod为通过lm ( )拟合的模型,xlevels是一个列表,指定变量要设定的常量值,multiline=TRUE选项表示添加相应直线。

欢迎关注:

posted @ 2015-04-23 09:10  机器学习算法与Python  阅读(706)  评论(0编辑  收藏  举报