Announcement

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

  • How to T-Test means of the same variables at different points in time?

    Hello,

    I am looking to perform a t-test of the means of different variables between 2013-2014 and 2015-2016. I realize the command would be something of the sort of ttest var1 == var2 but as they are the same variable, I am unsure if this command performs suitably when evaluating different periods of time.

  • #2
    You need to explain the structure of your data better in order to get an answer to this question.

    If the observations in 2015-2016 are made on the same entities (people, firms, whatever your unit of observation is) as the ones in 2013-2014, then you do in fact have matched pair data. As it sounds like they are in long form, you would need to -reshape- them into wide layout and then you would have two separate variables, and you could use the -ttest- syntax you described. Alternatively, you can get the same result in long layout by running -regress var1 i.era i.id-, where era is a variable that is coded 1 for observations made in 2015-2016 and 0 for those from 2013-2014, and id is a variable that identifies distinct people (or firms, or whatever they are).

    If however, the observations in 2015-2016 are made on different entities from the ones in 2013-2014, then you do not have matched pair data and the syntax you proposed is not appropriate. Instead you should leave the data in long layout and just do -ttest var1, by(era)-.

    If you want more concrete advice, post back, and include some example data, using the -dataex- command to do so. 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.



    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Hello, below you'll find part of the sample data. In reference to your answer it appears to be matched pair data as they are made of the same entities (different towns) across time and the same variables. The problem I see is grouping these years between the time periods required (2013 & 2014 and 2015 & 2016) and, afterwards, performing the ttest.

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float ano double codmpio float tasa_homi int(terrorismot secuestros reten_ileg)
      2013 5002 25.279123  0 0 0
      2014 5002 16.852749  2 0 0
      2015 5002 21.065937  0 0 0
      2016 5002  29.49231  0 0 0
      2013 5021 21.132713  0 0 0
      2014 5021         0  0 0 0
      2015 5021 21.132713  0 0 0
      2016 5021 21.132713  0 0 0
      2013 5030 110.72235  0 0 0
      2014 5030  48.71783  0 0 0
      2015 5030  39.86005  0 0 0
      2016 5030  39.86005  0 0 0
      2013 5031  81.32442  1 2 0
      2014 5031 121.98664  0 0 0
      2015 5031  98.75109  0 0 0
      2016 5031  40.66221  0 0 0
      2013 5034  84.53085  2 0 0
      2014 5034  87.25765  0 0 0
      2015 5034 68.170044  0 0 0
      2016 5034  62.71644  0 0 0
      2013 5036 123.41326  0 0 0
      2014 5036   52.8914  0 0 0
      2015 5036  70.52186  0 0 0
      2016 5036  88.15233  0 0 0
      2013 5040  111.3109 13 0 2
      2014 5040 196.93466 11 0 3
      2015 5040  188.3723  3 0 0
      2016 5040  85.62377  1 0 0
      2013 5042  66.46557  1 0 0
      2014 5042  61.35283  0 1 0
      2015 5042  81.80377  0 0 0
      2016 5042 122.70566  0 0 0
      2013 5044  61.07803  0 0 0
      2014 5044         0  0 0 0
      2015 5044 15.269506  0 0 0
      2016 5044 106.88655  0 0 0
      2013 5045  53.26153  0 2 0
      2014 5045  60.65896  1 0 0
      2015 5045  39.94615  1 0 0
      2016 5045  54.74102  0 0 0
      2013 5059  65.30612  0 0 0
      2014 5059  16.32653  0 0 0
      2015 5059  48.97959  0 0 0
      2016 5059  97.95918  0 0 0
      2013 5079  163.3217  0 2 0
      2014 5079  153.8993  0 0 0
      2015 5079 116.20968  2 1 0
      2016 5079 37.689625  0 0 0
      2013 5091  9.436633  0 1 0
      2014 5091 122.67623  0 0 0
      2015 5091 160.42276  0 0 0
      2016 5091  141.5495  0 0 0
      2013 5093  62.22345  0 0 0
      2014 5093   41.4823  0 1 0
      2015 5093  76.05089  0 0 0
      2016 5093  69.13717  1 0 0
      2013 5101  81.24077  0 0 0
      2014 5101  81.24077  0 1 0
      2015 5101 151.40324  0 0 0
      2016 5101  99.70458  1 0 0
      2013 5113  89.55224  2 0 0
      2014 5113 104.47762  2 0 0
      2015 5113  59.70149  0 0 0
      2016 5113  74.62687  1 0 0
      2013 5125 31.897926  0 0 0
      2014 5125 15.948963  0 0 0
      2015 5125 31.897926  0 0 0
      2016 5125  63.79585  0 0 0
      2013 5129  68.30601  1 0 0
      2014 5129  39.03201  0 0 0
      2015 5129  40.98361  0 1 0
      2016 5129 33.177204  0 1 0
      2013 5134  42.15852  2 0 0
      2014 5134  52.69815  2 0 1
      2015 5134  21.07926  2 0 1
      2016 5134  10.53963  1 0 0
      2013 5138 15.531166  0 0 0
      2014 5138  10.35411  0 0 0
      2015 5138 15.531166  0 0 0
      2016 5138 25.885277  0 0 0
      2013 5145  41.68404  0 0 0
      2014 5145 13.894678  0 0 0
      2015 5145 27.789356  0 0 0
      2016 5145         0  0 0 0
      2013 5148  55.30973  0 1 0
      2014 5148 32.021423  1 0 0
      2015 5148 32.021423  1 0 0
      2016 5148 29.110386  0 0 0
      2013 5150  49.12798  0 0 0
      2014 5150  24.56399  0 0 0
      2015 5150  24.56399  0 0 0
      2016 5150  49.12798  0 0 0
      2013 5172  64.66632  0 0 0
      2014 5172  51.73306  0 1 0
      2015 5172  36.21314  0 0 0
      2016 5172  69.83963  0 0 0
      2013 5190  63.85017  0 0 0
      2014 5190  95.77525  0 0 0
      2015 5190  95.77525  0 0 0
      2016 5190  63.85017  0 0 0
      end
      format %ty ano
      label values ano resum

      Comment


      • #4
        Whatever you do, watch out that P-values are based on ideas of independence that could be dubious at best for data in time.

        Comment


        • #5
          Originally posted by JuanA Samper View Post
          The problem I see is grouping these years between the time periods required (2013 & 2014 and 2015 & 2016) and, afterwards, performing the ttest.
          Something like the following will do what you want to do.

          .ÿ
          .ÿversionÿ15.1

          .ÿ
          .ÿclearÿ*

          .ÿ
          .ÿquietlyÿinputÿintÿanoÿintÿcodmpioÿdoubleÿtasa_homiÿbyte(terrorismotÿsecuestrosÿreten_ileg)

          .ÿ
          .ÿ//ÿFirst
          .ÿregressÿtasa_homiÿibn.anoÿi.codmpio,ÿnoconstant

          ÿÿÿÿÿÿSourceÿ|ÿÿÿÿÿÿÿSSÿÿÿÿÿÿÿÿÿÿÿdfÿÿÿÿÿÿÿMSÿÿÿÿÿÿNumberÿofÿobsÿÿÿ=ÿÿÿÿÿÿÿ100
          -------------+----------------------------------ÿÿÿF(28,ÿ72)ÿÿÿÿÿÿÿ=ÿÿÿÿÿ18.91
          ÿÿÿÿÿÿÿModelÿ|ÿÿ499811.643ÿÿÿÿÿÿÿÿ28ÿÿ17850.4158ÿÿÿProbÿ>ÿFÿÿÿÿÿÿÿÿ=ÿÿÿÿ0.0000
          ÿÿÿÿResidualÿ|ÿÿ67952.2365ÿÿÿÿÿÿÿÿ72ÿÿ943.781062ÿÿÿR-squaredÿÿÿÿÿÿÿ=ÿÿÿÿ0.8803
          -------------+----------------------------------ÿÿÿAdjÿR-squaredÿÿÿ=ÿÿÿÿ0.8338
          ÿÿÿÿÿÿÿTotalÿ|ÿÿ567763.879ÿÿÿÿÿÿÿ100ÿÿ5677.63879ÿÿÿRootÿMSEÿÿÿÿÿÿÿÿ=ÿÿÿÿ30.721

          ------------------------------------------------------------------------------
          ÿÿÿtasa_homiÿ|ÿÿÿÿÿÿCoef.ÿÿÿStd.ÿErr.ÿÿÿÿÿÿtÿÿÿÿP>|t|ÿÿÿÿÿ[95%ÿConf.ÿInterval]
          -------------+----------------------------------------------------------------
          ÿÿÿÿÿÿÿÿÿanoÿ|
          ÿÿÿÿÿÿÿ2013ÿÿ|ÿÿÿ26.39388ÿÿÿ16.25604ÿÿÿÿÿ1.62ÿÿÿ0.109ÿÿÿÿ-6.011934ÿÿÿÿÿ58.7997
          ÿÿÿÿÿÿÿ2014ÿÿ|ÿÿÿÿ20.8197ÿÿÿ16.25604ÿÿÿÿÿ1.28ÿÿÿ0.204ÿÿÿÿ-11.58611ÿÿÿÿ53.22552
          ÿÿÿÿÿÿÿ2015ÿÿ|ÿÿÿ24.04927ÿÿÿ16.25604ÿÿÿÿÿ1.48ÿÿÿ0.143ÿÿÿÿ-8.356547ÿÿÿÿ56.45508
          ÿÿÿÿÿÿÿ2016ÿÿ|ÿÿÿ21.42726ÿÿÿ16.25604ÿÿÿÿÿ1.32ÿÿÿ0.192ÿÿÿÿ-10.97855ÿÿÿÿ53.83308
          ÿÿÿÿÿÿÿÿÿÿÿÿÿ|
          ÿÿÿÿÿcodmpioÿ|
          ÿÿÿÿÿÿÿ5021ÿÿ|ÿÿ-7.322995ÿÿÿ21.72304ÿÿÿÿ-0.34ÿÿÿ0.737ÿÿÿÿ-50.62709ÿÿÿÿÿ35.9811
          ÿÿÿÿÿÿÿ5030ÿÿ|ÿÿÿ36.61754ÿÿÿ21.72304ÿÿÿÿÿ1.69ÿÿÿ0.096ÿÿÿÿ-6.686551ÿÿÿÿ79.92163
          ÿÿÿÿÿÿÿ5031ÿÿ|ÿÿÿ62.50856ÿÿÿ21.72304ÿÿÿÿÿ2.88ÿÿÿ0.005ÿÿÿÿÿ19.20447ÿÿÿÿ105.8127
          ÿÿÿÿÿÿÿ5034ÿÿ|ÿÿÿ52.49622ÿÿÿ21.72304ÿÿÿÿÿ2.42ÿÿÿ0.018ÿÿÿÿÿ9.192125ÿÿÿÿ95.80031
          ÿÿÿÿÿÿÿ5036ÿÿ|ÿÿÿ60.57218ÿÿÿ21.72304ÿÿÿÿÿ2.79ÿÿÿ0.007ÿÿÿÿÿ17.26809ÿÿÿÿ103.8763
          ÿÿÿÿÿÿÿ5040ÿÿ|ÿÿÿ122.3879ÿÿÿ21.72304ÿÿÿÿÿ5.63ÿÿÿ0.000ÿÿÿÿÿ79.08379ÿÿÿÿÿ165.692
          ÿÿÿÿÿÿÿ5042ÿÿ|ÿÿÿ59.90943ÿÿÿ21.72304ÿÿÿÿÿ2.76ÿÿÿ0.007ÿÿÿÿÿ16.60534ÿÿÿÿ103.2135
          ÿÿÿÿÿÿÿ5044ÿÿ|ÿÿÿ22.63599ÿÿÿ21.72304ÿÿÿÿÿ1.04ÿÿÿ0.301ÿÿÿÿÿ-20.6681ÿÿÿÿ65.94008
          ÿÿÿÿÿÿÿ5045ÿÿ|ÿÿÿ28.97939ÿÿÿ21.72304ÿÿÿÿÿ1.33ÿÿÿ0.186ÿÿÿÿ-14.32471ÿÿÿÿ72.28348
          ÿÿÿÿÿÿÿ5059ÿÿ|ÿÿÿ33.97033ÿÿÿ21.72304ÿÿÿÿÿ1.56ÿÿÿ0.122ÿÿÿÿ-9.333766ÿÿÿÿ77.27442
          ÿÿÿÿÿÿÿ5079ÿÿ|ÿÿÿ94.60755ÿÿÿ21.72304ÿÿÿÿÿ4.36ÿÿÿ0.000ÿÿÿÿÿ51.30345ÿÿÿÿ137.9116
          ÿÿÿÿÿÿÿ5091ÿÿ|ÿÿÿ85.34875ÿÿÿ21.72304ÿÿÿÿÿ3.93ÿÿÿ0.000ÿÿÿÿÿ42.04466ÿÿÿÿ128.6528
          ÿÿÿÿÿÿÿ5093ÿÿ|ÿÿÿ39.05092ÿÿÿ21.72304ÿÿÿÿÿ1.80ÿÿÿ0.076ÿÿÿÿ-4.253169ÿÿÿÿ82.35501
          ÿÿÿÿÿÿÿ5101ÿÿ|ÿÿÿ80.22481ÿÿÿ21.72304ÿÿÿÿÿ3.69ÿÿÿ0.000ÿÿÿÿÿ36.92072ÿÿÿÿ123.5289
          ÿÿÿÿÿÿÿ5113ÿÿ|ÿÿÿ58.91703ÿÿÿ21.72304ÿÿÿÿÿ2.71ÿÿÿ0.008ÿÿÿÿÿ15.61293ÿÿÿÿ102.2211
          ÿÿÿÿÿÿÿ5125ÿÿ|ÿÿÿ12.71264ÿÿÿ21.72304ÿÿÿÿÿ0.59ÿÿÿ0.560ÿÿÿÿ-30.59146ÿÿÿÿ56.01673
          ÿÿÿÿÿÿÿ5129ÿÿ|ÿÿÿ22.20218ÿÿÿ21.72304ÿÿÿÿÿ1.02ÿÿÿ0.310ÿÿÿÿ-21.10191ÿÿÿÿ65.50627
          ÿÿÿÿÿÿÿ5134ÿÿ|ÿÿÿÿ8.44636ÿÿÿ21.72304ÿÿÿÿÿ0.39ÿÿÿ0.699ÿÿÿÿ-34.85773ÿÿÿÿ51.75045
          ÿÿÿÿÿÿÿ5138ÿÿ|ÿÿÿÿ-6.3471ÿÿÿ21.72304ÿÿÿÿ-0.29ÿÿÿ0.771ÿÿÿÿ-49.65119ÿÿÿÿ36.95699
          ÿÿÿÿÿÿÿ5145ÿÿ|ÿÿ-2.330511ÿÿÿ21.72304ÿÿÿÿ-0.11ÿÿÿ0.915ÿÿÿÿÿ-45.6346ÿÿÿÿ40.97358
          ÿÿÿÿÿÿÿ5148ÿÿ|ÿÿÿ13.94321ÿÿÿ21.72304ÿÿÿÿÿ0.64ÿÿÿ0.523ÿÿÿÿ-29.36088ÿÿÿÿÿ57.2473
          ÿÿÿÿÿÿÿ5150ÿÿ|ÿÿÿ13.67346ÿÿÿ21.72304ÿÿÿÿÿ0.63ÿÿÿ0.531ÿÿÿÿ-29.63064ÿÿÿÿ56.97755
          ÿÿÿÿÿÿÿ5172ÿÿ|ÿÿÿ32.44051ÿÿÿ21.72304ÿÿÿÿÿ1.49ÿÿÿ0.140ÿÿÿÿ-10.86358ÿÿÿÿÿ75.7446
          ÿÿÿÿÿÿÿ5190ÿÿ|ÿÿÿ56.64018ÿÿÿ21.72304ÿÿÿÿÿ2.61ÿÿÿ0.011ÿÿÿÿÿ13.33609ÿÿÿÿ99.94427
          ------------------------------------------------------------------------------

          .ÿ
          .ÿ//ÿandÿthenÿeither
          .ÿtestÿ2013.anoÿ+ÿ2014.anoÿ=ÿ2015.anoÿ+ÿ2016.ano

          ÿ(ÿ1)ÿÿ2013bn.anoÿ+ÿ2014.anoÿ-ÿ2015.anoÿ-ÿ2016.anoÿ=ÿ0

          ÿÿÿÿÿÿÿF(ÿÿ1,ÿÿÿÿ72)ÿ=ÿÿÿÿ0.02
          ÿÿÿÿÿÿÿÿÿÿÿÿProbÿ>ÿFÿ=ÿÿÿÿ0.8880

          .ÿ
          .ÿ//ÿor
          .ÿcontrastÿ{anoÿ1ÿ1ÿ-1ÿ-1}

          Contrastsÿofÿmarginalÿlinearÿpredictions

          Marginsÿÿÿÿÿÿ:ÿasbalanced

          ------------------------------------------------
          ÿÿÿÿÿÿÿÿÿÿÿÿÿ|ÿÿÿÿÿÿÿÿÿdfÿÿÿÿÿÿÿÿÿÿÿFÿÿÿÿÿÿÿÿP>F
          -------------+----------------------------------
          ÿÿÿÿÿÿÿÿÿanoÿ|ÿÿÿÿÿÿÿÿÿÿ1ÿÿÿÿÿÿÿÿ0.02ÿÿÿÿÿ0.8880
          ÿÿÿÿÿÿÿÿÿÿÿÿÿ|
          ÿDenominatorÿ|ÿÿÿÿÿÿÿÿÿ72
          ------------------------------------------------

          --------------------------------------------------------------
          ÿÿÿÿÿÿÿÿÿÿÿÿÿ|ÿÿÿContrastÿÿÿStd.ÿErr.ÿÿÿÿÿ[95%ÿConf.ÿInterval]
          -------------+------------------------------------------------
          ÿÿÿÿÿÿÿÿÿanoÿ|
          ÿÿÿÿÿÿÿÿ(1)ÿÿ|ÿÿÿ1.737053ÿÿÿ12.28841ÿÿÿÿÿ-22.75944ÿÿÿÿ26.23355
          --------------------------------------------------------------

          .ÿ
          .ÿexit

          endÿofÿdo-file


          .


          Be sure to look into those occasional zeroes in the outcome variable. If you plan to do something similar with any of the rightmost three variables, then you'll need to consider something different from a linear model.

          Comment


          • #6
            If you want to be more formal, then you could go this route, too, although it gives the same answer.
            Code:
            xtset codmpio
            xtreg tasa_homi i.ano, fe
            lincom (_b[_cons] + _b[_cons] + _b[2014.ano]) - (_b[_cons] + _b[2015.ano] + _b[_cons] + _b[2016.ano])

            Comment

            Working...
            X