R语言 如何将更多信息值Map到sjPlot::plot_model的fill参数上

eqqqjvef  于 2023-06-27  发布在  其他
关注(0)|答案(1)|浏览(110)

虽然主效应的方向可以从估计的符号来解释,但交互作用效应的解释通常需要绘图。这个任务由R包sjPlot来完成。例如,使用plot_model函数,我绘制了连续变量和分类变量之间的相互作用。将分类变量传递给plot_model的fill参数。

library(lme4)
#> Loading required package: Matrix
library(sjPlot)
#> Install package "strengejacke" from GitHub (`devtools::install_github("strengejacke/strengejacke")`) to load all sj-packages at once!
library(ggplot2)

theme_set(theme_sjplot())

cake$recipe_recoded = ifelse(cake$recipe == 'A', -0.5,
                             ifelse(cake$recipe == 'B', 0,
                                    ifelse(cake$recipe == 'C', 0.5,
                                           NA)))

fit = lmer(angle ~ recipe_recoded * temp + 
             (1|recipe_recoded:replicate), 
           cake, REML= FALSE)

plot_model(fit, type = 'pred', terms = c('temp', 'recipe_recoded'))
#> Warning: Ignoring unknown parameters: linewidth

创建于2023-06-24带有reprex v2.0.2
然而,我需要一个额外的特性,因为分类变量不是很有用,因为它是一个求和编码的转换。因此,我希望图显示原始变量的值(即“200”和“1200”),而不是模型中使用的求和编码变量的值(即-0.5和0.5)。

46qrfjad

46qrfjad1#

下面是一个使用自定义函数alias_interaction_plot的解决方案,来自https://pablobernabeu.github.io/2022/plotting-two-way-interactions-from-mixed-effects-models-using-alias-variables

library(lme4)
#> Loading required package: Matrix
library(sjPlot)
library(ggplot2)

theme_set(theme_sjplot())

cake$recipe_recoded = ifelse(cake$recipe == 'A', -0.5,
                             ifelse(cake$recipe == 'B', 0,
                                    ifelse(cake$recipe == 'C', 0.5,
                                           NA)))

fit = lmer(angle ~ recipe_recoded * temp + 
             (1|recipe_recoded:replicate), 
           cake, REML= FALSE)

# plot_model(fit, type = 'pred', terms = c('temp', 'recipe_recoded'))

# Displaying the original variable instead

# Read in function from GitHub
source('https://raw.githubusercontent.com/pablobernabeu/language-sensorimotor-simulation-PhD-thesis/main/R_functions/alias_interaction_plot.R')

alias_interaction_plot(
  model = fit, 
  dataset = cake,
  x = 'temp',
  fill = 'recipe_recoded',
  fill_alias = 'recipe',
  fill_title = 'recipe'
)
#> Loading required package: rlang
#> Loading required package: dplyr
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
#> Loading required package: RColorBrewer
#> Loading required package: ggtext
#> Loading required package: Cairo
#> Warning: Ignoring unknown parameters: linewidth
#> Scale for 'y' is already present. Adding another scale for 'y', which will
#> replace the existing scale.
#> Scale for 'colour' is already present. Adding another scale for 'colour',
#> which will replace the existing scale.

创建于2023-06-24带有reprex v2.0.2

相关问题