asd7
asd7

Reputation: 19

How to get Stargazer Summary Statistics by Year from Panel Data

I have a panel data set with 159 countries between 2009 and 2017.

When I use Stargazer to create a table with summary statistics it uses the values for all years. E.g. the mean of GDP would be the mean value for all countries and years.

What I would like to get is the summary statistics of each year. E.g. Summary statistics for 2009, 2010,..., 2017.

The panel data set looks like this:

   area_code year   area_name         area_group Executive Constraints Government Effectiveness
1        AFG 2009 Afghanistan       Asia-Pacific              39.60269                 28.00944
2        AFG 2010 Afghanistan       Asia-Pacific              39.60269                 28.07446
3        AFG 2011 Afghanistan       Asia-Pacific              39.60269                 20.82287
4        AFG 2012 Afghanistan       Asia-Pacific              39.60269                 20.85591
5        AFG 2013 Afghanistan       Asia-Pacific              39.60269                 21.32710
6        AFG 2014 Afghanistan       Asia-Pacific              39.60269                 21.19488
7        AFG 2015 Afghanistan       Asia-Pacific              39.60269                 21.48040
8        AFG 2016 Afghanistan       Asia-Pacific              38.26936                 21.52523
9        AFG 2017 Afghanistan       Asia-Pacific              38.93603                 26.52632
10       AGO 2009      Angola Sub-Saharan Africa              37.83082                 22.59876
11       AGO 2010      Angola Sub-Saharan Africa              37.83082                 23.14201
12       AGO 2011      Angola Sub-Saharan Africa              37.83082                 32.67216
13       AGO 2012      Angola Sub-Saharan Africa              37.83082                 32.39330
14       AGO 2013      Angola Sub-Saharan Africa              37.83082                 33.26756
15       AGO 2014      Angola Sub-Saharan Africa              37.83082                 32.09595

Ideally I would like to get an output for a the year(s) specified.

E.g. For 2009:

Descriptive statistics 2009
================================================================================================================
Statistic                                     N      Mean     St. Dev.    Min   Pctl(25)   Pctl(75)      Max    
----------------------------------------------------------------------------------------------------------------
Executive Constraints                       1,412   55.484     16.464   17.545   42.888     64.848     94.848   
Government Effectiveness                    1,412   52.581     21.991    8.305   36.281     65.687     96.493  

Upvotes: 0

Views: 1290

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173803

You could use stargazer in an lapply after using split on your data frame to split by year. Here I have used the mtcars data set since there isn't really enough sample data in the question to demonstrate. This produces a summary table for each group of cars according to the number of cylinders they have. Of course, in your case you would instead split by year.

result <- lapply(split(mtcars, mtcars$cyl), stargazer::stargazer, type = "text")
#> 
#> =============================================================
#> Statistic N   Mean   St. Dev.  Min   Pctl(25) Pctl(75)  Max  
#> -------------------------------------------------------------
#> mpg       11 26.664   4.510   21.400  22.800   30.400  33.900
#> cyl       11  4.000   0.000     4       4        4       4   
#> disp      11 105.136  26.872    71     78.8    120.7    147  
#> hp        11 82.636   20.935    52     65.5      96     113  
#> drat      11  4.071   0.365   3.690   3.810    4.165   4.930 
#> wt        11  2.286   0.570   1.513   1.885    2.622   3.190 
#> qsec      11 19.137   1.682   16.700  18.560   19.950  22.900
#> vs        11  0.909   0.302     0       1        1       1   
#> am        11  0.727   0.467     0      0.5       1       1   
#> gear      11  4.091   0.539     3       4        4       5   
#> carb      11  1.545   0.522     1       1        2       2   
#> -------------------------------------------------------------
#> 
#> ============================================================
#> Statistic N  Mean   St. Dev.  Min   Pctl(25) Pctl(75)  Max  
#> ------------------------------------------------------------
#> mpg       7 19.743   1.454     18     18.6      21      21  
#> cyl       7  6.000   0.000     6       6        6       6   
#> disp      7 183.314  41.562   145     160     196.3    258  
#> hp        7 122.286  24.260   105     110      123     175  
#> drat      7  3.586   0.476   2.760   3.350    3.910   3.920 
#> wt        7  3.117   0.356   2.620   2.822    3.440   3.460 
#> qsec      7 17.977   1.707   15.500  16.740   19.170  20.220
#> vs        7  0.571   0.535     0       0        1       1   
#> am        7  0.429   0.535     0       0        1       1   
#> gear      7  3.857   0.690     3      3.5       4       5   
#> carb      7  3.429   1.813     1      2.5       4       6   
#> ------------------------------------------------------------
#> 
#> =============================================================
#> Statistic N   Mean   St. Dev.  Min   Pctl(25) Pctl(75)  Max  
#> -------------------------------------------------------------
#> mpg       14 15.100   2.560   10.400  14.400   16.250  19.200
#> cyl       14  8.000   0.000     8       8        8       8   
#> disp      14 353.100  67.771   276    301.8     390     472  
#> hp        14 209.214  50.977   150    176.2    241.2    335  
#> drat      14  3.229   0.372   2.760   3.070    3.225   4.220 
#> wt        14  3.999   0.759   3.170   3.533    4.014   5.424 
#> qsec      14 16.772   1.196   14.500  16.098   17.555  18.000
#> vs        14  0.000   0.000     0       0        0       0   
#> am        14  0.143   0.363     0       0        0       1   
#> gear      14  3.286   0.726     3       3        3       5   
#> carb      14  3.500   1.557     2      2.2       4       8   
#> -------------------------------------------------------------

Created on 2020-08-22 by the reprex package (v0.3.0)

Upvotes: 2

Related Questions