如何绘制和提取我的斜率并截取来自lmer的不同组的值?

时间:2019-01-29 20:10:14

标签: r ggplot2 lme4 mixed-models

我正在R中使用lmer运行混合效果模型,但无法按组绘制模型并提取直线方程。

我认为我需要查看固定效应和随机效应,使我的方程式成为下面的最终条件模型(即cond_ind_waterxsilver)。当我为模型提取这些值时,截取值比我期望的要高得多。有人可以为我提供一些有关从输出中提取模型值以进行直线绘制以及如何绘制模型结果的指导吗?

#conditional model     
cond_waterxsilver <- lmer(LnAg ~ LnVolume + (LnVolume | FilterID) + SilverType + WaterType + SilverType*WaterType + SilverType*LnVolume +  WaterType*LnVolume, data=capwater_removed.data)
aov_cond_ind_waterxsilver <- anova(cond_ind_waterxsilver)
summary(cond_ind_waterxsilver)
aov_cond_ind_waterxsilver

正如我提到的那样,我为每个组(由SilverType和WaterType计算)得出的方程的截距远高于预期。

这是我要解释的输出的一部分:

Random effects:
 Groups   Name        Variance Std.Dev. Corr 
 FilterID (Intercept) 1.84693  1.3590        
          LnVolume    0.07073  0.2660   -0.91
 Residual             0.75533  0.8691        
Number of obs: 187, groups:  FilterID, 33

Fixed effects:
                             Estimate Std. Error       df t value Pr(>|t|)    
(Intercept)                   5.36298    0.67452 25.81163   7.951 2.09e-08 ***
LnVolume                     -1.10424    0.16764 22.59782  -6.587 1.11e-06 ***
SilverTypeCitrate             1.42888    0.90853 27.98025   1.573   0.1270    
SilverTypePVP                -1.68084    0.91226 37.20240  -1.842   0.0734 .  
WaterTypeB                    1.42923    0.78770 26.11938   1.814   0.0811 .  
SilverTypeCitrate:WaterTypeB -1.81514    0.67120 24.94076  -2.704   0.0122 *  
SilverTypePVP:WaterTypeB     -0.03597    0.70150 31.06516  -0.051   0.9594    
LnVolume:SilverTypeCitrate    0.14857    0.20368 14.34068   0.729   0.4775    
LnVolume:SilverTypePVP        0.42758    0.25071 25.73948   1.705   0.1001    
LnVolume:WaterTypeB          -0.14359    0.19678 17.43737  -0.730   0.4753    

1 个答案:

答案 0 :(得分:0)

我不确定100%地确定您要做什么,但是我认为ranef中的lmer函数正是您想要的。

类似的事情可能会起作用:

library(tidyverse)
rand_effects <- ranef(cond_ind_waterxsilver)
rand_tbl <- rand_effects$FilterID %>%
  rownames_to_column() %>%
  as_tibble()

应该为您的数据中的每个组提供一个带有随机截距和LnVolume的随机斜率的小标题。那是您要找的东西吗?

此外,如果仍然无法解决问题,请尝试检查rand_effects对象-其中的data.frame的名称应为(我认为)FilterID,因为这是您的分组变量,但可能有所不同吗?