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

如何基于lm()多元回归模型延伸2022趋势线并在ggplot中差异化展示

问题

我拥有2016至2021年的数据,需使用R语言的lm()多元回归模型进行预测,并将趋势延伸至2022年,同时在ggplot绘图中用不同线型和颜色展示该延伸趋势。此前尝试过prophet模型,但因它的方程与lm()模型不同,结果存在差异,会影响作业完成,因此寻求严格遵循lm()模型方程的实现方法。

当前使用的ggplot代码:

ggplot(data=df, mapping = aes(x=Date)) +
        geom_line(mapping= aes(y=observed, 
        color='ObservedCases'))+ 
        geom_point(mapping= aes(y=observed, 
        color='ObservedCases')) +
        geom_line(mapping=aes(y=predicted, 
        color='PredictedCases'), size=1) +
        geom_point(mapping=aes(y=predicted, 
        color='PredictedCases'), size=1) +
        scale_x_date('Time',date_breaks='1 year',
                 date_labels="%Y") +
            labs(y='Number of Cases', colour='Trend Lines')

数据样例:

df <- data.frame(
  Date = structure(c(16801, 16832, 16861, 16892, 16922, 16953), class = "Date"), 
  observed = c(1793, 2708, 1834, 1015, 1195, 809), 
  predicted = c(3946.87980311387, 2603.21225568186, 1582.70357908881, 2788.41468480798, 862.923305922486, 268.345471468495))
解决方法

核心是基于你拟合好的lm()模型生成2022年的预测数据,再与原数据合并后绘图,全程严格遵循lm()的回归方程。

步骤1:重新拟合lm()模型

lm()无法直接处理日期类型,需将Date转换为数值型(比如距离起始日期的天数)作为自变量。以下以单变量回归为例,你可替换为自己的多元回归公式:

# 将日期转为数值型
df$Date_num <- as.numeric(df$Date)

# 拟合lm模型
lm_model <- lm(observed ~ Date_num, data = df)

步骤2:生成2022年的预测数据集

根据原数据的时间间隔,创建包含2022年日期的新数据集,并完成预测:

# 生成2022年日期序列(可根据原数据时间间隔调整by参数,比如"quarter")
future_dates <- seq.Date(from = as.Date("2022-01-01"), 
                         to = as.Date("2022-12-01"), 
                         by = "month")

# 转为数据框并添加数值型日期列
future_df <- data.frame(Date = future_dates)
future_df$Date_num <- as.numeric(future_df$Date)

# 用lm模型预测2022年病例数
future_df$predicted_extended <- predict(lm_model, newdata = future_df)

步骤3:合并数据并绘图

将原数据与预测数据合并,通过linetype区分原有预测和延伸趋势,color区分观测值与预测值:

# 给原数据和预测数据添加标识列
df$trend_type <- "Original"
future_df$trend_type <- "Extended"
future_df$observed <- NA  # 2022年无观测值

# 合并数据
combined_df <- rbind(df, future_df)

# 绘图
ggplot(data = combined_df, mapping = aes(x = Date)) +
  # 绘制观测值
  geom_line(aes(y = observed, color = "Observed Cases")) +
  geom_point(aes(y = observed, color = "Observed Cases")) +
  # 绘制原有预测
  geom_line(aes(y = predicted, color = "Predicted Cases", linetype = trend_type), size = 1) +
  geom_point(aes(y = predicted, color = "Predicted Cases"), size = 1) +
  # 绘制延伸趋势
  geom_line(aes(y = predicted_extended, color = "Predicted Cases", linetype = trend_type), size = 1) +
  # 调整坐标轴与图例
  scale_x_date("Time", date_breaks = "1 year", date_labels = "%Y") +
  scale_linetype_manual(values = c("Original" = "solid", "Extended" = "dashed")) +
  labs(y = "Number of Cases", colour = "Series", linetype = "Trend Type") +
  theme_minimal()

补充说明

  • 若你使用的是多元回归,只需在lm_model的公式中加入其他自变量,并确保future_df包含这些自变量的对应取值(比如季节变量需给2022年日期匹配对应值)。
  • 线型、颜色可通过scale_linetype_manual和scale_color_manual自定义调整。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 07:05:42