I'm trying to use the spline2 package in R to build a monotone spline.
I'm having trouble in evaluating the model for new values of the independent variable. In general, I'm having trouble grasping R treatment of "predict" and its relation to spline2, and how to use the bs object produced.
I tried to follow this example which uses spline1. My data is in a dataframe named BRIyII, with independent variable t and dependent variable P so:
plot(BRIyII$t,BRIyII$P)
So I do:
knots=c(9)
myMat=mSpline(BRIyII$t, knots = knots, degree = 3, intercept = TRUE)
mylm=lm(BRIyII$P~myMat)
Now, if we:
pr = predict(mylm,data.frame(BRIyII$t))
points(BRIyII$t,pr,col = "red")
So my questions are:
1- Since the rightmost predicted value (red dot) is lower than the one to its left, am I misunderstanding the "monotonic" nature of the m splines ?
2- How can I evaluate the spline in values other than those defined in BRIyII$t ? I tried several combinations of stuff but I lack the R syntax knowledge. I would ideally want to do something like :
newdata=seq.int(0,41.5,0.1)
I'll address Question #1 because Question #2 is addressed in the comments.
The splines2 documentation refers to M-spline basis as the "monotone regression spline" basis, but I think this is misleading. The way to estimate a monotone regression spline is to use the I-spline basis, with the restriction that the regression coefficients be non-negative. I-splines are the integrals of M-splines and are thus non-decreasing. So any non-negative linear combination of them will also be non-decreasing. So you can use the splines2::iSpline along with a non-negative regression method like nnls::nnls to estimate a monotone regression function.
For an explanation of M-splines, I-splines and monotone regression, check out:
Ramsay, JO (1988) Monotone regression splines in action. Statistical Science 3(4), 425-461.