rstataanovacompare-contrast

Equivalent to contrast (Stata) in R


I wonder if there is some equivalent to Stata's contrast command in R.

Specifically I am interested in sex@agegroup#group2

I know package margins to calculate marginal (or partial) effects from model objects.

From Stata web page I extracted an example: 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

Solution

  • Thanks to Ben Bolker I found emmeans and I could replicate Stata's contrast command results:

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