Announcement

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

  • Plotting set of coefficients from regression

    I have run a regression as follows:

    Code:
    Code:
     reg lgross c.lmovielikes##i.release_year lcastlikes lbudget imdb_score i.content_rating i.is_* , robust
    For ease, here is a clip of some of the output:

    Click image for larger version

Name:	Screen Shot 2018-02-27 at 13.26.49.png
Views:	6
Size:	170.2 KB
ID:	1431952

    I'm looking to plot the coefficients of the interactive variable to show the upward trend. What would be the best way to do this? Thank you!
    Attached Files

  • #2
    Perhaps coefplot will give you what you want.

    Code:
    ssc describe coefplot
    --
    Bruce Weaver
    Email: [email protected]
    Web: http://sites.google.com/a/lakeheadu.ca/bweaver/
    Version: Stata/MP 18.0 (Windows)

    Comment


    • #3
      Thanks for the reply Bruce. I have tried to use -coefplot- previously, however I was unable to isolate the coefficients for the interactive dummy, as well as plot it in a well-displayed graphic. Any guidance on how to do this?

      Comment


      • #4
        IMO, you'd be better off using margins & marginsplot to display fitted values of Y at selected combinations of the interacting variables, or to display simple slopes of one variable at selected values of the other. I think most people find those types of displays easier to interpret than the coefficients. You can see some examples here: HTH.
        --
        Bruce Weaver
        Email: [email protected]
        Web: http://sites.google.com/a/lakeheadu.ca/bweaver/
        Version: Stata/MP 18.0 (Windows)

        Comment


        • #5
          I have tried this method but it does not work well with the continuous log variable of lmovielikes. Is there anyway around this?

          Comment


          • #6
            What do you mean when you say it doesn't work well? Perhaps you should post an example (including a small dataset) that illustrates the problem. See item 12 in the FAQ for details about how to do that. HTH.
            --
            Bruce Weaver
            Email: [email protected]
            Web: http://sites.google.com/a/lakeheadu.ca/bweaver/
            Version: Stata/MP 18.0 (Windows)

            Comment


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

              I can sense that you're frustrated at not getting replies you want, but Bruce is bang on here. "does not work well" is factual for you, but no guidance for us.

              Comment


              • #8
                Thanks to both of you for the replies. I apologise for not being as helpful as I possibly could have been.

                In regards to data, here is a look at the variables relevant to this post:

                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input float(lgross lmovielikes) long release_year
                20.449493 10.404263  7
                  19.1142 11.350407 13
                19.920595 12.007622 10
                18.106773  10.08581 10
                19.117857  10.27505  8
                 19.94454  11.67844 13
                19.525795 9.2103405  7
                 19.61536  12.19096 14
                 19.86296  8.517193  4
                  18.3074 10.778956 11
                 19.48891  11.67844 11
                20.250505  11.71994 10
                19.300573 10.968198  9
                19.003014 10.596635 10
                  19.3572 11.082143 12
                 19.38397 10.933107 10
                18.471561  9.740969  8
                19.369846 11.326596 11
                 19.82481 11.184422 14
                17.992558 10.691945 10
                 20.29583  11.91839 13
                 19.53372 11.289782 10
                19.829206 11.461632 11
                19.627207  10.08581  8
                 19.40832 10.691945 11
                end
                label values release_year release_year
                label def release_year 4 "2006", modify
                label def release_year 7 "2009", modify
                label def release_year 8 "2010", modify
                label def release_year 9 "2011", modify
                label def release_year 10 "2012", modify
                label def release_year 11 "2013", modify
                label def release_year 12 "2014", modify
                label def release_year 13 "2015", modify
                label def release_year 14 "2016", modify
                When using coefplot to limit the coefficients to the interactive terms using the -keep()- option, I am returned with: (.: no coefficients found, all dropped, or none kept)
                (nothing to plot)

                I suspect this is due to the nature of the indicator variables and my poor knowledge of the -coefplot- command despite reading the documentation. Any advice on how to solve this error?

                Comment


                • #9
                  Thanks for posting some data. Some problems arise due to the sparseness of the data with this small sample size. But I think you want something along these lines.

                  Code:
                  summarize lmovielikes
                  * Variable lmovielikes ranges from 8 to 12, roughly.
                  
                  regress lgross c.lmovielikes##i.release_year, robust
                  * Get fitted values of Y at selected combinations of the two variables
                  margins release_year, at(lmovielikes=(8(1)12)) vsquish
                  marginsplot
                  graph rename plot1
                  * Warnings are likely due to small n in some cells.
                  * Get a frequency table.
                  tabulate release_year
                  * Presumably, you won't have this problem when you use all of the data.
                  
                  * Get the simple slope for lmovielikes in each release year
                  margins, dydx(lmovielikes) over(release_year)
                  marginsplot
                  graph rename plot2
                  HTH.
                  --
                  Bruce Weaver
                  Email: [email protected]
                  Web: http://sites.google.com/a/lakeheadu.ca/bweaver/
                  Version: Stata/MP 18.0 (Windows)

                  Comment


                  • #10
                    Thanks Bruce, that works well! Is there any way to drop the 2004 and 2016 margins as well as add a best fit line of these plotted marginal points?

                    Comment


                    • #11
                      Do you want to exclude 2004 andf 2016 from the regression model, or just from the margins command? If the latter, I think you can just add an if qualifier to your margins command. Assuming 2004 corresponds to release_year = 2:

                      Code:
                      margins release_year if !inlist(release_year,2,14), at(lmovielikes=(8(1)12)) vsquish
                      Note that ! means 'not'.

                      HTH.
                      --
                      Bruce Weaver
                      Email: [email protected]
                      Web: http://sites.google.com/a/lakeheadu.ca/bweaver/
                      Version: Stata/MP 18.0 (Windows)

                      Comment


                      • #12
                        Perfect thank you! And any advice on the line of best fit of these marginal values?

                        Comment


                        • #13
                          If you want a linear fit for release year, why are you not treating it as continuous in your regression model?
                          --
                          Bruce Weaver
                          Email: [email protected]
                          Web: http://sites.google.com/a/lakeheadu.ca/bweaver/
                          Version: Stata/MP 18.0 (Windows)

                          Comment


                          • #14
                            To briefly return to #9, how would I drop the 2004 and 2016 values for plot2?

                            My intention was to add a linear fit of the plotted marginal effects, purely to illustrate some trend in the effects of this interactive term over the years. So the marginplot that you suggested is correct, I would just like to add a linear fit line of these plotted values over the top to show some trend over time. Does this make sense?

                            EDIT: To give some indication of what I am looking for, here is a idea, with a purely guessed best fit line (ignoring the 2004 and 2016 values):

                            Click image for larger version

Name:	Screen Shot 2018-03-01 at 20.07.43.png
Views:	1
Size:	312.8 KB
ID:	1432273
                            Last edited by Curan Tahim; 01 Mar 2018, 13:10.

                            Comment


                            • #15
                              I suppose you could save the results from -margins- to a tempfile and go from there. Something like this, perhaps?

                              Code:
                              * Example generated by -dataex-. To install: ssc install dataex
                              clear *
                              graph close *
                              input float(lgross lmovielikes) long release_year
                              20.449493 10.404263  7
                                19.1142 11.350407 13
                              19.920595 12.007622 10
                              18.106773  10.08581 10
                              19.117857  10.27505  8
                               19.94454  11.67844 13
                              19.525795 9.2103405  7
                               19.61536  12.19096 14
                               19.86296  8.517193  4
                                18.3074 10.778956 11
                               19.48891  11.67844 11
                              20.250505  11.71994 10
                              19.300573 10.968198  9
                              19.003014 10.596635 10
                                19.3572 11.082143 12
                               19.38397 10.933107 10
                              18.471561  9.740969  8
                              19.369846 11.326596 11
                               19.82481 11.184422 14
                              17.992558 10.691945 10
                               20.29583  11.91839 13
                               19.53372 11.289782 10
                              19.829206 11.461632 11
                              19.627207  10.08581  8
                               19.40832 10.691945 11
                              end
                              label values release_year release_year
                              label def release_year 4 "2006", modify
                              label def release_year 5 "2007", modify
                              label def release_year 6 "2008", modify
                              label def release_year 7 "2009", modify
                              label def release_year 8 "2010", modify
                              label def release_year 9 "2011", modify
                              label def release_year 10 "2012", modify
                              label def release_year 11 "2013", modify
                              label def release_year 12 "2014", modify
                              label def release_year 13 "2015", modify
                              label def release_year 14 "2016", modify
                              label var release_year "Year of Release"
                              
                              summarize lmovielikes
                              * Variable lmovielikes ranges from 8 to 12, roughly.
                              
                              regress lgross c.lmovielikes##i.release_year, robust
                              * Get fitted values of Y at selected combinations of the two variables
                              margins release_year if !inlist(release_year,4,14), at(lmovielikes=(8(1)12)) vsquish
                              marginsplot
                              graph rename plot1
                              * Warnings are likely due to small n in some cells.
                              * Get a frequency table.
                              tabulate release_year
                              * Presumably, you won't have this problem when you use all of the data.
                              
                              preserve // Preserve the working data
                              tempfile temp1
                              margins if !inlist(release_year,4,14), /// Adjust values as needed
                              dydx(lmovielikes) over(release_year) ///
                              saving(`temp1')
                              marginsplot
                              graph rename plot2
                              use `temp1', clear
                              list _by1 _margin _se _ci_lb _ci_ub, noobs clean
                              
                              quietly regress _margin _by1
                              margins, at(_by1=(7(1)13))
                              marginsplot, recast(line) noci  ///
                              addplot(scatter _margin _by1 || rcap _ci_ub _ci_lb _by1, legend(off))
                              graph rename plot3
                              
                              * Given that you are fitting a line to data points that are
                              * marginal means, you might want to use WLS regression, where
                              * the weight is the inverse of the variance.
                              
                              generate w = 1/_se^2
                              quietly regress _margin _by1 [aweight=w] // Weight by inverse of variance
                              margins, at(_by1=(7(1)13))
                              marginsplot, recast(line) noci  ///
                              addplot(scatter _margin _by1 || rcap _ci_ub _ci_lb _by1, legend(off))
                              graph rename plot4
                              restore
                              --
                              Bruce Weaver
                              Email: [email protected]
                              Web: http://sites.google.com/a/lakeheadu.ca/bweaver/
                              Version: Stata/MP 18.0 (Windows)

                              Comment

                              Working...
                              X