How to adjust a dataframe for statmodel

Viewed 60

My Code currently

import statsmodels.api as statmodel
data = pandas.read_csv('sacramento.csv')

X = data[["baths", "beds", "sqft", "price"]]
Y = data[['baths']].values
model = statmodel.OLS(Y, X).fit()
model.predict(X)

print(model.params.round(2))
print(model.pvalues.round(2))
print('The smallest p-value is for sqft')

Current Output

beds     0.0
sqft    -0.0
price    0.0
dtype: float64
baths    0.0
beds     1.0
sqft     1.0
price    0.0
dtype: float64
The smallest p-value is for sqft

Expected Output

Optimization terminated successfully.
    Current function value: 0.282823
    Iterations 9 
.{0,}-7.29 
.{0,}0.44 
.{0,}0.01 
.{0,}-0.00 
dtype: float64 
.{0,}0.00 
.{0,}0.03 
.{0}, 0.00 
.{0,}0.06 
dtype: float64 
The smallest p-value is for sqft

sacramento.csv

"","city","zip","beds","baths","sqft","type","price","latitude","longitude"
"1","SACRAMENTO","z95838",2,1,836,"Residential",59222,38.631913,-121.434879
"2","SACRAMENTO","z95823",3,1,1167,"Residential",68212,38.478902,-121.431028
"3","SACRAMENTO","z95815",2,1,796,"Residential",68880,38.618305,-121.443839
"4","SACRAMENTO","z95815",2,1,852,"Residential",69307,38.616835,-121.439146
"5","SACRAMENTO","z95824",2,1,797,"Residential",81900,38.51947,-121.435768
"6","SACRAMENTO","z95841",3,1,1122,"Condo",89921,38.662595,-121.327813
"7","SACRAMENTO","z95842",3,2,1104,"Residential",90895,38.681659,-121.351705
"8","SACRAMENTO","z95820",3,1,1177,"Residential",91002,38.535092,-121.481367
"9","RANCHO_CORDOVA","z95670",2,2,941,"Condo",94905,38.621188,-121.270555
"10","RIO_LINDA","z95673",3,2,1146,"Residential",98937,38.700909,-121.442979

How can I adjust it in such a way that it should make it such that those observations of 1 bath correspond to a value of 0, and those with more than 1 bath correspond to a 1? I'm pretty new to statsmodels. Can anybody guide me through it?

1 Answers

It's not very clear from your question, if your dependent variable is whether bath is 1 or greater than 1, it sounds like you should be using a logistic regression. And it doesn't make sense to include bath in your independent variable.

You can convert bath to binary by using a boolean.

So something like this should get you closer:

import statsmodels.api as sm
data = pd.read_csv('sacramento.csv')
data['baths'] = (data['baths']>1).astype(int)

X = data[["beds", "sqft", "price"]]
Y = data[['baths']].values
model = sm.Logit(Y, X).fit()
model.predict(X)

print(model.params.round(2))
print(model.pvalues.round(2))
print('The smallest p-value is for sqft')

Output looks like this:

Optimization terminated successfully.
         Current function value: 0.519093
         Iterations 7
beds     3.15
sqft    -0.02
price    0.00
dtype: float64
beds     0.53
sqft     0.32
price    0.24
dtype: float64
The smallest p-value is for sqft
Related