参考:第22页,http://www.statoek.wiso.uni-goettingen.de/mitarbeiter/ogi/pub/r_workshop.pdf
我的问题在这篇文章中是自成一体的。数据位于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)
我收到错误:
Warning message:
In predict.gam(fit, data.frame(HP = x)) :
not all required variables have been supplied in newdata!
答案 0 :(得分:3)
如果您废弃'attach'似乎可以正常工作,请相应地调整代码并按照@GeorgeDontas的建议使用SP
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)
答案 1 :(得分:0)
我认为它应该是MPG~HP,因为它非常合适而且SP很差(使用默认参数)
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)