[数据挖掘] 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())
[数据挖掘] 43 时间序列预测2 季节性效应 基于回归的预测 美国铁路(美铁)客运公司数据
于 2023-07-16 01:33:10 首次发布