我想基于cyl变量分组计算am变量的平均边际效应,为什么下面两个方法给出的结果不一样?
library(marginaleffects)
library(tidyverse)
tmp <- mtcars %>%
as_tibble() %>%
transmute(am = factor(am), cyl = factor(cyl), mpg = mpg)
mod <- lm(mpg ~ am * cyl, data = tmp)
# method_1
avg_comparisons(mod, variables = "am", by = "cyl")
# method_2
comparisons(mod, variables = c("am")) %>%
as_tibble() %>%
group_by(cyl) %>%
summarise(Estimate = mean(estimate))