takje
takje

Reputation: 2800

Regression with spline under constraints

I am looking for a way to interpolate data with splines but force the values and the derivatives at the end points. This is my data:

library(tidyverse)
data <- tibble(x = 0:600, y = pnorm((-300:300)/100)+(runif(0:600)-0.5)/20)
# add noise
data$y[1] <- 0.35
data$y[2] <- 0.25
ggplot(data, aes(x = x, y = y)) + geom_point()

Now let's fit this data with a spline

fit <- lm(formula = y ~ splines::ns(x, 5),
          data = data)
data$y_new <- predict.lm(fit)
ggplot(data, aes(x = x, y = y_new)) + geom_point(aes(y = y), alpha = 0.5) + 
geom_point(color = 'red')

enter image description here

As you can see, because of the noise, there is a slight upward curvature remaining in the spline on the left hand side (and a downward curvature on the right hand side).

I want to force the value of the spline to be 0 and the derivative of the spline to be 0 (horizontal) when x = 0. So far I haven't been able to do this.

I've also tried to play with the Boundary.knots argument, but that didn't help me either.

Upvotes: 2

Views: 661

Answers (1)

lukeA
lukeA

Reputation: 54237

Here's one way to do it

spline <- with(data, cobs::cobs(x, y, pointwise=rbind(c(0,min(x),0),c(0,max(x),1))))
data$y_new <- predict(spline, z = data$x)[,2]
ggplot(data, aes(x = x, y = y_new)) + 
  geom_point(aes(y = y), alpha = 0.5) + 
  geom_point(color = 'red')

enter image description here

(via)

Upvotes: 2

Related Questions