Hi everyone

I am currently working on a project (using Stata 15) to analyse changes in kidney function (ckd) after switching from one medication to another (med1 to med2). I fitted a complex mixed effects model as follows:

Code:
mixed ckd_value c.time##c.time##i.med1_med2##i.ckdgroup_baseline2 c.time##i.med1_med2##i.ckdgroup_baseline2 age10 i.sex i.ethnicity_simple i.cd4_group2 i.rtv i.dtg i.bactrim i.cobi i.comorb_cvd i.comorb_aht i.comorb_dm i.comorb_dyslip i.hcv_chronic i.hbs_ag, || id:
- output omitted -

ckd_value: kidney function
time: time from baseline to ckd_value measurement (in years)
med1_med2: defines which medication used (0=med1 1=med2)
ckdgroup_baseline2: stratifies data further into 3 groups of ckd_value at baseline
rest are demographic variables / confounders

I was able to describe changes over time with this model very nicely. To analyse this effect for subgroups, I would like to predict the change in ckd_value after 1 year for the demographic variables (age10=continuous, sex, ethnicity_simple, comorb_dm etc) when patients are switched to med2.

I used following command which gives me ckd_values at baseline (time=0) and after 1 year:

Code:
margin sex ethnicity_simple cd4_group2 rtv dtg bactrim cobi comorb_cvd comorb_aht comorb_dm hcv_chronic hbs_ag, at( time=(0 1) med1_med2=1) atmeans



1._at        : time            =           0
               med1_med2         =           1
              ***REST omitted***

2._at        : time            =           1
               med1_med2         =           1
               ***REST omitted***


--------------------------------------------------------------------------------------
                     |            Delta-method
                     |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
---------------------+----------------------------------------------------------------
             _at#sex |
             1#male  |   93.47407   .2998836   311.70   0.000     92.88631    94.06183
           1#female  |     91.788   .3897248   235.52   0.000     91.02416    92.55185
             2#male  |   92.21797   .2493154   369.88   0.000     91.72932    92.70662
           2#female  |    90.5319   .3523787   256.92   0.000     89.84125    91.22255
                     |
_at#ethnicity_simple |
            1#White  |   91.33833   .2988618   305.62   0.000     90.75257    91.92409
            1#Black  |   102.4775   .4830898   212.13   0.000     101.5306    103.4243
            1#Other  |   91.64493   .5553936   165.01   0.000     90.55638    92.73348
            2#White  |   90.08222   .2478723   363.42   0.000      89.5964    90.56804
            2#Black  |   101.2214   .4536119   223.15   0.000     100.3323    102.1104
            2#Other  |   90.38882   .5305386   170.37   0.000     89.34899    91.42866

*** Rest of output omitted
What I want to is to calculate the difference between 1._at and 2._at for each demographic value. I was able to do it overall:

Code:
margins, at(time=(0 1) med1_med2=1) atmeans contrast(atcontrast(r._at))



------------------------------------------------
             |         df        chi2     P>chi2
-------------+----------------------------------
         _at |          1       14.47     0.0001
------------------------------------------------

--------------------------------------------------------------
             |            Delta-method
             |   Contrast   Std. Err.     [95% Conf. Interval]
-------------+------------------------------------------------
         _at |
   (2 vs 1)  |  -1.256105   .3302356     -1.903355   -.6088551
--------------------------------------------------------------
Which gives me the difference between time=1 and time=0 overall. When I try to calculate this for any given covariate (such as sex) in the margin, it doesn't work as expected:


Code:
margins sex, at(time=(0 1) med1_med2=1) atmeans contrast(atcontrast(r._at))



------------------------------------------------
             |         df        chi2     P>chi2
-------------+----------------------------------
     _at#sex |          1        0.00     1.0000
------------------------------------------------
Is there a way to achieve this with margins? And how would you incude age10 (continuous)? do I have to specify a dummy_variable for age above/below 50 years?

Any help in achieving this is warmly appreciated. Thanks in advance!