基于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

相关推荐
JavaPub-rodert2 分钟前
我把 OpenAI 协议塞进了 Go 工具库:go-commons 开始支持 AI 了
开发语言·人工智能·golang
caimouse2 分钟前
ReactOS 图形系统分析(23):引擎内存管理 — mem.c
c语言·开发语言
caimouse3 分钟前
ReactOS 图形系统分析(15):窗口对象 — EWNDOBJ(engwindow.c)
c语言·开发语言
青 春 记 忆6 分钟前
零基础入门python07:让程序记住数据——JSON文件和异常处理
开发语言·windows·python·json·python3.11
W_3260028 分钟前
Python 常用标准 / 第三方库:random、tqdm、turtle、jieba 用法
开发语言·python
caimouse1 小时前
ReactOS 图形系统分析(17):区域填充 — EngPaint / IntEngPaint(paint.c)
c语言·开发语言·算法
生态学者1 小时前
Ecological Indicators | 机场鸟调的新方法:用 AWM-PC1 读懂干扰梯度下的鸟类群落
人工智能·算法·r语言·微信公众平台
北风toto1 小时前
研发效能与后端核心技术全景指南:从CI/CD到共性组件实战
java·开发语言·ci/cd
Billy121381 小时前
Day12-C++20 Coroutines(上):协程原理与Promise/Awaitable机制
开发语言·c++进阶学习
01二进制代码漫游日记1 小时前
C++基础入门速通
java·开发语言·c++