library(mgcv)
library(performance)
df <- mtcars |>
tibble::as_tibble() |>
dplyr::mutate(carb = as.integer(carb))
model <- mgcv::gam(
carb~s(wt, k=5),
data = df,
family = mgcv::ocat(R=8)
)
plot(check_model(model))
不起作用:
Error in d[, !(names(data) %in% all.varying), drop = FALSE] :
incorrect number of dimensions
我正在尝试获得类似于this的东西:
唯一的问题似乎是,当performance
调用
simulate
时,不支持它得到的。我可以使用mgcViz
:获得模拟结果
library(ggplot2)
sims <- mgcViz::simulate.gam(model, newdata=df, nsim=10) |> tibble::as_tibble() |>
tidyr::pivot_longer(dplyr::everything())
ggplot(sims, aes(x = value)) +
geom_point(aes(y = after_stat(count)), stat = "count")
问题是我只能获得一个点,而不是上图中的点抖动。我该如何实现?
performance
使用check_predictions
进行后验预测。使用:
check_predictions(model)
Error: Could not simulate responses. Maybe there is no `simulate()` for
objects of class `gam`?
正如您所说的那样,有mgcViz::simulate.gam
,加载该软件包现在将找到一个模拟函数。但是,它以不同的格式返回数据(数组)与
stats::simulate
(给出列表)的数据,因此它将无法与
performance
提供的功能一起使用。您可以绘制模拟数据并轻松地观察数据。例如:
library(ggplot2)
sims <- mgcViz::simulate.gam(model, newdata=df, nsim=100) |>
tibble::as_tibble() |>
tidyr::pivot_longer(dplyr::everything(), names_to = 'replicate', values_to = 'carb')
ggplot(mapping = aes(carb)) +
geom_density(
aes(color = 'Model-predicted data', group = replicate),
sims, key_glyph = 'smooth'
) +
geom_density(aes(color = 'Observed data'), df, linewidth = 2, key_glyph = 'smooth') +
scale_color_manual(values = c(alpha('black', 0.3), 'firebrick')) +
theme_classic()