多向交互:获取数值系数估算值的简便方法? [英] Multi-way interaction: easy way to get numerical coefficient estimates?
问题描述
假设存在4向交互,具有2x2x2析因设计以及一个连续变量.
因子具有默认的对比度编码(contr.treatment
).这是一个示例:
Let's say there's a 4-way interaction, with a 2x2x2 factorial design plus a continuous variable.
Factors have the default contrast coding (contr.treatment
). Here's an example:
set.seed(1)
cat1 <- as.factor(sample(letters[1:2], 1000, replace = TRUE))
cat2 <- as.factor(sample(letters[3:4], 1000, replace = TRUE))
cat3 <- as.factor(sample(letters[5:6], 1000, replace = TRUE))
cont1 <- rnorm(1000)
resp <- rnorm(1000)
df <- data.frame(cat1, cat2, cat3, cont1, resp)
mod <- lm(resp ~ cont1 * cat1 * cat2 * cat3, data = df)
看看coef(mod)
的输出,我们得到类似的东西:
Looking at the output of coef(mod)
, we get something like:
(Intercept) cont1 cat1b
0.019822407 0.011990238 0.207604677
cat2d cat3f cont1:cat1b
-0.010132897 0.105397591 -0.001153867
cont1:cat2d cat1b:cat2d cont1:cat3f
0.023358901 -0.194991402 0.060960695
cat1b:cat3f cat2d:cat3f cont1:cat1b:cat2d
-0.240624582 -0.117278931 -0.069880751
cont1:cat1b:cat3f cont1:cat2d:cat3f cat1b:cat2d:cat3f
-0.120446848 -0.141688864 0.136945262
cont1:cat1b:cat2d:cat3f
0.201792298
并获取(例如)cat1b
的估计截距,我们将添加隐式(Intercept)
项和cat1b
,即coef(mod)[1] + coef(mod)[3]
.要获得同一类别的坡度变化,我们可以使用coef(mod)[2] + coef(mod)[6]
, a 似乎没有任何功能可以立即完成.
And to get the estimated intercept for cat1b
(for example), we would add our implicit (Intercept)
term and cat1b
, i.e. coef(mod)[1] + coef(mod)[3]
. To get the change in slope for the same category, we would use coef(mod)[2] + coef(mod)[6]
, a la this r-bloggers post. It gets pretty tedious to write all of them out, and methods(class="lm")
doesn't look like it has any functions that do this right out of the gate.
是否存在一些显而易见的方法来获取每种因素组合的截距和斜率的数值估计?
Is there some obvious way to get numerical estimates for the intercept and slope for each combination of factors?
推荐答案
您正在寻找lsmeans
软件包.检查一下:
You're looking for the lsmeans
package. Check it out:
lstrends(mod, specs = c('cat1', 'cat2', 'cat3'), var = 'cont1')
cat1 cat2 cat3 cont1.trend SE df lower.CL upper.CL
a c e 0.01199024 0.08441129 984 -0.15365660 0.1776371
b c e 0.01083637 0.08374605 984 -0.15350502 0.1751778
a d e 0.03534914 0.09077290 984 -0.14278157 0.2134799
b d e -0.03568548 0.09644117 984 -0.22493948 0.1535685
a c f 0.07295093 0.08405090 984 -0.09198868 0.2378905
b c f -0.04864978 0.09458902 984 -0.23426916 0.1369696
a d f -0.04537903 0.09363128 984 -0.22911897 0.1383609
b d f -0.03506820 0.08905581 984 -0.20982934 0.1396929
这篇关于多向交互:获取数值系数估算值的简便方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!