相当于R中的对比(Stata)

Equivalent to contrast (Stata) in R

我想知道 R 中是否有与 Stata 的 contrast 命令等效的命令。

具体我感兴趣sex@agegroup#group2

我知道包 margins 可以计算模型对象的边际(或部分)效应。

我从 Stata 网页中提取了一个示例: https://www.stata.com/features/overview/contrasts/

regress y agegroup##sex

contrast r.sex@agegroup
Contrasts of marginal linear predictions

Margins      : asbalanced
                                         df           F        P>F

          sex@agegroup       

(female vs male) 10-19                    1        1.13     0.2945

(female vs male) 20-29                    1        3.36     0.0743

(female vs male) 30-39                    1        5.00     0.0310

(female vs male) 40-59                    1        0.41     0.5279

(female vs male) 60-79                    1        2.71     0.1076

                 Joint                    5        2.52     0.0448                           

            Denominator                  40

                                   Contrast   Std. Err.     [95% Conf. Interval]

             sex@agegroup        

(female vs male) 10-19             6.841855   6.441542     -6.176987     19.8607

(female vs male) 20-29            -11.80631   6.441542     -24.82515    1.212534

(female vs male) 30-39            -14.40607   6.441542     -27.42491   -1.387228

(female vs male) 40-59            -4.101691   6.441542     -17.12053    8.917151

(female vs male) 60-79            -10.60137   6.441542     -23.62022    2.417469

感谢 Ben Bolker,我找到了 emmeans 并且我可以复制 Stata 的对比命令结果:

library(emmeans) 

lm.model= lm(y ~ agegroup*sex,data= df)

emmeans(lm.model, pairwise ~ sex*agegroup,simple = c("sex"))