0

I'm running a GLM and have to hand over discrete values that come from the variable*coefficient to our IT department.

That said, I'm not sure how to calculate the slopes in a piecewise regression model using the bs() function from patsy.

Let's say I have the following model:

y ~ bs(length, degree = 1, knots = [32]

This gives me two rows of the standard pystatsmodel parameters (coefficeints, pvalues, standard error, etc).

Those values are,

variable                                          coeff 
y ~ bs(length, degree = 1, knots = [32][0]        .3763
y ~ bs(length, degree = 1, knots = [32][1]        .4335

I can also run it like this: y ~ length + np.maximum(length-32,0)

Which yields

variable      coeff 
length        .0118
length        -.0074

What I don't understand is when I run a test set through both of these models, they yield the same prediction.

I'm not sure what patsy is doing in the background in either case and I'm wondering, to answer my question, should I

slope 1 for length should come right from the exponent of the coefficient and slope 2 for length is the exponent(coefficient1 + ceoff2). If that's the case, does that rule apply to both types of syntax?

Jordan
  • 1,415
  • 3
  • 18
  • 44
  • 1
    why not just call `predict` on the estimated model? – Sam Mason Sep 19 '19 at 16:16
  • predict on the training set? – Jordan Sep 19 '19 at 16:17
  • you've not given much to go on, but like this https://www.statsmodels.org/stable/generated/statsmodels.regression.linear_model.OLS.predict.html – Sam Mason Sep 19 '19 at 16:18
  • My computer is with IT right now but if I read this correctly, I do model.predict(x_train) or model.predict(model.params) ? – Jordan Sep 19 '19 at 16:27
  • 1
    I don't use `statsmodels` so don't really know their API, just guessing you use it based on question (`pystatsmodel` isn't a python package). you also don't say if you're using OLS or something else. that said, you'd somehow want to pass the values you want predictions for, in other words your "test set" or whatever you're calling the thing your "IT department" want values for – Sam Mason Sep 19 '19 at 16:33
  • Figured it out for `y ~ length + np.maximum(length-32,0)`. it's exp(b0+(x*b1)+((x-knot)*b2). Still not sure for the b spline though. The predictions are the same but even the patsy design matrix does not match. – Jordan Sep 19 '19 at 19:43

0 Answers0