Announcement

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

  • Coefplot with different models on the y-axis

    Hello everyone! I am using Stata 17 and I had a question about the coefplot command. I am running analysis on survey data, and I will have about 10 models with different dependent variables but the same independent variables. What I would like to do is have a coefplot for each independent variable separately, and then along the y-axis, have labels for the specific dependent variable I am looking at and plot the magnitude of that independent variable accordingly. Here is an example of my data.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(rob police sexassault edu income newage)
     5  6  6 3  3 22
     7  6  8 7  6 30
    12  6  8 7  4 39
     7  5  7 5  5 42
     6 11  6 7  6 53
    10  4  9 5  6 45
     2  4  7 7 10 40
     1  1  1 7  2 33
     6  5  5 3  2 33
     7  6  8 7  5 39
     5  3  3 8  5 30
     7  7  5 4  4 49
     6  9  7 8  8 27
    12  5 12 5 10 22
     8  5  8 5  3 67
    12 10 10 5  4 72
     7  7  7 7  6 73
     7 10  8 5  7 67
     3  9  9 5  8 26
    13 13  1 3  2 58
     6  6  7 8 10 40
     7  8  7 7  6 79
     3  7  7 7  6 29
    13  1  1 4  6 70
     6 11  7 7 16 70
     4 11  9 7  4 64
     6  6  5 7  5 74
     7 11 10 8  6 59
     8  4  8 3  3 40
     3  7  7 3  4 75
     6  4  6 3  3 34
     3 10  9 7  9 37
     6  5 12 5  4 80
     5 13  6 5  3 36
     7  9  8 7  9 66
     7  4  8 7  2 76
     8  4  6 7  4 73
    10  7  9 3  3 68
     7  3  8 8 10 58
    11  8 10 5  5 51
     7  7  8 3  2 52
     5  6 10 7  4 69
     7  3  8 7  3 81
     6  8  6 2  3 41
     7  4  7 3  8 74
     6  7  3 7  5 67
    12  8 12 5  2 68
    12 10 11 5 10 58
     7  6  9 7  2 61
    13 13 13 5 15 27
    11  2  2 4  2 81
     7  8 11 4  6 33
     8 10  7 4  2 42
     6  5  7 2  1 34
     7  8  8 7 10 70
     7 12  7 3  2 41
     7 12  8 5 10 68
     8  7 12 7  1 42
    11  7 12 8  6 38
     5  7  6 3  5 59
    end
    Below are three models which I've then stored. :

    Code:
    regress rob edu income newage  
    estimates store model1
    regress police edu income newage
    estimates store model2
    regress sexassault edu income newage
    estimates store model3
    At this point, I become a little lost on what to do next. Here, my dependent variables are rob, police, and sexassault, and my independent variables are edu, income, and newage. I would like to have a gridded set of coefplots that look something like the crude picture I've drawn below (I know screenshots are generally frowned upon, but I couldn't think of a better way to demonstrate what I'm trying to do). For example, I want to pull the coefficient for education from all three regression models, and plot them together with the magnitude on the x-axis, and the label for the model from which I pulled it on the y-axis. I've tried several for loops, but they don't seem to work. Again, in this example I am only using 3 models, but would expand it to 10 and with more independent variables as well.

    Thanks for any insight or help! You all are great!

    Click image for larger version

Name:	Screenshot 2023-12-07 at 9.11.38 AM.png
Views:	1
Size:	1.46 MB
ID:	1736369
    Last edited by Nathan Brophy; 07 Dec 2023, 08:23.

  • #2
    coefplot is from SSC (FAQ Advice #12). Thanks for the data example. One way is to use margins to merge the inter model coefficients. Since the regressors are comon across models, you can estimate with mvreg.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(rob police sexassault edu income newage)
     5  6  6 3  3 22
     7  6  8 7  6 30
    12  6  8 7  4 39
     7  5  7 5  5 42
     6 11  6 7  6 53
    10  4  9 5  6 45
     2  4  7 7 10 40
     1  1  1 7  2 33
     6  5  5 3  2 33
     7  6  8 7  5 39
     5  3  3 8  5 30
     7  7  5 4  4 49
     6  9  7 8  8 27
    12  5 12 5 10 22
     8  5  8 5  3 67
    12 10 10 5  4 72
     7  7  7 7  6 73
     7 10  8 5  7 67
     3  9  9 5  8 26
    13 13  1 3  2 58
     6  6  7 8 10 40
     7  8  7 7  6 79
     3  7  7 7  6 29
    13  1  1 4  6 70
     6 11  7 7 16 70
     4 11  9 7  4 64
     6  6  5 7  5 74
     7 11 10 8  6 59
     8  4  8 3  3 40
     3  7  7 3  4 75
     6  4  6 3  3 34
     3 10  9 7  9 37
     6  5 12 5  4 80
     5 13  6 5  3 36
     7  9  8 7  9 66
     7  4  8 7  2 76
     8  4  6 7  4 73
    10  7  9 3  3 68
     7  3  8 8 10 58
    11  8 10 5  5 51
     7  7  8 3  2 52
     5  6 10 7  4 69
     7  3  8 7  3 81
     6  8  6 2  3 41
     7  4  7 3  8 74
     6  7  3 7  5 67
    12  8 12 5  2 68
    12 10 11 5 10 58
     7  6  9 7  2 61
    13 13 13 5 15 27
    11  2  2 4  2 81
     7  8 11 4  6 33
     8 10  7 4  2 42
     6  5  7 2  1 34
     7  8  8 7 10 70
     7 12  7 3  2 41
     7 12  8 5 10 68
     8  7 12 7  1 42
    11  7 12 8  6 38
     5  7  6 3  5 59
    end
    
    
    mvreg rob police sexassault= edu income newage
    est sto all
    foreach var of varlist edu income newage{
        est restore all
        margins, dydx(`var') post
        est sto `var'
    }
    coefplot edu||income||newage, ylab(1 "rob" 2 "police" 3 "sexassault")

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	35.4 KB
ID:	1736396

    Comment


    • #3
      That worked! But is there a way to add a factor level variable too? For example, race? Would it be possible to create individual plots for different levels of a variable in the same manner?

      Comment


      • #4
        Levels are denoted #.catvar, where # is a number and "catvar" is the name of your categorical variable. So extract these from one of the stored macros in e() or the column names of e(b) or e(V) and proceed as illustrated.

        Comment

        Working...
        X