You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用GAM/GLM预测数据并将预测值添加至原始数据集

使用GAM模型预测并扩展数据集至2010年

问题背景

我有一个大型数据集需要预测部分数值,以下是可复现的小型示例。希望用GAM或GLM模型预测未来数年数据,但缺乏统计背景无从下手。请指导如何预测到2010年(额外3年)并将预测值添加到原始数据集m中?

原始代码:

library(mgcv)
library(tidyverse)
m <- structure(list(year = c(2003, 2003, 2003, 2003, 2003, 2003, 2003, 
2003, 2003, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 
2005, 2005, 2005, 2006, 2006, 2006, 2006, 2006, 2007, 2007, 2007, 
2007, 2007), month = c("August", "August", "September", "September", 
"October", "October", "November", "November", "December", "August", 
"August", "September", "September", "October", "October", "November", 
"November", "December", "August", "August", "September", "October", 
"October", "November", "November", "December", "August", "August", 
"September", "September", "October"), date = structure(c(12265, 
12281, 12296, 12311, 12326, 12342, 12357, 12372, 12387, 12631, 
12647, 12662, 12677, 12692, 12708, 12723, 12738, 12753, 12996, 
13012, 13027, 13422, 13438, 13453, 13468, 13483, 13726, 13742, 
13757, 13772, 13787), class = "Date"), turb = c(12.5, 11.8, 8.9, 
6.2, 5.1, 3.4, -0.1, 0, -0.1, 12.5, 10.4, 8.6, 6.1, 4.7, 1.4, 
0.1, -0.1, -0.1, 12.5, 11, 8, 4.4, 1.1, 0.5, -0.2, -0.2, 12.5, 
11.7, 10, 5.9, 3.6)), row.names = c(NA, -31L), class = c("tbl_df", 
"tbl", "data.frame"))

m$date <- as.numeric(m$date)
mod1 <- gam(turb ~ s(date), data = m)
summary(mod1)

#How can I predict 3 more years (to 2010) and have those predictions
#added to the bottom of the original dataset(m)?

解决方案步骤

1. 恢复日期格式(更直观且便于后续操作)

你之前将date转为数值型,其实可以先恢复为Date格式,mgcv包的GAM模型会自动处理日期变量,后续生成预测序列也更方便:

# 恢复date为Date类型
m$date <- as.Date(m$date, origin = "1970-01-01")
# 重新训练模型(效果与数值型date训练的模型一致)
mod1 <- gam(turb ~ s(date), data = m)

2. 创建未来预测的日期序列

先确定原始数据的最后观测日期,再生成到2010年12月31日的日期序列,保持和原始数据一致的15天观测间隔:

# 获取原始数据的最后日期
last_date <- max(m$date)
# 生成未来日期序列,匹配原始数据的15天间隔
future_dates <- seq.Date(from = last_date + 1, to = as.Date("2010-12-31"), by = "15 days")

# 将未来日期整理为模型可识别的数据框格式
future_data <- tibble(date = future_dates)

3. 用GAM模型生成预测值

使用predict()函数生成预测结果,同时可以选择计算预测值的标准误(可选):

# 预测turb值,同时计算标准误
predictions <- predict(mod1, newdata = future_data, se.fit = TRUE)

# 给未来数据添加预测值、标准误,以及年份、月份信息(匹配原始数据格式)
future_data <- future_data %>%
  mutate(
    turb = predictions$fit,
    turb_se = predictions$se.fit,
    year = lubridate::year(date),
    month = lubridate::month(date, label = TRUE, abbr = FALSE)
  )

4. 合并原始数据与预测数据

给原始数据补充turb_se列(填充NA值),再将原始数据和预测数据按行合并:

# 给原始数据添加turb_se列
m <- m %>%
  mutate(turb_se = NA_real_)

# 合并数据集
m_combined <- bind_rows(m, future_data)

最终验证

你可以通过以下代码查看合并后的数据集,确认原始数据和预测数据是否正确拼接:

# 查看前5行原始数据
head(m_combined, 5)
# 查看后5行预测数据
tail(m_combined, 5)

补充说明

  • 如果想用GLM模型,只需将gam()替换为glm(),例如mod_glm <- glm(turb ~ date, data = m),后续预测步骤完全一致。但从数据趋势来看,GAM更适合捕捉非线性时间规律,预测效果会更好。
  • 若需要调整预测频率,比如每月一个预测值,只需将by = "15 days"改为by = "month"即可。

内容的提问来源于stack exchange,提问作者Salvador

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 11:42:20