R 当高阶参数保留时,如何删除模型中的低阶参数?

R 当高阶参数保留时,如何删除模型中的低阶参数?,r,lm,R,Lm,问题:只要高阶参数(即交互作用)保留在模型中,我就无法删除模型中的低阶参数(例如,主效应参数)。即使这样做,也会重构模型,并且新模型不会嵌套在更高的模型中。 请参见以下示例(我来自ANOVAs,因此使用了contr.sum): d这里有一种答案;据我所知,没有办法直接用公式来表述这个模型 如上所述构造数据: d <- data.frame(A = rep(c("a1", "a2"), each = 50), B = c("b1", "b2"), value

问题:只要高阶参数(即交互作用)保留在模型中,我就无法删除模型中的低阶参数(例如,主效应参数)。即使这样做,也会重构模型,并且新模型不会嵌套在更高的模型中。
请参见以下示例(我来自ANOVAs,因此使用了
contr.sum
):


d这里有一种答案;据我所知,没有办法直接用公式来表述这个模型

如上所述构造数据:

d <- data.frame(A = rep(c("a1", "a2"), each = 50),
                B = c("b1", "b2"), value = rnorm(100))
options(contrasts=c('contr.sum','contr.poly'))
此方法仅适用于允许明确指定模型矩阵的少数特殊情况(例如,
lm.fit
glm.fit

更一般地说:

## need to drop intercept column (or use -1 in the formula)
X1 <- X1[,!colnames(X1) %in% "(Intercept)"]
## : will confuse things -- substitute something inert
colnames(X1) <- gsub(":","_int_",colnames(X1))
newf <- reformulate(colnames(X1),response="value")
m4 <- lm(newf,data=data.frame(value=d$value,X1))
coef(m4)
## (Intercept)          B1   A1_int_B1 
##  -0.2376631  -0.1301932  -0.0642158 
##需要删除截距列(或在公式中使用-1)

X1我认为最简单的解决方案是使用
model.matrix
。也许,你可以通过一些别致的步法和习惯对比来实现你想要的。但是,如果您想要“Type3-esque”p值,您可能希望模型中的每个术语都使用它,在这种情况下,我认为我使用
model.matrix
的方法非常方便,因为您可以很容易地隐式循环所有模型,每次只删除一列。提供一种可能的方法并不是认可它在统计上的优点,但我确实认为你提出了一个明确的问题,似乎知道它在统计上可能不可靠,因此我认为没有理由不回答它

## initial data
set.seed(10)
d <- data.frame(
  A = rep(c("a1", "a2"), each = 50),
  B = c("b1", "b2"),
  value = rnorm(100))

options(contrasts=c('contr.sum','contr.poly'))

## create design matrix
X <- model.matrix(~ A * B, data = d)

## fit models dropping one effect at a time
## change from 1:ncol(X) to 2:ncol(X)
## to avoid a no intercept model
m <- lapply(1:ncol(X), function(i) {
  lm(value ~ 0 + X[, -i], data = d)
})
## fit (and store) the full model
m$full <- lm(value ~ 0 + X, data = d)
## fit the full model in usual way to compare
## full and regular should be equivalent
m$regular <- lm(value ~ A * B, data = d)
## extract and view coefficients
lapply(m, coef)
到目前为止,对于使用
lm
的机型来说,这很好。您提到这最终是针对
lmer()
,因此这里是一个使用混合模型的示例。我相信,如果你有超过一个随机截距(即,需要从模型的固定和随机部分删除效果),它可能会变得更复杂


阅读Bill Venables关于III型平方和的内容。这是一个统计问题。一种方法是构造完整的设计矩阵(使用
model.matrix
),删除不需要的列,然后将模型与其余列相匹配。如果有机会,我会举一个例子……看一下。Base
R
不支持这一点(参见我之前对Bill Venables的评论。我知道III类平方和的话题对统计社区来说是一个敏感话题。但是,我没有特别征求任何统计建议。我有一个关于如何在R中做某件事的技术问题。你应该知道我的领域(心理学)期望得到类型3,所以我想给出类型3的结果。此外,为什么否决?我的问题是合理的,包含一个可复制的例子,并且是具体的。由于没有解释,它似乎是基于对类型3结果的不喜欢。这是相当不合理的。@Henrik我接受你的观点(在你的领域中期望得到类型III),但对我来说,这只是做某事最糟糕的一个原因;仅仅是因为其他人做了。你读过由mnel链接的Venable的注释吗?我读了有一段时间了,但IIRC它远远超出了对III型SS的厌恶。非常感谢你的伟大回答。我将给你100分(正如你特别展示了如何使用
lmer
)但我会接受本·博尔克的答案(参见此处的基本原理)。感谢你的回答。我会接受你的答案(我认为两者都是相似的),因为你展示了如何构建公式,并提到了两个以上级别的分类预测值问题。
X0 <- model.matrix(m1)
## drop Intercept column *and* A from model matrix
X1 <- X0[,!colnames(X0) %in% "A1"]
m3 <- lm.fit(x=X1,y=d$value)
coef(m3)
## (Intercept)          B1       A1:B1 
## -0.2376631  -0.1301932  -0.0642158 
## need to drop intercept column (or use -1 in the formula)
X1 <- X1[,!colnames(X1) %in% "(Intercept)"]
## : will confuse things -- substitute something inert
colnames(X1) <- gsub(":","_int_",colnames(X1))
newf <- reformulate(colnames(X1),response="value")
m4 <- lm(newf,data=data.frame(value=d$value,X1))
coef(m4)
## (Intercept)          B1   A1_int_B1 
##  -0.2376631  -0.1301932  -0.0642158 
## initial data
set.seed(10)
d <- data.frame(
  A = rep(c("a1", "a2"), each = 50),
  B = c("b1", "b2"),
  value = rnorm(100))

options(contrasts=c('contr.sum','contr.poly'))

## create design matrix
X <- model.matrix(~ A * B, data = d)

## fit models dropping one effect at a time
## change from 1:ncol(X) to 2:ncol(X)
## to avoid a no intercept model
m <- lapply(1:ncol(X), function(i) {
  lm(value ~ 0 + X[, -i], data = d)
})
## fit (and store) the full model
m$full <- lm(value ~ 0 + X, data = d)
## fit the full model in usual way to compare
## full and regular should be equivalent
m$regular <- lm(value ~ A * B, data = d)
## extract and view coefficients
lapply(m, coef)
[[1]]
   X[, -i]A1    X[, -i]B1 X[, -i]A1:B1 
  -0.2047465   -0.1330705    0.1133502 

[[2]]
X[, -i](Intercept)          X[, -i]B1       X[, -i]A1:B1 
        -0.1365489         -0.1330705          0.1133502 

[[3]]
X[, -i](Intercept)          X[, -i]A1       X[, -i]A1:B1 
        -0.1365489         -0.2047465          0.1133502 

[[4]]
X[, -i](Intercept)          X[, -i]A1          X[, -i]B1 
        -0.1365489         -0.2047465         -0.1330705 

$full
X(Intercept)          XA1          XB1       XA1:B1 
  -0.1365489   -0.2047465   -0.1330705    0.1133502 

$regular
(Intercept)          A1          B1       A1:B1 
 -0.1365489  -0.2047465  -0.1330705   0.1133502 
## mixed example
require(lme4)

## data is a bit trickier
set.seed(10)
mixed <- data.frame(
  ID = factor(ID <- rep(seq_along(n <- sample(3:8, 60, TRUE)), n)),
  A = sample(c("a1", "a2"), length(ID), TRUE),
  B = sample(c("b1", "b2"), length(ID), TRUE),
  value = rnorm(length(ID), 3) + rep(rnorm(length(n)), n))

## model matrix as before
X <- model.matrix(~ A * B, data = mixed)

## as before but allowing a random intercept by ID
## becomes trickier if you need to add/drop random effects too
## and I do not show an example of this
mm <- lapply(1:ncol(X), function(i) {
  lmer(value ~ 0 + X[, -i] + (1 | ID), data = mixed)
})

## full model
mm$full <- lmer(value ~ 0 + X + (1 | ID), data = mixed)
## full model regular way
mm$regular <- lmer(value ~ A * B + (1 | ID), data = mixed)

## view all the fixed effects
lapply(mm, fixef)
[[1]]
   X[, -i]A1    X[, -i]B1 X[, -i]A1:B1 
 0.009202554  0.028834041  0.054651770 

[[2]]
X[, -i](Intercept)          X[, -i]B1       X[, -i]A1:B1 
        2.83379928         0.03007969         0.05992235 

[[3]]
X[, -i](Intercept)          X[, -i]A1       X[, -i]A1:B1 
        2.83317191         0.02058800         0.05862495 

[[4]]
X[, -i](Intercept)          X[, -i]A1          X[, -i]B1 
        2.83680235         0.01738798         0.02482256 

$full
X(Intercept)          XA1          XB1       XA1:B1 
  2.83440919   0.01947658   0.02928676   0.06057778 

$regular
(Intercept)          A1          B1       A1:B1 
 2.83440919  0.01947658  0.02928676  0.06057778