[英]R: Effects package | Mixed effects model. Plot model estimates
我正在尝试 plot 我的一些 model 估计。 我是混合效果模型和效果 package 的新手,遇到了一些麻烦。 我的 model 看起来像这样:
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
我尝试使用效果 package,但据我所知,我只能 plot 固定预测频率(分类为“低、中、高、非常高”)。 我使用以下代码来执行此操作:
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()
有没有办法对交叉随机(音素|个人)和随机(1|字)预测变量做类似的事情? 然而,最重要的是交叉随机。
提前致谢,。! 如果您需要任何进一步的信息,请告诉我。
只是为了扩展本的评论。 以下是一些使用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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.