Announcement

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

  • Line Graph

    Hi all,

    I am attempting to create a line graph for NO2 levels across four geographical areas (coded as MMM). I am trying to make it so the total levels of NO2 for each geographical area are plotted.

    I have tried to use the following basic code: twoway line no2 mmm - however, this produces a very messy graph with multiple lines (see attached).

    I then tried to use: twoway line no2 mmm, sorted - however, this too does not work appropriately (see attached).

    I am slo trying to include confidence intervals in this graph, however, am just trying to work out how to do this first step!

    Any help would be incredibly appreciated!

    Attached Files

  • #2
    Why a line graph at all if 1 2 3 4 are areas? Are they in some kind of spatial sequence? What is the overall structure of the data? Please give a data example using dataex.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Why a line graph at all if 1 2 3 4 are areas? Are they in some kind of spatial sequence? What is the overall structure of the data? Please give a data example using dataex.
      Hi Nick, my apologies I should have provided more context - 1 to 4 is a scale that denotes rurality of the area in a sequence scale (i.e. 1 = is most urban and 4 = the most remote). Having difficulty giving a data example using dataex (was referring to this thread: https://www.statalist.org/forums/for...-for-statalist)

      I have come to realise that I probably need to graph the mean of the NO2 variable so that way I do not have many, many points on the graph etc. is there a code that would allow for this so that I can graph the mean of the NO2 over the MMM groups? (While also including confidence intervals)

      Comment


      • #4
        The easiest solution is to realize that linear regression is just computing conditional means, and that is exactly what you want. Here is an example:

        Code:
        . sysuse auto, clear
        (1978 automobile data)
        
        . reg price i.rep78
        
              Source |       SS           df       MS      Number of obs   =        69
        -------------+----------------------------------   F(4, 64)        =      0.24
               Model |  8360542.63         4  2090135.66   Prob > F        =    0.9174
            Residual |   568436416        64     8881819   R-squared       =    0.0145
        -------------+----------------------------------   Adj R-squared   =   -0.0471
               Total |   576796959        68  8482308.22   Root MSE        =    2980.2
        
        ------------------------------------------------------------------------------
               price | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
        -------------+----------------------------------------------------------------
               rep78 |
                  2  |   1403.125   2356.085     0.60   0.554    -3303.696    6109.946
                  3  |   1864.733   2176.458     0.86   0.395    -2483.242    6212.708
                  4  |       1507   2221.338     0.68   0.500    -2930.633    5944.633
                  5  |     1348.5   2290.927     0.59   0.558    -3228.153    5925.153
                     |
               _cons |     4564.5   2107.347     2.17   0.034     354.5913    8774.409
        ------------------------------------------------------------------------------
        
        .
        . // get the means from the model
        . margins i.rep78
        
        Adjusted predictions                                        Number of obs = 69
        Model VCE: OLS
        
        Expression: Linear prediction, predict()
        
        ------------------------------------------------------------------------------
                     |            Delta-method
                     |     Margin   std. err.      t    P>|t|     [95% conf. interval]
        -------------+----------------------------------------------------------------
               rep78 |
                  1  |     4564.5   2107.347     2.17   0.034     354.5913    8774.409
                  2  |   5967.625   1053.673     5.66   0.000     3862.671    8072.579
                  3  |   6429.233   544.1145    11.82   0.000      5342.24    7516.227
                  4  |     6071.5   702.4489     8.64   0.000     4668.197    7474.803
                  5  |       5913   898.5756     6.58   0.000     4117.889    7708.111
        ------------------------------------------------------------------------------
        
        .
        . // see that these margins are really just means
        . table (rep78), stat(mean price)
        
        ------------------------------
                           |      Mean
        -------------------+----------
        Repair record 1978 |          
          1                |    4564.5
          2                |  5967.625
          3                |  6429.233
          4                |    6071.5
          5                |      5913
          Total            |  6146.043
        ------------------------------
        
        .
        . // turn it in the graph we want
        . qui margins i.rep78
        
        . marginsplot
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	68.3 KB
ID:	1683747
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment


        • #5
          Originally posted by Maarten Buis View Post
          The easiest solution is to realize that linear regression is just computing conditional means, and that is exactly what you want. Here is an example:

          Code:
          . sysuse auto, clear
          (1978 automobile data)
          
          . reg price i.rep78
          
          Source | SS df MS Number of obs = 69
          -------------+---------------------------------- F(4, 64) = 0.24
          Model | 8360542.63 4 2090135.66 Prob > F = 0.9174
          Residual | 568436416 64 8881819 R-squared = 0.0145
          -------------+---------------------------------- Adj R-squared = -0.0471
          Total | 576796959 68 8482308.22 Root MSE = 2980.2
          
          ------------------------------------------------------------------------------
          price | Coefficient Std. err. t P>|t| [95% conf. interval]
          -------------+----------------------------------------------------------------
          rep78 |
          2 | 1403.125 2356.085 0.60 0.554 -3303.696 6109.946
          3 | 1864.733 2176.458 0.86 0.395 -2483.242 6212.708
          4 | 1507 2221.338 0.68 0.500 -2930.633 5944.633
          5 | 1348.5 2290.927 0.59 0.558 -3228.153 5925.153
          |
          _cons | 4564.5 2107.347 2.17 0.034 354.5913 8774.409
          ------------------------------------------------------------------------------
          
          .
          . // get the means from the model
          . margins i.rep78
          
          Adjusted predictions Number of obs = 69
          Model VCE: OLS
          
          Expression: Linear prediction, predict()
          
          ------------------------------------------------------------------------------
          | Delta-method
          | Margin std. err. t P>|t| [95% conf. interval]
          -------------+----------------------------------------------------------------
          rep78 |
          1 | 4564.5 2107.347 2.17 0.034 354.5913 8774.409
          2 | 5967.625 1053.673 5.66 0.000 3862.671 8072.579
          3 | 6429.233 544.1145 11.82 0.000 5342.24 7516.227
          4 | 6071.5 702.4489 8.64 0.000 4668.197 7474.803
          5 | 5913 898.5756 6.58 0.000 4117.889 7708.111
          ------------------------------------------------------------------------------
          
          .
          . // see that these margins are really just means
          . table (rep78), stat(mean price)
          
          ------------------------------
          | Mean
          -------------------+----------
          Repair record 1978 |
          1 | 4564.5
          2 | 5967.625
          3 | 6429.233
          4 | 6071.5
          5 | 5913
          Total | 6146.043
          ------------------------------
          
          .
          . // turn it in the graph we want
          . qui margins i.rep78
          
          . marginsplot
          [ATTACH=CONFIG]n1683747[/ATTACH]
          Thank you kindly for your response Maarten. Apologies if this is a basic question, but what code should I use to try and get the mean values for my NO2 variable so that I can graph this on my line graph?

          Comment


          • #6
            @Maarten Buis' point was that the regression calculates the means for you. That's what a regression is, or does, gives the mean outcome as a function of predictors. Do work through his worked example, which is deliberately one that you should be able to run yourself in your Stata.

            A different point is that I would expect NO2 to be better analysed on logarithmic scale. The mean of log NO2 is equivalent to the geometric mean of NO2.

            Comment

            Working...
            X