【发布时间】:2021-04-13 14:55:29
【问题描述】:
编辑:根据以下回复中的交互,我相信plot() 或plot.gam() 函数在处理gam 输出时可能存在一些问题。请参阅下面的回复。
我正在运行非参数回归 model <- gam(y ~ x, bs = "cs", data = data)。
我的数据如下所示,其中 x 在日志中。我有 273 个观察结果
y x
[1,] 0.010234756 10.87952
[2,] 0.009165001 10.98407
[3,] 0.001330975 11.26850
[4,] 0.008000957 10.97803
[5,] 0.008579472 10.94924
[6,] 0.009746714 11.01823
我想绘制模型的输出,基本上是拟合曲线。当我这样做时
# graph
plot(model)
或
ggplot(data = data, mapping = aes(x = x y = y)) +
geom_point(size = 0.5, alpha = 0.5) +
geom_smooth(method="gam", formula= y~s(x, bs = "cs") )
我得到了所需的输出图(对原始标签表示歉意):
[
但是,两条绘制的曲线并不完全相同,我没有设法找到要调整的参数以消除差异。因此我想手动绘制曲线。 这是我目前的尝试。
model <- gam(y~ s(x), bs = "cs", data = data)
names(model)
# summary(model)
model_fit <- as.data.frame(cbind(model$y, model$fitted.values,
model$linear.predictors, data$x,
model$residuals))
names(model_fit) <- c("y", "y_fit", "linear_pred", "x", "res")
### here the plotting
ggplot(model_fit) +
geom_point(aes(x = x, y = y_fit), size = 0.5, alpha = 0.5) +
geom_line(aes(x = x, y = y_fit))
但是我收到以下警告
geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
我似乎无法修复最后一张图(似乎错误在 geom_point() 中)并添加置信区间,也无法找到调整前两个以使其完全相同的位置。
【问题讨论】:
-
对于原始比较,您可能只需将
n更改为stat_smooth- 请参阅 stackoverflow.com/questions/33040344/… -
我怀疑 80 和 100 评估点之间的差异是造成这些差异的原因。这更有可能是由于用于选择平滑度参数的算法不同; OP 使用默认 GCV,但 stat_smooth 使用 REML,这是首选。
-
REML 方法解决了曲线斜率,但我认为
plot()和plot.gam()存在问题。我做了一些测试并将结果发布在这里stackoverflow.com/a/67096214/2291642。我将不胜感激。谢谢。