Announcement

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

  • Plot of the same coefficient over time

    Dear Stata users,

    I saw there are some posts about this, but I could not find a solution. I have a panel dataset. I am running one logit regression, year by year, for 13 years, in a loop.

    forvalues i = 2006(1)2018 {
    logit Y X1 X2 X3 X4, vce (cluster ID), if year ==`i'
    }

    I would like to visualize the coefficients of my main independent variable over time, with confidence intervals. I have just installed coefplot command, which appears to be suitable for multiple models. However, still finding a way to make a graph. I hope some has experience with it. Thank you in advance!

    Best regards,
    Jovana

  • #2
    Just a thought, perhaps you can post the coefficients into a new frame along with the year and plot that data

    Code:
    frame create coefs year coef
    forvalues i = 2006(1)2018 {
    logit Y X1 X2 X3 X4, vce (cluster ID), if year ==`i'
    local coef=e(b)[1,1]
    frame put coefs (`i') (`coef')
    }
    frame coefs: your preferred graph

    Comment


    • #3
      Hi, Jovana.

      In my humble experience, an alternative and efficient way is to combine twoway rcap and scatter. The data need to be organized manually or with a bit of additional programming. Example:

      Code:
      clear
      set obs 10
      *! odds ratio (natural scale)
      gene or = runiform()+1
      gene se = 0.05
      *! 95% CI (natural scale)
      gene lower  =exp( ln(or)-(1.96*se))
      gene upper  =exp( ln(or)+(1.96*se))
      sort or
      *! time is entered as 1,2,3,... to create evenly spaced points and labels are given inside the graph only
      gene time = _n
       twoway (scatter or time, legend(row(1) order(1 "Point estimate" 2 "95% CI") pos(6) )  xtitle("Time")  ytitle("OR") xlabel(1 "1 week" 2 "5 weeks" 8 "10 months")) (rcap lower upper  time, yscale(log) )

      Comment


      • #4
        Dear,

        Daniel Shin, Thank you. The idea is simple, but I have an error. 2006 invalid name. Maybe I misunderstood the code.

        Tiago Pereira I am not sure how to apply it. I am still new in Stata.

        Comment


        • #5
          I didn't check the code carefully (copy and paste). I've cleaned it up and added CIs.

          Code:
          frame create coefs year coef ll ul
          forvalues i = 2006(1)2018 {
          logit Y X1 X2 X3 X4 if year ==`i', vce (cluster ID) // fixed the condition options
          local coef=r(table)[1,1] // COEF
          local ll=r(table)[5,1] // CI LL
          local ul=r(table)[6,1] //  CI UL
          frame put coefs (`i') (`coef') (`ll') (`ul')
          }
          frame coefs: your preferred graph

          Comment


          • #6
            If you install estout and store the estimates, you can plot these directly with coefplot. Both commands are from SSC (FAQ Advice #12).

            Code:
            ssc install estout, replace
            Code:
            set obs 13
            gen year= 2005+_n
            expand 200
            bys year: gen ID=_n
            set seed 06092021
            foreach var in Y X1 X2 X3 X4{
                local g =cond("`var'"=="Y", "runiformint(0,1)", "rnormal(0, 1)") 
                gen `var'=  `g'
            }
            *START HERE
            bys year: eststo: logit Y X1 X2 X3 X4, vce (cluster ID)
            
            *PLOT YEARS 2006-2009
            set scheme s1color
            coefplot (est1, label(2006)) (est2, label(2007)) (est3, label(2008)) ///
            (est4, label(2009)), vert drop(_cons)
            
            *ALL YEARS (MESSY)
            coefplot est*, vert nokey drop(_cons) title(Coefficients 2006-2018)
            Click image for larger version

Name:	Graph.png
Views:	1
Size:	16.5 KB
ID:	1613990

            Comment


            • #7
              Hi Andrew Musau, this looks great! Could we use something to plot the marginal effects of each of these models?

              I'm trying with the following, for example:

              Code:
              * Compute marginal effects
              qui levelsof year, local(years)
              foreach yy of local years {
                  qui logit Y X1 X2 X3 X4 if year==`yy', vce(cluster ID)
                  qui eststo: estpost margins, dydx(X1 X2 X3 X4) predict(p10)
                  eststo marg_p10_`yy'
              }
              
              * Plot marginal effects
                  coefplot    (marg_p10_2006, label(2006)) ///
                              (marg_p10_2007, label(2007)) ///
                              (marg_p10_2008, label(2008)) ///
                              (marg_p10_2009, label(2009)), ///
                              ver drop(_cons)
              But after the loop I get
              Code:
              option p10 not allowed
              r(198);
              Thank you.
              Last edited by Emanuel Agu; 13 Aug 2023, 10:52.

              Comment


              • #8
                Originally posted by Emanuel Agu View Post
                Hi Andrew Musau, this looks great! Could we use something to plot the marginal effects of each of these models?
                Possibly. Provide a data example.

                Comment


                • #9
                  Originally posted by Andrew Musau View Post

                  Possibly. Provide a data example.
                  Hi, sorry, I just read your reply. I didn't get any warning before. I was just using the data example you built for this thread. Thank you!

                  Comment


                  • #10
                    You probably mean -predict(pr)- in #7. This works.

                    Code:
                    clear
                    set obs 13
                    gen year= 2005+_n
                    expand 200
                    bys year: gen ID=_n
                    set seed 06092021
                    foreach var in Y X1 X2 X3 X4{
                        local g =cond("`var'"=="Y", "runiformint(0,1)", "rnormal(0, 1)") 
                        gen `var'=  `g'
                    }
                    * Compute marginal effects
                    qui levelsof year, local(years)
                    foreach yy of local years {
                        qui logit Y X1 X2 X3 X4 if year==`yy', vce(cluster ID)
                        qui eststo: estpost margins, dydx(X1 X2 X3 X4) predict(pr)
                        eststo marg_p10_`yy'
                    }
                    
                    * Plot marginal effects
                        coefplot    (marg_p10_2006, label(2006)) ///
                                    (marg_p10_2007, label(2007)) ///
                                    (marg_p10_2008, label(2008)) ///
                                    (marg_p10_2009, label(2009)), ///
                                    vert drop(_cons)

                    Comment

                    Working...
                    X