# Create some data first
n = 20
set.seed(86) # For reproducibility of the random numbers
x = sort(runif(n))
y = sort(runif(n))
if(is.R()) library(splines) # To get ns() in R
# This will work for R 1.6.0 and later, but fail for S-PLUS
fit = lm(y ~ ns(x, df=5))
plot(x, y)
lines(x, fitted(fit))
newx = seq(0, 1, len=n)
points(newx, predict(fit, data.frame(x=newx)), type="b", col=2, err=-1)
# The following fails for R 1.6.x and later but works with smart prediction
fit = lm(y ~ ns(scale(x), df=5))
fit$smart.prediction
plot(x, y)
lines(x, fitted(fit))
newx = seq(0, 1, len=n)
points(newx, predict(fit, data.frame(x=newx)), type="b", col=2, err=-1)
# The following requires the VGAM package to be loaded
library(VGAM)
fit = vlm(y ~ ns(scale(x), df=5))
fit@smart.prediction
plot(x, y)
lines(x, fitted(fit))
newx = seq(0, 1, len=n)
points(newx, predict(fit, data.frame(x=newx)), type="b", col=2, err=-1)
Run the code above in your browser using DataLab