0

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)

yields: enter image description here

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")

It yields: enter image description here

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) 
cladelpino
  • 337
  • 3
  • 14
  • I'm not sure about the monotonicity issue. In fact, when I ran a regression with `mSpline` and another with `bs` (using the built-in `mtcars` data frame), I got exactly the same coefficients. Regarding predictions: First, you can do the regression in one line like this: `mylm = lm(P ~ mSpline(t, knots=knots, degree=3, intercept=TRUE), data=BRIyII)`. – eipi10 Jul 26 '17 at 16:51
  • 1
    Second, to get predictions with the data you used to fit the model, you can do `predict(mylm)` and it will automatically use the data frame used to fit the model. To get predictions for other values of the predictor variables, you need to feed `predict` a data frame with the new values (and the column names in the new data frame have to match the column names in the data frame used to fit the model). For example, `predict(mylm, newdata=data.frame(t = seq(0,41.5,0.1)))`. – eipi10 Jul 26 '17 at 16:52
  • thanks for solving the "easy" part at least ! :) – cladelpino Jul 26 '17 at 16:57

1 Answers1

6

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.

saberpowers
  • 176
  • 1
  • 3