Search code examples
rdataframepredictionbspline

"predict" function in R does not predict on test set


I was trying to fit the data on the training set, and then apply it to the full set to see the result. But the problem is that I have to use a data.frame to store the x and y, since I am planning on using bootstrap. I was doing some testing like so:

library(splines)

# generating the data

x_ = c(0.2, 0.9, 1.4, 1.7)
y_ = c(2.5, 4.3, 5.2, 2.5)
n = 50 - length(x_)
set.seed(0)
x = seq(0,3, length.out=n) + runif(n,0,0.1)
y = x*sin(3*x) + runif(n)
x = sort(c(x, x_))
y = c(y, y_)
df <- data.frame(x=sort(x), y=y)

# fitting the model

df1 <- df[c(2,4,6,8,16,20,25,30,35,40,45,50),]
ft <- lm(df1$y ~ bs(df1$x, knots=knots, degree=3))
pr <- predict(ft, df$x)
length(pr)

The problem is that predict does not like df$x, it only works with data.frame(df$x) (Why?). Also, it refuses to predict more than 12 values, which is extremely strange to me.


Solution

  • Because predict() is expecting an input as a data.frame() object which contains the column 'x'. So when you pass a vector, it doesn't recognize it.

    ft <- lm(y ~ bs(x, knots=knots, degree=3),data=df1)
    pr <- predict(ft, newdata=df)
    length(pr)