Announcement

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

  • Using Scalars in Graphics

    For a program I'm writing, I calculate the root mean squared error for a real-versus predicted model. Cigsale3 represents the real smoking per capita in California from 1970-2000, and cf represents the counterfactual predicted by my estimator. I want to include the RMSE in my graph. Here's my code.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(year cigsale3) double cf float(relative diff_ te_ub te_lb cf_ub cf_lb)
    1970   123 119.82134651069296 -19   3.1786535  13.181376  -6.824069         .         .
    1971   121 121.28473667832696 -18   -.2847367   9.717986  -10.28746         .         .
    1972 123.5 124.80728902850656 -17   -1.307289   8.695435 -11.310012         .         .
    1973 124.4 123.71281491583973 -16    .6871866   10.68991 -9.3155365         .         .
    1974 126.7 125.58585418431278 -15   1.1141428  11.116866   -8.88858         .         .
    1975 127.1 125.50124154491989 -14    1.598757   11.60148  -8.403966         .         .
    1976   128 127.23161992677203 -13      .76838  10.771103  -9.234344         .         .
    1977 126.4 125.22142446733864 -12   1.1785771    11.1813  -8.824146         .         .
    1978 126.1 124.30211296755871 -11   1.7978855   11.80061  -8.204838         .         .
    1979 121.9  121.4519594845576 -10     .448042  10.450766  -9.554681         .         .
    1980 120.2 120.31033638806436  -9  -.11033944   9.892384 -10.113063         .         .
    1981 118.6 118.45282869275808  -8   .14716978  10.149893  -9.855554         .         .
    1982 115.4 116.16303441553967  -7   -.7630329    9.23969 -10.765756         .         .
    1983 110.8 112.16553654588606  -6  -1.3655335    8.63719 -11.368257         .         .
    1984 104.8 104.79919815323449  -5 .0008048985  10.003528 -10.001918         .         .
    1985 102.8 103.78663228449747  -4   -.9866292   9.016094 -10.989352         .         .
    1986  99.7 100.55414064169827  -3   -.8541437    9.14858 -10.856867         .         .
    1987  97.5  98.53773049469044  -2  -1.0377305   8.964993 -11.040454         .         .
    1988  90.1  94.31016267480518  -1   -4.210164   5.792559 -14.212887 108.52305   88.5176
    1989  82.4  91.43004762108781   0   -9.030046   .9726767  -19.03277 110.46281  90.45737
    1990  77.8  86.73737755464903   1   -8.937374   1.065349 -18.940098 105.67747  85.67203
    1991  68.7  81.49684970441062   2  -12.796853   -2.79413 -22.799576 104.29642  78.70272
    1992  67.5  80.17442849432022   3   -12.67443  -2.671706  -22.67715 102.85158  77.50272
    1993  63.4  79.30744600654128   4  -15.907445  -5.904722  -25.91017 105.21761 73.402725
    1994  58.6   77.5410142580436   5  -18.941015 -8.9382925  -28.94374 106.48476  68.60272
    1995  56.4  77.24890915711582   6   -20.84891 -10.846185  -30.85163 108.10054 66.402725
    1996  54.5  75.21275766342676   7  -20.712757 -10.710034  -30.71548 105.92824  64.50272
    1997  53.8  75.18500615087264   8   -21.38501 -11.382285  -31.38773 106.57274  63.80272
    1998  52.3  76.59469871504923   9    -24.2947 -14.291976  -34.29742 110.89212  62.30272
    1999  47.2  73.84499577210156  10  -26.644995 -16.642271 -36.647717 110.49271  57.20272
    2000  41.6  68.03738237069064  11  -26.437384  -16.43466 -36.440105 104.47749  51.60272
    end
    
    
    
    tempname treat contr loss err
    
    mkmat cigsale3 if rel < 0, mat(`treat')
    
    mkmat cf if rel < 0, mat(`contr')
    
    mat `loss' = (`treat' - `contr')' * (`treat' - `contr')
    
    mat `loss' = `loss' / rowsof(`treat')
    cls
    mat l `loss'
    qui {
    mata
    
    X = round(sqrt(st_matrix("`loss'")),.01)
    
    st_matrix("`loss'", lossround)
    
    end
    }
    mat rowname `loss' = "RMSPE"
    cls
    sca `err' = `loss'[1,1]
    
    di `err'
    
    qui: su diff_ if relative >= 0, mean
    
    /* This is the ATT for a single intervention. The
    average of the difference after the intervention. */
    
    loc ATT: disp %9.4g `r(mean)'
    
    loc cf_color 237 41 57
    
    loc tr_color  black
    
    tw ///
        (line cig cf year, lcol("`tr_color'" red) lpat(solid dash)) /// Real Outcomes
         (rline cf_lb cf_ub year, lcolor(green) lwidth(medthick) lpattern(solid)), /// Potential Outcomes
            xli(1989, lcol(blue) lpat(dash) lwidth(thick)) ///
            legend(order(1 "Real California" 2 "Synthetic California" 3 "Upper/Lower Bound") ///
            color(black) fcolor(white) region(fcolor(white)) position(7) rows(3) ring(0)) ///
            yti("Cigarette Smoking Per Capita") ///
            ylab(, noticks) xlab(, noticks) ///
            xsize(4) ysize(4) ///
            note("RMSE = `err', ATT: `ATT'", position(6)) //  "Chernozhukov, Zhu and Wuthrich's T: `czw_t'"
    It says the RMSE is __00003. Well, why? When we do
    Code:
    di `err'
    we get 1.53. I presume using scalars is the same as using local macros, right? What am I missing here?

  • #2
    Hi Jared
    No, they are not the same.
    Perhaps what you can do is:

    note("RMSE = `=scalar(`err')', ATT: `ATT'", position(6))

    You may even want to create a local with a formated version of this (to avoid excess decimals)
    local note_err:display %5.3g `err'

    Perhaps this will help
    F

    Comment


    • #3
      Welp, I was mistaken then. Thanks a lot Fernando!!!

      No need to reformat it, I rounded it in mata

      Comment


      • #4
        Some additional notes for context. It's important to distinguish the namespaces that Stata maintains. A namespace is essentially a list of names being used by objects (generic sense) and within a namespace, names cannot refer to multiple objects. For example, variables have their own namespace; macros have their own namespace; and scalars and matrices share a common namespace. That means that the same name can be used across multiple namespaces and Stata can (usually unambiguously) determine which one you are referring to based on the punctuation or context. Where subtle errors can creep up is when there are common names used in multiple namespaces.

        Consider an example, where a mix of common names can result in very different results. When there is the potential for a name clash, it is usually better to use the -tempvar- or -tempname- facilities (especially for programs) or else make explicit the reference.

        Code:
        input int x
        1
        2
        3
        end
        local x 5
        scalar x = 10
        
        di x    // equivalent to: di x[1]
        di `x'
        di `=scalar(x)'
        Result

        Code:
        . di x    // equivalent to: di x[1]
        1
        
        . di `x'
        5
        
        . di `=scalar(x)'
        10

        Comment

        Working...
        X