Announcement

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

  • how to calculate variance of a variable in a panel data structure

    Hello dear Stata users,
    I have a panel dataset of household weekly income at regional level (10 regions) for 20 years. I need to calculate variance of household income. Is there a straightforward Stata command to calculate variance of the household income?
    I think the calculated variance would represent variance in household income across the regions?
    Could anyone please advise me in this regard? Look forward to hearing from you.
    Thanks
    Akhtar

  • #2
    -xtsum- will give you the between, within, and overall standard deviations. Square those to get the corresponding variances. You need to clarify for yourself which of the three statistics is the relevant one for your purposes.

    Comment


    • #3
      Thanks Clyde.
      Yes xtsum generates a summary statistics of variance. Can it be generated as time series observation for each year and each id? For example, variance of income for id 1 in year 1 compared to sample average or compared to national average income.
      Thanks
      Akhtar

      Comment


      • #4
        Your question is unclear to me. If this is panel data on income, id 1 in year 1 refers to a single observation so the variance is necessarily 0. Evidently I'm missing something. I think you need to provide either a better description of your data (preferrably accompanied by some example data using the -dataex- command, a better description of what you want, or both.

        If you are running version 15.1 or a fully updated version 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

        Comment


        • #5
          Sorry Clyde, I wasn't clear about the query. I attached the data file so that you could see the structure of the data. Can I use -xtsum- to generate a new variable measuring variance (within or between) of household income?
          Thanks
          Akhtar
          Attached Files

          Comment


          • #6
            Please read the FAQ, particularly the topic about sharing data/command/output.

            Please read #4 and act accordingly.

            That being said, if I understand correctly your query, you may use - mixed - for that matter.
            Last edited by Marcos Almeida; 20 Dec 2018, 02:34.
            Best regards,

            Marcos

            Comment


            • #7
              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input int year byte id float weekly_income
              1998 1  702
              1999 1  754
              2000 1  736
              2001 1  786
              2002 1  846
              2003 1  899
              2004 1  925
              2005 1 1074
              2006 1 1055
              2007 1 1175
              2008 1 1030
              2009 1 1144
              2010 1 1186
              2011 1 1205
              2012 1 1116
              2013 1 1231
              2014 1 1288
              1998 2 1066
              1999 2 1074
              2000 2 1097
              2001 2 1162
              2002 2 1206
              2003 2 1264
              2004 2 1362
              2005 2 1365
              2006 2 1344
              2007 2 1426
              2008 2 1517
              2009 2 1468
              2010 2 1521
              2011 2 1601
              2012 2 1658
              2013 2 1664
              2014 2 1767
              1998 3  864
              1999 3  839
              2000 3  892
              2001 3  921
              2002 3  999
              2003 3 1017
              2004 3 1081
              2005 3 1141
              2006 3 1192
              2007 3 1301
              2008 3 1314
              2009 3 1343
              2010 3 1309
              2011 3 1372
              2012 3 1502
              2013 3 1432
              2014 3 1418
              1998 4  841
              1999 4  865
              2000 4  815
              2001 4  899
              2002 4  925
              2003 4 1001
              2004 4 1001
              2005 4 1025
              2006 4 1107
              2007 4 1243
              2008 4 1245
              2009 4 1276
              2010 4 1312
              2011 4 1293
              2012 4 1342
              2013 4 1389
              2014 4 1420
              1998 5  784
              1999 5  846
              2000 5  856
              2001 5  879
              2002 5  902
              2003 5  975
              2004 5  972
              2005 5 1016
              2006 5 1129
              2007 5 1168
              2008 5 1237
              2009 5 1267
              2010 5 1251
              2011 5 1227
              2012 5 1313
              2013 5 1266
              2014 5 1325
              end
              format %ty year
              format weekly_income %9.0fc
              label var weekly_income "Average Income from All Sources collected ($ weekly)"
              Code:
              xtset id year, yearly
              
              . desc
              
              Contains data
                obs:           170                          
               vars:             3                          
               size:         1,190                          
              ---------------------------------------------------------------------------------------------------------------------------------------------
                            storage   display    value
              variable name   type    format     label      variable label
              ---------------------------------------------------------------------------------------------------------------------------------------------
              year            int     %ty                  
              id              byte    %8.0g                
              weekly_income   float   %9.0fc                Average Income from All Sources collected ($ weekly)
              ---------------------------------------------------------------------------------------------------------------------------------------------
              
              
              
              . xtsum
              
              Variable         |      Mean   Std. Dev.       Min        Max |    Observations
              -----------------+--------------------------------------------+----------------
              year     overall |      2006   4.928054       1998       2014 |     N =      85
                       between |                    0       2006       2006 |     n =       5
                       within  |             4.928054       1998       2014 |     T =      17
                               |                                            |
              id       overall |         3   1.422607          1          5 |     N =      85
                       between |             1.581139          1          5 |     n =       5
                       within  |                    0          3          3 |     T =      17
                               |                                            |
              weekly~e overall |  1153.682   238.3902        702       1767 |     N =      85
                       between |             142.8004   1008.941       1386 |     n =       5
                       within  |             200.8037   819.9176   1534.682 |     T =      17
              
              
              . summ
              
                  Variable |        Obs        Mean    Std. Dev.       Min        Max
              -------------+---------------------------------------------------------
                      year |         85        2006    4.928054       1998       2014
                        id |         85           3    1.422607          1          5
              weekly_inc~e |         85    1153.682    238.3902        702       1767
              
              
              
              . tabstat weekly_income, by(id) stats(n mean median min max sd var)
              
              Summary for variables: weekly_income
                   by categories of: id
              
                    id |         N      mean       p50       min       max        sd  variance
              ---------+----------------------------------------------------------------------
                     1 |        17  1008.941      1055       702      1288  192.0513  36883.68
                     2 |        17      1386      1365      1066      1767  219.3556  48116.88
                     3 |        17  1172.765      1192       839      1502  219.7401  48285.69
                     4 |        17  1117.588      1107       815      1420   208.222  43356.38
                     5 |        17  1083.118      1129       784      1325  187.2124  35048.49
              ---------+----------------------------------------------------------------------
                 Total |        85  1153.682      1168       702      1767  238.3902  56829.89
              --------------------------------------------------------------------------------

              Comment


              • #8
                Thanks David for this wonderful response. Cheers.

                Comment

                Working...
                X