我正在尝试使用下面的代码在 R 中绘制 GAM。这段代码过去总是有效的,我可以绘制平滑项的部分效果,但随机它不再有效,始终触发此错误:
Error in Ops.data.frame(guide_loc, panel_loc) :
‘==’ only defined for equally-sized data frames
我的代码如下所示:
library(mgcv)
library(gratia)
gam1 <- mgcv::gam(mpg ~ cyl +
s(disp), data = mtcars, method = "REML")
draw(gam1)
# directly drawing function from the package also triggers the error
gratia::draw(gam1)
我收到的错误消息对我来说毫无意义,因为我不明白它如何暗示数据帧大小(或者至少不是如措辞所示的多个数据帧。
当我尝试使用模型的导数并使用以下代码绘制它们时,首次出现此错误:
derivatives1 <- derivatives(gam1,
term = "s(disp)",
order = 1L,
type = "central",
n = 250,
eps = 1,
interval = "simultaneous",
n_sim = 500000,
unconditional = TRUE)
draw(derivatives1)
得出同样的错误:
Error in Ops.data.frame(guide_loc, panel_loc) :
‘==’ only defined for equally-sized data frames
但现在每当我尝试绘制游戏或其衍生物时都会发生此错误。我已尝试重新启动 r 并卸载软件包以确保没有软件包冲突导致此问题,但错误仍然出现。
如何解决此错误并绘制我的 GAM 和 GAM 平滑的导数?
您必须拥有旧版本的 {gratia} (尽管我怀疑我是否见过 CRAN 发布版本的软件包报告此错误 - 如果您从 GitHub 安装,请从 CRAN 更新您的软件包以获取版本 0.9。 0) 或加载了一些其他包,这些包会屏蔽或与 {gratia} 交互不良。
随着 CRAN 版本的 {gratia},我无法重现您的问题。
r$> library(mgcv)
library(gratia)
gam1 <- mgcv::gam(mpg ~ cyl +
s(disp), data = mtcars, method = "REML")
draw(gam1)
# directly drawing function from the package also triggers the error
gratia::draw(gam1)
Loading required package: nlme
This is mgcv 1.9-1. For overview type 'help("mgcv-package")'.
r$> derivatives1 <- derivatives(gam1,
term = "s(disp)",
order = 1L,
type = "central",
n = 250,
eps = 1,
interval = "simultaneous",
n_sim = 500000,
unconditional = TRUE)
draw(derivatives1)
Warning message:
The `term` argument of `derivatives()` is deprecated as of gratia 0.8.9.9.
ℹ Please use the `select` argument of `derivativess()` instead.
This warning is displayed once every 8 hours.
Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
generated.
(您可以忽略该警告;如果它打扰您,请将
term = "s(disp)"
更改为select = "s(disp)"
。