Reputation: 1495
I'm trying to code a multivariate spline model with some independent variables having multiple knots and some having none. The variables with splines will always have a degree of one. I have some code but I don't know if I trust it because I haven't done a spline regression in r (only in some proprietary "black box" software). Below is the code.
I have checked a lot of the 6,000 posts on splines. I see so many different codes that I'm confused.
Will anyone either a)tell me if this code is doing what I want it to do (degree = 1/different knots) b)is there a better way to do this?
fit1 <- glm(freq ~ channel + term2 + pay_plan_bucket_2 +
state + eff_year + marital_status +
vehicle_type + insured_age_bucket + I(pmax(0, insured_age_bucket-
26)) + I(pmax(0, insured_age_bucket - 70)) +
vehicle_length_bucket + I(pmax(0, vehicle_length_bucket - 45)) +
veh_age + I(pmax(veh_age -7)) + I(pmax(veh_age - 18)) +
rba_bucket + I(pmax(0, rba_bucket - 3500)) + I(pmax(0, rba_bucket - 27000)) +
credit_tier_bucket + I(pmax(0, credit_tier_bucket - 3)),
family=quasipoisson(link="log"),
data=comp_training_set_newpayplan)
Thank you.
Upvotes: 2
Views: 259
Reputation: 4930
Your brute force approach to splines is probably correct. Verify your output using bs
from the splines package, for instance: bs(credit_tier_bucket, knots=3, degree=1)
as a single term in the formula. Since the basis is formed differently than you have done here, gather the predicted values from both models and verify they are equal to ensure the two approaches to coding splines provides equivalent estimation and inference.
Upvotes: 1