【R语言小小屋】:NO.1 第三周案例

1. 案例介绍

1.1 案例背景

线性回归模型的构建。

1.2 案例任务

  1. 学会查看和调用语言自带的数据集,并会查看各个数据集信息。

  2. 以women数据集为研究对象,建立回归模型。对于建立回归模型的流程包括:

    (1)描述weight与height之间的关系,绘制散点图;

    (2)构建预测weight关于height的一次线性回归模型,并给出对应的模型;

    (3)评价所构建的模型,给出评估指标,如

    (4)可视化所构建模型的效果,如原始值-预测值走势图和残差图;

    (5)附加:尝试建立预测weight关于height的多项式线性回归模型。

  3. 总结在完成的任务时遇到的困难与进一步的展望。

2. R语言实现

2.1 数据集

2.1.1 调用数据集与查看数据集信息

 # print(data(package='具体的package名')) 打印package中包含的datasets
 # ?数据集名称 给出数据集的描述,展示在右侧的“help”窗口。
 print(data(package='MASS'))  # 查看MASS包的所有数据集

2.1.2 获取women数据集

  # ? women # 查看数据集描述,也就是使用help()函数

Description

This data set gives the average heights and weights for American women aged 30–39.

Usage

women

Format

A data frame with 15 observations on 2 variables.

[,1] height numeric Height (in) [,2] weight numeric Weight (lbs)

Details

The data set appears to have been taken from the American Society of Actuaries Build and Blood Pressure Study for some (unknown to us) earlier year.

The World Almanac notes: “The figures represent weights in ordinary indoor clothing and shoes, and heights with shoes”.

Source

The World Almanac and Book of Facts, 1975.

References

McNeil, D. R. (1977) Interactive Data Analysis. Wiley.

 data(women)
 str(women)  # 数据描述
 head(women)  # 预览数据集结构

2.2 统计相关性分析

2.2.1 相关系数

 height_weight_cor<-cor(women$height,women$weight,method = "pearson")
 print(paste("身高与体重之间的相关系数为:",height_weight_cor))

2.2.2 散点图绘制

 plot(women, xlab = "Height (in)", ylab = "Weight (lb)", main = "women data: American women aged 30-39",col=rainbow(15))  # 绘制身高-体重散点图

2.3 线性回归模型构建

2.3.1 一次线性回归模型-1

 model_1<-lm(women$weight~.,data=women)  # 建立线性回归模型
 summary(model_1)  # 给出模型的摘要
 # “model_1”语句可以给出模型的简约介绍
 print(paste("所构建的模型为:","weight=",coef(model_1)[1],"+",coef(model_1)[2],"* height"))
 plot(women$height,women$weight,xlab="height",ylab="weight",main="线性回归模型曲线")  # 打出height与weight实际关系的散点图
 abline(model_1)  # 添加线性回归线

2.3.2 多项式线性回归模型-2

 women<-transform(women,height_2 = women$height**2)
 model_2<-lm(women$weight~women$height+women$height_2,data=women)  # 建立线性回归模型
 summary(model_2)  # 给出模型的摘要
 print(paste("所构建的模型为:","weight=",coef(model_2)[1],coef(model_2)[2],"* height"," + ",coef(model_2)[3],"* height_2"))

2.4 模型评价

2.4.1

 print(paste("线性回归模型拟合优度为:",0.991))
 print(paste("多项式线性回归模型拟合优度为:",0.9995))

2.4.2

 print(paste("线性回归模型MSE为:",1.525))
 print(paste("多项式线性回归模型MSE为:",0.3841))

2.5 可视化

2.5.1 原始值-预测值走势图

 plot(women$height,women$weight,xlab="height",ylab="weight",main="线性回归模型曲线")  # 打出height与weight实际关系的散点图
 abline(model_1)  # 添加线性回归线

2.5.2 残差图

 # 模型预测效果比较
 plot(rownames(women),women$weight,type="p",xlab="height",ylab="weight")
 lines(rownames(women),model_1$fit)  # 模型拟合效果比较
 
 plot(rownames(women),women$weight,type="p",xlab="height",ylab="weight")
 lines(rownames(women),model_2$fit)  # 模型拟合效果比较
 
 # 模型残差图
 predict<-predict(model_1,women)
 women<-transform(women,e_1=weight-predict)
 # 可以使用res<-residuals(model_1)得到残差值
 # 注意fitted(model_1)得到样本中的拟合值,而predict函数可预测新数据响应变量值
 plot(women$e_1,pch=3)
 abline(h=0)
 
 predict<-predict(model_2,women)
 women<-transform(women,e_2=weight-predict)
 plot(women$e_2,pch=3)
 abline(h=0)

2.5.3 模型检验

 print("模型1假设检验")
 anova(model_1)  # 模型假设检验-方差分析
 print("模型2假设检验")
 anova(model_2)  # 模型假设检验-方差分析

2.6 案例总结

本次案例偏简单。其中在讲解过程中,我们可以清晰发现:我们部分的同学在学习态度上需要进行调整,要注重代码书写的质量和结果分析的能力。另外,还需要多多交流。


撰稿:Janck.C

时间:2020/10/10

posted @ 2020-10-24 16:52  janck  阅读(399)  评论(0)    收藏  举报