R:效果包|混合效果模型。绘图模型估计

时间:2020-07-22 14:28:59

标签: r ggplot2 mixed-models

我正在尝试绘制一些模型估计值。我是混合效果模型和效果包的新手,遇到了一些麻烦。我的模型如下:

nurse_female.lmer8 <- lmer(F1 ~ (phoneme|individual) + (1|word) + frequency, data = nurse_female)

Linear mixed model fit by REML ['lmerMod']
Formula: F1 ~ (phoneme | individual) + (1 | word) + frequency
   Data: nurse_female

REML criterion at convergence: 693.4

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-3.6681 -0.5060 -0.0163  0.4837  3.0160 

Random effects:
 Groups     Name        Variance Std.Dev. Corr       
 word       (Intercept) 0.12345  0.3514              
 individual (Intercept) 0.37990  0.6164              
            phonemeIr   0.08146  0.2854    0.07      
            phonemeVr   0.21856  0.4675   -0.42 -0.39
 Residual               0.29672  0.5447              
Number of obs: 334, groups:  word, 116; individual, 23

Fixed effects:
                   Estimate Std. Error t value
(Intercept)         -0.1043     0.2860  -0.365
frequencylow         0.7845     0.2661   2.948
frequencymid         0.0876     0.4005   0.219
frequencyvery high   1.1477     0.3965   2.895

Correlation of Fixed Effects:
            (Intr) frqncyl frqncym
frequencylw -0.884                
frequencymd -0.592  0.632         
frqncyvryhg -0.584  0.634   0.406 

我尝试使用效果包,但是据我所知,我只能绘制固定的预测频率(按“低,中,高,非常高”分类)。我用下面的代码来做到这一点:

  nurse_female_F1.effect <- effect("frequency", nurse_female.lmer8)
  summary(nurse_female_F1.effect)  
  
  # For plotting, convert the effect list object into a data frame
  nurse_female_F1.effect <- as.data.frame(nurse_female_F1.effect)
  nurse_female_F1.effect
  
  # Plotting using ggplot2
  ggplot(nurse_female_F1.effect, aes(frequency, fit)) +
    geom_point() +
    geom_errorbar(aes(ymin = fit-se, ymax = fit + se), width = 0.4) +
    theme_classic()

这很好,我认为我有一个有用的情节。enter image description here

有没有办法对交叉的随机(音素|个体)和随机(1 |单词)预测变量进行类似的处理?但是,最重要的是交叉随机数。

提前谢谢!!!如果您需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

只是扩大Ben的评论。以下是使用sleepstudy数据集(内置于lme4软件包中)的一些示例

library(tidyverse)
library(lme4)
library(lattice) ## for dotplot, qqmath
library(broom.mixed)

# Model the data

head(sleepstudy,10)

fm1 <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy)

ranef(fm1) # Inspect random effects

str(rr1 <- ranef(fm1)) # Put them in a list

# Plot them
dotplot(rr1) 

qqmath(rr1)

# Plot with ggplot2
## as.data.frame() provides RE's and conditional standard deviations:
str(dd <- as.data.frame(rr1))

ggplot(dd, aes(y=grp,x=condval)) +
    geom_point() + facet_wrap(~term,scales="free_x") +
    geom_errorbarh(aes(xmin=condval -2*condsd,
                       xmax=condval +2*condsd), height=0)


# Use broom.mixed to get the model estimate into the original dataframe
df <- broom.mixed::augment(fm1)

# Pivot data to gather .fitted and raw data for ggplot2 legend

df <- df %>% 
  pivot_longer(cols = c(1,4)) %>% 
  mutate(name  = recode(name,'Reaction' = 'Raw Data','.fitted' = 'Random Effect')) # Rename the column
  

# More ggplot2
ggplot(df,aes(Days,value,group = interaction(Subject,name),color = name)) + 
  geom_point() +
  geom_line() + 
  scale_color_grey() +
  theme_bw()

enter image description here enter image description here enter image description here enter image description here