[英]lmer and lm - use all factor levels as 'control' group
在线性 model 或 lmer 公式中存在分类变量(无序因子)的情况下,function 使用第一个因子水平作为对比的“控制”组。 在我的例子中,我有一个具有多个级别的分类变量,并且希望每个级别都是“控制”基组。 是否有一个 function 可以自动执行此过程并为所有组合创建一个带有 p 值的漂亮矩阵? 这是使用钻石数据集的示例代码。
library(lmer);library(lmerTest)
#creating unordered factor
diamonds$color=factor(sample(c('red','white','blue','green','black'),nrow(diamonds),replace=T))
#lmer formula with factor in fixed effects
mod=lmer(data=diamonds,carat~color+(1|clarity))
summary(mod,corr=F)
如摘要中所示,“黑色”用作控件,因此我希望所有其他 colors 用作控件。
线性混合 model 由 REML 拟合。 t-tests 使用 Satterthwaite 的方法 [ lmerModLmerTest] 公式:carat ~ color + (1 | clarity) 数据:diamonds
收敛时的 REML 标准:64684
缩放残差: Min 1Q Median 3Q Max -2.228 -0.740 -0.224 0.540 8.471
随机效应:组名称方差 Std.Dev。 清晰度(拦截) 0.0763 0.276 残差 0.1939 0.440
obs 数量:53940,groups:清晰度,8固定效应:估计标准。 Error df t value Pr(>|t|) (Intercept) 0.786709 0.097774 7.005805 8.05 0.000087 *** colorblue -0.000479 0.005989 53927.996020 -0.08 0.94 colorgreen 0.007455 0.005998 53927.990722 1.24 0.21 colorred 0.000746 0.005986 53927.988909 0.12 0.90 colorwhite 0.000449 0.005971 53927.993708 0.08 0.94
--- 标志。 代码:0 ' ' 0.001 ' ' 0.01 ' ' 0.05 '。 0.1''1
我可以想象出于两个原因之一想要这样做。 首先,将在无序因子的每个级别获得结果的预测值(控制模型中的所有其他内容)。 另一种方法是计算因子水平上的所有成对差异。 如果其中任何一个是您的目标,那么有更好的方法可以做到。 让我们来看第一个 - 为保持其他一切不变的因素的每个值生成预测结果。 让我们从使用diamonds
数据开始,并使用现有的color
变量,但将其设为无序因子。
library(lme4)
library(lmerTest)
library(multcomp)
library(ggeffects)
#creating unordered factor
data(diamonds, package="ggplot2")
diamonds$color <- as.factor(as.character(diamonds$color))
现在,我们可以运行 model:
#lmer formula with factor in fixed effects
mod=lmer(data=diamonds,carat~color+(1|clarity))
multcomp
glht
中的 function glht 测试因子水平之间的成对差异。 这是 output。
summary(glht(mod, linfct = mcp(color="Tukey")))
#>
#> Simultaneous Tests for General Linear Hypotheses
#>
#> Multiple Comparisons of Means: Tukey Contrasts
#>
#>
#> Fit: lmer(formula = carat ~ color + (1 | clarity), data = diamonds)
#>
#> Linear Hypotheses:
#> Estimate Std. Error z value Pr(>|z|)
#> E - D == 0 0.025497 0.006592 3.868 0.00216 **
#> F - D == 0 0.116241 0.006643 17.497 < 0.001 ***
#> G - D == 0 0.181010 0.006476 27.953 < 0.001 ***
#> H - D == 0 0.271558 0.006837 39.721 < 0.001 ***
#> I - D == 0 0.392373 0.007607 51.577 < 0.001 ***
#> J - D == 0 0.511159 0.009363 54.592 < 0.001 ***
#> F - E == 0 0.090744 0.005997 15.130 < 0.001 ***
#> G - E == 0 0.155513 0.005789 26.863 < 0.001 ***
#> H - E == 0 0.246061 0.006224 39.536 < 0.001 ***
#> I - E == 0 0.366876 0.007059 51.975 < 0.001 ***
#> J - E == 0 0.485662 0.008931 54.380 < 0.001 ***
#> G - F == 0 0.064768 0.005807 11.154 < 0.001 ***
#> H - F == 0 0.155317 0.006258 24.819 < 0.001 ***
#> I - F == 0 0.276132 0.007091 38.939 < 0.001 ***
#> J - F == 0 0.394918 0.008962 44.065 < 0.001 ***
#> H - G == 0 0.090548 0.006056 14.952 < 0.001 ***
#> I - G == 0 0.211363 0.006910 30.587 < 0.001 ***
#> J - G == 0 0.330150 0.008827 37.404 < 0.001 ***
#> I - H == 0 0.120815 0.007276 16.606 < 0.001 ***
#> J - H == 0 0.239602 0.009107 26.311 < 0.001 ***
#> J - I == 0 0.118787 0.009690 12.259 < 0.001 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> (Adjusted p values reported -- single-step method)
如果你想要carat
对于不同color
值的所有预测值,你可以使用 ggeffects package 中的ggeffects
ggpredict()
:
g <- ggpredict(mod, terms = "color")
plot(g)
绘制g
object 产生 plot,但打印它会显示值和置信区间/
由reprex package (v2.0.1) 创建于 2023-02-01
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.