基于R语言实现线性回归模型及测试应用

基于R语言实现线性回归模型及测试应用

mpg=B0+B1*dart+E

调用R语言函数

r 复制代码
>data(mtcars)
>head(mtcars)
> mod <- lm(formula = mpg ~ drat, data = mtcars)
> summary(mod)
> # 画出拟合的线
>abline(mod, col = "blue")
>points(mtcars$drat, mtcars$mpg) 

显示效果图:

作为示例,我们使用线性回归模型拟合汽车燃油消耗量(miles per gallon, mpg)和后轴数比(Rear axle ratio, dart)之间的关系。我们将构建的模型形式为:

mpg=B0+B1*dart+E

r 复制代码
#模型形式为
mpg字段=B0+B1*dart+E

#我们拟合的模型为:
mpg= -7.525 + 7.678*drat+E     (   E~Normal(0,4.485^2)   )

R语言中,拟合线性回国模型只需要使用lm()函数指定formula和data。formula参数的完整形式应该为y ~ x1 + x2 + ... + 1,

其中x1,x2等表示自变量;1表示截距,可以省略不写。拟合以后,我们使用summary()函数显示拟合的结果。

我们使用R软件自带的mtcars数据集为示例。该数据集来自于1974年美国杂志《Motor Trend》,它包含了32辆汽车的燃油消耗量与10个描述汽车设计和性能的参数数据。我们使用data()命令即可导入该数据集到计算机内存:

r 复制代码
#加载cars数据
> data(mtcars)

#显示前6行
> head(mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
#绘制二维平面点图
>plot(mtcars$drat,mtcars$mpg,xlab="Rear axle ratio",ylab="Miles per gallon")

> mod <- lm(formula = mpg ~ drat, data = mtcars)
> summary(mod)

Call:
lm(formula = mpg ~ drat, data = mtcars)

Residuals:
    Min      1Q  Median      3Q     Max 
-9.0775 -2.6803 -0.2095  2.2976  9.0225 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)   -7.525      5.477  -1.374     0.18    
drat           7.678      1.507   5.096 1.78e-05 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 4.485 on 30 degrees of freedom
Multiple R-squared:  0.464,	Adjusted R-squared:  0.4461 
F-statistic: 25.97 on 1 and 30 DF,  p-value: 1.776e-05

> abline(mod, col = "blue")
> points(mtcars$drat, mtcars$mpg) 

这里我们暂时忽略其他的输出结果,仅关注Coefficients部分展示的内容以及模型的标准误差rse(Residual standard error)。可以看到,模型估计出参数:

Estimate=估计

Std. Error=标准差

rse=Residual standard error= 4.485

r 复制代码
         估计          标准差
B0      -7.525         5.477
B1      7.678          1.507
Rse     4.485

可以计算得到线性回归模型公式

mpg字段与drat字段的线性回归模型公式如下所示:

mpg= -7.525 + 7.678*drat+E ( E~Normal(0,4.485^2) )

r 复制代码
# 画出拟合的线
abline(mod, col = "blue")
# 画出数据点
points(mtcars$drat, mtcars$mpg) 

本blog地址:https://blog.csdn.net/hsg77

相关推荐
郑州光合科技余经理4 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1234 天前
matlab画图工具
开发语言·matlab
dustcell.4 天前
haproxy七层代理
java·开发语言·前端
norlan_jame4 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone4 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054964 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月4 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_531237174 天前
C语言-数组练习进阶
c语言·开发语言·算法
Railshiqian4 天前
给android源码下的模拟器添加两个后排屏的修改
android·开发语言·javascript
雪人不是菜鸡4 天前
简单工厂模式
开发语言·算法·c#