0
$\begingroup$

I am trying to plot a fitted spline with both ns() and smooth.spline() models. When I fit the spline I am unsure how I can set the newdata argument in the prediction function.

For the ns() model, it seems that I have to write the prediction function as predict(fit, newdata= list(x= x.grid)) such that I can plot the fitted curve. If I leave out the newdata argument it will pop the error of x y differing in length.

For the smooth.spline() model, the prediction looks like this predict(fit, x.grid)$y. If I add the newdata= argument it then returns me the x y differing in length error. Can someone explain why there is such a difference in the use of prediction function?

The code I am working on for natural spline model:

nob = 2
ncubic_ult = glm(mpg~ns(weight, df=2), data = Auto)
autote= Auto[te,]

testweightRange= range(autote$weight)
testweightgrid = seq( from=testweightRange[1], to=testweightRange[2], length.out=100 )
# prediction with list():
fittedmpg= predict(ncubic_ult, newdata = list(weight= testweightgrid))
plot( autote$weight, autote$mpg ,col='grey', main = 'newdata= list')
lines(testweightgrid, fittedmpg, col='red', lw=4)

Smoothing spline:

nob = 3
smooth_ult = smooth.spline(horsepower, mpg, df= nob)
autote= Auto[te,]
test_hpwRange= range(autote$horsepower)
test_hpwgrid = seq( from=test_hpwRange[1], to=test_hpwRange[2], length.out=100 )
fittedmpg= predict(smooth_ult,test_hpwgrid)$y
plot( autote$horsepower, autote$mpg ,col='grey', main = 'newdata= list')
lines(test_hpwgrid, fittedmpg, col='red', lw=4)

```
$\endgroup$
2
  • $\begingroup$ Hi, could you provide the data Auto with dput(Auto)? $\endgroup$
    – Tim-TU
    Jul 10, 2020 at 5:57
  • $\begingroup$ The function predict is a generic and has some methods. Use methods(predict) in your console to figure out which methods exist. $\endgroup$
    – Tim-TU
    Jul 10, 2020 at 6:21

1 Answer 1

1
$\begingroup$

I don't know what te is, I have set it to 1:10.

In the first case using glm the function predict.glm needs a data frame with a column named like the predictor variable.

In the second case the return of predict.smooth.spline is a list with x as input data (vector) and y as fitted values (also a vector)..meaning a list of vectors.

Here is my working example for you:

library(splines)
library(ISLR) # for Auto datset: 

nob <- 2
ncubic_ult <- glm(mpg~ns(weight, df=2), data = Auto)

# don't now that te is... I set it to 1 to 10!
te <- 1:10
autote <- Auto[te,]

testweightRange <- range(autote$weight)
testweightgrid <- seq( from=testweightRange[1], to=testweightRange[2], length.out=100 )

# newdata; see ?predict.glm... it should be a data frame which to look for variables with which to predict!
new_data <- data.frame(weight = testweightgrid)

# prediction with newdata:
fittedmpg <- predict(ncubic_ult, newdata = new_data)
plot( autote$weight, autote$mpg ,col='grey', main = 'newdata= data.frame')
lines(testweightgrid, fittedmpg, col='red', lw=4)



nob <- 3
smooth_ult <- smooth.spline(Auto$horsepower, Auto$mpg, df= nob)
autote <- Auto[te,]
test_hpwRange <- range(autote$horsepower)
test_hpwgrid <- seq( from=test_hpwRange[1], to=test_hpwRange[2], length.out=100 )

# see ?predict.smooth.spline. the output is a list with x as input data and y fitted values. 

fittedmpg <- predict(smooth_ult, x = test_hpwgrid)
plot( autote$horsepower, autote$mpg ,col='grey', main = 'newdata= vector')
lines(test_hpwgrid, fittedmpg$y, col='red', lw=4)

Created on 2020-07-10 by the reprex package (v0.3.0)

$\endgroup$
2
  • $\begingroup$ Oh I was not aware of the R documentation of the specific prediction functions for individual models. If the description mentions argument 'x', then is it referring to a vector/ sequence? $\endgroup$
    – siegfried
    Jul 10, 2020 at 12:32
  • $\begingroup$ In R it is often the case that x refers to a vector. In good docs like ?t.test it is explicitly said that x refers to ' a (non-empty) numeric vector of data values.' $\endgroup$
    – Tim-TU
    Jul 10, 2020 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.