Announcement

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

  • Plotting a line graph of the regression coefficients of one variable

    Hi everyone,

    I am running regressions on the RIF (recentered influence functions) for deciles as Y separated by women and men. Now I want to plot a line graph showing the coefficient of one of the independent variables for male and female over the 10 deciles of wage.

    I tried with eststo and esttab. Can I transform the resulting tables in a line graph? Or do I need to use a different approach? I am using Stata17.

    regress rif_q1 X1 X2 X3 if GENDER ==1
    eststo rif10_m
    regress rif_q1 X1 X2 X3 if GENDER ==2
    eststo rif10_f

    regress rif_q2 X1 X2 X3 if GENDER ==1
    eststo rif20_m
    regress rif_q2 X1 X2 X3 if GENDER ==2
    eststo rif20_f

    regress rif_q3 X1 X2 X3 if GENDER ==1
    eststo rif30_m
    regress rif_q3 X1 X2 X3 if GENDER ==2
    eststo rif10_3

    regress rif_q4 X1 X2 X3 if GENDER ==1
    eststo rif40_m
    regress rif_q4 X1 X2 X3 if GENDER ==2
    eststo rif40_f

    regress rif_q5 X1 X2 X3 if GENDER ==1
    eststo rif50_m
    regress rif_q5X1 X2 X3 if GENDER ==2
    eststo rif50_f

    regress rif_q6 X1 X2 X3 if GENDER ==1
    eststo rif60_m
    regress rif_q6 X1 X2 X3 if GENDER ==2
    eststo rif60_f

    regress rif_q7 X1 X2 X3 if GENDER ==1
    eststo rif70_m
    regress rif_q7 X1 X2 X3 if GENDER ==2
    eststo rif70_f

    regress rif_q8 X1 X2 X3 if GENDER ==1
    eststo rif80_m
    regress rif_q8 X1 X2 X3 if GENDER ==2
    eststo rif80_f

    regress rif_q9 X1 X2 X3 if GENDER ==1
    eststo rif90_m
    regress rif_q9 X1 X2 X3 if GENDER ==2
    eststo rif90_f

    esttab rif10_f rif20_f rif40_f rif50_f rif60_f rif70_f rif80_f rif90_f, keep(X1)

    esttab rif10_m rif20_m rif30_m rif40_m rif50_m rif60_m rif70_m rif80_m rif90_m, keep(X1)


  • #2
    Graphs are drawn from data sets. Holding the results in intermediate tables from -esttab- just adds more complication. Directly saving them in a data set makes more sense.

    Code:
    frame create results int(decile gender) float coefficient
    
    forvalues d = 1/9 {
        forvalues g = 1/2 {
            regress rif_`d'0 X1 X2 X3 if GENDER == `g'
            frame post results (`d') (`g') (_b[X1])
        }
    }
    
    frame change results
    isid decile gender
    reshape wide coefficient, i(decile) j(gender)
    graph twoway line coefficient* decile, sort
    You can improve the appearance of the graph by labeling the variables coefficient* before you plot the graph, and, of course, you can add whatever other options of -graph twoway- you like.

    Note: As no example data was provided, this code is untested and may contain typos or other errors.

    Comment


    • #3
      AMAZING!!! Thank you so much!! It works perfectly with the code you provided!

      Comment


      • #4
        This is also untested. In several senses the approach is cruder than that of Clyde Schechter; in one sense perhaps it is little more direct.


        Code:
        gen decile = . 
        gen gender = . 
        gen coeff_X1 = . 
        local i = 1 
        
        forvalues d = 1/9 {
            forvalues g = 1/2 {
                regress rif_`d'0 X1 X2 X3 if GENDER == `g'
                replace decile = `d' in `i'
                replace gender = `g' in `i'
                replace coeff_X1 = _b[X1] in `i'
                local ++i 
            }
        }
        
        separate coeff_X1, by(gender) veryshortlabel  
        
        graph twoway line coeff* decile, sort
        This is in effect postfile done yourself but without file (or frame) choreography. The weak assumption is that you have enough observations for this to work, but if not then the regressions in this set-up won't make sense.

        Comment


        • #5
          sorry, for again having a question to this topic!

          it worked well for this code:

          Code:
          *Graph for HYBRID1 
          
          forvalues d = 1/9 {
              forvalues g = 1/2 {
                  regress rif_`d'0 HYBRID1 HYBRID2 HUSUAL AGE i.SKILL_LEVEL i.ISCED3 KIA [pweight = EXTRID] if GENDER == `g'
                  frame post result (`d') (`g') (_b[HYBRID1])
              }
          }
          
          frame change result
          isid decile GENDER
          reshape wide coefficient, i(decile) j(GENDER)
          graph twoway line coefficient* decile, name(Graph2) sort
          
          frame change default
          Now I want to repeat this and create a second graph for the coefficients of a second variable. By just repeating the command with exchanging the variable Hybrid1 by HYBRID2,

          Code:
          frame create result2 int(decile GENDER) float coefficient
          forvalues d = 1/9 {
              forvalues g = 1/2 {
                  regress rif_`d'0 HYBRID1 HYBRID2 FULL_HOME HUSUAL AGE i.SKILL_LEVEL i.ISCED3 NBENFIND_MEN PARTNER i.URBAN CONTRACT_YEAR i.PBIRTH KIA [pweight = EXTRID] if GENDER == `g'
                  frame post result (`d') (`g') (_b[HYBRID2])
              }
          }
          
          frame change result2
          isid decile GENDER
          reshape wide coefficient, i(decile) j(GENDER)
          graph twoway line coefficient* decile, name(Graph3) sort
          
          frame change default
          I get the error code: variable GENDER contains all missing values

          I don't see why I can not repeat the command

          Greetings
          Anja

          Comment


          • #6
            I don't see why you can't repeat the command either. Please post back with example data that when run through both the original code and the second code produces this error message and I will try to troubleshoot it. To show the example data, be sure to use the -dataex- command. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

            Comment


            • #7
              Thank you for you quick answer!

              Here is the complete code
              Code:
              xtile decile = LN_SALHOUR [pweight = EXTRID], nq(10)
              pctile P10 = LN_SALHOUR [pweight = EXTRID], nquantiles(10)
              tab decile
              list P10 if P10 !=., noobs
              
              
              *generate quantil variables
              forvalues d = 1/9 {
                  gen DEC`d'0 = 0
                  replace DEC`d'0 = 1 if decile <= `d'
              } 
              
              *generate RIF-variables
              forvalues d = 1/9 {
                  egen rif_`d'0 = rifvar(LN_SALHOUR), q(`d'0) kernel(gaussian) weight( EXTRID )
              } 
              
              
              
              frame create result int(decile GENDER) float coefficient
              
              *Graph for HYBRID1 
              
              forvalues d = 1/9 {
                  forvalues g = 1/2 {
                      regress rif_`d'0 HYBRID1 HYBRID2 HUSUAL AGE SKILL_LEVEL ISCED3 KIA [pweight = EXTRID] if GENDER == `g'
                      frame post result (`d') (`g') (_b[HYBRID1])
                  }
              }
              
              frame change result
              isid decile GENDER
              reshape wide coefficient, i(decile) j(GENDER)
              graph twoway line coefficient* decile, name(Graph2) sort
              
              frame change default
              
              *Graph for HYBRID2
              
              frame create result2 int(decile GENDER) float coefficient
              forvalues d = 1/9 {
                  forvalues g = 1/2 {
                      regress rif_`d'0 HYBRID1 HYBRID2 HUSUAL AGE SKILL_LEVEL ISCED3 KIA [pweight = EXTRID] if GENDER == `g'
                      frame post result2 (`d') (`g') (_b[HYBRID2])
                  }
              }
              
              frame change result2
              isid decile GENDER
              reshape wide coefficient, i(decile) j(GENDER)
              graph twoway line coefficient* decile, name(Graph3) sort
              
              frame change default
              And this is my example data
              Code:
              
              
              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input double EXTRID byte GENDER int AGE byte HOMEWORK double(LN_SALRED LN_SALHOUR HUSUAL) byte(KIA SKILL_LEVEL ISCED3) float(HYBRID1 HYBRID2)
               3388.625158135492 2 71 4  7.761319180947987 1.3643895257318408  150 0 3 2 0 0
               2596.450437341822 2 54 4  6.248042874508429 1.3652409519220583   33 0 2 2 0 0
               4135.085712110409 2 35 4  6.309918278226516 1.3682758556172123   35 0 2 2 0 0
              3999.7724135805543 2 56 1  7.003065458786462 1.3682758556172123   70 0 2 2 0 0
               2189.684288187868 2 34 1  6.309918278226516 1.3682758556172123   35 0 . 1 0 0
               3859.328731008996 1 54 1  6.315358001522335 1.3737155789130306   35 0 2 1 0 0
              3185.6426553617794 2 54 4 6.5722825426940075 1.3793256918037973   45 0 2 1 0 0
              4016.3377212420146 2 36 4   6.57507584059962   1.38211898970941   45 0 2 1 0 0
               3573.028403724037 2 58 1 4.1588830833596715 1.3862943611198906    4 0 1 1 0 0
               2492.324062757864 2 60 4  5.768320995793772 1.3862943611198906   20 0 2 2 0 0
               4656.522995221441 2 18 1  4.382026634673881 1.3862943611198906    5 0 2 1 0 0
              3887.6773436425287 2 54 1  5.991464547107982 1.3862943611198906   25 0 1 1 0 0
               4627.337282547061 2 49 1  6.684611727667927 1.3862943611198906   50 0 2 2 0 0
               4234.265645304304 2 44 4  7.275172319452771  1.389068288002616   90 0 3 2 0 0
               4741.217464616625 2 23 1  6.333279628139691  1.391637205530386   35 1 3 3 0 0
               3666.950337431162 2 49 1  6.336825731146441 1.3951833085371366   35 0 2 2 0 0
               4570.741569390438 1 60 1  6.338594078203183 1.3969516555938786   35 0 3 3 0 0
               3910.902674612927 2 42 4  6.423246963533519 1.3993664426872434   38 0 2 2 0 0
               4995.317378166322 1 23 1  6.396929655216146 1.3997173814520314   37 0 2 2 0 0
               4437.811812616362 2 63 1  4.867534450455582 1.4017985476558559    8 1 2 2 0 0
               799.4816713127938 2 62 1  5.783825182329737 1.4017985476558559   20 0 1 2 0 0
               6345.951437089846 2 29 1  4.867534450455582 1.4017985476558559    8 1 1 2 0 0
              3757.5764767232927 1 65 1  5.857933154483459 1.4035858582299516 21.5 0 2 2 0 0
              2607.8333183629115 2 45 4  6.480044561926653  1.404870746692826   40 0 2 2 0 0
              3911.0801426939024 2 63 4  6.349138991379798 1.4074965687704935   35 0 2 2 0 0
               4425.857517381921 2 47 1  6.214608098422191 1.4271163556401458   30 0 1 1 0 0
               4753.088684733391 1 67 1  6.214608098422191 1.4271163556401458   30 0 2 2 0 0
              3610.2569655746865 1 57 1  5.991464547107982 1.4271163556401458   24 0 1 2 0 0
               2464.897660078473 2 54 4  6.396929655216146 1.4271163556401458   36 0 2 3 0 0
                853.998396984154 2 65 1  6.620073206530356 1.4271163556401458   45 0 1 1 0 0
              3911.0801426939024 2 62 4  6.214608098422191 1.4271163556401458   30 0 2 2 0 0
               8199.732298723706 1 64 2  6.214608098422191 1.4271163556401458   30 1 3 3 1 0
               4138.056822493147 2 51 1  6.214608098422191 1.4271163556401458   30 0 2 2 0 0
               4732.961438594319 2 21 1  5.991464547107982 1.4271163556401458   24 0 3 3 0 0
               7231.419805602383 2 54 4  7.313220387090301 1.4271163556401458   90 1 3 2 0 0
               2658.382222814558 1 19 1  6.214608098422191 1.4271163556401458   30 0 2 2 0 0
              3373.7743855919566 2 52 1  6.551080335043404 1.4271163556401458   42 1 1 2 0 0
              3986.4036162377242 2 43 1  6.476972362889683 1.4271163556401458   39 0 2 2 0 0
              1876.0305539421006 1 65 4  6.214608098422191 1.4271163556401458   30 1 3 3 0 0
               4619.242971507956 2 19 1  6.214608098422191 1.4271163556401458   30 1 3 2 0 0
               5126.333125974022 2 24 1  6.214608098422191 1.4271163556401458   30 0 2 3 0 0
              3760.6025096908315 2 56 4  6.709304340258298 1.4311896810277815   49 0 2 2 0 0
               3434.818835135634 1 19 1  5.123963979403259 1.4350845252893227   10 0 2 2 0 0
              5310.9450032344275 2 48 4  6.633318433280377 1.4403615823901663   45 0 2 2 0 0
               4973.118370936779 1 50 1  6.396929655216146 1.4411025976148855 35.5 0 1 1 0 0
               4365.890582742039 2 28 4  6.478509642208569 1.4415570397949402 38.5 0 2 2 0 0
               2378.260666876706 2 60 4  6.577861357721047  1.442062920670785 42.5 0 2 2 0 0
               3977.014386604119 1 45 1  6.551080335043404 1.4512139072192063   41 0 2 2 0 0
               1135.473637864442 2 34 4  6.551080335043404 1.4512139072192063   41 0 2 2 0 0
               784.4769640717337 2 59 4 6.2422232654551655  1.454731522673119   30 0 1 1 0 0
              3959.8519676486267 2 61 1  6.396929655216146  1.455287232606842   35 1 1 1 0 0
              4009.8933766498844 2 51 4  6.396929655216146  1.455287232606842   35 0 2 2 0 0
              1741.7424038011466 2 20 1  6.396929655216146  1.455287232606842   35 1 3 2 0 0
              1105.7311866174134 2 26 1  6.396929655216146  1.455287232606842   35 1 2 3 0 0
               5947.379186864307 2 31 1  6.396929655216146  1.455287232606842   35 0 1 1 0 0
               5590.460919174161 1 18 1  6.396929655216146  1.455287232606842   35 1 1 2 0 0
              2662.3066979460978 2 38 1  6.396929655216146  1.455287232606842   35 0 2 1 0 0
               1976.140719711125 2 20 1  6.396929655216146  1.455287232606842   35 0 3 1 0 0
               568.2483552299632 1 22 1  6.396929655216146  1.455287232606842   35 1 3 2 0 0
               8886.541233750697 2 46 4  6.660575149839686 1.4565684627628905 45.5 0 2 2 0 0
               3680.876728392865 1 51 4  6.309918278226516 1.4578880143068995   32 0 2 3 0 0
              2972.4172548685806 2 55 4  6.309918278226516 1.4578880143068995   32 0 2 2 0 0
              2784.6022093652755 2 39 1  5.332718793265369  1.461517782357478   12 0 2 3 0 0
               5209.394631715325 2 22 1   5.84354441703136  1.461517782357478   20 1 3 3 0 0
              3254.8892849997123 2 49 1  5.049856007249537  1.466337068793427    9 0 2 2 0 0
              4372.5043152506505 2 17 1  5.560681631015528  1.466337068793427   15 0 1 1 0 0
               4780.661418544955 2 49 1  5.802118375377063 1.4713850350907318   19 0 1 1 0 0
              3847.8068229182013 1 50 1  6.551080335043404 1.4759065198095778   40 1 2 3 0 0
              3176.4546103557122 1 62 2  4.941642422609304 1.4759065198095778    8 1 3 3 1 0
              3760.6025096908315 2 60 4  6.551080335043404 1.4759065198095778   40 0 2 2 0 0
               535.2468145526516 1 29 1  6.551080335043404 1.4759065198095778   40 0 2 2 0 0
               3418.081919297607 2 55 4   7.24422751560335 1.4759065198095778   80 1 3 2 0 0
               3574.639674543124 2 64 4  6.551080335043404 1.4759065198095778   40 0 2 1 0 0
               4245.026014403731 1 20 2  6.418364935936212 1.4767225133269073   35 1 3 2 1 0
              1654.4947092964328 2 56 4  6.628041376179533   1.48054689936608   43 0 2 2 0 0
              2868.0586187506974 2 61 2  5.958424693029782 1.4810878785515753   22 0 2 1 1 0
               4487.754732553305 1 30 1   6.42648845745769 1.4848460348483865   35 0 1 1 0 0
              1821.9261817511194 2 32 1  5.017279836814924  1.490919312198763  8.5 0 1 2 0 0
               6263.618730150602 2 23 1  6.214608098422191 1.4961092271270973   28 1 3 3 0 0
              2744.4753461495466 2 64 1  6.396929655216146  1.499089855265235 33.5 1 3 3 0 0
               3207.469191580522 1 43 1  6.440946540632921 1.4993041180236164   35 1 1 1 0 0
               3983.544544676609 2 34 4  6.354370040797351 1.5023397768777333   32 0 2 2 0 0
                4313.43471726945 1 47 4  6.802394763324311 1.5040773967762742   50 1 3 3 0 0
               4959.544313238195 2 33 4  6.802394763324311 1.5040773967762742   50 0 2 3 0 0
               6137.045709228756 2 31 1 6.1092475827643655 1.5040773967762742   25 0 2 2 0 0
              3632.9683174693178 1 63 1  7.495541943884256 1.5040773967762742  100 1 3 3 0 0
               4543.360643297916 2 54 4  7.495541943884256 1.5040773967762742  100 1 3 3 0 0
              3235.1655693256457 2 19 1  6.697034247666484 1.5040773967762742   45 1 2 2 0 0
               4471.063433392772 1 76 1 6.1092475827643655 1.5040773967762742   25 0 2 3 0 0
               3465.106960585391 1 72 1  6.802394763324311 1.5040773967762742   50 1 3 3 0 0
              3887.6773436425287 2 64 4 6.3561076606958915 1.5040773967762742   32 0 2 2 0 0
               3466.740312173587 2 51 2  5.598421958998375 1.5040773967762742   15 1 3 3 1 0
               4614.696566617372 2 29 1  6.473890696352274 1.5040773967762742   36 0 1 2 0 0
               4957.022813267293 2 19 1 6.1092475827643655 1.5040773967762742   25 0 2 2 0 0
               5009.011230625052 2 31 1  6.447305862541213 1.5056634399319089   35 0 1 1 0 0
               5225.961535291982 2 23 1  6.326149473155099 1.5058679075500623   31 1 3 2 0 0
               2464.928347322727 2 60 4  6.093569770045136 1.5086022913745636 24.5 0 2 2 0 0
               4411.410846942429 2 38 4  6.745236349484363  1.508794386654413   47 0 2 3 0 0
               3574.639674543124 2 59 1  5.298317366548036 1.5141277326297755   11 0 1 2 0 0
               576.0956244776316 2 50 1  5.298317366548036 1.5141277326297755   11 0 2 1 0 0
              end
              label values GENDER labels4
              label def labels4 1 "male", modify
              label def labels4 2 "female", modify
              label values HOMEWORK labels5
              label def labels5 1 "never", modify
              label def labels5 2 "less than half of working houres", modify
              label def labels5 4 "always", modify
              label values KIA labels9
              label def labels9 0 "Less knowledge intense activities", modify
              label def labels9 1 "Knowledge intentense activities", modify
              label values SKILL_LEVEL labels10
              label def labels10 1 "Skill level 1 elementary occupatons", modify
              label def labels10 2 "Skill level 2 (clerks, sales, service, agricultures, craft, machine operator)", modify
              label def labels10 3 "Skill level 3 (Managers, technicians, professionals)", modify
              label values ISCED3 labels11
              label def labels11 1 "Low education", modify
              label def labels11 2 "medium education", modify
              label def labels11 3 "High education", modify

              Comment


              • #8
                Running the code in #8 with the accompanying example data does not reproduce the problem you described in #7. It produces a different problem: the variable HYBRID2 is in fact always 0 in the example data, so the regressions always omit it and the "coefficient" that gets posted is, accordingly, always 0. Perhaps in your complete data, this problem does not arise.

                But, crucially, the -reshape- command works just fine and does not declare that variable GENDER has all missing values, nor even any missing values. So I'm still at a loss to discern the source of your difficulty. Can you find a different data example that does reproduce that error?

                Comment


                • #9
                  Hi Clyde, I found the mistake in my first code! It is working now with my original data, where hybrid2 is not 0 in all the cases! I did not post to the right frame.

                  I am very sorry to have waisted your time. I am still very new to stata!

                  Thank you for all the support you give in the community!

                  Comment


                  • #10
                    Hi, sorry for having an other question on this topic. Is it also possible to include information on whether the coefficient is significant or not? Can this be included in my previous code?

                    Code:
                     
                     *Graph for HYBRID1   forvalues d = 1/9 {     forvalues g = 1/2 {         regress rif_`d'0 HYBRID1 HYBRID2 HUSUAL AGE i.SKILL_LEVEL i.ISCED3 KIA [pweight = EXTRID] if GENDER == `g'         frame post result (`d') (`g') (_b[HYBRID1])     } }  frame change result isid decile GENDER reshape wide coefficient, i(decile) j(GENDER) graph twoway line coefficient* decile, name(Graph2) sort  frame change default
                    Thank you very much for any hint!

                    Comment


                    • #11
                      Well, yes it can be done. Here's a hint, but not a complete solution:
                      Code:
                      *Graph for HYBRID1
                      frame create result  byte (decile gender) float(coeff pvalue)
                      forvalues d = 1/9 {     
                          forvalues g = 1/2 {
                              regress rif_`d'0 HYBRID1 HYBRID2 HUSUAL AGE i.SKILL_LEVEL i.ISCED3 KIA [pweight = EXTRID] if GENDER == `g'
                              matrix M = r(table)
                              frame post result (`d') (`g') (M["b", "HYBRID1"]) (M["pvalue", "HYBRID1"])   
                          }
                      }  
                      frame change result
                      isid decile GENDER
                      reshape wide coefficient pvalue, i(decile) j(GENDER)
                      graph twoway line coefficient* decile, name(Graph2) sort  // MODIDFY AS YOU WISH
                      frame change default
                      The reason I am giving only a hint and not a full solution is that I think this is a terrible idea and I don't really want to be your accomplice in statistical crime. What you are proposing here is just a graphical version of those horrible tables with significance stars, which really ought to be a felony. Apart from my generally strong reservations about the entire concept of statistical significance, which I won't riff on here, this kind of device encourages you to make erroneous inferences like "this coefficient is different from that one because one of them is significant and the other isn't." That's simply wrong. And, in fact, nobody has ever shown me any useful and valid conclusions that can be drawn from such a device. Of all the things you can say about a coefficient, it's statistical significance is the least informative. If you want to do something along these lines, the following would be better:
                      1. Put the p-values themselves on the graph in some way. This would be a slight improvement.
                      2. Use error bars to put the confidence intervals for the coefficients on the graph. This would be a huge improvement. (See -help toway rcap-.)
                      Anyway, the code I have shown above puts the p-values into frame result along with the coefficients. If you take a look at the matrix M that is created in the loop, it will be obvious how to modify the code to put the confidence intervals into frame result instead or in addition. I will leave it to you to modify the -graph- command.

                      Comment


                      • #12
                        Thank you so much, Clyde! I really appreciate the support you give to the Stata community! My code worked with help of your hints!

                        Anyway, I will also take your alternative solutions to discuss them with my professor!

                        Comment

                        Working...
                        X