最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ggplot2 - Posterior predictive check for GAM (mgcv in R) - Stack Overflow

programmeradmin6浏览0评论

I am fitting GAMs in R using the mgcv R package. I then want to perform posterior predictive checks to compare observed and simulated outcomes. A toy example using the performance R package:

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))

That does not work:

Error in d[, !(names(data) %in% all.varying), drop = FALSE] : 
  incorrect number of dimensions

I am trying to get something similar to this:

The only issue seems to be that when the performance calls simulate, what it gets is not supported. I can obtain simulated outcomes using 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")

The problem is that I only obtain one point, not the jitter of points as in the image above. How can I achieve that?

发布评论

评论列表(0)

  1. 暂无评论