天天看点

拓端数据tecdat|R语言建立和可视化混合效应模型mixed effect model

我们已经学习了如何处理混合效应模型。本文的重点是如何建立和可视化 混合效应模型的结果。

设置

本文使用数据集,用于探索草食动物种群对珊瑚覆盖的影响。

1.  knitr::opts_chunk$set(echo = TRUE)
2.   
3.  library(tidyverse) # 数据处理
4.  library(lme4) # lmer glmer 模型
5.   
6.   
7.   
8.  me_data <- read_csv("mixede.csv")      

创建一个基本的混合效应模型:

该模型以珊瑚覆盖层为因变量(elkhorn_LAI),草食动物种群和深度为固定效应(c。 urchinden,c.fishmass,c.maxD)和调查地点作为随机效应(地点)。

注意:由于食草动物种群的测量规模存在差异,因此我们使用标准化的值,否则模型将无法收敛。我们还使用了因变量的对数。我正在根据这项特定研究对数据进行分组。

summary(mod)      
1.  ## Linear mixed model fit by maximum likelihood ['lmerMod']
2.   
3.  ##
4.  ## AIC BIC logLik deviance df.resid
5.  ## 116.3 125.1 -52.1 104.3 26
6.  ##
7.  ## Scaled residuals:
8.  ## Min 1Q Median 3Q Max
9.  ## -1.7501 -0.6725 -0.1219 0.6223 1.7882
10.  ##
11.  ## Random effects:
12.  ## Groups Name Variance Std.Dev.
13.  ## site (Intercept) 0.000 0.000
14.  ## Residual 1.522 1.234
15.  ## Number of obs: 32, groups: site, 9
16.  ##
17.  ## Fixed effects:
18.  ## Estimate Std. Error t value
19.  ## (Intercept) 10.1272 0.2670 37.929
20.  ## c.urchinden 0.5414 0.2303 2.351
21.  ## c.fishmass 0.4624 0.4090 1.130
22.  ## c.maxD 0.3989 0.4286 0.931
23.  ##
24.  ## Correlation of Fixed Effects:
25.  ## (Intr) c.rchn c.fshm
26.  ## c.urchinden 0.036
27.  ## c.fishmass -0.193 0.020
28.  ## c.maxD 0.511 0.491 -0.431
29.  ## convergence code: 0
30.  ## boundary (singular) fit: see ?isSingular      

绘制效应大小图:

如果您有很多固定效应,这很有用。

plot(mod)      
拓端数据tecdat|R语言建立和可视化混合效应模型mixed effect model

效应大小的格式化图:

让我们更改轴标签和标题。

  1.  # 注意:轴标签应按从下到上的顺序排列。
  2.  # 要查看效应大小和p值,设置show.values和show.p= TRUE。只有当效应大小的值过大时,才会显示P值。
  3.  title="草食动物对珊瑚覆盖的影响")
拓端数据tecdat|R语言建立和可视化混合效应模型mixed effect model

模型结果表输出:

创建模型摘要输出表。这将提供预测变量,包括其估计值,置信区间,估计值的p值以及随机效应信息。

tab(mod)      
拓端数据tecdat|R语言建立和可视化混合效应模型mixed effect model

格式化表格

  1.  # 注:预测标签(pred.labs)应从上到下排列;dv.labs位于表格顶部的因变量的名称。
  2.  pred.labels =c("(Intercept)", "Urchins", "Fish", "Depth"),
拓端数据tecdat|R语言建立和可视化混合效应模型mixed effect model

用数据绘制模型估计

我们可以在实际数据上绘制模型估计值!我们一次只针对一个变量执行此操作。注意:数据已标准化以便在模型中使用,因此我们绘制的是标准化数据值,而不是原始数据

步骤1:将效应大小估算值保存到data.frame中

1.  # 使用函数。 term=固定效应,mod=你的模型。
2.   
3.  effect(term= "c.urchinden", mod= mod)
4.  summary(effects) #值的输出
5.  ##
6.  ## c.urchinden effect
7.  ## c.urchinden
8.  ## -0.7 0.4 2 3 4
9.  ## 9.53159 10.12715 10.99342 11.53484 12.07626
10.  ##
11.  ## Lower 95 Percent Confidence Limits
12.  ## c.urchinden
13.  ## -0.7 0.4 2 3 4
14.  ## 8.857169 9.680160 10.104459 10.216537 10.306881
15.  ##
16.  ## Upper 95 Percent Confidence Limits
17.  ## c.urchinden
18.  ## -0.7 0.4 2 3 4
19.  ## 10.20601 10.57414 11.88238 12.85314 13.84563
20.  # 将效应值另存为df:
21.  x <- as.data.frame(effects)      

步骤2:使用效应值df绘制估算值

如果要保存基本图(仅固定效应和因变量数据),可以将其分解为单独的步骤。注意:对于该图,我正在基于此特定研究对数据进行分组。

1.  #基本步骤:
2.  #1创建空图
3.   
4.  #2 从数据中添加geom_points()
5.   
6.  #3 为模型估计添加geom_point。我们改变颜色,使它们与数据区分开来
7.   
8.  #4 为MODEL的估计值添加geom_line。改变颜色以配合估计点。
9.   
10.  #5 添加具有模型估计置信区间的geom_ribbon
11.   
12.  #6 根据需要编辑标签!
13.   
14.  #1
15.  chin_plot <- ggplot() +
16.  #2
17.  geom_point(data , +
18.  #3
19.  geom_point(data=x_, aes(x= chinde, y=fit), color="blue") +
20.  #4
21.  geom_line(data=x, aes(x= chinde, y=fit), color="blue") +
22.  #5
23.  geom_ribbon(data= x , aes(x=c.urchinden, ymin=lower, ymax=upper), alpha= 0.3, fill="blue") +
24.  #6
25.  labs(x="海胆(标准化)", y="珊瑚覆盖层")
26.   
27.  chin_plot