使用R语言建立回归模型并分割训练集和测试集

通过简单的回归实例,可以说明数据分割为训练集和测试集的必要性。以下先建立示例数据:

R 复制代码
set.seed(123) #设置随机种子
x <- rnorm(100, 2, 1) # 生成100个正态分布的随机数,均值为2,标准差为1
y = exp(x) + rnorm(5, 0, 2) 
# 生成一个新的变量y,它是x的指数函数值加上5个正态分布的随机数
# 均值为0,标准差为2
plot(x, y)
linear <- lm(y ~ x)
abline(a = coef(linear)[1], b = coef(linear)[2], lty = 2)

查看建立的数据信息:

R 复制代码
summary(linear)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.6481 -3.7122 -1.9390  0.9698 29.8283 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -13.6323     1.6335  -8.345 4.63e-13 ***
## x            11.9801     0.7167  16.715  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.51 on 98 degrees of freedom
## Multiple R-squared:  0.7403, Adjusted R-squared:  0.7377 
## F-statistic: 279.4 on 1 and 98 DF,  p-value: < 2.2e-16

接受一些x和y的模拟数据,然后绘制一个最贴切的线性模型。根据以上的出结果,多重R方值为0.738,越接近1越好。再试一下通过标准三七开的随机采样分割数据:

R 复制代码
data <- data.frame(x, y)
data.samples <- sample(1:nrow(data), nrow(data) * 0.7, replace = FALSE)
training.data <- data[data.samples, ]
test.data <- data[-data.samples, ]
train.linear <- lm(y ~ x, training.data)
train.output <- predict(train.linear, test.data)

计算均方根误差 ,根据输入x,比较y与测试集中的实际值,在评估时使用特定的因变量。可采用均方根误差作为测试指标:

R 复制代码
RMSE.df = data.frame(predicted = train.output, actual = test.data$y,
                     SE = ((train.output - test.data$y)^2/length(train.output)))
head(RMSE.df)
##    predicted    actual         SE
## 2   7.874300  6.383579 0.07407499
## 3  28.504227 34.624423 1.24855995
## 4  11.341893  7.233768 0.56255641
## 5  12.019753  6.505638 1.01351529
## 12 14.678243 11.102747 0.42613909
## 15  4.118657  2.335049 0.10604193
R 复制代码
sqrt(sum(RMSE.df$SE))
## [1] 6.946493
R 复制代码
train.quadratic <- lm(y ~ x^2 + x, training.data)
quadratic.output <- predict(train.quadratic, test.data)
RMSE.quad.df = data.frame(predicted = quadratic.output, actual = test.data$y, SE = ((quadratic.output - test.data$y)^2/length(train.output)))
head(RMSE.quad.df)
##    predicted    actual         SE
## 2   7.874300  6.383579 0.07407499
## 3  28.504227 34.624423 1.24855995
## 4  11.341893  7.233768 0.56255641
## 5  12.019753  6.505638 1.01351529
## 12 14.678243 11.102747 0.42613909
## 15  4.118657  2.335049 0.10604193
sqrt(sum(RMSE.quad.df$SE))
## [1] 6.946493

根据上述输出表明,将多项式从一次调整为二次有助于减少模型预测值与实际值之间的误差,接着再提高多项式的次数并查看对均方根误差的影响

R 复制代码
train.polyn <- lm(y ~ poly(x, 4), training.data)
polyn.output <- predict(train.polyn, test.data)
RMSE.quad.df = data.frame(predicted = polyn.output, actual = test.data$y,
                          SE = ((polyn.output - test.data$y)^2/length(train.output)))
head(RMSE.quad.df)
##    predicted    actual           SE
## 2   5.228193  6.383579 0.0444972216
## 3  34.410640 34.624423 0.0015234381
## 4   7.312166  7.233768 0.0002048764
## 5   7.789798  6.505638 0.0549688692
## 12  9.946884 11.102747 0.0445339986
## 15  3.482548  2.335049 0.0438918352
sqrt(sum(RMSE.quad.df$SE))
## [1] 0.8836878

与二次方程的拟合情况相比,可以看到均方根误差有所上升,符合用高次方程过度拟合数据的结果。

相关推荐
道不尽世间的沧桑1 小时前
第17篇:网络请求与Axios集成
开发语言·前端·javascript
久绊A1 小时前
Python 基本语法的详细解释
开发语言·windows·python
软件黑马王子4 小时前
C#初级教程(4)——流程控制:从基础到实践
开发语言·c#
闲猫4 小时前
go orm GORM
开发语言·后端·golang
李白同学6 小时前
【C语言】结构体内存对齐问题
c语言·开发语言
黑子哥呢?7 小时前
安装Bash completion解决tab不能补全问题
开发语言·bash
青龙小码农7 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿7 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
彳卸风8 小时前
Unable to parse timestamp value: “20250220135445“, expected format is
开发语言
dorabighead8 小时前
JavaScript 高级程序设计 读书笔记(第三章)
开发语言·javascript·ecmascript