Python Forum
Statsmodels Multiple Regression Syntax Error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Statsmodels Multiple Regression Syntax Error
#1
I've been able to use the statsmodels.api regression when assigning variables to x and y with no issues, however, now I am trying to use the statsmodels.formula.api to to run a multiple regression that includes 1 categorical variable while utilizing the formual= function. I'm familiar with regression models in R, but now I'm switching over to Python and running into issues. I keep getting the following error:

File "<unknown>, Line 1

C(Work Country)

SyntaxError: invalid syntax



The code I am running that is causing the error is below:

import pandas
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import openpyxl
import statsmodels.formula.api as smf
import statsmodels.formula.api as ols

df = pd.read_excel('C:/File/data1')

model = smf.ols(formula= 'Age ~ C(Work Country) + Height', data = df).fit()



Any help would be grateful
Reply
#2
post a reproducible code-example...with representative features in your dataframe
Reply
#3
I have located the answer. When using statsmodels whitespace is not properly recognized as part of a column name. See below:

https://stackoverflow.com/questions/5286...iple-words
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Factors in statsmodels mixedlm rleduc42 1 2,310 Oct-23-2020, 03:16 PM
Last Post: rleduc42
  How to find what is causing the unboundlocalerror 'crumb' and invalid syntax error? JonathanBanks 1 2,286 Jul-28-2020, 11:46 AM
Last Post: Yoriz
  Too Many Indexers Error In regression code Bitten 3 3,479 Mar-25-2020, 12:14 AM
Last Post: Larz60+
  I need help fixing a syntax error! chenqin348 5 4,172 Dec-27-2019, 12:07 PM
Last Post: Larz60+
  Newbie question: how to generate dataframe and use multiple regression zydjohn 0 2,278 Dec-10-2017, 09:49 AM
Last Post: zydjohn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020