[数据挖掘] 43 时间序列预测2 季节性效应 基于回归的预测 美国铁路(美铁)客运公司数据

[数据挖掘] 43 时间序列预测2 季节性效应 基于回归的预测 美国铁路(美铁)客运公司数据_哔哩哔哩_bilibili
# 1 季节性效应模型
ridership_df = tsatools.add_trend(ridership_ts, trend='c')
print(ridership_df.head())
ridership_df['Month'] = ridership_df.index.month # 1 .month建立分类变量
print(ridership_df.head(13))

train_df = ridership_df[:nTrain]
test_df= ridership_df[nTrain:]
ridership_lm_season = smf.ols(formula='Ridership ~ C(Month)', data=train_df).fit() # 2 自动创建虚拟变量,并 3 在线性回归模型中添加Month预测变量


#绘图
fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(9,7.5), dpi=180)
ridership_lm_season.predict(train_df).plot(color='yellowgreen', ax=axes[0])
ridership_lm_season.predict(test_df).plot(color='yellowgreen',linestyle='dashed', ax=axes[0])
residual = train_df['Ridership'] - ridership_lm_season.predict(train_df) #残差
residual.plot(ax=axes[1], color='mediumvioletred')
residual = test_df['Ridership'] - ridership_lm_season.predict(test_df)
residual.plot(ax=axes[1], color='mediumvioletred', linestyle='dashed')
graphLayout(axes, train_df, test_df)
plt.show()

#季节性效应模型-摘要输出
print(ridership_lm_season.summary())


# 2 趋势和季节性效应模型
ridership_seasondf = ridership_df
train_df_season = ridership_seasondf[:nTrain]
test_df_season = ridership_seasondf[nTrain:]

formula = 'Ridership~trend + np.square(trend) + C(Month)'  # trend_squared
ridership_lm_trendseason = smf.ols(formula=formula, data=train_df_season).fit()

#绘图
fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(9, 7.5), dpi=200)
ridership_lm_trendseason.predict(train_df_season).plot(color='purple', ax=axes[0])
ridership_lm_trendseason.predict(test_df_season).plot(color='purple',linestyle='dashed', ax=axes[0])
residual = train_df_season['Ridership'] - ridership_lm_trendseason.predict(train_df_season) #残差
residual.plot(ax=axes[1], color='green')
residual = test_df_season['Ridership'] - ridership_lm_trendseason.predict(test_df_season)
residual.plot(ax=axes[1], color='green', linestyle='dashed')
graphLayout(axes, train_df_season, test_df_season)
plt.show()

#趋势和季节性效应模型-摘要输出
print(ridership_lm_trendseason.summary())

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值