Skip to main content
 首页 » 编程设计

python之用Python的statsmodels的OLS线性回归进行曲线拟合时,公式中的常数如何选择

2024年11月24日13JeffreyZhao

  • 我想将不同程度的线性回归模型拟合到数据集,并根据调整后的 r^2 选择最合适的模型。
  • 基于 other answers ,我正在使用 OLS 公式 "y ~ 1 + "+ "+ ".join("I(x**{})".format(i) for i in range(1, degree+1) ),
  • 我没有足够的统计知识来理解:是否需要 1 + 常量?如果需要,常量值应该是多少?
import numpy 
import pandas 
import matplotlib 
import matplotlib.offsetbox 
import statsmodels.tools 
import statsmodels.formula.api 
 
data = numpy.array([ 
  [1999, 197.0], 
  [2000, 196.5], 
  [2001, 194.3], 
  [2002, 193.7], 
  [2003, 192.0], 
  [2004, 189.2], 
  [2005, 189.3], 
  [2006, 187.6], 
  [2007, 186.9], 
  [2008, 186.0], 
  [2009, 185.0], 
  [2010, 186.2], 
  [2011, 185.1], 
  [2012, 185.6], 
  [2013, 185.0], 
  [2014, 185.6], 
  [2015, 185.4], 
  [2016, 185.1], 
  [2017, 183.9], 
]) 
 
df = pandas.DataFrame(data, columns=["Year", "CrudeRate"]) 
 
cause = "Malignant neoplasms" 
x = df["Year"].values 
y = df["CrudeRate"].values 
degree = 2 
predict_future_years = 5 
 
# https://stackoverflow.com/a/34617603/4135310 
olsdata = {"x": x, "y": y} 
formula = "y ~ 1 + " + " + ".join("I(x**{})".format(i) for i in range(1, degree+1)) 
model = statsmodels.formula.api.ols(formula, olsdata).fit() 
 
print(model.summary()) 
 
ax = df.plot("Year", "CrudeRate", kind="scatter", grid=True, title="Deaths from {}".format(cause)) 
 
# https://stackoverflow.com/a/37294651/4135310 
func = numpy.poly1d(model.params.values[::-1]) 
matplotlib.pyplot.plot(df["Year"], func(df["Year"])) 
 
predicted = func(df.Year.values[-1] + predict_future_years) 
print("Predicted in {} years: {}".format(predict_future_years, predicted)) 
 
ax.add_artist(matplotlib.offsetbox.AnchoredText("$\\barR^2$ = {:0.2f}".format(model.rsquared_adj), loc="upper center")) 
ax.add_artist(matplotlib.offsetbox.AnchoredText("Predicted in +{} = {:0.2f}".format(predict_future_years, predicted), loc="upper right")) 
 
ax.xaxis.set_major_formatter(matplotlib.ticker.FormatStrFormatter("%d")) 
fig = matplotlib.pyplot.gcf() 
fig.autofmt_xdate(bottom=0.2, rotation=30, ha="right", which="both") 
matplotlib.pyplot.tight_layout() 
cleaned_title = cause.replace(" ", "_").replace("(", "").replace(")", "") 
#matplotlib.pyplot.savefig("{}_{}.png".format(cleaned_title, degree), dpi=100) 
matplotlib.pyplot.show() 

请您参考如下方法:

根据@ALollz 的评论,当使用 Patsy 符号(例如 statsmodels.formula.api.ols("y ~ x"))时,您不需要需要包含 1 +,因为该常量默认添加到模型中,尽管这并未指定您的模型具有取值为 1 的常量。相反,它指定您有一个常数,其大小将由截距系数给出。这是 OLS 确定的常数,所以它就是您想要的。