title: Prediction using a natural spline fit
body: <p>I have a fitted a simple natural spline (df = 3) model and I'm trying to predict for some out of sample observations. Using the function <code>predict()</code>, I'm able to get fitted values for in-sample observations but I've not been able to get the predicted value for new observations.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>library(splines)
  
  set.seed(12345)
  x &lt;- seq(0, 2, by = 0.01)
  y &lt;- rnorm(length(x)) + 2*sin(2*pi*(x-1/4))
  
  # My n.s fit:
  fit.temp &lt;- lm(y ~ ns(x, knots = seq(0.01, 2, by = 0.1)))
  
  # Getting fitted values:
  fit.temp.values &lt;- predict(fit.temp,interval="prediction", level = 1 - 0.05)
  
  # Plotting the data, the fit, and the 95% CI:
  plot(x, y, ylim = c(-6, +6))
  lines(x, fit.temp.values[,1], col = "darkred")
  lines(x, fit.temp.values[,2], col = "darkblue", lty = 2)
  lines(x, fit.temp.values[,3], col = "darkblue", lty = 2)
  
  # Consider the points for which we want to get the predicted values:
  x.new &lt;- c(0.275, 0.375, 0.475, 0.575, 1.345)
  </code></pre>
  
  <p><em><strong>How can I get the predicted values for x.new?</em></strong> </p>
  
  <p>Thanks very much for your help,</p>
  
  <p>p.s. I searched all related questions on SO and I didn't find the answer.</p>