Announcement

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

  • #16
    There was no pid variable in the example you posted, so I can't really work with it in a mixed model. Also, all of the observations you showed were controls. I'll need those things fixed in order to work on it. I also note that you have some negative ages in the data: that won't matter for the purposes of my experimenting with code, but if that's in your real data, you need to look into that.

    As for your question 1., b) is correct, not a).

    As for how to handle the predicted difference of the bmi after 24 months, the approach is this. You will have 3 time spline variables, timespl1, timespl2, and timespl3. Then you would do something like this:

    Code:
    forvalues i = 1/3 {
        summ timespl`i' if time == 24, meanonly
        local sp24_`i' = r(mean) 
    }
    
    lincom `sp24_1'*_b[1.case#timespl1] + `sp24_2'*_b[1.case#timespl2] + `sp24_3'*_b[1.case#timespl3]
    As for 2), it's not good, but probably not fatal. One possibility relates to the way you have constructed the cubic splines. Having knots at the endpoints of the data range is not very helpful. You might want to consider using knots(6 12 24), or something like that. Or perhaps just specify nknots(4) and let Stata locate them for you.


    Comment


    • #17
      Thank you very much! I am very sorry for the wrong and incomplete "data ex" post earlier!
      I am not sure if the hundred observation are enough to play around. Turns out, it's not that easy to generate randomised useful data.

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input int pid byte(time sex) double bmi float(case type_surgery timesq age)
         3 36 1 27.795711137540636 1 1 1296  45.23434
        12 36 1   40.4182116036769 1 0 1296  42.85219
        34 24 0  40.47088870070875 1 0  576 25.811335
        38 12 0 37.636846599169075 1 0  144  41.47675
        43 36 0  46.37598120416514 1 0 1296  24.75772
        45 30 1  29.98891804041341 1 0  900  40.51639
        48  6 0 28.250645559979603 1 0   36 26.214363
        48 24 0 33.996475293021646 1 1  576  41.34387
        50  0 0   49.3393788987305 1 1    0 24.205444
        60 36 1 25.057400486804546 1 0 1296  45.15212
        62 36 1 33.831986168632284 1 0 1296   49.7994
        64 24 1  35.05041347723454 1 0  576    47.375
        68 12 1  36.15728800059296 1 0  144  36.54261
        69 30 0  33.43513063038699 1 0  900   49.9425
        70  0 1  46.36175407525152 1 1    0  44.17369
        74 12 1 52.215299900295214 1 1  144  66.42235
        74 30 0  45.36042745700106 1 0  900  47.83125
        75 18 0 28.101808385690674 1 1  324  40.38145
        76  0 1 59.435519045218825 1 0    0 26.848434
        77  0 1  47.09574883980677 1 1    0  28.92621
        83  6 0  48.97281633503735 1 0   36  21.21467
        86 30 1  38.91017593652941 1 0  900  46.17112
        87 24 0 30.862265846366064 1 1  576  22.13721
        89 12 1  35.13481263113208 1 0  144 25.155167
        91 24 1  46.99475313550793 1 0  576 36.070095
        92  0 1 57.270582399703564 1 0    0 22.059793
        96 24 0 30.528605085331947 1 1  576 25.161015
        99 12 1 31.932709329156204 1 1  144   41.3581
       102 18 1 36.838235853705555 1 0  324 35.258358
       110 18 0 37.170957014616576 1 1  324  45.72072
       115 18 1  34.22423920407891 1 0  324  43.69953
       120 12 0 34.333853874076155 1 0  144 26.748985
       121  6 0  35.28915910711512 1 1   36    22.262
       123  0 1 45.192717771977186 1 1    0  55.78115
       130  0 0   47.3027160812635 1 1    0    47.045
       132  6 1 48.156804852792995 1 1   36  32.19939
       141 12 1 38.355356552870944 1 1  144  55.95682
       146  6 1  39.86728651789017 1 0   36 32.957336
       147 18 1    38.615829961095 1 1  324 37.417435
       149 24 1 43.130509851686654 1 1  576  42.62851
       152  0 1  49.00493605020456 1 1    0 30.563097
       153 24 1 28.483561549335718 1 1  576  38.10367
       162 36 1  32.84104164224118 1 1 1296  48.95276
       165 12 0   47.3087249870412 1 0  144  66.99651
       165 18 1  45.45226414380595 1 0  324  44.40793
       170 24 1  40.61210453659296 1 1  576   57.4024
       171  6 1  31.81756936153397 1 0   36  68.56161
       172 18 0  41.74388271011412 1 1  324  51.48291
       174 12 1  50.46416010032408 1 1  144   24.6528
       174 24 1  32.21021591522731 1 0  576  43.00269
       177 36 1  33.24649471831508 1 0 1296  48.64157
      2001  0 0 54.965977714350444 0 1    0  39.42086
      2001 12 0 42.319059727573766 0 0  144  24.95633
      2001 24 0  20.85674711014144 0 1  576  22.76447
      2006  6 0  39.17652874141932 0 0   36  40.86106
      2010  6 1 53.551471669366585 0 0   36 23.857365
      2014 24 0  32.23327186112292 0 1  576  47.34951
      2016 36 0  34.29309333725833 0 0 1296  43.00515
      2017 18 1 23.893835086608306 0 1  324  39.32271
      2022 30 0  27.84154804330319 0 0  900  56.36491
      2024  6 0  36.03218522779643 0 1   36  46.24323
      2025  6 0  44.38448038380593 0 1   36  41.31748
      2027  0 0  57.69999341834337 0 1    0  34.75654
      2029 18 1  27.11729699675925 0 0  324  28.51779
      2034 12 0   34.6453605953604 0 0  144  50.48364
      2043 24 0   39.6535985362716 0 0  576  30.75055
      2043 36 0  37.49715015375987 0 1 1296 25.183216
      2044 12 1 25.603820276400075 0 0  144  43.12627
      2048 24 0 34.838206013385204 0 1  576  37.00909
      2050  0 1  58.26982050482184 0 0    0  39.07775
      2059 30 0  30.66739819943905 0 1  900  67.73131
      2068 30 1  27.62861569598317 0 0  900  25.52851
      2074 18 0 37.473219498060644 0 0  324  31.70158
      2076  6 1  34.15007617883384 0 1   36  56.40875
      2076 18 0  27.29489605813287 0 1  324  31.07399
      2077  6 0  45.37959129102528 0 0   36  49.73098
      2085  6 1  38.27780978372321 0 0   36  36.97701
      2089 12 0  38.18412678521126 0 0  144 28.712303
      2090  0 0  44.87093111537397 0 0    0  25.80727
      2090 30 1  39.50716947764158 0 0  900 32.711994
      2090 36 1  33.73981603132561 0 1 1296  48.43534
      2096 36 0 28.363479793444277 0 1 1296  22.03686
      2098 30 1 25.739723841194063 0 0  900  22.64652
      2100 30 0  36.80726512754336 0 1  900  46.40441
      2102 12 1 30.186957059800626 0 1  144  35.05153
      2107  6 1 33.447919169673696 0 1   36  43.00396
      2108  0 0  46.80792374559678 0 0    0  48.24437
      2112  0 1  52.08848464661278 0 1    0  41.96966
      2116  6 0  33.37653284170665 0 1   36  41.93347
      2118 30 1  36.29261606791988 0 1  900  46.18892
      2119 18 0  41.58177953148261 0 1  324 23.921726
      2120 18 0 32.792363804765046 0 0  324  48.56776
      2122 12 0  37.43985858783126 0 0  144 37.138557
      2123 36 0 31.226775728771464 0 1 1296  28.32346
      2124 12 1  27.82427311139181 0 1  144  27.13964
      2124 18 1  32.01306085065007 0 1  324  38.14071
      2124 36 1 26.438915594713762 0 0 1296  20.42225
      end
      label values sex sex_lab
      label def sex_lab 0 "male", modify
      label def sex_lab 1 "female", modify
      label values case caseLab
      label def caseLab 0 "control", modify
      label def caseLab 1 "case", modify
      label values type_surgery type_surg_lab
      label def type_surg_lab 0 "Banding", modify
      label def type_surg_lab 1 "Sleeve", modify
      2.)
      Thank you very much for the clarification and the lincom command.
      I used the knots(0 6 12 24 36) at the end and start to workaround the "restricted" cubic spline i.e. linear spline between 0 and 6 as well as 24 and 30 (using knots 6,12,24), but it seems to make some sense to use it with restriction...

      If I understand correctly what I read now about restriced cubic spline using knots (6,12,24) and ignoring sex, type_surgery, case, age and error terms, the model predicts four spline curves over the time course:

      1: bmi = a1 + b1 x time from 0 to 6 months (linear)
      2: bmi = a2 + b2 x time + c2 x time^2 + d2 x time^3 from 6 to 12 months (cubic)
      3: bmi = a3 + b3 x time + c3 x time^2 d2 x time^3 from 12 to 24 months (cubic)
      4: bmi = a4 + b4 x time from 24 to 36 months

      All the information necessary to predict a_i, b_i and c_i and d_i must be in the models output timespl* (e.g. a_i for i>2 is defined by the endpoint of the spline before and a_1 is probably the mean bmi at baseline. Furthermore I guess the spline is differentiable at the knots, so that gives information about the other coefficients), but I still don't understand what information is stored in the variable timespl2 for example. The values are the same for each point in time, but are different from zero in the boundaries out of the "lifezone of the spline" (I thought they would be zero). Is it easy to understand what is stored in there (reading the manual mkspline and google around some time, it's probably not that easy)?

      If I now want to incorporate being a case and interaction over time in the model, I thought it's something like:
      1: bmi = a1 + b1 x time + _b[case]*case from 0 to 6 months (linear)
      2: bmi = a2 + b2 x time + c2 x time^2 + d2 x time^3 + sp_(time) x _b[1.case#timespl1] from 6 to 12 months (cubic)
      3: bmi = a3 + b3 x time + c3 x time^2 d2 x time^3 + sp_(time) x _b[1.case#timespl2] from 12 to 24 months (cubic)
      4: bmi = a4 + b4 x time x ??? from 24 to 36 months

      So, I actually don't understand i) why the formula you posted for the lincom is not taking into account the coefficient case and ii) where the information to calculate "???" is stored and iii) what would change if I want to predict the bmi after 30months? Hope you understand my problems.

      I am starting to feel like a statistician. :-)

      Best wishes M.

      Comment


      • #18
        I will try to work with the example data and see if I can come up with a workaround. I'll get back to you later on that.

        So, I actually don't understand i) why the formula you posted for the lincom is not taking into account the coefficient case and
        You are quite right; that was an error on my part. _b[1.case] should be added to the expression I showed in the -lincom- command.

        ii) where the information to calculate "???" is stored
        The splines contain no information at all about any of the variables in the data set other than time itself. They simply constitute a different way of representing time in the model. First, time is split over several different variables (each of which is a non-decreasing function of time in its own right), and this provides more flexibility than just a single time variable or a time and time^2 variable. Each of the separate time variables "takes off" in a different range of the data, and most of them bend at the knots. The regression model you put them in will then, in effect, allocate the time effect among these different functions of time. The net result is a highly flexible way to represent the effect of time in the model. (You can think of the regression as accomplishing something like breaking down a complex sound wave into a combination of several pure-tone components. This analogy is fairly loose: we don't have periodic functions here and we are not doing Fourier analysis, but I find it helpful to think about splines this way.)

        and iii) what would change if I want to predict the bmi after 30months?
        Your data only go out to 30 months; you have no right to try to predict beyond that time period, no matter what model you have fit. If you want to "break the rules" you would have to find the actual formulas used for each of the spline functions, and then calculate their values at time = 30 and then create a -lincom- expression like the one for the quadratic model. For a quadratic, that's easy enough to do. For these splines that would be a difficult task. In this case, I think it's a good thing that it's difficult, since you shouldn't be doing it anyway.

        If I understand correctly what I read now about restriced cubic spline using knots (6,12,24) and ignoring sex, type_surgery, case, age and error terms, the model predicts four spline curves over the time course:

        1: bmi = a1 + b1 x time from 0 to 6 months (linear)
        2: bmi = a2 + b2 x time + c2 x time^2 + d2 x time^3 from 6 to 12 months (cubic)
        3: bmi = a3 + b3 x time + c3 x time^2 d2 x time^3 from 12 to 24 months (cubic)
        4: bmi = a4 + b4 x time from 24 to 36 months
        Well, yes. But I think it's important to remember that each spline is pegged to zero up to the time where it "takes off." So, a2 and b2 can be written as:
        Code:
        a2 = a1 + a2'
        b2 = b1 + b2'
        and for your third equation:
        Code:
        a3 = a1 + a2' + a3'
        b3 = b1 + b2' + b3'
        c3 = c2 + c3'
        d2 = d2 + d3'
        and similarly for the fourth equation.

        Comment


        • #19
          Well, what I thought I was going to do to emulate -margins- will not work. The problem, of course, is with the random intercepts and slopes. If the values of the fixed-level variables are changed, the estimates of those would also change, whereas, presumably in real life, the random intercept and slope are actually fixed, unchanging attributes of the person (or group variable defining the higher level.) Thinking of it that way, it probably makes sense to treat those random slopes and intercepts as fixed constants when calculating the "margins." That can be done with the following code:

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input int pid byte(time sex) double bmi float(case type_surgery timesq age)
             3 36 1 27.795711137540636 1 1 1296  45.23434
            12 36 1   40.4182116036769 1 0 1296  42.85219
            34 24 0  40.47088870070875 1 0  576 25.811335
            38 12 0 37.636846599169075 1 0  144  41.47675
            43 36 0  46.37598120416514 1 0 1296  24.75772
            45 30 1  29.98891804041341 1 0  900  40.51639
            48  6 0 28.250645559979603 1 0   36 26.214363
            48 24 0 33.996475293021646 1 1  576  41.34387
            50  0 0   49.3393788987305 1 1    0 24.205444
            60 36 1 25.057400486804546 1 0 1296  45.15212
            62 36 1 33.831986168632284 1 0 1296   49.7994
            64 24 1  35.05041347723454 1 0  576    47.375
            68 12 1  36.15728800059296 1 0  144  36.54261
            69 30 0  33.43513063038699 1 0  900   49.9425
            70  0 1  46.36175407525152 1 1    0  44.17369
            74 12 1 52.215299900295214 1 1  144  66.42235
            74 30 0  45.36042745700106 1 0  900  47.83125
            75 18 0 28.101808385690674 1 1  324  40.38145
            76  0 1 59.435519045218825 1 0    0 26.848434
            77  0 1  47.09574883980677 1 1    0  28.92621
            83  6 0  48.97281633503735 1 0   36  21.21467
            86 30 1  38.91017593652941 1 0  900  46.17112
            87 24 0 30.862265846366064 1 1  576  22.13721
            89 12 1  35.13481263113208 1 0  144 25.155167
            91 24 1  46.99475313550793 1 0  576 36.070095
            92  0 1 57.270582399703564 1 0    0 22.059793
            96 24 0 30.528605085331947 1 1  576 25.161015
            99 12 1 31.932709329156204 1 1  144   41.3581
           102 18 1 36.838235853705555 1 0  324 35.258358
           110 18 0 37.170957014616576 1 1  324  45.72072
           115 18 1  34.22423920407891 1 0  324  43.69953
           120 12 0 34.333853874076155 1 0  144 26.748985
           121  6 0  35.28915910711512 1 1   36    22.262
           123  0 1 45.192717771977186 1 1    0  55.78115
           130  0 0   47.3027160812635 1 1    0    47.045
           132  6 1 48.156804852792995 1 1   36  32.19939
           141 12 1 38.355356552870944 1 1  144  55.95682
           146  6 1  39.86728651789017 1 0   36 32.957336
           147 18 1    38.615829961095 1 1  324 37.417435
           149 24 1 43.130509851686654 1 1  576  42.62851
           152  0 1  49.00493605020456 1 1    0 30.563097
           153 24 1 28.483561549335718 1 1  576  38.10367
           162 36 1  32.84104164224118 1 1 1296  48.95276
           165 12 0   47.3087249870412 1 0  144  66.99651
           165 18 1  45.45226414380595 1 0  324  44.40793
           170 24 1  40.61210453659296 1 1  576   57.4024
           171  6 1  31.81756936153397 1 0   36  68.56161
           172 18 0  41.74388271011412 1 1  324  51.48291
           174 12 1  50.46416010032408 1 1  144   24.6528
           174 24 1  32.21021591522731 1 0  576  43.00269
           177 36 1  33.24649471831508 1 0 1296  48.64157
          2001  0 0 54.965977714350444 0 1    0  39.42086
          2001 12 0 42.319059727573766 0 0  144  24.95633
          2001 24 0  20.85674711014144 0 1  576  22.76447
          2006  6 0  39.17652874141932 0 0   36  40.86106
          2010  6 1 53.551471669366585 0 0   36 23.857365
          2014 24 0  32.23327186112292 0 1  576  47.34951
          2016 36 0  34.29309333725833 0 0 1296  43.00515
          2017 18 1 23.893835086608306 0 1  324  39.32271
          2022 30 0  27.84154804330319 0 0  900  56.36491
          2024  6 0  36.03218522779643 0 1   36  46.24323
          2025  6 0  44.38448038380593 0 1   36  41.31748
          2027  0 0  57.69999341834337 0 1    0  34.75654
          2029 18 1  27.11729699675925 0 0  324  28.51779
          2034 12 0   34.6453605953604 0 0  144  50.48364
          2043 24 0   39.6535985362716 0 0  576  30.75055
          2043 36 0  37.49715015375987 0 1 1296 25.183216
          2044 12 1 25.603820276400075 0 0  144  43.12627
          2048 24 0 34.838206013385204 0 1  576  37.00909
          2050  0 1  58.26982050482184 0 0    0  39.07775
          2059 30 0  30.66739819943905 0 1  900  67.73131
          2068 30 1  27.62861569598317 0 0  900  25.52851
          2074 18 0 37.473219498060644 0 0  324  31.70158
          2076  6 1  34.15007617883384 0 1   36  56.40875
          2076 18 0  27.29489605813287 0 1  324  31.07399
          2077  6 0  45.37959129102528 0 0   36  49.73098
          2085  6 1  38.27780978372321 0 0   36  36.97701
          2089 12 0  38.18412678521126 0 0  144 28.712303
          2090  0 0  44.87093111537397 0 0    0  25.80727
          2090 30 1  39.50716947764158 0 0  900 32.711994
          2090 36 1  33.73981603132561 0 1 1296  48.43534
          2096 36 0 28.363479793444277 0 1 1296  22.03686
          2098 30 1 25.739723841194063 0 0  900  22.64652
          2100 30 0  36.80726512754336 0 1  900  46.40441
          2102 12 1 30.186957059800626 0 1  144  35.05153
          2107  6 1 33.447919169673696 0 1   36  43.00396
          2108  0 0  46.80792374559678 0 0    0  48.24437
          2112  0 1  52.08848464661278 0 1    0  41.96966
          2116  6 0  33.37653284170665 0 1   36  41.93347
          2118 30 1  36.29261606791988 0 1  900  46.18892
          2119 18 0  41.58177953148261 0 1  324 23.921726
          2120 18 0 32.792363804765046 0 0  324  48.56776
          2122 12 0  37.43985858783126 0 0  144 37.138557
          2123 36 0 31.226775728771464 0 1 1296  28.32346
          2124 12 1  27.82427311139181 0 1  144  27.13964
          2124 18 1  32.01306085065007 0 1  324  38.14071
          2124 36 1 26.438915594713762 0 0 1296  20.42225
          end
          label values sex sex_lab
          label def sex_lab 0 "male", modify
          label def sex_lab 1 "female", modify
          label values case caseLab
          label def caseLab 0 "control", modify
          label def caseLab 1 "case", modify
          label values type_surgery type_surg_lab
          label def type_surg_lab 0 "Banding", modify
          label def type_surg_lab 1 "Sleeve", modify
          
          mixed bmi i.case##c.(time timesq) i.type_surgery i.sex c.age || pid: time timesq
          predict rr*, reffects
          
          //    GET A POSTFILE TO HOLD RESULTS FOR GRAPHS
          capture postutil clear
          tempfile tograph
          postfile handle byte case float time fitted using `tograph'
          
          //    PRESERVE ORIGINAL VALUES OF KEY VARIABLES
          clonevar case_original = case
          clonevar time_original = time
          clonevar timesq_original = timesq
          
          //    EMULATE WHAT -MARGINS- DOES
          forvalues c = 0/1 {
              foreach t of numlist 0(6)36 {
                  replace case = `c'
                  replace time = `t'
                  replace timesq = `t'*`t'
                  predict xb, xb
                  gen fitted = xb + rr1*time + rr2*timesq + rr3
                  summ fitted, meanonly
                  post handle (`c') (`t') (`r(mean)')
                  drop xb fitted 
              }
          }
          postclose handle
          
          //    RESTORE ORIGINAL VALUES OF KEY VARIABLES
          replace case = case_original
          replace time = time_original
          replace timesq = timesq_original
          
          preserve
          
          //    BRING IN THE GRAPHING DATA AND GRAPH IT
          use `tograph', clear
          reshape wide fitted, i(time) j(case)
          graph twoway line fitted* time, sort // CUSTOMIZE WITH OPTIONS AS YOU LIKE
          Now, one could criticize this assumption that the random effects, once estimated from the data, are actually fixed attributes of the patient. But I think this is the best we can do. At least I am out of ideas for this.

          Comment


          • #20
            Thank you very much!

            1. Fomula)
            Your data only go out to 30 months; you have no right to try to predict beyond that time period.
            I disagree on this. My data go up to 36months (It´s my good right :-) to predict it up to there; so it's actually not a good thing that it is difficult to find the formular for the spline and then use the adapted lincom command. :-) The beautiful lines you send for the lincom command to predict the differences between cases and controls are only working for the knots (if I understand the whole thing correctly). And the same problem (I need the actual formular for the splines) will be at time = 15 e.g.; which is as good or bad as time = 30, but not a knot). But its enough for my purpose and I thank you very much for this clarification!


            Further, I thank you very much for the nice analogy and the explanation what the splinevariables contain. For me, the transfer from the timevariables to the actual formulas of the models is quite difficult. Pretty sure this would form an even better understanding of how the spline and the variables in the models and the formulas are connected and would make it clear to me why the "case" effect at the knot points can be calculated using your lincom command.

            2. Plot)
            I really like and understand now the difference of what I did trying to predict and plot in contrast to your approach what you are trying by emulating the margin command.
            One conclusion of what you were saying is, that probably the margin command cannot be used - or better to say only under the assumption that the random effects, once estimated from the data, are actually fixed attributes of the patient - for any kind of "random intercept" or "random slopes" models, right?

            For example even in a linear mixed model with random intercept and slopes in time (don't wan to use is; just for me to understand)
            - mixed c.bmi i.case##c.time c.age i.sex i.type_surgery || pid: time, stddev base -
            the commands
            - margins i.case, at(time=(0(6)36)) - and for graphical representation - marginsplot, xdim(time) recastci(rarea)-
            assumes that the random effects once estimated are fixed attributes of the patient. Am I correct?

            Your code works perfectly - thank you very much! It is not adabtable to the spline situation without knowing the exact formula as far as I understand the code, right?

            You are a really great help and I really appreciate the support of you and this forum! I learned a lot!

            Comment


            • #21
              The method I used for getting the values of the spline variables corresponding to a particular time will work with any value of time that is actually instantiated in the data. Sorry for my confusion about 36 being out of range--you are, of course, correct. But you should be able to get the values of all the spline variables corresponding to time = 36 in the same way I showed in #16, just substituting 36 for 24.

              As for getting the values when time = 15, that is harder. But there is a simple trick. Before you create the spline variables, add an extra observation to your data and then set time = 15. Now create the splines, and then capture the values at time = 15. So, something like this:
              Code:
              //    CREATE EXTRA OBSERVATION FOR TIME = 15
              expand 2 in l
              replace time = 15 in l
              //    CREATE SPLINE VARIABLES AND STORE VALUES FOR TIME = 15
              mkspline timespl = time, knots(0 6 18 24 30) cubic
              foreach v of varlist timespl* {
                  summ `v' if time == 15
                  local `v'_15 = r(mean)
              }
              //    GET RID OF EXTRA OBSERVATION
              drop in l
              Then you can fit your model, and use these local macros in -lincom- as before.

              One conclusion of what you were saying is, that probably the margin command cannot be used - or better to say only under the assumption that the random effects, once estimated from the data, are actually fixed attributes of the patient - for any kind of "random intercept" or "random slopes" models, right?
              I agree.

              Your code works perfectly - thank you very much! It is not adabtable to the spline situation without knowing the exact formula as far as I understand the code, right?
              Well, not easily, but it can be done. The problem is what to do instead of -replace timesq = `t'*`t'-. I'm not going to actually work out the code in detail, but here's a bird's eye view of the process.

              1. Right after the splines have been created, create a crosswalk file from t to each of the spline variables. (Basically, keep time and the spline variables, drop duplicates, and save that to a crosswalk file--permanent or temporary as you see fit.)

              2. Inside the loop, after replacing time = `t', -merge- the data withe crosswalk file using the -update- option. That will fill in the appropriate values for the spline variables.

              Comment


              • #22
                Thank you very much for this interesting bird view on the process and the lincom command workaround (perfect!).
                I think, so far I am happy with the results of the lincom command. I won't present a marginplot.

                However, I have one last question because I did not understand the limitation completely.

                If I use the following code on my data (with sufficient follow-up-data to 48months):

                Code:
                mkspline timespl = time, cubic knots(6 12 36)
                mixed bmi i.case##c.timespl* c.age i.sex i.type_surgery || pid: timespl*
                margins i.case, over(time)
                marginsplot, xdim(time)
                The output is this very nice graph:
                Untitled.png


                I think, apart from the assumption that the random effects once estimated are a fixed part of a patient, this figure is a graphical representation of the mixed restricted cubic spline model above of the course of bmi in cases compared to controls controlled for type_surgery, age and sex.

                Aren't I correct? Or it is instead of "controlled for sex, age,..." -> "for the average age, sex and type of surgery"? Or is it useless because time is not longer part of the model as time is represented by timesp* in the model (does not look useless to me)?

                Hopefully this is my last question about this model. This thread is already epic :-) - thank you very much!

                Comment


                • #23
                  So here's the problem with this graph. Because you use -over(time)-, the margins are calculated at each time period using only the observations at that time period. If the patients included in the sample are not changing over time, then this is not a problem. But if there is attrition (or, less likely in most contexts, new people are joining) over time, then there can be differences in the distributions of age, sex, and surgery type at different time periods, and these margins will not be properly adjusted for those changes.

                  Comment

                  Working...
                  X