python - 如何在 statsmodel ols 中获取 "in-sample"预测值(y hat)?

标签 python regression statsmodels

这是代码:

from   statsmodels.formula.api import ols
import io
import requests

url = "https://raw.githubusercontent.com/RInterested/datasets/gh-pages/mtcars.csv"
contents = requests.get(url).content
mtcars = pd.read_csv(io.StringIO(contents.decode('utf-8')))
print(mtcars.describe())
reg = ols('mpg ~ C(cyl) + wt', data=mtcars).fit() 
print(reg.summary())

这似乎返回模型的截距,语法与我喜欢的 R 非常相似。 print(dir(reg)) 中有一个 reg.predictreg.get_predict,但它们都没有返回预测值数据集中每个示例(案例或主题)的值。看起来好像它可能正在等待“样本外”数组吐出这些预测值。

我想获得实际数据集中示例的预测mpg,绘制为与实际数据云不同颜色的点,并叠加在回归线上:

enter image description here

最佳答案

样本内预测可作为结果实例上的 reg.fittedvalues 属性使用,并且在不带参数调用预测时使用 reg.predict()

当预测方法具有其他选项时,可以通过不指定一组新的解释变量 exog 来计算样本内观测值。

关于python - 如何在 statsmodel ols 中获取 "in-sample"预测值(y hat)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61976585/

相关文章:

python - 计算一组股票的相同移动平均线

python - 在文档字符串中使用类型别名

python - statsmodels 无法使用诸如登录异构类型行之类的函数来预测公式

python - 如何在非通用 View / View 集中使用分页?

python - 查找两个数据帧的两列之间的额外内容 - 相减

r - GAM 中的权重选项

machine-learning - 使用caffe从图像回归年龄时如何设计损失层

machine-learning - 如何使用投票回归器来预测特定实例?

python - 使用 python statsmodels 设置 ADF 测试的 maxlag 不起作用?

Python 统计模型 - AttributeError : 'ARMAResults' object has no attribute 'plot_predict'