【问题标题】:Comparing two groups with the linear model将两组与线性模型进行比较
【发布时间】:2021-02-26 04:11:52
【问题描述】:

当 parendiv 是我的因变量并且routine1997 是我的自变量时,我想进行回归,并将男性与女性进行比较。代码是这样的:

structure(list(gender = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 1L, 
2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L), .Label = c("male", 
"female"), class = "factor"), parent = structure(c(2L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), .Label = c("intact", "parentaldivorce"), class = "factor"), 
    routine = structure(c(1L, 1L, 1L, 1L, NA, 3L, 3L, 3L, 
    3L, 3L, 3L, 3L, 3L, 1L, 2L, 3L, 2L, 1L, 3L, 3L), .Label = c("Med", 
    "High", "Low"), class = "factor")), row.names = c(3L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 16L, 18L, 19L, 21L, 22L, 23L, 24L, 
25L, 28L, 29L, 30L, 34L), class = "data.frame")

这是代码,我想专门比较男女之间的系数。

lm(parent~routine, data=nlsy97, subset=gender)

【问题讨论】:

  • 以性别作为虚拟变量拟合模型:out <- lm(parent ~ routine + gender, data=nlsy97); summary(out)。然后查看性别的估计系数。

标签: r regression subset


【解决方案1】:

有两种比较系数的方法。 最简单的方法是将性别编码为虚拟 (0/1) 并在模型中包含交互项。然后,您会得到性别对系数的影响,并带有一个 p 值:

out = lm(parent ~ routine + gender + routine*gender, data=nlsy97)

另一种方法是使用多组回归并将合并回归模型(包括所有性别)与未合并模型(性别的单独斜率或截距或两者)进行比较。 AIC 最小的模型最适合数据。如果您的随机斜率模型产生的 AIC 最低,则说明您的效果存在性别差异。如果随机截距是最好的,那么您只是在性别之间存在水平差异,但可能会产生相同的影响。

library(lme4)
pooled = lm(parent ~ routine, data=nlsy97)
r.inter = lmer(parent ~ routine + (1|gender), data=nlsy97)
r.slope = lmer(parent ~ routine + (routine|gender), data=nlsy97)
r.unpooled = lmer(parent ~ routine + (1+routine|gender), data=nlsy97)

AIC(pooled)
AIC(r.inter)
AIC(r.slope)
AIC(r.unpooled)

在具有最低 AIC 的模型上使用方法 coefficients() 可为您提供各个组的准确系数。

编辑:我刚刚注意到您总共只有 20 个案例。如果这是您的整个数据集,您可能根本不应该进行任何统计分析。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    • 2020-11-01
    • 1970-01-01
    • 2014-04-01
    • 2021-12-18
    • 2021-07-19
    • 1970-01-01
    相关资源
    最近更新 更多