多变量回归 statsmodels.api

我查看了文档,但仍然无法弄清楚。我想运行具有多重回归的 WLS。

statsmodels.api 作为 sm 导入

单变量示例。

X = Height
Y = Weight

res = sm.OLS(Y,X,).fit() 
res.summary()

说我也有:

X2 = 年龄

如何将其添加到我的回归中?


桃花长相依
浏览 190回答 2
2回答

饮歌长啸

您可以将它们放入 data.frame 并调出列(这样输出看起来也更好):import statsmodels.api as smimport pandas as pdimport numpy as npHeight = np.random.uniform(0,1,100)Weight = np.random.uniform(0,1,100)Age = np.random.uniform(0,30,100)df = pd.DataFrame({'Height':Height,'Weight':Weight,'Age':Age})res = sm.OLS(df['Height'],df[['Weight','Age']]).fit()In [10]: res.summary()Out[10]:&nbsp;<class 'statsmodels.iolib.summary.Summary'>"""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;OLS Regression Results&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;=======================================================================================Dep. Variable:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Height&nbsp; &nbsp;R-squared (uncentered):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.700Model:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OLS&nbsp; &nbsp;Adj. R-squared (uncentered):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0.694Method:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Least Squares&nbsp; &nbsp;F-statistic:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 114.3Date:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Mon, 24 Aug 2020&nbsp; &nbsp;Prob (F-statistic):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2.43e-26Time:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 15:54:30&nbsp; &nbsp;Log-Likelihood:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-28.374No. Observations:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;100&nbsp; &nbsp;AIC:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 60.75Df Residuals:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 98&nbsp; &nbsp;BIC:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 65.96Df Model:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Covariance Type:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nonrobust&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;==============================================================================&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;coef&nbsp; &nbsp; std err&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t&nbsp; &nbsp; &nbsp; P>|t|&nbsp; &nbsp; &nbsp; [0.025&nbsp; &nbsp; &nbsp; 0.975]------------------------------------------------------------------------------Weight&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.1787&nbsp; &nbsp; &nbsp; 0.090&nbsp; &nbsp; &nbsp; 1.988&nbsp; &nbsp; &nbsp; 0.050&nbsp; &nbsp; &nbsp; &nbsp;0.000&nbsp; &nbsp; &nbsp; &nbsp;0.357Age&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0.0229&nbsp; &nbsp; &nbsp; 0.003&nbsp; &nbsp; &nbsp; 8.235&nbsp; &nbsp; &nbsp; 0.000&nbsp; &nbsp; &nbsp; &nbsp;0.017&nbsp; &nbsp; &nbsp; &nbsp;0.028==============================================================================Omnibus:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2.938&nbsp; &nbsp;Durbin-Watson:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1.813Prob(Omnibus):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0.230&nbsp; &nbsp;Jarque-Bera (JB):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2.223Skew:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -0.211&nbsp; &nbsp;Prob(JB):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0.329Kurtosis:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2.404&nbsp; &nbsp;Cond. No.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;49.7==============================================================================

POPMUISE

我使用二阶多项式来预测身高和年龄如何影响士兵的体重。您可以在我的 GitHub 上获取 ansur_2_m.csv。&nbsp;df=pd.read_csv('ANSUR_2_M.csv', encoding = "ISO-8859-1",&nbsp; &nbsp;usecols=['Weightlbs','Heightin','Age'],&nbsp; dtype={'Weightlbs':np.integer,'Heightin':np.integer,'Age':np.integer})&nbsp;df=df.dropna()&nbsp;df.reset_index()&nbsp;df['Heightin2']=df['Heightin']**2&nbsp;df['Age2']=df['Age']**2&nbsp;formula="Weightlbs ~ Heightin+Heightin2+Age+Age2"&nbsp;model_ols = smf.ols(formula,data=df).fit()&nbsp;minHeight=df['Heightin'].min()&nbsp;maxHeight=df['Heightin'].max()&nbsp;avgAge = df['Age'].median()&nbsp;print(minHeight,maxHeight,avgAge)&nbsp;df2=pd.DataFrame()&nbsp;df2['Heightin']=np.linspace(60,100,50)&nbsp;df2['Heightin2']=df2['Heightin']**2&nbsp;df2['Age']=28&nbsp;df2['Age2']=df['Age']**2&nbsp;df3=pd.DataFrame()&nbsp;df3['Heightin']=np.linspace(60,100,50)&nbsp;df3['Heightin2']=df2['Heightin']**2&nbsp;df3['Age']=45&nbsp;df3['Age2']=df['Age']**2&nbsp;prediction28=model_ols.predict(df2)&nbsp;prediction45=model_ols.predict(df3)&nbsp;plt.clf()&nbsp;plt.plot(df2['Heightin'],prediction28,label="Age 28")&nbsp;plt.plot(df3['Heightin'],prediction45,label="Age 45")&nbsp;plt.ylabel="Weight lbs"&nbsp;plt.xlabel="Height in"&nbsp;plt.legend()&nbsp;plt.show()&nbsp;print('A 45 year old soldier is more probable to weight more than an 28 year old soldier')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python