如何从脚本将诊断图从 easystats 保存到文件

问题描述 投票:0回答:1

一个月前,我发布了这个问题并且我能够自己解决它;但是,更新后(

easystats::easystats_update()
),它不再起作用。因此,我的问题仍然是一样的:如何保存脚本中
check_model(model)
的输出的诊断图?

当我这样做时:

diagnost_fig = check_model(model)
ggsave(diagnostic_path, plot = diagnost_fig)

我收到错误:

Error in UseMethod("grid.draw") :  no applicable method for 'grid.draw' applied to an object of class "c('check_model', 'see_check_model')"

然后,当我第一次绘制它时:

diagnost_fig = plot(check_model(model))
ggsave(diagnostic_path, plot = diagnost_fig)

我现在收到错误:

Error in UseMethod("grid.draw") :  no applicable method for 'grid.draw' applied to an object of class "list"

有没有办法保存我可视化的绘图

这是一个复制此问题的脚本:

# Close all windows and clear all variables
rm(list = ls())
graphics.off()

# Load libraries
library(lme4)
library(ggplot2)
library(easystats)
library(see)

### Create example dataset
# Define the number of groups, time points, measures, and variables
n_groups = 2
time_points = c(1,5,9,12)
n_subjects = 8
n_variables = 2

# Create all combinations of groups, time points, and measures, and head data
data = expand.grid(group = paste0("Group",1:n_groups), time = time_points, subject = 1:n_subjects)
data = data[order(data$group, data$time, data$subject), ]
for (i in 1:n_variables) {data[[paste0("var", i)]] <- rnorm(nrow(data))}
head(data)

# Loop through variables
for (variable in paste0("var", 1:n_variables)) {
  # Create linear mixed model
  model = lmer(data[[variable]] ~ time*group + (1 + time | subject), data, control = lmerControl(optimizer ="Nelder_Mead"))

  # Create paths to save boxplot and diagnostic plot
  boxplot_path = paste0("Example_Boxplot_", variable, ".png")
  diagnostic_path = paste0("Example_Diagnostic_", variable, ".png")  
  
  # Create boxplot
  variable_boxplot = ggplot(data, 
                          aes(x = time, 
                          y = !!sym(variable), 
                          group = interaction(time, group),
                          fill = group)) +
                    geom_boxplot(width = 3, 
                          position = position_dodge(width = 1.5),
                          alpha = 0.6)
  # Save boxplot
  ggsave(boxplot_path, plot = variable_boxplot)

  # PROBLEM: Create and try to save diagnostic plot

  # First try:
  check_model(model)
#   ggsave(diagnostic_path) # Just saves again the boxplot

  # Second try (prompts error):
  diagnost_fig = check_model(model)
#   ggsave(diagnostic_path, plot = diagnost_fig)
      # Error in UseMethod("grid.draw") :
      #   no applicable method for 'grid.draw' applied to an object of class "c('check_model', 'see_check_model')"

  # Third try (prompts error):
  diagnost_fig = plot(check_model(model))
#   ggsave(diagnostic_path, plot = diagnost_fig)
      # Error in UseMethod("grid.draw") : 
      #   no applicable method for 'grid.draw' applied to an object of class "list"

}
r ggplot2 plot statistics ggsave
1个回答
0
投票

使用

png
功能。将此适应您的用例。

png(filename = file.path(path_to_file, "name_of_file.png"), width = 14, height = 12, units = "in", res = 300)
# windows(width = 14, height = 12)
performance::check_model(model)    
dev.off()

如果出现以下错误,请取消注释并使用

window
函数:

“错误:RStudio‘绘图’窗口太小,无法显示这组 地块。请把窗户弄大一点。”

© www.soinside.com 2019 - 2024. All rights reserved.