results.get_prediction
时间: 2023-06-26 13:04:47 浏览: 40
I'm sorry, but I need more context to understand what you are referring to. Can you provide more information or the code snippet that you are working on?
相关问题
多元回归中results.get_prediction怎么用
在多元回归中,`results.get_prediction()`函数用于根据模型预测给定的自变量值的因变量值。它返回一个包含预测值和置信区间的结果对象。
以下是使用`results.get_prediction()`进行预测的示例代码:
```python
import statsmodels.api as sm
# 加载数据
data = sm.datasets.get_rdataset("Guerry", "HistData").data
# 确定自变量和因变量
y = data['Lottery']
X = data[['Literacy', 'Wealth', 'Region']]
# 添加截距项
X = sm.add_constant(X)
# 拟合多元回归模型
model = sm.OLS(y, X).fit()
# 预测新的自变量值
new_X = [[1, 70, 20, 1]]
# 预测因变量值和置信区间
pred = model.get_prediction(new_X)
print(pred.summary_frame(alpha=0.05))
```
在上面的示例中,我们首先加载数据,确定自变量和因变量,然后拟合多元回归模型。接下来,我们创建一个包含新的自变量值的列表`new_X`,并使用`results.get_prediction()`函数来预测因变量值和置信区间。最后,我们使用`summary_frame()`函数将预测结果以表格形式输出。
# 提取时间序列的预测值和真值 pred_dynamic = best_results.get_prediction(start = pd.to_datetime('2022-04-28'), dynamic=True, full_results=True) # 预测32天时用12.9,预测64天时用11.7 pred_dynamic_ci = pred_dynamic.conf_int() #存储计算值 # 提取时间序列的预测值和真值 sales_ts_forecasted = pred_dynamic.predicted_mean # sales_ts_truth = sales_ts_log['2020-04-28':] sales_ts_truth = experiment_arima_ts_log['2022-04-28':] #2019-12-09 # 预测64天用11.7 # 预测值和实际值起始时间必须一致,否则无法对比# 绘制实际图与预测值对比,样本内日期 import matplotlib.pyplot as plt axis = experiment_arima_ts['2020':].plot(label='Observed', figsize=(30, 15)) np.power(10, pred_dynamic.predicted_mean).plot(ax=axis, label='Dynamic Forecast', alpha=0.8) axis.fill_between(pred_dynamic_ci.index, pred_dynamic_ci.iloc[:, 0], pred_dynamic_ci.iloc[:, 1], color='k', alpha=.25) axis.fill_betweenx(axis.get_ylim(), pd.to_datetime('2022-04-28'), experiment_arima_ts.index[-1], alpha=.1, zorder=-1) # 浅蓝色部分起始位置 axis.set_xlabel('Years') axis.set_ylabel('Tractor Sales') plt.legend(loc='best') plt.show() # 实际值与预测值的对比
这段代码是用来绘制实际值与预测值的对比图。它首先提取了时间序列的预测值和真值,然后使用预测值和真值的起始时间绘制了对比图。预测值使用了动态预测,其中预测32天时使用了12.9,预测64天时使用了11.7。图中的实线代表观测到的值,虚线代表动态预测的值,浅蓝色部分表示预测区间。该图可以帮助我们评估预测的准确性。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)