Python statsmodels.api OLS函数不产生输出

Python statsmodels.api OLS函数不产生输出,python,pandas,statsmodels,Python,Pandas,Statsmodels,我试图将一个绘图函数和一个回归函数累加到一个函数中,一个来自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) ret

我试图将一个绘图函数和一个回归函数累加到一个函数中,一个来自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))


Ingest here只是一个简单的函数,它结合了
pd.read\u csv()
pd.read\u 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)
为我工作:


您在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)
为我工作:


很难诊断不运行的示例。可能
fig.show()
暂停或停止函数执行。您是否试图根据该区域的火灾数量预测烟雾摄入?很难诊断不运行的示例。可能
fig.show()
暂停或停止函数执行。您是否试图根据该区域的火灾数量预测烟雾摄入?