3

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 predict(), 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.

Here is my code:

library(splines)

set.seed(12345)
x <- seq(0, 2, by = 0.01)
y <- rnorm(length(x)) + 2*sin(2*pi*(x-1/4))

# My n.s fit:
fit.temp <- lm(y ~ ns(x, knots = seq(0.01, 2, by = 0.1)))

# Getting fitted values:
fit.temp.values <- 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 <- c(0.275, 0.375, 0.475, 0.575, 1.345)

How can I get the predicted values for x.new?

Thanks very much for your help,

p.s. I searched all related questions on SO and I didn't find the answer.

4

2 回答 2

8

创建一个包含名为 的列的数据框x,并将其作为newdata参数传递给predict

predict(fit.temp, newdata=data.frame(x=x.new))
于 2013-08-13T19:31:06.067 回答
1

您将单个向量发送到lm. 如果您想查看这里出了什么问题,请输入:

 fit.temp$terms

...并注意 x 预测器的名称是:

attr(,"term.labels")
[1] "ns(x, knots = seq(0.01, 2, by = 0.1))"

您需要提供predict一个列表,其中包含x. 使用数据框参数要容易得多lmlm.predict这样可以通过对新值进行内部重新评估来完成预测。

 df <- data.frame(x,y)
 # My n.s fit:
 fit.temp <- lm(y ~ ns(x, knots = seq(0.01, 2, by = 0.1)) , data=df)
 predict(fit.temp, newdata=list(x =c(0.275, 0.375, 0.475, 0.575, 1.345) )  )
#        1         2         3         4         5 
#0.9264572 1.6549046 2.0743470 1.9507962 0.8220687 
points(x.new, predict(fit.temp, 
               newdata=list(x =c(0.275, 0.375, 0.475, 0.575, 1.345) )), 
       col="red", cex=2)
于 2013-08-13T19:28:34.563 回答