# Fitting a constrained natural spline

Why I think I need to fit a constrained natural spline

I'm working with IBI (interbeat intervals). The measured IBI's include some invalid values. I'm working on an imputation algorithm to impute for the invalid IBI's. Suppose I have an interval with 5 invalid IBI's. Part of my idea on imputing for the invalid IBI's is to have a natural spline fit that is constrained between the last valid point before these invalid IBI values and the first valid point after these invalid points.

Is there any way that I can fit a natural spline constrained to pass through a certain pre-specified points?

I am working in R, and R implementations would be particularly useful.

As an example, consider the data below:

data <- data.frame(X = rep(NA, 1000), Y = rep(NA, 1000))
set.seed(123456)
data$X <- cumsum(abs(c(0, rnorm(999, 0.8, 0.05)))) data$Y <- c(rnorm(100,700, 50), rnorm(100,800, 50), rnorm(100,900, 50), rnorm(100,800,
50), rnorm(100,700, 50), rnorm(100,800, 50), rnorm(300,700, 50), rnorm(100,600, 50))


Now, suppose that I'd like to have a natural spline with knots on every 20 seconds (assume time is in second) AND to have the fitted natural spline fit to pass through obs 4 and obs 15 (row # 4 and row # 15). Is there anyway to do that?

My solution: one solution is to use very close knots in those points.

• Using a constraint there doesn't make sense to me, but perhaps I misunderstood something. Possibly asking about how to address that problem would be an even better (if new) question. – Glen_b Sep 4 '13 at 1:55
• I have edited your question. Framed this way, it's more likely to avoid closure, and more likely to be reopened if it does close. By putting the reason for your question up front, you're more likely to solve the problem you actually have rather than the problem you think you have. – Glen_b Sep 4 '13 at 1:59

The function smooth.spline allows you to specify weights for each observation. I think if you set very high weights for the pair of valid observations on each side of a missing data run you would effectively force the spline to go through those valid observations.