Announcement

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

  • Moving average graph

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(edate treat) double(MA7 population) float deaths_pop
    22032 0  712.9999997466803 1188.49065    .5999206
    22573 0 400.28571559488773 1188.49065    .3368017
    22272 0 1109.0000005215406 1188.49065    .9331163
    21937 0                  0 1188.49065           0
    22280 0 1218.8571502268314 1188.49065   1.0255505
    22552 0 490.57142989337444 1188.49065   .41276845
    22549 0  82.14285771548748 1188.49065   .06911527
    22311 0  520.0000007450581 1188.49065    .4375297
    22357 0  483.7142884731293 1188.49065    .4069988
    22126 0 410.57142955064774 1188.49065    .3454562
    22553 0  379.5714303255081 1188.49065   .31937265
    22489 0  87.00000220537186 1188.49065  .073202096
    22244 0  597.4285709559917 1188.49065    .5026784
    22113 0 324.57142958045006 1188.49065    .2730955
    22120 0 387.71428751945496 1188.49065    .3262241
    21998 0   74.7142873853445 1188.49065   .06286485
    22559 0  516.5714277178049 1188.49065    .4346449
    22462 0  9.000001639127731 1188.49065  .007572631
    22451 0 27.000000953674316 1188.49065   .02271789
    22291 0  735.4285701960325 1188.49065    .6187921
    22172 0   155.000003144145 1188.49065   .13041753
    22439 0 106.00000266730785 1188.49065   .08918875
    22112 0 388.85714343190193 1188.49065    .3271857
    22274 0 406.71428829431534 1188.49065    .3422108
    22109 0 168.57143087685108 1188.49065   .14183657
    22366 0  295.1428577899933 1188.49065    .2483342
    22111 0  351.5714308321476 1188.49065   .29581338
    22233 0 326.99999959766865 1188.49065   .27513888
    22579 0 427.85714434087276 1188.49065    .3600004
    21997 0  37.14285770058632 1188.49065  .031252123
    22062 0   989.428577169776 1188.49065    .8325085
    22634 0 327.00000198185444 1188.49065   .27513888
    22102 0 192.28571647405624 1188.49065   .16178985
    22030 0  978.7142827361822 1188.49065    .8234935
    22626 0  68.71428698301315 1188.49065   .05781643
    22423 0  89.14285872876644 1188.49065    .0750051
    22403 0 143.28571698069572 1188.49065   .12056108
    22444 0   42.5714301019907 1188.49065  .035819743
    22548 0   433.142855912447 1188.49065    .3644478
    22127 0  470.0000021904707 1188.49065    .3954596
    22318 0  408.4285740852356 1188.49065    .3436532
    22491 0 137.42857557535172 1188.49065   .11563286
    22093 0  222.0000029951334 1188.49065   .18679154
    22239 0 497.00000186264515 1188.49065    .4181775
    22632 0 149.57142966985703 1188.49065    .1258499
    22315 0 1450.1428673416376 1188.49065    1.220155
    22157 0 121.14285911619663 1188.49065      .10193
    22584 0 111.57142968475819 1188.49065   .09387657
    22105 0  368.1428595483303 1188.49065    .3097566
    22510 0 285.14285810291767 1188.49065   .23992015
    21993 0  19.00000064074993 1188.49065  .015986664
    22327 0  604.4285725355148 1188.49065    .5085682
    22581 0  288.7142884135246 1188.49065   .24292517
    22133 0 140.71428868174553 1188.49065   .11839747
    22082 0  326.7142884135246 1188.49065    .2748985
    22337 0   702.000001847744 1188.49065   .59066516
    22026 0 1317.7142845392227 1188.49065   1.1087292
    22432 0 131.14285834133625 1188.49065   .11034404
    22575 0  436.8571424931288 1188.49065   .36757305
    22078 0  372.7142885029316 1188.49065     .313603
    22560 0 388.71428632736206 1188.49065    .3270655
    22077 0  412.1428583264351 1188.49065    .3467784
    22008 0 374.28571443259716 1188.49065   .31492525
    22566 0  438.5714297890663 1188.49065   .36901546
    22363 0  531.4285730719566 1188.49065    .4471458
    22314 0 1265.7142848074436 1188.49065   1.0649762
    22007 0   437.571428835392 1188.49065   .36817405
    22005 0 284.57143042981625 1188.49065   .23943935
    22506 0  73.28571546077728 1188.49065   .06166285
    22266 0  973.4285749942064 1188.49065    .8190461
    21936 0                  0 1188.49065           0
    22324 0 1184.9999979436398 1188.49065     .997063
    22132 0 335.42857261002064 1188.49065   .28223073
    22140 0 369.85714384913445 1188.49065     .311199
    22137 0 178.28571537137032 1188.49065    .1500102
    22064 0  603.4285714179277 1188.49065    .5077268
    22160 0  328.5714295655489 1188.49065    .2764611
    22241 0 232.00000162422657 1188.49065   .19520557
    22587 0  339.0000021606684 1188.49065   .28523573
    22295 0 1121.5714283138514 1188.49065    .9436939
    22457 0 26.428573086857796 1188.49065   .02223709
    22060 0 272.42857426404953 1188.49065    .2292223
    22429 0 117.28571592271328 1188.49065    .0986846
    22380 0 219.71428851783276 1188.49065   .18486834
    22057 0  679.9999964535236 1188.49065    .5721543
    22469 0  76.00000198185444 1188.49065   .06394666
    22569 0 109.14285756647587 1188.49065   .09183317
    22629 0 492.85714516043663 1188.49065    .4146917
    22141 0  357.4285767823458 1188.49065    .3007416
    22092 0  376.0000031888485 1188.49065    .3163677
    22354 0 228.42857393622398 1188.49065   .19220057
    22393 0  309.1428592056036 1188.49065   .26011384
    21984 0 1.0000000447034836 1188.49065 .0008414034
    22067 0   427.142859891057 1188.49065    .3593994
    22582 0  408.1428593546152 1188.49065    .3434128
    22161 0 332.71428990364075 1188.49065   .27994692
    22530 0  347.2857171446085 1188.49065   .29220736
    22390 0 229.00000275671482 1188.49065   .19268137
    22049 0  938.8571412712336 1188.49065    .7899575
    22070 0   561.714289277792 1188.49065    .4726283
    end
    format %td edate
    Code:
    lgraph deaths_pop edate treat, ///
                legend(symxsize(*1) size(*1) region(c(none)) position(6) rowgap(*0.5) cols(1)) ///
                legend( order ( 1 "Bottom 10% of Trump Voting Counties" 2 "Top 10% of Trump Voting Counties")) ///
                xtitle("Date") ytitle("Daily Covid Deaths per 100,000 Residents") ///
                title("Covid Deaths and Trump 2020 Vote Share")

    Trying to make a graph where I am looking at the daily number of covid deaths in Trump counties and in Biden counties. Here, my treatment variable is a dummy that equals 1 if the county was in the top 10% of Trump voters, and 0 if in the bottom 10%. However, the graph this makes stinks. Can someone help me make it look good.

    Really, I just want it to look smoother so that there are two distinct lines that looks more like a graph I saw in the NYT recently. I am using a 7 day moving average but it doesn't seem to have helped anything compared to when I use daily values.



  • #2
    Is lgraph a user written command? If so, the FAQ asks that you specify this in your question (me personally, I just include the code to install it if the user doesn't have it).

    You'd do this like

    Code:
        loc comm distinct gtools labvars
        
        foreach x of loc comm { // begin foreach
    
            qui: cap which `x'
        
                if _rc { // if command is missing
    
                qui: ssc inst `x', replace
            
                } // ends if
        } // ends foreach

    Comment


    • #3
      On #3 it is good to know that Jared Greathouse finds distinct useful but downloading it from SSC will find only a 2012 version which has been superseded by 2015 and 2020 updates from the Stata Journal. My fault, if you like.

      I don't understand the data and what has been done in #1 to comment helpfully.

      Comment


      • #4


        Sorry about that, have been using lgraph for a while. Forgot its not part of base Stata. https://ideas.repec.org/c/boc/bocode/s456849.html

        My code is producing this graph below but I want it to look like the NYT one I have attached...

        Click image for larger version

Name:	Screen Shot 2022-01-07 at 10.37.51 AM.png
Views:	1
Size:	948.3 KB
ID:	1644023

        Click image for larger version

Name:	Screen Shot 2022-01-07 at 10.39.46 AM.png
Views:	2
Size:	413.1 KB
ID:	1644025
        Attached Files
        Last edited by Tomer Stern; 07 Jan 2022, 09:45.

        Comment


        • #5
          I have almost never used lgraph -- which strikes me as a command that bundles together several different things that the programmer wanted while not offering much that wasn't already available under other headings. As many of my own commands could be described that way too, I am not complaining or criticising, just explaining why I won't try to read the help and work out how you should use lgraph differently. As I've remarked elsewhere a command that a programmer fondly imagines to be a Swiss Army knife can easily look like a Heath Robinson machine (exact US equivalent: Rube Goldberg machine) to others.

          In essence you have two time series that are busy enough already given daily resolution and some irregularities. The markers (filled circles) just add noise. I don't see why this isn't just a case for using line after smoothing.

          Although it is mostly a question of taste I really wouldn't use scheme s2color unless you like it or are obliged to use it.

          I'd also look into changing the display format for the x-axis date labels.

          Although it doesn't seem standard your data are so spiky that I would consider using a geometric version of a moving average mean, meaning exp(smooth(log())

          Comment


          • #6
            Originally posted by Nick Cox View Post
            I have almost never used lgraph -- which strikes me as a command that bundles together several different things that the programmer wanted while not offering much that wasn't already available under other headings. As many of my own commands could be described that way too, I am not complaining or criticising, just explaining why I won't try to read the help and work out how you should use lgraph differently. As I've remarked elsewhere a command that a programmer fondly imagines to be a Swiss Army knife can easily look like a Heath Robinson machine (exact US equivalent: Rube Goldberg machine) to others.

            In essence you have two time series that are busy enough already given daily resolution and some irregularities. The markers (filled circles) just add noise. I don't see why this isn't just a case for using line after smoothing.

            Although it is mostly a question of taste I really wouldn't use scheme s2color unless you like it or are obliged to use it.

            I'd also look into changing the display format for the x-axis date labels.

            Although it doesn't seem standard your data are so spiky that I would consider using a geometric version of a moving average mean, meaning exp(smooth(log())
            Makes sense. I realized I was 100% doing my moving average wrong somehow. Once I fixed it and got rid of the markers (as you suggested) things look a lot better. Thanks!

            Comment

            Working...
            X