Announcement

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

  • Suppressing individual point to point lines in a marginsplot

    Hello all,

    I am creating a marginsplot from my data of a mixed effects regression model. I am trying to prevent the appearance of a connecting line between values for time point 0 and time point 2 on my plot. The rest of the points (2 to 6; 6 to 12; 12 to 26; 26 to 52) should be connected. I have included an example of what I am trying to do in an image attached (https://jama.jamanetwork.com/article...jama.2018.6452)

    I have read a fair amount about the connectstyle and connect_options, but these seem to only work for the plot globally rather than on individual time point to time point.

    Thanks for your help.

    Click image for larger version

Name:	StataImg.png
Views:	1
Size:	98.9 KB
ID:	1483134

    Code:
    bootstrap: xtmixed eqvasfinal  i.random#time || participant_id:
    margins time, by(i.random)
    marginsplot, xdimension(time) scheme(lean2) plotopts(connect(i)))
    Data example
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float eqvasfinal byte random float time int participant_id
     96 1 52  1
     66 1  2  1
     92 1  0  1
     90 1  6  1
      . 0  2  9
     93 0  0  9
      . 0 52  9
     80 0  6 13
     91 0 26 13
     95 0 12 13
    100 0 52 13
     86 0  0 13
     64 0  2 13
     64 0  6 16
     95 0  0 16
     65 0  2 16
     98 0 52 16
     80 0 12 16
     62 1  2 27
    100 1 52 27
     91 1  0 27
     80 1  6 27
     70 1 12 27
     90 1 26 27
     92 0  0 28
    100 0 52 28
     80 0 12 28
     80 0  6 28
     90 0 26 28
     65 0  2 28
     70 0  2 31
     94 0  0 31
     95 0 52 31
     52 0 12 31
     76 0  6 31
     92 1 12 34
     99 1 52 34
     93 1 26 34
     65 1  2 34
     92 1  0 34
    100 1 52 37
     51 1  6 37
     90 1 26 37
     90 1 12 37
     92 1  0 37
     65 1  2 37
     91 0 52 38
     70 0  6 38
     60 0 26 38
     95 0  0 38
    end
    label values random randomlabel
    label def randomlabel 0 "External Fixation", modify
    label def randomlabel 1 "Intramedullary nail", modify

  • #2
    Wow.

    I think the approach you have to take is to generate the plot as four separate lines: two from time=2 to time=52, and two for time=0. The code below does that. First it runs the bootstrap and margins commands you gave, then it reruns the command with changes to accommodate my plan. A cursory comparison of the output (when run on your example data) suggests the results are unchanged beyond being divided into four groups rather than two. It is important that you carefully compare the results.

    The result is a start at what you want. You may need to mess with colors and the legend, but I think it's right. Probably should also run marginsplot after the "original" version and visually compare the lines and confidence limits in the two plots.

    Maybe somebody has a better idea, but even the graph editor doesn't seem to allow us to select a single line segment to make invisible.
    Code:
    set seed 42
    bootstrap: xtmixed eqvasfinal  i.random#time || participant_id:
    margins time, by(i.random)
    
    generate post = time>0
    set seed 42
    bootstrap: xtmixed eqvasfinal  i.post#i.random#time || participant_id:
    margins time, by(i.random i.post)
    marginsplot, xdimension(time) scheme(lean2)
    Click image for larger version

Name:	image_13422.png
Views:	1
Size:	188.9 KB
ID:	1483141

    Comment


    • #3
      Hi William,

      This looks like this should work for my purposes. There are some editing of the graph that I will have to do, but that is fairly simple. I am going to compare the output and graphs between various versions, and I will follow-up later today to close the loop on this.

      Thanks so much. This is super helpful.

      Patrick

      Comment


      • #4
        Here's another way:

        Code:
        bootstrap: xtmixed eqvasfinal  i.random#time || participant_id:
        
        cap drop est t2 ciupper cilower
        margins time if time ==0, by(i.random) 
        gen t2 = 0 in 1/2
        mat b = r(b)
        mat v = r(V)
        gen  est = b[1,1] in 1
        replace est  = b[1,2] in 2
        gen  ciupper =  est +  sqrt(v[1,1]) * invnormal(.975) in 1
        replace ciupper = est +sqrt(v[2,2]) * invnormal(.975) in 2
        gen  cilower =  est -  sqrt(v[1,1]) * invnormal(.975) in 1
        replace cilower  = est - sqrt(v[2,2]) * invnormal(.975) in 2
        
        margins time if time >=2, by(i.random)
        marginsplot , xdimension(time)  scheme(lean2)   /// 
          addplot(scatter est t2 in 1, ms(Oh) || scatter est t2 in 2, ms(O) /// 
          || rcap ciu cil t2 in 1/2 , xlabel(0 2 6 12 26 52) /// 
          legend(order(3 "External Fixation" 4 "Intramedullary nail" )) )
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	67.9 KB
ID:	1483167

        Comment


        • #5
          Hi Scott and William,

          Thank you for providing that information. William's method does work and the results are the same. This method does seem like it may require a little more playing around with the graph editor than Scott's method. However, both are excellent suggestions.

          Thanks so much!

          Comment

          Working...
          X