使用

问题描述 投票:0回答:1
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

时,不支持它得到的。我可以使用mgcVizposterior predictive check of toy model获得模拟结果

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)
r ggplot2 mgcv
1个回答
0
投票
提出更有用的错误:

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

	
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.