使用lm进行2样本比例测试


12

我一直在使用线性模型执行2样本比例测试,但是已经意识到这可能并不完全正确。看来,使用具有二项式族+身份链接的广义线性模型可以准确给出未汇集的2样本比例测试结果。但是,使用线性模型(或带有高斯族的glm)得出的结果略有不同。我认为这可能是由于R如何解决二项式和高斯族的glm,但是还有其他原因吗?

## prop.test gives pooled 2-sample proportion result
## glm w/ binomial family gives unpooled 2-sample proportion result
## lm and glm w/ gaussian family give unknown result

library(dplyr)
library(broom)
set.seed(12345)

## set up dataframe -------------------------
n_A <- 5000
n_B <- 5000

outcome <- rbinom(
  n = n_A + n_B,
  size = 1,
  prob = 0.5
)
treatment <- c(
  rep("A", n_A),
  rep("B", n_B)
)

df <- tbl_df(data.frame(outcome = outcome, treatment = treatment))


## by hand, 2-sample prop tests ---------------------------------------------
p_A <- sum(df$outcome[df$treatment == "A"])/n_A
p_B <- sum(df$outcome[df$treatment == "B"])/n_B

p_pooled <- sum(df$outcome)/(n_A + n_B)
z_pooled <- (p_B - p_A) / sqrt( p_pooled * (1 - p_pooled) * (1/n_A + 1/n_B) )
pvalue_pooled <- 2*(1-pnorm(abs(z_pooled)))

z_unpooled <- (p_B - p_A) / sqrt( (p_A * (1 - p_A))/n_A + (p_B * (1 - p_B))/n_B )
pvalue_unpooled <- 2*(1-pnorm(abs(z_unpooled)))


## using prop.test --------------------------------------
res_prop_test <- tidy(prop.test(
  x = c(sum(df$outcome[df$treatment == "A"]), 
        sum(df$outcome[df$treatment == "B"])),
  n = c(n_A, n_B),
  correct = FALSE
))
res_prop_test # same as pvalue_pooled
all.equal(res_prop_test$p.value, pvalue_pooled)
# [1] TRUE


# using glm with identity link -----------------------------------
res_glm_binomial <- df %>%
  do(tidy(glm(outcome ~ treatment, family = binomial(link = "identity")))) %>%
  filter(term == "treatmentB")
res_glm_binomial # same as p_unpooled
all.equal(res_glm_binomial$p.value, pvalue_unpooled)
# [1] TRUE


## glm and lm gaussian --------------------------------

res_glm <- df %>%
  do(tidy(glm(outcome ~ treatment))) %>%
  filter(term == "treatmentB")
res_glm 
all.equal(res_glm$p.value, pvalue_unpooled)
all.equal(res_glm$p.value, pvalue_pooled)

res_lm <- df %>%
  do(tidy(lm(outcome ~ treatment))) %>% 
  filter(term == "treatmentB")
res_lm
all.equal(res_lm$p.value, pvalue_unpooled)
all.equal(res_lm$p.value, pvalue_pooled)

all.equal(res_lm$p.value, res_glm$p.value)
# [1] TRUE

Answers:


8

这与他们如何解决与拟合模型相对应的优化问题无关,而与模型带来的实际优化问题有关。

具体来说,在大样本中,您可以将其有效地视为比较两个加权最小二乘问题

线性模型(lm)假设(未加权时)比例的方差是恒定的。glm假设比例的方差来自二项式假设。这对数据点的加权不同,因此得出的估算值有些不同*,差异的方差也不同。Var(p^)=Var(X/n)=p(1p)/n

*至少在某些情况下,尽管不一定要直接比较比例


0

在计算方面,比较lm与二项式glm的treatmentB系数的标准误差。您具有二项式glm(z_unpooled的分母)中treatmentB系数的标准误差的公式。标准lm中treatmentB系数的标准误差为(SE_lm):

    test = lm(outcome ~ treatment, data = df)
    treat_B =  as.numeric(df$treatment == "B")
    SE_lm = sqrt( sum(test$residuals^2)/(n_A+n_B-2) / 
              sum((treat_B - mean(treat_B))^2))

参见该帖子的推导,唯一的区别是在这里找到了样本误差而不是(即,由于失去的自由度,从减去2 )。如果没有,则当时,lm和二项式glm标准错误实际上似乎匹配。Ñ + Ñ - 2 Ñ = Ñ σ2nA+nB2nA=nB

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.