Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Mixed model repeated measures using f_able/f_rcspline

    Hi!
    Maybe a question for FernandoRios
    Following up on https://www.statalist.org/forums/for...es-mixed-model New question: I would like to try to model the relationship with splines instead of a quadratic transformation.


    We had this code (incl. data) to model weight change (diff) over time (age) adding sex (girl) interacted with age and including the subject (id) as a random effect. The model was forced to go through 0/0 (as the weight difference is measured from timepoint 0) . This was achieved using a mixed model and a quadratic transformation of age in order to handle the non-linear relationship.



    Code from previous thread with quadratic transformation:

    Code:
    webuse childweight, clear
    replace brthwt = brthwt/1000     // make it same unit as weight
    gen diff = weight-brthw  
    
    mixed diff i.girl#c.age##c.age, nocons || id:
    margins i.girl, at(age = (0(0.5)2.5))
    marginsplot, name(one, replace)

    Click image for larger version

Name:	image_34893.png
Views:	1
Size:	85.8 KB
ID:	1754444





    Now I discovered f_able & f_rcspline and would like to use this instead. I would assume that version 1 (see below) is correct - or am i completely wrong? I also cant explain, why v3 only estimates for girls and not boys.

    Code:
    webuse childweight, clear
    replace brthwt = brthwt/1000     // make it same unit as weight
    gen diff = weight-brthw  
    
    f_rcspline f_age = age, nknots(3)
    
    * version 1 - interaction with age and f_age2
    mixed diff i.girl#(c.age c.f_age2), nocons || id:
    f_able f_age2 , auto
    margins i.girl, at(age = (0(0.5)2.5))
    marginsplot, name(one, replace)
    
    * version 2 - interaction with age only
    mixed diff i.girl#c.age c.f_age2, nocons || id:
    f_able f_age2 , auto
    margins i.girl, at(age = (0(0.5)2.5))
    marginsplot, name(two, replace)
    
    * version 3
    mixed diff i.girl##(c.age c.f_age2) , nocons || id:
    f_able f_age2 , auto
    margins i.girl, at(age = (0(0.5)2.5))
    marginsplot, name(three, replace)
    
    
    graph combine one two three, col(3)
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	55.7 KB
ID:	1754454








    Version 3 - no margins estimate for boys
    Click image for larger version

Name:	Bildschirmfoto 2024-05-25 um 09.07.13.png
Views:	1
Size:	45.7 KB
ID:	1754456

    Last edited by Fabian Fortner; 25 May 2024, 02:25.

  • #2
    Followup comment:

    Code:
     mixed diff i.girl#(c.age c.f_age2), nocons || id:
    
     yields exactly the same results as
    
     mixed diff i.girl#(c.age c.f_age2) c.age c.f_age2, nocons || id:

    Comment


    • #3
      Hi Fabian
      1) All options are correct, however, Option 1 and 3 are the best because they allow for full heterogeneity (all coefficients change by gender)
      option 2 assumes f_age2 is the same for both girls and Boys

      2) The problem with option 3 is due to numerical errors, missing coefficients, and other internal settings with margins. Namely, when there are some settings like nocons, or near multicolinearity or non-estimated coefficients, margins no longer is able to correctly estimate coefficients and Standard errors, because it cannot detect the correct settings.

      If you would drop "nocons" the problem wouldn't trigger.
      On the other hand, if you add noestimcheck on the margin command, 3 will still produce the results you get in 1.

      I have some other cases and suggestions how to check on that on the paper. here https://www.stata-journal.com/articl...article=st0628
      HTH
      F

      Comment


      • #4
        Hi FernandoRios
        Thanks a lot! This helps!

        Regarding option 3: I don't truly understand the underlying internal workings of margins, but I assume the missing estimation for the 'boy' level could have to do with how the base level of that factor is handled: I tried option and e.g. using ibn.girl with the nocons option, which results again in the same result as when no nocons is used.

        Is there than another way of forcing the model to go through 0 and allowing margins to work?

        Comment

        Working...
        X