statsmodels linear regression - patsy formula to include all predictors in model

后端 未结 3 1216
粉色の甜心
粉色の甜心 2020-12-10 12:03

Say I have a dataframe (let\'s call it DF) where y is the dependent variable and x1, x2, x3 are my independent variables. In R I can f

3条回答
  •  孤街浪徒
    2020-12-10 12:25

    I haven't found . equivalent in patsy documentation either. But what it lacks in conciseness, it can make-up for by giving strong string manipulation in Python. So, you can get formula involving all variable columns in DF using

    all_columns = "+".join(DF.columns - ["y"])
    

    This gives x1+x2+x3 in your case. Finally, you can create a string formula using y and pass it to any fitting procedure

    my_formula = "y~" + all_columns
    result = lm(formula=my_formula, data=DF)
    

提交回复
热议问题