1

I have divided my data into training and validation samples and have successfully fit my model with three types of linear models. What I cannot figure out how to do is apply the model to the validation sample to evaluate the fit. When I attempt to apply the model to the holdout sample (sorry, I know that this isn't a reproducible example but I think that the issue is pretty clear. I'm just putting this snippet here for completeness. Please be gentle!):

valid = validation.loc[:, x + [ "sale_amt"]] 
holdout1 = m1.predict(valid)

I get the following error message:

AttributeError Traceback (most recent call last) in () 8 9 valid = validation.loc[:, x + [ "sale_amt"]] ---> 10 holdout1 = m1.predict(valid)

AttributeError: 'OLS' object has no attribute 'predict'`

Other Python OLS regression packages have a 'predict' method, but it doesn't seem that PySAL does. I realize that the function coefficients (betas) are available and will pursue applying them to my validation data directly, but I was hoping that there is a simple answer that I just missed.

ewgrashorn
  • 21
  • 3

1 Answers1

1

I apologize if it is bad form to answer my own question, but I did come up with a solution. I contacted Daniel Arribas-Bel, one of the PySAL developers, and he helped guide me to the result I was seeking. Note that my PySAL OLS object is named m1, and my validation dataframe is called 'validation':

m1 = ps.model.spreg.OLS(...)
m1.intercept = m1.betas[0]  # Get the intercept from the betas array
m1.coefficients = m1.betas[1:len(m1.betas)] # Get the coefficients from the betas array
validation['predicted_price'] = m1.intercept + validation.loc[:, x].dot(  m1.coefficients) 

Note that this is the method I would use for a non-spatial model adapted for the KNN model I built in PySAL and might not be technically fully correct for a spatial model. Caveat emptor.

ewgrashorn
  • 21
  • 3
  • It's ok to self-answer your question, especially if it wasn't already having a working answer. You can even accept it, though you won't get the points :) – Cristik Jul 19 '19 at 14:02