Announcement

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

  • Need to plot coefficients which includes constant of a regression

    I am a beginner here

    I am trying to regress log_wage (continuous) with Accounting_skill and Analytics_skill (both dummy) etc. or basically log_wage=b0 + b1 accounting_d + b2 analytics_d and so on......

    Now I have to Plot b0+bj for all skills.

    I was looking at coefplot but it only helps in plotting the coefficients and I have no idea how to add the constant term as well. I am basically looking at a plot that has confidence intervals for b0+bj.


  • #2
    Cross-posted at https://www.reddit.com/r/stata/comme...constant_of_a/

    Please note our request at https://www.statalist.org/forums/help#crossposting that you tell us about cross-posting -- which is also a rule for that part of Reddit.

    This may help. Clearly your dataset, your variable names and (likely) the number of your predictors will imply different code details.

    Code:
    webuse nlswork, clear 
    
    reg ln_wage msp nev_mar collgrad not_smsa c_city south union 
    
    gen coeff = _b[_cons] in 1/7 
    
    gen which = ""
    gen label = "" 
    gen ub = . 
    gen lb = . 
    
    forval j = 1/7 { 
        local pred : word `j' of msp    nev_mar   collgrad  not_smsa  c_city    south     union
        replace which = "`pred'" in `j'
        replace coeff = coeff + _b[`pred'] in `j' 
        lincom _cons + `pred'
        replace ub = r(ub) in `j'
        replace lb = r(lb) in `j' 
        local lbl : var label `pred'  
        replace label = "`lbl'" in `j'
    }
    
    sort coeff 
    
    list coeff which label in 1/7 
    
    
    gen predictor = _n 
    
    forval j = 1/7 {
        local lbl = which[`j']
        label def predictor `j' "`lbl'", modify 
    } 
    
    label val predictor predictor 
    
    
    twoway rspike ub lb predictor in 1/7, horizontal legend(off) /// 
    || scatter predictor coeff in 1/7, yla(1/7, valuelabel grid) xtitle(Intercept + coefficient)
    Click image for larger version

Name:	coefplot_not.png
Views:	1
Size:	29.0 KB
ID:	1716417

    Comment


    • #3
      If all you want to do is adding the constant, then that means you just chose the wrong reference category. What I think you actually meant was see the predicted outcome for different values of your categorical variable. This sometimes involves adding the constant. But now there is a problem: Remember that the logarithm is a non-linear transformation. So your model models the mean of the log of wage. You cannot easily derive the mean wage from that. This would be much easier if you model the logarithm of the mean of wage, in the GLM parlance: use a log link function. This is easily done in Stata with the poisson command combined with the vce(robust) option. For more, and references, see: https://blog.stata.com/2011/08/22/us...tell-a-friend/ So, whenever you typed regress of log wage, I will assume you should have done poisson with wage and robust standard errors. To get the predictions, you use the margins command, and after that marginsplot. Here is an example:

      Code:
      . // open example data
      . set scheme s1color
      
      . sysuse nlsw88, clear
      (NLSW, 1988 extract)
      
      .
      . // prepare data
      . gen byte urban = c_city + smsa
      
      . label define urban 2 "central city" ///
      >                    1 "suburban"     ///
      >                    0 "rural"
      
      . label value urban urban
      
      . label variable urban "urbanicity"
      
      .
      . // estimate model
      . poisson wage i.urban ttl_exp grade, vce(robust) irr
      note: you are responsible for interpretation of noncount dep. variable.
      
      Iteration 0:   log pseudolikelihood = -6859.2671  
      Iteration 1:   log pseudolikelihood =  -6859.267  
      
      Poisson regression                                      Number of obs =  2,244
                                                              Wald chi2(4)  = 460.42
                                                              Prob > chi2   = 0.0000
      Log pseudolikelihood = -6859.267                        Pseudo R2     = 0.1065
      
      -------------------------------------------------------------------------------
                    |               Robust
               wage |        IRR   std. err.      z    P>|z|     [95% conf. interval]
      --------------+----------------------------------------------------------------
              urban |
          suburban  |   1.293075   .0470894     7.06   0.000     1.203998    1.388742
      central city  |   1.227491   .0483287     5.21   0.000     1.136331    1.325964
                    |
            ttl_exp |   1.037047   .0032169    11.73   0.000     1.030761    1.043371
              grade |   1.081687   .0057583    14.75   0.000     1.070459    1.093032
              _cons |   1.421245   .1187812     4.21   0.000     1.206506    1.674204
      -------------------------------------------------------------------------------
      Note: _cons estimates baseline incidence rate.
      
      .
      . // compute predicted wage
      . margins i.urban, at(ttl_exp=12 grade=12)
      
      Adjusted predictions                                     Number of obs = 2,244
      Model VCE: Robust
      
      Expression: Predicted number of events, predict()
      At: ttl_exp = 12
          grade   = 12
      
      -------------------------------------------------------------------------------
                    |            Delta-method
                    |     Margin   std. err.      z    P>|z|     [95% conf. interval]
      --------------+----------------------------------------------------------------
              urban |
             rural  |   5.642466   .1731937    32.58   0.000     5.303012    5.981919
          suburban  |    7.29613   .1698222    42.96   0.000     6.963285    7.628976
      central city  |   6.926074   .1905493    36.35   0.000     6.552604    7.299544
      -------------------------------------------------------------------------------
      
      .
      . // graph predicted wage
      . marginsplot,                                                         ///
      >    plotopts(connect(none))                                           ///
      >    xscale(range(-0.1 2.1))                                           ///
      >    xtitle("")                                                        ///
      >    ytitle(predicted hourly wage)                                     ///
      >    title("predicted hourly wage for"                                 ///
      >          "women with 12 years experience and 12 years of schooling") ///
      >    ylab(,angle(0))
      
      Variables that uniquely identify margins: urban
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	57.6 KB
ID:	1716422

      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Thanks very much for this. This is exactly what I needed. But I cannot seem to get the confidence intervals like yours. I am using STATA14.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          Cross-posted at https://www.reddit.com/r/stata/comme...constant_of_a/

          Please note our request at https://www.statalist.org/forums/help#crossposting that you tell us about cross-posting -- which is also a rule for that part of Reddit.

          This may help. Clearly your dataset, your variable names and (likely) the number of your predictors will imply different code details.

          Code:
          webuse nlswork, clear
          
          reg ln_wage msp nev_mar collgrad not_smsa c_city south union
          
          gen coeff = _b[_cons] in 1/7
          
          gen which = ""
          gen label = ""
          gen ub = .
          gen lb = .
          
          forval j = 1/7 {
          local pred : word `j' of msp nev_mar collgrad not_smsa c_city south union
          replace which = "`pred'" in `j'
          replace coeff = coeff + _b[`pred'] in `j'
          lincom _cons + `pred'
          replace ub = r(ub) in `j'
          replace lb = r(lb) in `j'
          local lbl : var label `pred'
          replace label = "`lbl'" in `j'
          }
          
          sort coeff
          
          list coeff which label in 1/7
          
          
          gen predictor = _n
          
          forval j = 1/7 {
          local lbl = which[`j']
          label def predictor `j' "`lbl'", modify
          }
          
          label val predictor predictor
          
          
          twoway rspike ub lb predictor in 1/7, horizontal legend(off) ///
          || scatter predictor coeff in 1/7, yla(1/7, valuelabel grid) xtitle(Intercept + coefficient)
          [ATTACH=CONFIG]n1716417[/ATTACH]
          Thanks very much for this. This is exactly what I needed. But I cannot seem to get the confidence intervals like yours. I am using STATA14.

          Comment


          • #6
            The FAQ Advice explains that you should declare if you are using an out-of-date version of Stata. Please see https://www.statalist.org/forums/help#version and in passing also https://www.statalist.org/forums/help#spelling

            I no longer have access to Stata 14, but I am surprised at the implication that the code in #2 does not work in that version. To make progress, we need to see the code you tried and to know specifically what didn't work.

            Similarly #3 should work, as even margins and marginsplot were included before Stata 14.

            Comment


            • #7
              Originally posted by Nick Cox View Post
              The FAQ Advice explains that you should declare if you are using an out-of-date version of Stata. Please see https://www.statalist.org/forums/help#version and in passing also https://www.statalist.org/forums/help#spelling

              I no longer have access to Stata 14, but I am surprised at the implication that the code in #2 does not work in that version. To make progress, we need to see the code you tried and to know specifically what didn't work.

              Similarly #3 should work, as even margins and marginsplot were included before Stata 14.
              I ran the exact code you posted to test it out. But it seems that ub and lb variables continue to be empty after the loop.

              Comment


              • #8
                That is checkable and from https://www.stata.com/manuals14/rlincom.pdf I do see that in Stata 14 the saved results are r(estimate) and r(se) and r(df) so you would need to construct intervals yourself.

                Comment

                Working...
                X