我想将不同程度的线性回归模型拟合到数据集,然后根据调整后的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确定的常数,因此它是您想要的常数。
关于python - 使用Python的statsmodels的OLS线性回归进行曲线拟合时,如何在公式中选择常数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54582625/