Announcement

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

  • Formatting vs. Precision Problem Using Table

    I simulated a model 31 different times, and I want to compare how the estimator performs across various levels of Gaussian noise. - See my update at the bottom.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(variance premse postmse ratio)
     .1 .31061575  .3611799 1.1627868
     .2  .3121041 .36341465 1.1644021
     .3 .29441914  .3411821 1.1588312
     .4 .29515648  .3421026 1.1590548
     .5 .29718387  .3448728 1.1604693
     .6  .2986346  .3474558 1.1634814
     .7 .30110005  .3512891 1.1666857
     .8   .303456  .3542318 1.1673254
     .9  .3064812  .3580584 1.1682882
      1  .3100766   .363158 1.1711879
    1.1  .3154313  .3720172 1.1793923
    1.2  .3182329  .3742517  1.176031
    1.3    .32241  .3792378 1.1762595
    1.4  .3291871  .3897445 1.1839602
    1.5  .3338935  .3954707 1.1844217
    1.6  .3461653   .412933 1.1928782
    1.7  .3479584  .4143937 1.1909288
    1.8  .3488469  .4131078 1.1842093
    1.9  .3546743  .4203638 1.1852108
      2  .3602999   .427142 1.1855181
    2.1  .3661608  .4342337 1.1859097
    2.2  .3673443  .4312872 1.1740681
    2.3   .377447  .4473171 1.1851122
    2.4  .3957823  .4730073   1.19512
    2.5  .3819886  .4414285 1.1556065
    2.6  .4139824  .4952473 1.1963006
    2.7  .4175476  .4990176 1.1951153
    2.8  .4163164  .4958761 1.1911038
    2.9  .4248772  .5064601 1.1920152
      3  .4310946  .5135548 1.1912811
    6.2  .6136207  .7067071 1.1517003
    end
    
    graph hbar (asis) ratio, over(variance) ///
        bar(1, fcolor(cyan) lcolor(cyan)) ///
        blabel(bar, format(%6.3f)) ///
        ytitle(Post/Pre RMSE Ratio) //
    I originally tried a bar graph, and I've also experimented with histograms, but to me, it seems like a table would be better to display this information. So, I did
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(variance premse postmse ratio)
     .1 .31061575  .3611799 1.1627868
     .2  .3121041 .36341465 1.1644021
     .3 .29441914  .3411821 1.1588312
     .4 .29515648  .3421026 1.1590548
     .5 .29718387  .3448728 1.1604693
     .6  .2986346  .3474558 1.1634814
     .7 .30110005  .3512891 1.1666857
     .8   .303456  .3542318 1.1673254
     .9  .3064812  .3580584 1.1682882
      1  .3100766   .363158 1.1711879
    1.1  .3154313  .3720172 1.1793923
    1.2  .3182329  .3742517  1.176031
    1.3    .32241  .3792378 1.1762595
    1.4  .3291871  .3897445 1.1839602
    1.5  .3338935  .3954707 1.1844217
    1.6  .3461653   .412933 1.1928782
    1.7  .3479584  .4143937 1.1909288
    1.8  .3488469  .4131078 1.1842093
    1.9  .3546743  .4203638 1.1852108
      2  .3602999   .427142 1.1855181
    2.1  .3661608  .4342337 1.1859097
    2.2  .3673443  .4312872 1.1740681
    2.3   .377447  .4473171 1.1851122
    2.4  .3957823  .4730073   1.19512
    2.5  .3819886  .4414285 1.1556065
    2.6  .4139824  .4952473 1.1963006
    2.7  .4175476  .4990176 1.1951153
    2.8  .4163164  .4958761 1.1911038
    2.9  .4248772  .5064601 1.1920152
      3  .4310946  .5135548 1.1912811
    6.2  .6136207  .7067071 1.1517003
    end
    
    recast double variance
    
    clear matrix
    mkmat pre-ratio if !mi(ratio), mat(B)
    
    format variance  %2.1g
    
    
    levelsof variance, loc(vars)
    
    cls
    
    mat rownames B = `r(levels)'
    
    mat l B
    
    mata : st_matrix("r(B)", st_matrix("B"))
    mata : st_matrixrowstripe("r(B)", st_matrixrowstripe("B"))
    
    
    clear collect
    collect get r(B)
    collect layout (rowname)(colname)
    Where the row names are the levels of noise I introduced. However, the rows appear to be not aligned with the format I've specified. For example, take the very last number. When I do
    Code:
    di %2.1g 6.199999809265137
    I get
    Code:
    Res:
    6.2
    As expected. But, this isn't what the table shows, it shows the full number as though I've not reformatted it. Is this a display problem, or a precision one, and how might I do this such that the row names are rounded to a single decimal place, and the other three statistics are rounded to say, 3 decimal places?

    UPDATE:
    So, I've used Mata to reformat the. Now all I need to do is reformat the row names themselves
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(variance premse postmse ratio)
     .1 .31061575  .3611799 1.1627868
     .2  .3121041 .36341465 1.1644021
     .3 .29441914  .3411821 1.1588312
     .4 .29515648  .3421026 1.1590548
     .5 .29718387  .3448728 1.1604693
     .6  .2986346  .3474558 1.1634814
     .7 .30110005  .3512891 1.1666857
     .8   .303456  .3542318 1.1673254
     .9  .3064812  .3580584 1.1682882
      1  .3100766   .363158 1.1711879
    1.1  .3154313  .3720172 1.1793923
    1.2  .3182329  .3742517  1.176031
    1.3    .32241  .3792378 1.1762595
    1.4  .3291871  .3897445 1.1839602
    1.5  .3338935  .3954707 1.1844217
    1.6  .3461653   .412933 1.1928782
    1.7  .3479584  .4143937 1.1909288
    1.8  .3488469  .4131078 1.1842093
    1.9  .3546743  .4203638 1.1852108
      2  .3602999   .427142 1.1855181
    2.1  .3661608  .4342337 1.1859097
    2.2  .3673443  .4312872 1.1740681
    2.3   .377447  .4473171 1.1851122
    2.4  .3957823  .4730073   1.19512
    2.5  .3819886  .4414285 1.1556065
    2.6  .4139824  .4952473 1.1963006
    2.7  .4175476  .4990176 1.1951153
    2.8  .4163164  .4958761 1.1911038
    2.9  .4248772  .5064601 1.1920152
      3  .4310946  .5135548 1.1912811
    6.2  .6136207  .7067071 1.1517003
    end
    
    clear matrix
    
    mkmat pre-ratio if !mi(ratio), mat(B)
    
    cls
    mata
    B = st_matrix("B")
    
    B = strofreal(B, "%3.2f")
    
    B = round(st_matrix("B"),.001)
    
    st_matrix("B", B)
    
    end
    
    levelsof variance, loc(vars)
    
    
    mat rownames B = `r(levels)'
    
    mat l B
    Last edited by Jared Greathouse; 16 Jun 2022, 08:36. Reason: Updated with the Mata Code.

  • #2
    Code:
    mata : st_matrixrowstripe("r(B)", strofreal(strtoreal(st_matrixrowstripe("B")),"%2.1g"))
    Code:
    clear collect
    collect get r(B)
    collect style cell colname, nformat(%4.3f)
    collect layout (rowname)(colname)
    Last edited by Bjarte Aagnes; 16 Jun 2022, 08:41.

    Comment


    • #3
      Okay so here's the code as it's been updated.
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear *
      input float(variance premse postmse ratio)
       .1 .31061575  .3611799 1.1627868
       .2  .3121041 .36341465 1.1644021
       .3 .29441914  .3411821 1.1588312
       .4 .29515648  .3421026 1.1590548
       .5 .29718387  .3448728 1.1604693
       .6  .2986346  .3474558 1.1634814
       .7 .30110005  .3512891 1.1666857
       .8   .303456  .3542318 1.1673254
       .9  .3064812  .3580584 1.1682882
        1  .3100766   .363158 1.1711879
      1.1  .3154313  .3720172 1.1793923
      1.2  .3182329  .3742517  1.176031
      1.3    .32241  .3792378 1.1762595
      1.4  .3291871  .3897445 1.1839602
      1.5  .3338935  .3954707 1.1844217
      1.6  .3461653   .412933 1.1928782
      1.7  .3479584  .4143937 1.1909288
      1.8  .3488469  .4131078 1.1842093
      1.9  .3546743  .4203638 1.1852108
        2  .3602999   .427142 1.1855181
      2.1  .3661608  .4342337 1.1859097
      2.2  .3673443  .4312872 1.1740681
      2.3   .377447  .4473171 1.1851122
      2.4  .3957823  .4730073   1.19512
      2.5  .3819886  .4414285 1.1556065
      2.6  .4139824  .4952473 1.1963006
      2.7  .4175476  .4990176 1.1951153
      2.8  .4163164  .4958761 1.1911038
      2.9  .4248772  .5064601 1.1920152
        3  .4310946  .5135548 1.1912811
      6.2  .6136207  .7067071 1.1517003
      end
      
      mkmat pre-ratio if !mi(ratio), mat(B)
      
      cls
      mata : st_matrixrowstripe("r(B)", strofreal(strtoreal(st_matrixrowstripe("B")),"%2.1g"))
      
      collect get r(B)
      collect style cell colname, nformat(%4.3f)
      collect layout (rowname)(colname)
      The Mata line returns an error, however
      Code:
      . mata : st_matrixrowstripe("r(B)", strofreal(strtoreal(st_matrixrowstripe("B")),"%2.1g"))
          st_matrixrowstripe():  3300  argument out of range
                       <istmt>:     -  function returned error
      I updated the code right as you posted, I think, so I'm not sure if I was meant to delete my Mata code or add yours as an addendum to mine. Bjarte Aagnes

      Comment


      • #4
        Hi, given the code example 2 in #1 the idea was to replace your
        Code:
        mata : st_matrixrowstripe("r(B)", st_matrixrowstripe("B"))
        with
        Code:
        mata : st_matrixrowstripe("r(B)", strofreal(strtoreal(st_matrixrowstripe("B")),"%2.1g"))
        which seems to work:
        Code:
        clear all
        
        * Example generated by -dataex-. For more info, type help dataex
        
        input float(variance premse postmse ratio)
         .1 .31061575  .3611799 1.1627868
         .2  .3121041 .36341465 1.1644021
         .3 .29441914  .3411821 1.1588312
         .4 .29515648  .3421026 1.1590548
         .5 .29718387  .3448728 1.1604693
         .6  .2986346  .3474558 1.1634814
         .7 .30110005  .3512891 1.1666857
         .8   .303456  .3542318 1.1673254
         .9  .3064812  .3580584 1.1682882
          1  .3100766   .363158 1.1711879
        1.1  .3154313  .3720172 1.1793923
        1.2  .3182329  .3742517  1.176031
        1.3    .32241  .3792378 1.1762595
        1.4  .3291871  .3897445 1.1839602
        1.5  .3338935  .3954707 1.1844217
        1.6  .3461653   .412933 1.1928782
        1.7  .3479584  .4143937 1.1909288
        1.8  .3488469  .4131078 1.1842093
        1.9  .3546743  .4203638 1.1852108
          2  .3602999   .427142 1.1855181
        2.1  .3661608  .4342337 1.1859097
        2.2  .3673443  .4312872 1.1740681
        2.3   .377447  .4473171 1.1851122
        2.4  .3957823  .4730073   1.19512
        2.5  .3819886  .4414285 1.1556065
        2.6  .4139824  .4952473 1.1963006
        2.7  .4175476  .4990176 1.1951153
        2.8  .4163164  .4958761 1.1911038
        2.9  .4248772  .5064601 1.1920152
          3  .4310946  .5135548 1.1912811
        6.2  .6136207  .7067071 1.1517003
        end
        
        recast double variance
        
        clear matrix
        mkmat premse-ratio if !mi(ratio), mat(B)
        
        format variance  %2.1g
        levelsof variance, loc(vars)
        
        cls
        mat rownames B = `r(levels)'
        mat l B
        
        mata : st_matrix("r(B)", st_matrix("B"))
        mata : st_matrixrowstripe("r(B)", strofreal(strtoreal(st_matrixrowstripe("B")),"%2.1g"))
        
        collect get r(B)
        collect style cell colname, nformat(%4.3f)
        collect layout (rowname)(colname)
        Code:
        . collect layout (rowname)(colname)
        
        Collection: default
              Rows: rowname
           Columns: colname
           Table 1: 31 x 3
        
        -----------------------
            |    c1    c2    c3
        ----+------------------
        .1  | 0.311 0.361 1.163
        .2  | 0.312 0.363 1.164
        .3  | 0.294 0.341 1.159
        .4  | 0.295 0.342 1.159
        .5  | 0.297 0.345 1.160
        .6  | 0.299 0.347 1.163
        .7  | 0.301 0.351 1.167
        .8  | 0.303 0.354 1.167
        .9  | 0.306 0.358 1.168
        1   | 0.310 0.363 1.171
        1.1 | 0.315 0.372 1.179
        1.2 | 0.318 0.374 1.176
        1.3 | 0.322 0.379 1.176
        1.4 | 0.329 0.390 1.184
        1.5 | 0.334 0.395 1.184
        1.6 | 0.346 0.413 1.193
        1.7 | 0.348 0.414 1.191
        1.8 | 0.349 0.413 1.184
        1.9 | 0.355 0.420 1.185
        2   | 0.360 0.427 1.186
        2.1 | 0.366 0.434 1.186
        2.2 | 0.367 0.431 1.174
        2.3 | 0.377 0.447 1.185
        2.4 | 0.396 0.473 1.195
        2.5 | 0.382 0.441 1.156
        2.6 | 0.414 0.495 1.196
        2.7 | 0.418 0.499 1.195
        2.8 | 0.416 0.496 1.191
        2.9 | 0.425 0.506 1.192
        3   | 0.431 0.514 1.191
        6.2 | 0.614 0.707 1.152
        -----------------------

        Comment

        Working...
        X