如何解决返回某个x值的估算y值使用数据框,散点图,统计模型
我正在寻找:“我估算出的是60岁女性的肌肉质量估算值”,该图是通过以下代码生成的:
data = pd.DataFrame({'x': x,'y': y})
model = ols('y ~ x',data).fit()
print(model.summary())
a1 = model.params[1]
print('a1 (slope) = {:.4f}'.format(a1))
a0 = model.params[0]
print('a0 (intercept) = {:.4f}'.format(a0))
#line of regression
def y_model(x):
return a0 + a1*x
# now plot the regressed line as a function of the input data x
plt.plot(x,y_model(x),label='least-squares fit')
# plot the original data
plt.plot(x,y,'ro',label='original data')
plt.xlabel('age,years')
plt.ylabel('muscle mass')
plt.title('Muscle Mass in Women Ages 40-79' )
plt.legend()
plt.grid()
如果可能的话,我想使用模型中的a函数。即使我想出了一种方法来返回x == 60的y值,我如何使它成为“估计”?我应该平均吗?
解决方法
模型对象具有可以使用的“预测”方法。如果尚未拟合模型,则参数不是可选的,但如果已拟合,则参数将保存在模型实例中。 在这里阅读: https://www.statsmodels.org/dev/generated/statsmodels.regression.linear_model.OLS.predict.html#statsmodels.regression.linear_model.OLS.predict
您基本上可以这样做: y_new = model.predict(new_sample)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。