Announcement

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

  • Using marginsplot to create graphs similar to R

    Hello,

    I conducted a mixed-effects linear regression with an interaction term. The analysis examines whether PTSD is associated with higher cigarette consumption and whether the relationship between PTSD and cigarette consumption is moderated by perceived housing quality.

    I'm trying to create graphs based on the margin command's output. I would like the graph to look similar to the one someone produced in R for me (screenshot attached). I'm sure there must be a way to do this in Stata, but using the marginsplot command, I haven't achieved it. Can the margin's output be used to create a graph similar to the one pictured?

    The code I used for the mixed effects model and to generate the predictive margins is below.

    Thank you.

    This is the graph I would like to achieve, which was created using R.
    Click image for larger version

Name:	Screenshot 2024-08-19 at 5.19.26 PM.png
Views:	2
Size:	97.5 KB
ID:	1761963

    Code:
    mixed cig_per_day c.age i.gender_recode_pna_miss i.race_eth_fourlevel i.homequality_bi##i.ptsdCAT4 || property_num:
    
    margins, at(ptsdCAT4 = (0 1) homequality_bi=(0 1))

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float cig_per_day byte(age gender_recode_pna_miss race_eth_fourlevel homequality_bi ptsdCAT4 property_num)
    3.4285715 51 0 1 0 0 39
           10 38 0 2 1 0 39
            8 43 0 1 0 0 39
           20 64 2 0 1 0 22
            7 56 1 3 1 0 22
           10 59 0 1 1 0  4
           15 66 0 1 1 0  4
           25 39 0 2 1 1 16
            4 54 0 0 0 0 16
            3 45 0 0 0 0 39
            5 34 0 0 1 1 39
           10 40 0 0 0 0 39
            2 62 0 3 1 0 22
           10 44 0 2 1 0 16
            6 40 0 3 0 0  4
            5 40 0 0 1 1 16
            6 57 0 0 1 0 16
            3 44 0 3 1 0 39
            8 69 0 2 1 0 39
           20 70 1 2 1 0 22
            5 51 2 3 0 0 39
            5 60 1 0 1 0 22
            5 71 0 1 0 0  4
           12 40 1 0 0 0  4
           20 58 0 0 1 0  4
            8 56 1 1 0 1  4
     2.857143 57 1 0 1 0 16
           15 56 1 1 0 1 16
           10 36 0 1 1 0 20
            5 57 0 1 1 0 13
           10 56 2 1 0 1 39
    3.5714285 65 1 1 0 0 39
    .14285715 56 1 2 1 1 16
            2 33 1 1 1 1 22
           20 45 1 0 1 1  4
            2 67 2 1 1 0 13
     4.285714 62 1 1 1 0 13
            8 67 0 0 0 0 20
            6 60 1 0 1 1 39
           10 37 2 1 1 0 16
            3 50 0 1 0 0 16
            6 59 0 1 1 0 20
            6 65 0 1 1 0 20
     .5714286 66 1 1 1 1 13
            5 57 1 1 0 0 13
           10 62 0 0 1 1 20
            5 58 1 1 1 0 13
           20 65 1 1 1 . 20
            5 69 0 3 1 0 39
            2 44 0 0 1 0 39
            3 57 0 0 0 1  4
           20 35 0 0 1 0 16
            8 48 0 2 1 0 16
            6 68 0 1 0 0 13
            5 63 0 1 1 0 13
            5 67 1 3 1 0 20
            8 43 1 2 0 1 39
            6 60 0 1 1 1 20
            5 64 0 1 0 0 20
           10 63 0 2 1 0 13
           11 63 0 1 0 0 16
            6 45 1 0 0 1 20
           10 49 1 0 1 0 39
           15 44 0 3 1 1 39
            5 64 1 1 0 0 20
            7 65 0 2 1 1 20
            6 64 0 1 0 0 20
            5 67 0 0 1 0 38
           10 63 1 1 0 0 24
     .8571429 55 1 3 0 0 22
            6 68 0 3 0 0 24
           10 47 0 3 1 1 13
    1.7142857 59 1 1 0 0 13
           10 65 0 0 0 0 16
           20 61 0 1 1 0 33
            5 62 0 1 1 0 20
            5 76 0 0 1 0 33
            5 67 0 1 1 0 33
            7 64 0 1 1 0 37
            5 36 0 1 0 0 20
           20 60 0 1 0 1 38
    10.285714 60 1 3 0 1 37
           10 54 1 1 0 0 37
            2 60 0 1 1 1 24
     2.142857 31 . 3 0 1 22
            5 61 0 1 1 0 38
           10 69 0 0 0 0 38
           20 65 1 1 1 0 22
           10 58 1 1 0 0 20
           20 75 1 1 0 0 20
           40 53 0 0 0 1 29
           20 66 0 2 0 0 29
           15 60 0 1 1 1 13
            5 43 1 1 0 0  8
            5 59 1 1 1 0  8
     7.142857 56 0 1 1 0  8
           11 57 0 1 0 1 37
           10 61 1 0 0 0 24
     2.857143 64 0 1 0 0  8
           30 39 1 2 0 1 29
    end
    label values gender_recode_pna_miss gender_recode_pna_miss
    label def gender_recode_pna_miss 0 "Male", modify
    label def gender_recode_pna_miss 1 "Female", modify
    label def gender_recode_pna_miss 2 "Transgender/non-binary", modify
    label values race_eth_fourlevel race_eth_fourlevel
    label def race_eth_fourlevel 0 "White", modify
    label def race_eth_fourlevel 1 "Black", modify
    label def race_eth_fourlevel 2 "Hispanic/Latino/a/x", modify
    label def race_eth_fourlevel 3 "Additional races", modify
    label values homequality_bi homequality_bi
    label def homequality_bi 0 "Good/Excellent", modify
    label def homequality_bi 1 "Average/Poor", modify
    label values ptsdCAT4 ptsdCAT4
    label def ptsdCAT4 0 "Negative Screen", modify
    label def ptsdCAT4 1 "Positive Screen", modify
    Attached Files

  • #2
    Code:
    marginsplot, x(ptsdCAT4) by(homequality_bi) recast(scatter)
    works for me. The rest is fiddling with the plot and axis options.

    Comment


    • #3
      I appreciate it Andreas. That is very helpful!

      Comment

      Working...
      X