繁体   English   中英

statsmodels.api OLS 函数不产生输出

[英]statsmodels.api OLS function not producing output

我正在尝试将一个图形函数和一个回归函数累积起来,一个来自 statsmodels.api,另一个来自 plotly express 到一个函数中。 目前这就是我所拥有的。

def lineareg(df, y, x, log, constant, plot):
    if constant == True:
        x2 = sm.add_constant(x)
        return x2
    if log == True:
        y = np.log(y)
        return y
    model = sm.OLS(y, x2, missing='drop').fit()
    if plot == True:
        fig = px.scatter(df, x=x, y=y, trendline='ols')
        fig.show()
    return model.summary()

虽然我调用它时函数中没有错误,但它也不会产生任何输出,这是我的调用函数

if __name__ == '__main__':
    df = ingest('Smoking.csv')
    print(lineareg(df, y=df['bwght'], x=df['cigtax'], log=True, constant=True, plot=True))

这里的摄取只是一个简单的函数,它结合了pd.read_csv()pd.read_excel()函数,没有任何问题。

您在 if 语句中使用了return ,而实际上它仅用于转换值。

所以应该是:

def lineareg(df, y, x, log, constant, plot):
    if constant == True:
        x2 = sm.add_constant(x)
    if log == True:
        y = np.log(y)
    model = sm.OLS(y, x2, missing='drop').fit()
    if plot == True:
        fig = px.scatter(df, x=x, y=y, trendline='ols')
        fig.show()
    return model.summary()


df = pd.DataFrame({'bwght':np.random.uniform(0,1,100),
                   'cigtax':np.random.uniform(0,1,100)})
lineareg(df, y=df['bwght'], x=df['cigtax'], log=True, constant=True, plot=True)

对我有用:

在此处输入图片说明

OLS Regression Results
Dep. Variable:  bwght   R-squared:  0.001
Model:  OLS Adj. R-squared: -0.009
Method: Least Squares   F-statistic:    0.08564
Date:   Sat, 21 Nov 2020    Prob (F-statistic): 0.770
Time:   13:59:44    Log-Likelihood: -147.27
No. Observations:   100 AIC:    298.5
Df Residuals:   98  BIC:    303.8
Df Model:   1       
Covariance Type:    nonrobust       
coef    std err t   P>|t|   [0.025  0.975]
const   -1.1543 0.211   -5.475  0.000   -1.573  -0.736
cigtax  0.1084  0.370   0.293   0.770   -0.627  0.843
Omnibus:    32.671  Durbin-Watson:  2.285
Prob(Omnibus):  0.000   Jarque-Bera (JB):   52.573
Skew:   -1.468  Prob(JB):   3.84e-12
Kurtosis:   5.001   Cond. No.   4.37

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM