Announcement

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

  • Extracting the number of countries in the estimation as a scalar



    I wish to extract the number of countries in an estimation as a scalar.

    I export the results to LaTeX using esttab and I wish to add the number of countries in the results table.

    I have prepared a MWE with a sample of the data

    MWE:
    Code:
    clear
    input str16 country    lbe    numscore1
    "Slovak Republic"    .1609062    .5714904
    "Canada"    .6978026    -.708186
    "Sweden"    .4750029    .4303062
    "Poland"    .7625731    .3039408
    "Canada"    .6978026    -1.142108
    "Russian Federation"    .3112665    .4964201
    "Estonia"    .6068639    .2495946
    "Peru"    .1787041    1.120539
    "United Kingdom"    .5715541    1.295992
    "Chile"    .0202874    2.339742
    "United Kingdom"    .5715541    .6408792
    "Canada"    .6978026    .1551091
    "New Zealand"    .5912121    -.622528
    "Canada"    .6978026    -.1379412
    "Israel"    .3363137    1.716994
    "Finland"    .6105343    .3230855
    "Chile"    .0202874    -.5432029
    "Chile"    .0202874    1.095871
    "Kazakhstan"    .4950767    -.5600912
    end
    
    reg numscore1 lbe
    estadd scalar AdjuR2 =  e(r2_a)
    eststo reg1
    esttab reg1, stats(N AdjuR2, fmt(%12.0fc 2) labels("N" "Adjusted R-sq"))


    My aim is to add another row (which will contain the number of countries in the estimation) to the results table.

    I use the following code to know how many countries are in the this estimation (13 in this case):

    Code:
    cap drop flag
    egen flag = tag(country)
    tab country if flag











    Stata/MP 15.1

  • #2
    You may use - egen - with rowtotal() function plus "if flag ==1"
    Best regards,

    Marcos

    Comment


    • #3
      Alternatively, without creating extra variables, you can simply do tab country. The number of rows will be the number of countries, which tab stores in r(r).
      Code:
      eststo reg1: reg numscore1 lbe
      
      tab country
      estadd scalar n_countries = r(r)
      
      esttab reg1, stats(N r2_a n_countries, fmt(%12.0fc 2 0) labels("N" "Adjusted R-sq" "Countries"))

      Comment

      Working...
      X