我正在使用ISLR库中的工资数据集。我的目标是在3个位置执行带节点的样条回归(见下面的代码)。我可以做这个回归。那部分没问题。
我的问题是回归曲线的可视化。使用基R函数,我似乎得到了正确的曲线。但我似乎不能很好地利用蒂迪弗斯曲线。这是人们所期望的,也是我使用基本函数得到的:

这就是ggplot吐出来的东西

很明显不一样。R在运行ggplot函数时给我以下消息:
Geom_smooth()‘using method = 'gam’和公式'y ~ s(x,bs = "cs")
这意味着什么,我该如何解决呢?
library(tidyverse)
library(ISLR)
attach(Wage)
agelims <- range(age)
age.grid <- seq(from = agelims[1], to = agelims[2])
fit <- lm(wage ~ bs(age, knots = c(25, 40, 60), degree = 3), data = Wage) #Default is 3
plot(age, wage, col = 'grey', xlab = 'Age', ylab = 'Wages')
points(age.grid, predict(fit, newdata = list(age = age.grid)), col = 'darkgreen', lwd = 2, type = "l")
abline(v = c(25, 40, 60), lty = 2, col = 'darkgreen')
ggplot(data = Wage) +
geom_point(mapping = aes(x = age, y = wage), color = 'grey') +
geom_smooth(mapping = aes(x = age, y = fit$fitted.values), color = 'red')我也试过
ggplot() +
geom_point(data = Wage, mapping = aes(x = age, y = wage), color = 'grey') +
geom_smooth(mapping = aes(x = age.grid, y = predict(fit, newdata = list(age = age.grid))), color = 'red')但这看起来和第二张照片非常相似。
谢谢你的帮助!
发布于 2021-04-02 00:47:14
splines::bs()和s(., type="bs")从mgcv做的事情非常不同;后者是一个惩罚回归样条。我会尝试(未经测试!)
geom_smooth(method="lm",
formula= y ~ splines::bs(x, knots = c(25, 40, 60), degree = 3))https://stackoverflow.com/questions/66913066
复制相似问题