# R in action读书笔记（8）-第八章：回归（上）

8.1回归的多面性

8.2 OLS回归

OLS回归拟合模型形式:

8.2.1用lm()拟合回归模型

myfit<-lm(formula,data)

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

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

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

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

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选项表示添加相应直线。

Top