Reputation:
Reference: Page 22 of, http://www.statoek.wiso.uni-goettingen.de/mitarbeiter/ogi/pub/r_workshop.pdf
My question is self-contained in this post though. The data is in http://134.76.173.220/R_workshop
car <- read.table("car.dat", header = TRUE)
attach(car)
library(mgcv)
fit <- gam(MPG~s(SP))
plot(HP, MPG)
x <- seq(0, 350, length = 500)
y <- predict(fit, data.frame(HP = x))
lines(x, y, col = "red", lwd = 2)
Error im receiving:
Warning message:
In predict.gam(fit, data.frame(HP = x)) :
not all required variables have been supplied in newdata!
Upvotes: 3
Views: 2726
Reputation: 6532
I think it is supposed to be MPG~HP as that is a good fit and SP is poor (with default parameters)
par(mfrow=c(1,2))
fit <- gam(MPG~s(SP), data=car)
plot(car$SP, car$MPG)
x <- seq(0, 350, length = 500)
y <- predict(fit, data.frame(SP = x))
lines(x, y, col = "red", lwd = 2)
fit <- gam(MPG~s(HP), data=car)
plot(car$HP, car$MPG)
x <- seq(0, 350, length = 500)
y <- predict(fit, data.frame(HP = x))
lines(x, y, col = "red", lwd = 2)
Upvotes: 0
Reputation: 822
It seems to work if you scrap the 'attach' adjust the code accordingly and use SP as suggested by @GeorgeDontas
fit <- gam(MPG~s(SP), data=car)
plot(car$HP, car$MPG)
x <- seq(0, 350, length = 500)
y <- predict(fit, data.frame(SP = x))
lines(x, y, col = "red", lwd = 2)
Upvotes: 3