-------------
      name:  <unnamed>
       log:  C:\Users\jowo\OneDrive - Folkehelseinstituttet\Documents\Teaching\23 Stata Course\Session 8_FE\Stata\8-fixed-effects_real-world-example.smcl
  log type:  smcl
 opened on:  16 Apr 2023, 12:44:59

.
.
.
.
.
.
.
. *---------------------- xtsetting and describing the data-----------------------
.
. // xtset
. xtset id wave

 Panel variable: id (unbalanced)


Time variable: wave, 1   to 9  , but with gaps


         Delta: 1 unit

.
. // Describe data
. xtdescribe, patterns(1000)

      id:  174000, 309000, ..., 7.497e+08                    n =       6201
    wave:  1, 2, ..., 9                                      T =          9
           Delta(wave) = 1 unit
           Span(wave)  = 9 periods
           (id*wave uniquely identifies each observation)

Distribution of T_i:   min      5%     25%       50%       75%     95%     max
                         1       1       2         5         9       9       9

     Freq.  Percent    Cum. |  Pattern
 ---------------------------+-----------
     1783     28.75   28.75 |  111111111
     1422     22.93   51.69 |  1........
      683     11.01   62.70 |  11.......
      421      6.79   69.49 |  111......
      326      5.26   74.75 |  1111.....
      251      4.05   78.79 |  11111....
      220      3.55   82.34 |  111111...
      156      2.52   84.86 |  1111111..
      149      2.40   87.26 |  11111111.
       68      1.10   88.36 |  1.1111111
       61      0.98   89.34 |  1.1......
       59      0.95   90.29 |  1111111.1
       54      0.87   91.16 |  11.111111
       54      0.87   92.03 |  111111.11
       51      0.82   92.86 |  1111.1111
       49      0.79   93.65 |  111.11111
       42      0.68   94.32 |  11111.111
       32      0.52   94.84 |  1.11.....
       32      0.52   95.36 |  11.1.....
       28      0.45   95.81 |  111.1....
       21      0.34   96.15 |  11.11....
       19      0.31   96.45 |  1.111....
       17      0.27   96.73 |  1111.1...
       16      0.26   96.98 |  111111.1.
       15      0.24   97.23 |  111.11...
       13      0.21   97.44 |  1.111111.
       11      0.18   97.61 |  1.11111..
       11      0.18   97.79 |  11111.1..
       10      0.16   97.95 |  111.111..
        9      0.15   98.10 |  1.1111...
        9      0.15   98.24 |  1111.11..
        9      0.15   98.39 |  11111.11.
        8      0.13   98.52 |  11.111...
        7      0.11   98.63 |  11.1111..
        7      0.11   98.74 |  1111.111.
        6      0.10   98.84 |  1.1.11111
        6      0.10   98.94 |  11.11111.
        5      0.08   99.02 |  1.11.1...
        5      0.08   99.10 |  111.1.111
        5      0.08   99.18 |  1111.11.1
        4      0.06   99.24 |  1.1111.11
        4      0.06   99.31 |  111.11.11
        4      0.06   99.37 |  11111.1.1
        3      0.05   99.42 |  11.11.111
        3      0.05   99.47 |  1111.1.11
        2      0.03   99.50 |  1.1.1....
        2      0.03   99.53 |  11.1.1111
        2      0.03   99.56 |  11.111.11
        2      0.03   99.60 |  111.1.11.
        2      0.03   99.63 |  111.1111.
        2      0.03   99.66 |  1111.1.1.
        1      0.02   99.68 |  1.1.1.1..
        1      0.02   99.69 |  1.1.1.1.1
        1      0.02   99.71 |  1.1.1.11.
        1      0.02   99.73 |  1.1.11...
        1      0.02   99.74 |  1.1.11.1.
        1      0.02   99.76 |  1.1.11.11
        1      0.02   99.77 |  1.1.111.1
        1      0.02   99.79 |  1.1.1111.
        1      0.02   99.81 |  1.11.1.1.
        1      0.02   99.82 |  1.11.111.
        1      0.02   99.84 |  1.111.1..
        1      0.02   99.85 |  1.111.111
        1      0.02   99.87 |  1.11111.1
        1      0.02   99.89 |  11.1.11..
        1      0.02   99.90 |  11.1.11.1
        1      0.02   99.92 |  11.11.1..
        1      0.02   99.94 |  11.11.1.1
        1      0.02   99.95 |  11.111.1.
        1      0.02   99.97 |  11.1111.1
        1      0.02   99.98 |  111.1.1..
        1      0.02  100.00 |  111.111.1
 ---------------------------+-----------
     6201    100.00         |  XXXXXXXXX

.
. // Transitions in marital status between waves
. tab marstat, m

                         Marital status |      Freq.     Percent        Cum.
----------------------------------------+-----------------------------------
                        1 Never married |     19,169       61.44       61.44
                  2 Married/civil union |     10,374       33.25       94.69
       3 Divorced/dissolved civil union |      1,477        4.73       99.42
4 Widowed/surviving partner in civil un |         53        0.17       99.59
                                      . |        128        0.41      100.00
----------------------------------------+-----------------------------------
                                  Total |     31,201      100.00

. xttab marstat                                                                                                                           // regular tabulation to understand coding/labels

                  Overall             Between            Within
  marstat |    Freq.  Percent      Freq.  Percent        Percent
----------+-----------------------------------------------------
 1 Never  |   19169     61.69      4145     66.91          94.25
 2 Marrie |   10374     33.39      2305     37.21          87.10
 3 Divorc |    1477      4.75       384      6.20          71.05
 4 Widowe |      53      0.17        13      0.21          60.58
----------+-----------------------------------------------------
    Total |   31073    100.00      6847    110.52          90.48
                              (n = 6195)

. xttrans marstat, freq

   Marital |               Marital status
    status |         1          2          3          4 |     Total
-----------+--------------------------------------------+----------
         1 |    15,024        449          6          0 |    15,479
           |     97.06       2.90       0.04       0.00 |    100.00
-----------+--------------------------------------------+----------
         2 |         0      8,017        130          9 |     8,156
           |      0.00      98.30       1.59       0.11 |    100.00
-----------+--------------------------------------------+----------
         3 |         0         77      1,089          0 |     1,166
           |      0.00       6.60      93.40       0.00 |    100.00
-----------+--------------------------------------------+----------
         4 |         0          1          0         40 |        41
           |      0.00       2.44       0.00      97.56 |    100.00
-----------+--------------------------------------------+----------
     Total |    15,024      8,544      1,225         49 |    24,842
           |     60.48      34.39       4.93       0.20 |    100.00

.
. // Variation in happiness between waves
. xtsum happy

Variable         |      Mean   Std. dev.       Min        Max |    Observations
-----------------+--------------------------------------------+----------------
happy    overall |  7.590432   1.668981          0         10 |     N =   31167
         between |             1.413803          0         10 |     n =    6198
         within  |             1.100338  -.7845678    13.2571 | T-bar = 5.02856

.
.
.
.
.
.
. *-------------------------- Sample selection #1 --------------------------------
. *-------------------------- Other statuses (divorce) ---------------------------
.
. // #1
. // Keep only persons who are "never married" at the beginning
. gen help = 0

. replace help = 1 if wave == 1 & marstat == 1                                                                    // "help" tags observations "never married" in first wave
(4,145 real changes made)

. bysort id : egen single_w1 = max(help)                                                                                  // "single_w1" tags all obs of persons "never married" in first wave

. br id wave marstat help single_w1

.
. keep if single_w1 == 1                                                                                                                  // Drop all observations from persons not single in first wave
(10,182 observations deleted)

. drop help single_w1                                                                                                                             // Drop auxiliary variables

.
. xttrans marstat, freq                                                                                                                   // Inspect pattern of transition in marital status

   Marital |          Marital status
    status |         1          2          3 |     Total
-----------+---------------------------------+----------
         1 |    15,024        449          6 |    15,479
           |     97.06       2.90       0.04 |    100.00
-----------+---------------------------------+----------
         2 |         0      1,286         32 |     1,318
           |      0.00      97.57       2.43 |    100.00
-----------+---------------------------------+----------
         3 |         0         13         56 |        69
           |      0.00      18.84      81.16 |    100.00
-----------+---------------------------------+----------
     Total |    15,024      1,748         94 |    16,866
           |     89.08      10.36       0.56 |    100.00

. tab wave marstat, m

Erhebungsj |               Marital status
       ahr | 1 Never m  2 Married  3 Divorce          . |     Total
-----------+--------------------------------------------+----------
 1 2008/09 |     4,145          0          0          0 |     4,145
 2 2009/10 |     2,989         90          2          2 |     3,083
 3 2010/11 |     2,576        142          4          1 |     2,723
 4 2011/12 |     2,154        181         11          1 |     2,347
 5 2012/13 |     1,862        225         14          0 |     2,101
 6 2013/14 |     1,625        249         16          0 |     1,890
 7 2014/15 |     1,415        266         13          0 |     1,694
 8 2015/16 |     1,249        291         18          2 |     1,560
 9 2016/17 |     1,154        304         16          2 |     1,476
-----------+--------------------------------------------+----------
     Total |    19,169      1,748         94          8 |    21,019

.
.
.
. // #2
. // Drop observations as soon as a person is divorced
.
. // #2.1
. // Find first wave when person is divorced
. gen help = 0

. replace help = 1 if marstat == 3                                                                                                // "help" tags observations with marstat = divorced
(94 real changes made)

. bysort id : egen help2 = min(wave) if help == 1                                                                 // find first year ("smallest wave") in divorced status for each person
(20,925 missing values generated)

.
. // #2.2
. // Copy first wave of divorce to all rows (within a person)
. bysort id : egen wave_divorced = max(help2)
(20,737 missing values generated)

. br id wave marstat help help2 wave_divorced

.
. // #2.3
. // Drop the wave of first divorce, and waves after divorce
. drop if wave >= wave_divorce
(130 observations deleted)

. drop help help2 wave_divorce

.
. // #2.4
. // Check marital status and patterns of missingness
. tab marstat, m

                         Marital status |      Freq.     Percent        Cum.
----------------------------------------+-----------------------------------
                        1 Never married |     19,169       91.77       91.77
                  2 Married/civil union |      1,712        8.20       99.96
                                      . |          8        0.04      100.00
----------------------------------------+-----------------------------------
                                  Total |     20,889      100.00

. xtdescribe, patterns(1000)                                                                                                              // Think about whether gaps are a problem or not

      id:  174000, 423000, ..., 7.497e+08                    n =       4145
    wave:  1, 2, ..., 9                                      T =          9
           Delta(wave) = 1 unit
           Span(wave)  = 9 periods
           (id*wave uniquely identifies each observation)

Distribution of T_i:   min      5%     25%       50%       75%     95%     max
                         1       1       2         5         9       9       9

     Freq.  Percent    Cum. |  Pattern
 ---------------------------+-----------
     1147     27.67   27.67 |  111111111
      897     21.64   49.31 |  1........
      450     10.86   60.17 |  11.......
      313      7.55   67.72 |  111......
      249      6.01   73.73 |  1111.....
      175      4.22   77.95 |  11111....
      153      3.69   81.64 |  111111...
      109      2.63   84.27 |  1111111..
      101      2.44   86.71 |  11111111.
       48      1.16   87.86 |  1111111.1
       43      1.04   88.90 |  1.1......
       42      1.01   89.92 |  111111.11
       40      0.97   90.88 |  1.1111111
       38      0.92   91.80 |  111.11111
       38      0.92   92.71 |  1111.1111
       32      0.77   93.49 |  11111.111
       30      0.72   94.21 |  11.111111
       23      0.55   94.76 |  1.11.....
       23      0.55   95.32 |  111.1....
       15      0.36   95.68 |  11.1.....
       15      0.36   96.04 |  11.11....
       12      0.29   96.33 |  1.111....
       11      0.27   96.60 |  1.111111.
       11      0.27   96.86 |  111.11...
       10      0.24   97.10 |  111111.1.
        9      0.22   97.32 |  1.11111..
        9      0.22   97.54 |  1111.1...
        8      0.19   97.73 |  111.111..
        7      0.17   97.90 |  11111.1..
        6      0.14   98.05 |  1.1111...
        6      0.14   98.19 |  1111.111.
        5      0.12   98.31 |  1.1.11111
        5      0.12   98.43 |  1111.11..
        5      0.12   98.55 |  1111.11.1
        5      0.12   98.67 |  11111.11.
        4      0.10   98.77 |  11.111...
        4      0.10   98.87 |  11.1111..
        4      0.10   98.96 |  111.1.111
        4      0.10   99.06 |  111.11.11
        3      0.07   99.13 |  1.11.1...
        2      0.05   99.18 |  1.1.1....
        2      0.05   99.23 |  1.1111.11
        2      0.05   99.28 |  11.11.111
        2      0.05   99.32 |  11.111.11
        2      0.05   99.37 |  11.11111.
        2      0.05   99.42 |  111.1.11.
        2      0.05   99.47 |  1111.1.1.
        2      0.05   99.52 |  1111.1.11
        2      0.05   99.57 |  11111.1.1
        1      0.02   99.59 |  1.1.1.1..
        1      0.02   99.61 |  1.1.1.1.1
        1      0.02   99.64 |  1.1.1.11.
        1      0.02   99.66 |  1.1.11...
        1      0.02   99.69 |  1.1.11.1.
        1      0.02   99.71 |  1.1.111.1
        1      0.02   99.73 |  1.1.1111.
        1      0.02   99.76 |  1.11.1.1.
        1      0.02   99.78 |  1.11.111.
        1      0.02   99.81 |  1.111.1..
        1      0.02   99.83 |  1.111.111
        1      0.02   99.86 |  11.1.11.1
        1      0.02   99.88 |  11.1.1111
        1      0.02   99.90 |  11.11.1.1
        1      0.02   99.93 |  11.111.1.
        1      0.02   99.95 |  11.1111.1
        1      0.02   99.98 |  111.1.1..
        1      0.02  100.00 |  111.1111.
 ---------------------------+-----------
     4145    100.00         |  XXXXXXXXX

.
.
.
.
.
.
.
. *-------------------------- Sample selection #2 --------------------------------
. *-------------------------- Missing on any relevant variable -------------------
.
. // Drop if missing on any variable used in the analyses below
. drop if missing(male) | missing(marstat) | missing(happy) | missing(isced) | missing(birthyear)
(33 observations deleted)

.
.
.
.
.
.
. *-------------------------- Sample selection #3 --------------------------------
. *-------------------------- Only one observation -------------------------------
.
.
. // Dummy for single/married                                                                                                             // Dummy for marriage vs. never married:
. recode marstat (1=0) (2=1), gen(married)                                                                                // Not specific to this stage of sample selection...
(20856 differences between marstat and married)

. label define marriedlbl 0 "never married" 1 "married"                                                   // ... but helpful when running the model

. label value married marriedlbl

. label variable married "Married"

. tab marstat married, m

                      |        Married
       Marital status | never mar    married |     Total
----------------------+----------------------+----------
      1 Never married |    19,146          0 |    19,146
2 Married/civil union |         0      1,710 |     1,710
----------------------+----------------------+----------
                Total |    19,146      1,710 |    20,856

.
.
. // Exclude persons with only one observation from the model
. xtreg happy married i.wave, vce(cluster id)                                                                             // Run the model you intend to run

Random-effects GLS regression                   Number of obs     =     20,856
Group variable: id                              Number of groups  =      4,138

R-squared:                                      Obs per group:
     Within  = 0.0042                                         min =          1
     Between = 0.0015                                         avg =        5.0
     Overall = 0.0045                                         max =          9

                                                Wald chi2(9)      =      69.73
corr(u_i, X) = 0 (assumed)                      Prob > chi2       =     0.0000

(Std. err. adjusted for 4,138 clusters in id)
-------------+----------------------------------------------------------------
             |               Robust
        happy | Coefficient   std. err.       z    P>|z|      [95% con f. interval]
-------------+----------------------------------------------------------------
     married |    .1718167     .052871      3.25    0.001      .0681916     .2754419
             |
        wave |
  2 2009/10  |    .1385083    .0298007      4.65    0.000      .0800999     .1969166
  3 2010/11  |    .0008393    .0325593      0.03    0.979     -.0629758     .0646545
  4 2011/12  |   -.0612015     .036915     -1.66    0.097     -.1335536     .0111507
  5 2012/13  |   -.0858744    .0368382     -2.33    0.020      -.158076    -.0136729
  6 2013/14  |   -.0767682    .0397353     -1.93    0.053     -.1546479     .0011116
  7 2014/15  |   -.0993164    .0419066     -2.37    0.018     -.1814518    -.0171811
  8 2015/16  |   -.1217844    .0426663     -2.85    0.004     -.2054088      -.03816
  9 2016/17  |   -.1118014    .0433444     -2.58    0.010     -.1967548     -.026848
             |
       _cons |    7.594455     .026889    282.44    0.000      7.541754     7.647157
-------------+----------------------------------------------------------------
     sigma_u |  1.1471682
     sigma_e |  1.2229042
         rho |  .46807742   (fraction of variance due to u_i)
-------------+----------------------------------------------------------------

. gen sample = e(sample)                                                                                                                  // Generate dummy indicating sample (yes/no) based on stored results

. bysort id : egen obs = total(sample)                                                                                    // For each person: Find number of occasion with sample = 1

. br id wave happy married sample obs                                                                                             // Check data

. xttab obs                                                                                                                                               // Describe obs-variable

                  Overall             Between            Within
      obs |    Freq.  Percent      Freq.  Percent        Percent
----------+-----------------------------------------------------
        1 |     895      4.29       895     21.63         100.00
        2 |     984      4.72       492     11.89         100.00
        3 |    1050      5.03       350      8.46         100.00
        4 |    1220      5.85       305      7.37         100.00
        5 |    1060      5.08       212      5.12         100.00
        6 |    1170      5.61       195      4.71         100.00
        7 |    1225      5.87       175      4.23         100.00
        8 |    2992     14.35       374      9.04         100.00
        9 |   10260     49.19      1140     27.55         100.00
----------+-----------------------------------------------------
    Total |   20856    100.00      4138    100.00         100.00
                              (n = 4138)

.
. /* use "obs>=2" further down when running the model */
.
.
.
.
.
.
. *---------------------------- Running the FE-model -----------------------------
.
. // #1
. // Running the model for all persons with 2+ observations
. xtreg happy married i.wave if obs >= 2, fe vce(cluster id)                                              // Including time with dummies for wave

Fixed-effects (within) regression               Number of obs     =     19,961
Group variable: id                              Number of groups  =      3,243

R-squared:                                      Obs per group:
     Within  = 0.0043                                         min =          2
     Between = 0.0013                                         avg =        6.2
     Overall = 0.0039                                         max =          9

                                                F(9,3242)         =       6.94
corr(u_i, Xb) = 0.0155                          Prob > F          =     0.0000

(Std. err. adjusted for 3,243 clusters in id)
-------------+----------------------------------------------------------------
             |               Robust
        happy | Coefficient   std. err.       t    P>|t|      [95% con f. interval]
-------------+----------------------------------------------------------------
     married |     .104892    .0611661      1.71    0.086     -.0150362     .2248202
             |
        wave |
  2 2009/10  |     .131649    .0307569      4.28    0.000      .0713441      .191954
  3 2010/11  |   -.0116614    .0338047     -0.34    0.730     -.0779422     .0546195
  4 2011/12  |   -.0757008    .0384237     -1.97    0.049      -.151038    -.0003636
  5 2012/13  |   -.0974271    .0386432     -2.52    0.012     -.1731946    -.0216595
  6 2013/14  |   -.0863209    .0416056     -2.07    0.038     -.1678969    -.0047449
  7 2014/15  |   -.1071946    .0436747     -2.45    0.014     -.1928275    -.0215617
  8 2015/16  |   -.1247069    .0446659     -2.79    0.005     -.2122832    -.0371307
  9 2016/17  |   -.1143612    .0457465     -2.50    0.012     -.2040562    -.0246662
             |
       _cons |    7.627283    .0236327    322.74    0.000      7.580946     7.673619
-------------+----------------------------------------------------------------
     sigma_u |  1.2268681
     sigma_e |  1.2229042
         rho |  .50161807   (fraction of variance due to u_i)
-------------+----------------------------------------------------------------

. eststo fe_all                                                                                                                                   // Remember: Always include some form of time!

.
. xttab married                                                                                                                                   // Control group is already included!

                  Overall             Between            Within
  married |    Freq.  Percent      Freq.  Percent        Percent
----------+-----------------------------------------------------
 never ma |   19146     91.80      4138    100.00          94.47
  married |    1710      8.20       449     10.85          50.92
----------+-----------------------------------------------------
    Total |   20856    100.00      4587    110.85          90.21
                              (n = 4138)

.                                                                                                                                                                 // 4,138 persons report "never married",
.                                                                                                                                                                 // only 449 of them marry - rest is control
.
.
. // #2
. // Time constant controls: dropped
. xtreg happy married birthyear male i.wave if obs >= 2, fe vce(cluster id)


note: birthyear omitted because of collinearity.


note: male omitted because of collinearity.


Fixed-effects (within) regression               Number of obs     =     19,961
Group variable: id                              Number of groups  =      3,243

R-squared:                                      Obs per group:
     Within  = 0.0043                                         min =          2
     Between = 0.0013                                         avg =        6.2
     Overall = 0.0039                                         max =          9

                                                F(9,3242)         =       6.94
corr(u_i, Xb) = 0.0155                          Prob > F          =     0.0000

(Std. err. adjusted for 3,243 clusters in id)
-------------+----------------------------------------------------------------
             |               Robust
        happy | Coefficient   std. err.       t    P>|t|      [95% con f. interval]
-------------+----------------------------------------------------------------
     married |     .104892    .0611661      1.71    0.086     -.0150362     .2248202
   birthyear |           0   (omitted)
        male |           0   (omitted)
             |
        wave |
  2 2009/10  |     .131649    .0307569      4.28    0.000      .0713441      .191954
  3 2010/11  |   -.0116614    .0338047     -0.34    0.730     -.0779422     .0546195
  4 2011/12  |   -.0757008    .0384237     -1.97    0.049      -.151038    -.0003636
  5 2012/13  |   -.0974271    .0386432     -2.52    0.012     -.1731946    -.0216595
  6 2013/14  |   -.0863209    .0416056     -2.07    0.038     -.1678969    -.0047449
  7 2014/15  |   -.1071946    .0436747     -2.45    0.014     -.1928275    -.0215617
  8 2015/16  |   -.1247069    .0446659     -2.79    0.005     -.2122832    -.0371307
  9 2016/17  |   -.1143612    .0457465     -2.50    0.012     -.2040562    -.0246662
             |
       _cons |    7.627283    .0236327    322.74    0.000      7.580946     7.673619
-------------+----------------------------------------------------------------
     sigma_u |  1.2268681
     sigma_e |  1.2229042
         rho |  .50161807   (fraction of variance due to u_i)
-------------+----------------------------------------------------------------

.
. // #3
. // Time varying controls: possible
. xtsum isced                                                                                                                                             // There might be better examples of time-varying variables...

Variable         |      Mean   Std. dev.       Min        Max |    Observations
-----------------+--------------------------------------------+----------------
isced    overall |  2.761651   2.798364          0          8 |     N =   20856
         between |             2.515213          0          8 |     n =    4138
         within  |             1.359505  -4.095492   9.872762 | T-bar = 5.04012

. xttrans isced                                                                                                                                   // ... than education among adults.

  ISCED-97 |
classifica |
   tion of |                                ISCED-97 classification of education
 education |         0          1          2          3          4          5          6          7          8 |     Total
-----------+---------------------------------------------------------------------------------------------------+----------
         0 |     84.10       0.77       1.36       1.40       4.19       4.03       1.99       2.12       0.04 |    100.00
         1 |     13.33      86.19       0.48       0.00       0.00       0.00       0.00       0.00       0.00 |    100.00
         2 |     12.47       0.00      87.07       0.00       0.23       0.23       0.00       0.00       0.00 |    100.00
         3 |     25.61       0.00       0.00      73.85       0.00       0.54       0.00       0.00       0.00 |    100.00
         4 |      0.00       0.00       0.00       0.00      98.19       0.00       0.80       1.01       0.00 |    100.00
         5 |     55.17       0.00       0.00       0.00       0.00      44.60       0.23       0.00       0.00 |    100.00
         6 |      0.00       0.00       0.00       0.00       0.00       0.00      95.57       4.43       0.00 |    100.00
         7 |      0.00       0.00       0.00       0.00       0.00       0.00       0.00      99.77       0.23 |    100.00
         8 |      0.00       0.00       0.00       0.00       0.00       0.00       0.00       0.00     100.00 |    100.00
-----------+---------------------------------------------------------------------------------------------------+----------
     Total |     42.36       1.45       2.95       2.30      21.77       3.09       8.34      17.20       0.55 |    100.00

. xtreg happy married i.isced i.wave if obs >= 2, fe vce(cluster id)

Fixed-effects (within) regression               Number of obs     =     19,961
Group variable: id                              Number of groups  =      3,243

R-squared:                                      Obs per group:
     Within  = 0.0070                                         min =          2
     Between = 0.0351                                         avg =        6.2
     Overall = 0.0162                                         max =          9

                                                F(17,3242)        =       5.32
corr(u_i, Xb) = 0.0628                          Prob > F          =     0.0000

(Std. err. adjusted for 3,243 clusters in id)
------------------------------------------------------+----------------------------------------------------------------
                                                      |               Robust
                                                 happy | Coefficient   std. err.       t    P>|t|      [95% con f. interval]
------------------------------------------------------+----------------------------------------------------------------
                                              married |    .0989426    .0611518      1.62    0.106     -.0209576     .2188427
                                                      |
                                                isced |
                                    1 no degree (1b)  |   -.0800018    .2065804     -0.39    0.699     -.4850432     .3250395
                    2 lower secondary education (2b)  |   -.3024981    .1543293     -1.96    0.050      -.605091     .0000947
                    3 lower secondary education (2a)  |   -.3910595     .147023     -2.66    0.008      -.679327     -.102792
         4 upper secondary education vocational (3b)  |   -.1616099    .0617566     -2.62    0.009     -.2826958     -.040524
            5 upper secondary education general (3a)  |   -.2410654    .0708018     -3.40    0.001     -.3798862    -.1022447
6 post-secondary non tertiary education general (4a)  |     .079415    .0765262      1.04    0.299     -.0706297     .2294596
             7 first stage of tertiary education (5)  |    .0158136    .0754805      0.21    0.834     -.1321807     .1638078
            8 second stage of tertiary education (6)  |     .233107    .3465736      0.67    0.501     -.4464184     .9126325
                                                      |
                                                 wave |
                                           2 2009/10  |    .1369054    .0307779      4.45    0.000      .0765593     .1972516
                                           3 2010/11  |    .0014644    .0341327      0.04    0.966     -.0654595     .0683884
                                           4 2011/12  |   -.0543032    .0390726     -1.39    0.165     -.1309127     .0223064
                                           5 2012/13  |   -.0655802    .0396035     -1.66    0.098     -.1432307     .0120703
                                           6 2013/14  |   -.0575605    .0431966     -1.33    0.183     -.1422558     .0271349
                                           7 2014/15  |   -.0823356    .0456779     -1.80    0.072     -.1718961     .0072248
                                           8 2015/16  |   -.1024695    .0470147     -2.18    0.029     -.1946512    -.0102879
                                           9 2016/17  |   -.0918129    .0493425     -1.86    0.063     -.1885586     .0049328
                                                      |
                                                _cons |    7.659935    .0285494    268.31    0.000      7.603959     7.715912
------------------------------------------------------+----------------------------------------------------------------
                                              sigma_u |  1.2129541
                                              sigma_e |   1.221582
                                                  rho |   .4964561   (fraction of variance due to u_i)
------------------------------------------------------+----------------------------------------------------------------

.
. // #4
. // Interactions of time-varying and time-constant variables
. xtreg happy i.married##i.male if obs >= 2, fe vce(cluster id)


note: 1.male omitted because of collinearity.


Fixed-effects (within) regression               Number of obs     =     19,961
Group variable: id                              Number of groups  =      3,243

R-squared:                                      Obs per group:
     Within  = 0.0000                                         min =          2
     Between = 0.0005                                         avg =        6.2
     Overall = 0.0002                                         max =          9

                                                F(2,3242)         =       0.06
corr(u_i, Xb) = 0.0164                          Prob > F          =     0.9445

(Std. err. adjusted for 3,243 clusters in id)
-------------+----------------------------------------------------------------
             |               Robust
        happy | Coefficient   std. err.       t    P>|t|      [95% con f. interval]
-------------+----------------------------------------------------------------
     married |
    married  |    .0268287    .0879544      0.31    0.760     -.1456232     .1992807
             |
        male |
          1  |           0   (omitted)
             |
married#male |
  married#1  |   -.0382526    .1179472     -0.32    0.746     -.2695112      .193006
             |
       _cons |    7.600431    .0050327   1510.22    0.000      7.590564     7.610299
-------------+----------------------------------------------------------------
     sigma_u |  1.2276089
     sigma_e |  1.2253061
         rho |  .50093877   (fraction of variance due to u_i)
-------------+----------------------------------------------------------------

.
.
.
.
.
.
.
. *---------------------------- Time profiles ------------------------------------
. *---------------------------- Dummy impact functions ---------------------------
.
.
. // #0
. // Install the coefplot ado if not available on your computer:
. // ssc install coefplot
.
. // #1
. // Generate a variable counting time from/to event of marriage
.
. // #1.1
. // Tag the year when a person was first married
. bysort id (wave) : gen first_married = sum(married == 1) == 1  & sum(married[_n - 1] == 1) == 0

.
. // #1.2
. // Generate a new time-scale, with the first year of marriage = 0
. gen help = wave if first_married == 1                                                                                   // Find wave with first marriage
(20,407 missing values generated)

. bysort id : egen year_married = max(help)                                                                               // Copy wave with first marriage to all obs of a person
(17,517 missing values generated)

. gen time = wave - year_married                                                                                                  // Time scale in years, centered to year of marriage
(17,517 missing values generated)

. drop help

.
. // Transfer time-scale to positive values
. xtreg happy i.time i.wave if obs >= 2, fe vce(cluster id)                                               // Stata tells us we need positive values on this variable
time:  factor variables may not contain negative values
r(452);

end of do-file

r(452);

. do "C:\Users\jowo\AppData\Local\Temp\STD134c_000000.tmp"

.
. gen time_p = time + 8                                                                                                                   // Transfer scale to 0-15
(17,517 missing values generated)

. #delimit ;                                                                                                                                              // Give labels so the new time-variable is understandable
delimiter now ;
. label define time_plbl  0 "-8"                                                                                                  // "Year 0" is now labeled as the first year married ("+1")
>                                                 1 "-7"
>                                                 2 "-6"
>                                                 3 "-5"
>                                                 4 "-4"
>                                             5 "-3"
>                                                 6 "-2"
>                                                 7 "-1"
>                                                 8 "+1"
>                                                 9 "+2"
>                                                 10 "+3"
>                                                 11 "+4"
>                                                 12 "+5"
>                                                 13 "+6"
>                                                 14 "+7"
>                                                 15 "+8"
>                                                 ;

. #delimit cr
delimiter now cr
. label value time_p time_plbl

.
. // Persons who never married have a missing on time_p
. // However, they should be included in the model to estimate time trend
. bysort id : egen ever_married = max(married)

. tab time_p ever_married, m

           |     ever_married
    time_p |         0          1 |     Total
-----------+----------------------+----------
        -8 |         0         28 |        28
        -7 |         0         77 |        77
        -6 |         0        113 |       113
        -5 |         0        159 |       159
        -4 |         0        219 |       219
        -3 |         0        267 |       267
        -2 |         0        352 |       352
        -1 |         0        414 |       414
        +1 |         0        449 |       449
        +2 |         0        351 |       351
        +3 |         0        268 |       268
        +4 |         0        223 |       223
        +5 |         0        172 |       172
        +6 |         0        127 |       127
        +7 |         0         81 |        81
        +8 |         0         39 |        39
         . |    17,517          0 |    17,517
-----------+----------------------+----------
     Total |    17,517      3,339 |    20,856

. replace time_p = 10000 if ever_married == 0                                                                             // Give those never marrying a non-missing, constant value.
(17,517 real changes made)

.                                                                                                                                                                 // Such, they are not dropped from the model, but contribute to the estimation of the "wave"-effect, but not time since marriage (constant)
.
.
. // #2
. // Model
. xtreg happy ib7.time_p i.wave if obs >= 2, fe vce(cluster id)


note: 10000.time_p omitted because of collinearity.


Fixed-effects (within) regression               Number of obs     =     19,961
Group variable: id                              Number of groups  =      3,243

R-squared:                                      Obs per group:
     Within  = 0.0068                                         min =          2
     Between = 0.0007                                         avg =        6.2
     Overall = 0.0008                                         max =          9

                                                F(23,3242)        =       4.56
corr(u_i, Xb) = -0.0592                         Prob > F          =     0.0000

(Std. err. adjusted for 3,243 clusters in id)
-------------+----------------------------------------------------------------
             |               Robust
        happy | Coefficient   std. err.       t    P>|t|      [95% con f. interval]
-------------+----------------------------------------------------------------
      time_p |
         -8  |   -.8607885     .254573     -3.38    0.001     -1.359929    -.3616482
         -7  |    -.591821    .1607438     -3.68    0.000     -.9069907    -.2766513
         -6  |   -.3936472    .1462476     -2.69    0.007     -.6803943    -.1069001
         -5  |   -.3398714    .1202226     -2.83    0.005     -.5755914    -.1041514
         -4  |   -.3077957    .1049158     -2.93    0.003     -.5135037    -.1020878
         -3  |   -.2098274    .0987026     -2.13    0.034     -.4033532    -.0163016
         -2  |   -.0149153    .0771306     -0.19    0.847     -.1661449     .1363143
         +1  |    .0357686    .0771806      0.46    0.643     -.1155592     .1870964
         +2  |   -.0343712    .0865379     -0.40    0.691     -.2040457     .1353034
         +3  |   -.0408575    .0956976     -0.43    0.669     -.2284914     .1467763
         +4  |   -.0408494    .1092866     -0.37    0.709     -.2551271     .1734283
         +5  |   -.1289605    .1159796     -1.11    0.266     -.3563612     .0984402
         +6  |    .1790117     .120063      1.49    0.136     -.0563953     .4144187
         +7  |    .0823674    .1390782      0.59    0.554     -.1903227     .3550574
         +8  |    .3795513     .160364      2.37    0.018      .0651262     .6939765
      10000  |           0   (omitted)
             |
        wave |
  2 2009/10  |    .1214076    .0309542      3.92    0.000      .0607159     .1820993
  3 2010/11  |   -.0289514    .0341089     -0.85    0.396     -.0958285     .0379258
  4 2011/12  |   -.0975774    .0388701     -2.51    0.012     -.1737899    -.0213648
  5 2012/13  |   -.1236009    .0393388     -3.14    0.002     -.2007323    -.0464696
  6 2013/14  |   -.1150843    .0426218     -2.70    0.007     -.1986528    -.0315159
  7 2014/15  |   -.1434951    .0451706     -3.18    0.002      -.232061    -.0549293
  8 2015/16  |   -.1629421    .0466756     -3.49    0.000     -.2544588    -.0714254
  9 2016/17  |   -.1596447    .0480074     -3.33    0.001     -.2537727    -.0655167
             |
       _cons |    7.672008     .026313    291.57    0.000      7.620416     7.723599
-------------+----------------------------------------------------------------
     sigma_u |  1.2319083
     sigma_e |  1.2219301
         rho |  .50406632   (fraction of variance due to u_i)
-------------+----------------------------------------------------------------

. coefplot,       vertical yline(0) ///
>                         drop(_cons 1b.wave 2.wave 3.wave 4.wave 5.wave 6.wave 7.wave 8.wave 9.wave) // Reference group not shown (-1)

.
.
.
. // #2.1
. // Getting the reference category into the graph
.
. // Using the constant to represent the reference category
. tempname bmat                                                                                                                                   // Set the constant to 0...

. matrix `bmat' = e(b)                                                                                                                    // ... and use as the reference category

. matrix `bmat'[1,27]=0

. erepost b = `bmat'

. mat list e(b)

e(b)[1,27]
             0.          1.          2.          3.          4.          5.          6.         7b.          8.          9.         10.         11.
        time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p
y1  -.86078849  -.59182099   -.3936472  -.33987136  -.30779574  -.20982739  -.01491527           0   .03576861  -.03437117  -.04085753  -.04084936

            12.         13.         14.         15.     10000o.         1b.          2.          3.          4.          5.          6.          7.
        time_p      time_p      time_p      time_p      time_p        wave        wave        wave        wave        wave        wave        wave
y1   -.1289605   .17901173   .08236737   .37955134           0           0    .1214076  -.02895137  -.09757736  -.12360094  -.11508433  -.14349513

             8.          9.
          wave        wave       _cons
y1  -.16294208   -.1596447           0

.
. tempname Vmat                                                                                                                                   // Reference category does not need a confidence interval

. matrix `Vmat' = e(V)                                                                                                                    // Set respective value in covariance matrix to 0

. matrix `Vmat'[27,27]=0

. erepost V = `Vmat'

. mat list e(V)

symmetric e(V)[27,27]
                       0.          1.          2.          3.          4.          5.          6.         7b.          8.          9.         10.         11.
                  time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p
    0.time_p   .06480741
    1.time_p   .01210557   .02583856
    2.time_p   .00763803    .0067619   .02138836
    3.time_p   .00820228   .00484448   .00853941   .01445348
    4.time_p   .00347032   .00284307   .00575845   .00572672   .01100732
    5.time_p   .00444364    .0027595   .00403358   .00548182   .00421423    .0097422
    6.time_p   .00283362   .00186758   .00270376   .00345421   .00317357   .00383795   .00594912
   7b.time_p           0           0           0           0           0           0           0           0
    8.time_p   .00208086   .00172523   .00200041   .00249319   .00164107   .00249327   .00174946           0   .00595685
    9.time_p   .00180353   .00085391   .00230208   .00286386   .00185391   .00248534   .00246182           0   .00321908   .00748881
   10.time_p   .00139224   .00077437   .00166094   .00249229   .00173554   .00259135   .00238192           0   .00285189   .00380331   .00915803
   11.time_p   .00146709   .00080526   .00185221   .00277714   .00222389   .00274998   .00204867           0   .00321515    .0035363   .00555048   .01194355
   12.time_p   .00130521    .0007226   .00170245   .00249415   .00201357   .00265687   .00233993           0   .00310715   .00422326   .00561577    .0070281
   13.time_p   .00152554   .00090042    .0018869   .00264469   .00222942   .00285781   .00288244           0   .00318894   .00452461   .00687204    .0070907
   14.time_p   .00104961   .00049727    .0013817   .00206597   .00179666   .00240734   .00194409           0   .00329924   .00376773   .00577712   .00709842
   15.time_p   .00097975   .00065512   .00128398    .0020547   .00171921   .00224725   .00213941           0   .00349074   .00404663    .0062527     .005662
10000o.tim~p           0           0           0           0           0           0           0           0           0           0           0           0
     1b.wave           0           0           0           0           0           0           0           0           0           0           0           0
      2.wave   .00085449   .00014662   .00011226   .00015385   .00010727   .00011984   .00008073           0  -.00024594  -.00009449  -.00013038  -.00019823
      3.wave   .00100933   .00057887   .00032032    .0001988   .00020594   .00004651   .00011234           0  -.00017789  -.00030538   -.0002015  -.00014049
      4.wave   .00138597   .00071389   .00054512   .00029279   .00040893   .00009898   .00009468           0  -.00027919  -.00035878  -.00048111  -.00039245
      5.wave   .00133788   .00092808   .00080237   .00064725   .00046086   .00030607   .00022239           0  -.00026514  -.00031638  -.00044455  -.00058091
      6.wave   .00157005   .00099519   .00085217   .00068767   .00059256   .00027832   .00006838           0  -.00025071  -.00026817  -.00042234  -.00070038
      7.wave   .00170083   .00139785   .00108634   .00075225   .00073274   .00037599   .00018588           0  -.00016591  -.00022432  -.00062048  -.00057687
      8.wave   .00173731   .00138908   .00087295   .00089321   .00058307   .00039561   .00030094           0  -.00013867  -.00054192  -.00053907  -.00080042
      9.wave    .0017304    .0010737   .00107937   .00079134    .0005496    .0004556   .00022605           0  -.00047338  -.00053391  -.00075108  -.00095567
       _cons  -.00165289  -.00105924  -.00107819   -.0010181  -.00082879  -.00073735  -.00055658           0  -.00025039   -.0002665  -.00023626  -.00022244

                      12.         13.         14.         15.     10000o.         1b.          2.          3.          4.          5.          6.          7.
                  time_p      time_p      time_p      time_p      time_p        wave        wave        wave        wave        wave        wave        wave
   12.time_p   .01345126
   13.time_p   .00780944   .01441512
   14.time_p   .00885917   .00929597   .01934275
   15.time_p   .00874968   .00865714   .01092131   .02571663
10000o.tim~p           0           0           0           0           0
     1b.wave           0           0           0           0           0           0
      2.wave  -.00009813  -.00009378  -.00006602  -.00015449           0           0   .00095816
      3.wave  -.00017469  -.00012368  -.00011624   -.0002011           0           0   .00060649   .00116342
      4.wave  -.00040513  -.00025716  -.00037359  -.00032292           0           0   .00059587   .00078477   .00151089
      5.wave  -.00042893  -.00036096  -.00038491  -.00058108           0           0   .00059292   .00074052    .0009197   .00154754
      6.wave  -.00052235    -.000428  -.00038934  -.00044472           0           0   .00059949   .00075171   .00094331   .00108076   .00181662
      7.wave  -.00089172  -.00077875  -.00061505  -.00072989           0           0   .00055861   .00069567   .00088938   .00104189   .00119491   .00204038
      8.wave  -.00097115   -.0012586  -.00160922  -.00116047           0           0   .00055757   .00070745   .00086528   .00099211   .00105997   .00126752
      9.wave  -.00112912  -.00145556  -.00166568  -.00236931           0           0   .00060669   .00072737   .00091436   .00104731   .00114442   .00123681
       _cons  -.00023875  -.00028084   -.0002044  -.00012292           0           0  -.00054578  -.00065128   -.0007533  -.00079685  -.00084601  -.00086016

                       8.          9.
                    wave        wave       _cons
      8.wave   .00217861
      9.wave   .00142078   .00230471
       _cons  -.00084512   -.0008592           0

.
. // Neat coefplot using the adjusted matrices e(b) and e(V)
. #delimit ;
delimiter now ;
. coefplot, vertical yline(0)
>                   drop(1b.wave 2.wave 3.wave 4.wave 5.wave 6.wave 7.wave 8.wave 9.wave )
>                   order(0.time_p 1.time_p 2.time_p 3.time_p 4.time_p 5.time_p 6.time_p
>                   _cons 8.time_p 9.time_p 10.time_p 11.time_p 12.time_p 13.time_p 14.time_p 15.time_p)  // add _cons for reference category
>                   coeflabels(_cons = "-1")                                                                                                                              // label _cons = "-1"
>                   yscale(range(-1.5 1))
>                   ylab(-1.5(.5)1)
>                   xtitle(Years before/after marriage)
>                   ytitle(Happiness)
>                   name(time1, replace)
>                   ;

. #delimit cr
delimiter now cr
.
.
.
.
.
. // #2.2
. // Same graph as above, but without controlling time (for comparison)
. xtreg happy ib7.time_p if obs >= 2, fe vce(cluster id)


note: 10000.time_p omitted because of collinearity.


Fixed-effects (within) regression               Number of obs     =     19,961
Group variable: id                              Number of groups  =      3,243

R-squared:                                      Obs per group:
     Within  = 0.0017                                         min =          2
     Between = 0.0052                                         avg =        6.2
     Overall = 0.0006                                         max =          9

                                                F(15,3242)        =       2.32
corr(u_i, Xb) = -0.0849                         Prob > F          =     0.0027

(Std. err. adjusted for 3,243 clusters in id)
-------------+----------------------------------------------------------------
             |               Robust
        happy | Coefficient   std. err.       t    P>|t|      [95% con f. interval]
-------------+----------------------------------------------------------------
      time_p |
         -8  |   -.6858201    .2506024     -2.74    0.006     -1.177175     -.194465
         -7  |   -.3937917    .1578641     -2.49    0.013     -.7033151    -.0842682
         -6  |    -.204026    .1424827     -1.43    0.152     -.4833913     .0753392
         -5  |   -.2130882    .1185229     -1.80    0.072     -.4454756     .0192993
         -4  |   -.2178124    .1040019     -2.09    0.036     -.4217286    -.0138962
         -3  |     -.14939    .0981604     -1.52    0.128     -.3418528     .0430728
         -2  |    .0172591     .076838      0.22    0.822     -.1333969      .167915
         +1  |    .0213166     .076904      0.28    0.782     -.1294687      .172102
         +2  |   -.0999108    .0855924     -1.17    0.243     -.2677315     .0679098
         +3  |   -.1400182    .0942023     -1.49    0.137     -.3247203     .0446838
         +4  |   -.1588131    .1069553     -1.48    0.138       -.36852     .0508938
         +5  |   -.2632106    .1130658     -2.33    0.020     -.4848982     -.041523
         +6  |    .0207028    .1147726      0.18    0.857     -.2043315      .245737
         +7  |   -.0934206    .1315462     -0.71    0.478     -.3513427     .1645015
         +8  |    .1924369    .1507929      1.28    0.202     -.1032222      .488096
      10000  |           0   (omitted)
             |
       _cons |     7.61751    .0088388    861.83    0.000       7.60018      7.63484
-------------+----------------------------------------------------------------
     sigma_u |   1.230782
     sigma_e |  1.2247244
         rho |  .50246693   (fraction of variance due to u_i)
-------------+----------------------------------------------------------------

.
. tempname bmat                                                                                                                                   // Set the constant to 0...

. matrix `bmat' = e(b)                                                                                                                    // ... and use as the reference category

. matrix `bmat'[1,18]=0

. erepost b = `bmat'

. mat list e(b)

e(b)[1,18]
             0.          1.          2.          3.          4.          5.          6.         7b.          8.          9.         10.         11.
        time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p
y1  -.68582012  -.39379166  -.20402603  -.21308816  -.21781238  -.14939001   .01725906           0   .02131662  -.09991081  -.14001824  -.15881308

            12.         13.         14.         15.     10000o.
        time_p      time_p      time_p      time_p      time_p       _cons
y1  -.26321061   .02070279  -.09342062   .19243688           0           0

.
. tempname Vmat                                                                                                                                   // Reference category does not need a confidence interval

. matrix `Vmat' = e(V)                                                                                                                    // Set respective value in covariance matrix to 0

. matrix `Vmat'[18,18]=0

. erepost V = `Vmat'

. mat list e(V)

symmetric e(V)[18,18]
                       0.          1.          2.          3.          4.          5.          6.         7b.          8.          9.         10.         11.
                  time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p
    0.time_p   .06280157
    1.time_p    .0107084   .02492106
    2.time_p   .00647874   .00578597   .02030132
    3.time_p   .00738333   .00416579   .00784157   .01404769
    4.time_p   .00282223   .00230794   .00521813   .00535635    .0108164
    5.time_p   .00405973   .00246756   .00373055   .00525195   .00410031   .00963547
    6.time_p    .0026087   .00171242   .00255546   .00333709   .00309825   .00380713   .00590407
   7b.time_p           0           0           0           0           0           0           0           0
    8.time_p   .00243126    .0019637   .00209289   .00260929    .0017147   .00255151   .00181535           0   .00591423
    9.time_p   .00226645   .00122159   .00256016   .00306098   .00197634   .00256545   .00252596           0   .00312615   .00732606
   10.time_p   .00208482   .00136529   .00212235   .00286092    .0019879   .00279569    .0024841           0   .00277427    .0036229   .00887407
   11.time_p   .00227037   .00152635   .00241069   .00326966   .00252373   .00300118   .00213888           0   .00312059   .00331821   .00519188   .01143944
   12.time_p   .00225573   .00160395   .00240317   .00307096   .00242102   .00299583   .00248343           0   .00305413    .0040109   .00523344   .00648549
   13.time_p   .00251205   .00186265   .00261939   .00328104   .00265549   .00321945   .00307182           0   .00310712    .0042162   .00639547    .0064409
   14.time_p   .00213443   .00148047   .00217582   .00279199   .00219255   .00282537   .00219822           0   .00314692   .00332967   .00524055   .00630592
   15.time_p   .00214823   .00152536   .00221349    .0027725   .00212224   .00270539   .00235202           0   .00313296   .00357005   .00557516   .00472642
10000o.tim~p           0           0           0           0           0           0           0           0           0           0           0           0
       _cons  -.00055234  -.00040131  -.00055906  -.00062212  -.00049223  -.00055579   -.0004373           0  -.00045366  -.00051404  -.00056721  -.00061205

                      12.         13.         14.         15.     10000o.
                  time_p      time_p      time_p      time_p      time_p       _cons
   12.time_p   .01278387
   13.time_p   .00688753   .01317276
   14.time_p   .00781997   .00773251    .0173044
   15.time_p   .00758692   .00692552   .00881048   .02273851
10000o.tim~p           0           0           0           0           0
       _cons  -.00064033  -.00066796  -.00061803  -.00059498           0           0

.
. #delimit ;
delimiter now ;
. coefplot, vertical yline(0)
>                   drop(1b.wave 2.wave 3.wave 4.wave 5.wave 6.wave 7.wave 8.wave 9.wave )
>                   order(0.time_p 1.time_p 2.time_p 3.time_p 4.time_p 5.time_p 6.time_p
>                   _cons 8.time_p 9.time_p 10.time_p 11.time_p 12.time_p 13.time_p 14.time_p 15.time_p)
>                   coeflabels(_cons = "-1")
>                   yscale(range(-1.5 1))
>                   ylab(-1.5(.5)1)
>                   xtitle(Years before/after marriage)
>                   ytitle(Happiness)
>                   name(time2, replace)
>                   ;

. #delimit cr
delimiter now cr
.
.
.
.
.
. // #2.3
. // Time profile for women and men
.
. // Estimates for women
. xtreg happy ib7.time_p i.wave if obs >= 2 & male == 0, fe vce(cluster id)               // Model for women


note: 10000.time_p omitted because of collinearity.


Fixed-effects (within) regression               Number of obs     =      9,405
Group variable: id                              Number of groups  =      1,534

R-squared:                                      Obs per group:
     Within  = 0.0064                                         min =          2
     Between = 0.0000                                         avg =        6.1
     Overall = 0.0016                                         max =          9

                                                F(23,1533)        =       2.36
corr(u_i, Xb) = -0.0407                         Prob > F          =     0.0003

(Std. err. adjusted for 1,534 clusters in id)
-------------+----------------------------------------------------------------
             |               Robust
        happy | Coefficient   std. err.       t    P>|t|      [95% con f. interval]
-------------+----------------------------------------------------------------
      time_p |
         -8  |   -.4509504    .3031874     -1.49    0.137     -1.045656     .1437554
         -7  |   -.3589129    .1987672     -1.81    0.071     -.7487973     .0309715
         -6  |   -.4245023    .2157804     -1.97    0.049     -.8477582    -.0012464
         -5  |   -.3026969    .1923471     -1.57    0.116     -.6799881     .0745943
         -4  |    -.223532     .147783     -1.51    0.131     -.5134102     .0663463
         -3  |   -.2722273    .1625718     -1.67    0.094      -.591114     .0466593
         -2  |    .0365192    .1209974      0.30    0.763     -.2008188     .2738572
         +1  |    .0952691      .11983      0.80    0.427     -.1397789     .3303171
         +2  |   -.0563385     .137508     -0.41    0.682     -.3260623     .2133852
         +3  |     .019913     .148726      0.13    0.894      -.271815      .311641
         +4  |   -.0193726    .1669931     -0.12    0.908     -.3469317     .3081865
         +5  |   -.0441248    .1743445     -0.25    0.800     -.3861037     .2978541
         +6  |    .2986515    .1669336      1.79    0.074     -.0287908     .6260939
         +7  |    .1620203    .1858207      0.87    0.383     -.2024693     .5265099
         +8  |    .4644246    .1935447      2.40    0.017      .0847841      .844065
      10000  |           0   (omitted)
             |
        wave |
  2 2009/10  |    .0887792    .0461008      1.93    0.054     -.0016481     .1792065
  3 2010/11  |   -.0243024    .0504555     -0.48    0.630     -.1232714     .0746667
  4 2011/12  |   -.0905904    .0565465     -1.60    0.109      -.201507     .0203263
  5 2012/13  |   -.1501447    .0583523     -2.57    0.010     -.2646035    -.0356858
  6 2013/14  |   -.1533837     .064213     -2.39    0.017     -.2793383    -.0274291
  7 2014/15  |    -.185738    .0661283     -2.81    0.005     -.3154495    -.0560266
  8 2015/16  |   -.1806258    .0690441     -2.62    0.009     -.3160565     -.045195
  9 2016/17  |   -.1238618    .0692482     -1.79    0.074     -.2596931     .0119695
             |
       _cons |    7.695975    .0389712    197.48    0.000      7.619532     7.772417
-------------+----------------------------------------------------------------
     sigma_u |  1.1739762
     sigma_e |  1.2445416
         rho |  .47084767   (fraction of variance due to u_i)
-------------+----------------------------------------------------------------

.
. tempname bmat                                                                                                                                   // Set the constant to 0...

. matrix `bmat' = e(b)                                                                                                                    // ... and use as the reference category

. matrix `bmat'[1,27]=0

. erepost b = `bmat'

. mat list e(b)

e(b)[1,27]
             0.          1.          2.          3.          4.          5.          6.         7b.          8.          9.         10.         11.
        time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p
y1  -.45095039   -.3589129  -.42450231  -.30269692  -.22353197  -.27222732    .0365192           0   .09526912  -.05633855   .01991301  -.01937257

            12.         13.         14.         15.     10000o.         1b.          2.          3.          4.          5.          6.          7.
        time_p      time_p      time_p      time_p      time_p        wave        wave        wave        wave        wave        wave        wave
y1  -.04412478   .29865151    .1620203   .46442457           0           0   .08877917  -.02430238  -.09059039  -.15014467  -.15338369  -.18573802

             8.          9.
          wave        wave       _cons
y1  -.18062577  -.12386176           0

.
. tempname Vmat                                                                                                                                   // Reference category does not need a confidence interval

. matrix `Vmat' = e(V)                                                                                                                    // Set respective value in covariance matrix to 0

. matrix `Vmat'[27,27]=0

. erepost V = `Vmat'

. mat list e(V)

symmetric e(V)[27,27]
                       0.          1.          2.          3.          4.          5.          6.         7b.          8.          9.         10.         11.
                  time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p
    0.time_p   .09192257
    1.time_p   .01667178    .0395084
    2.time_p   .01264696   .01788085   .04656117
    3.time_p   .02055471   .01386683    .0204771   .03699739
    4.time_p    .0036003   .00672923   .01248177   .01292082   .02183982
    5.time_p   .00981453   .00808698   .00988076   .01102444   .00979698   .02642959
    6.time_p   .00646934   .00497786   .00483369   .00712489   .00757895   .00970417   .01464038
   7b.time_p           0           0           0           0           0           0           0           0
    8.time_p   .00651734   .00443291   .00594383   .00767626   .00655898    .0080459   .00552331           0   .01435922
    9.time_p   .00514807   .00417145    .0067358   .00878405   .00606893   .00722314      .00851           0   .00875067   .01890846
   10.time_p   .00321812   .00263159   .00387132   .00612365   .00480763    .0068778    .0053905           0   .00825512   .01133341   .02211943
   11.time_p   .00352408   .00276187   .00459413    .0063825   .00598987   .00706036   .00518612           0   .00884291   .01149451   .01576679    .0278867
   12.time_p   .00329538   .00265691   .00428269   .00625055   .00515391    .0079709   .00573381           0   .00865951   .01237396   .01425995   .01733255
   13.time_p   .00387001     .003135    .0047219   .00664367   .00611092   .00805877   .00819354           0   .00802828   .01275475   .01590904   .01865367
   14.time_p   .00331116   .00264082   .00427544   .00603319    .0057117   .00740872   .00671778           0   .00867182   .01179333   .01531426   .01830488
   15.time_p   .00286535   .00267704   .00381066   .00592137    .0053984   .00681833   .00644855           0   .00874949   .01162959   .01486766   .01763745
10000o.tim~p           0           0           0           0           0           0           0           0           0           0           0           0
     1b.wave           0           0           0           0           0           0           0           0           0           0           0           0
      2.wave   .00151109   .00040986   .00032865  -.00007522   .00015654   .00025421   .00019929           0   -.0005205   -.0001261  -.00044739  -.00052807
      3.wave   .00178159   .00132809   .00054035   -.0000245   .00057365   .00020936   .00035349           0  -.00033273  -.00066036  -.00061368  -.00057408
      4.wave   .00284389   .00152111   .00104477    .0001903   .00050848   .00036197    .0005804           0  -.00061451  -.00062853  -.00122934  -.00099782
      5.wave   .00230517   .00211499    .0014742   .00100168   .00101381    .0008599   .00071096           0  -.00065137  -.00067841  -.00088115   -.0012806
      6.wave   .00309242    .0022254   .00178555   .00134929   .00119774    .0006471   .00018189           0  -.00054748   -.0010065  -.00120766    -.001447
      7.wave   .00345502   .00297656   .00260025   .00155402   .00169369   .00088092   .00061949           0  -.00027211  -.00003709  -.00110526  -.00092425
      8.wave   .00366565   .00307763   .00194231   .00190098   .00142809     .000934   .00072193           0  -.00006361  -.00071462  -.00134593  -.00181467
      9.wave   .00341004   .00215603   .00204544   .00111714   .00084421   .00077682   .00027387           0  -.00115823  -.00146216   -.0017099  -.00237012
       _cons  -.00329444  -.00246369  -.00240501  -.00211807  -.00190216  -.00192644  -.00149661           0  -.00084663   -.0010094  -.00069694  -.00073838

                      12.         13.         14.         15.     10000o.         1b.          2.          3.          4.          5.          6.          7.
                  time_p      time_p      time_p      time_p      time_p        wave        wave        wave        wave        wave        wave        wave
   12.time_p     .030396
   13.time_p   .01926087   .02786682
   14.time_p   .02083476   .02249187   .03452933
   15.time_p    .0203244   .02327369   .02402397   .03745957
10000o.tim~p           0           0           0           0           0
     1b.wave           0           0           0           0           0           0
      2.wave  -.00023922  -.00033072  -.00011712  -.00037148           0           0   .00212528
      3.wave  -.00063228  -.00038954  -.00040368  -.00048976           0           0   .00127207   .00254575
      4.wave  -.00090697  -.00071042  -.00085387  -.00079859           0           0   .00132109   .00168235    .0031975
      5.wave   -.0006593  -.00067399  -.00064046   -.0008278           0           0   .00133055   .00161716   .00193674     .003405
      6.wave  -.00138997  -.00109824  -.00104614  -.00122801           0           0    .0013321   .00170139   .00206062   .00243789   .00412331
      7.wave  -.00157995  -.00164537  -.00105974  -.00112798           0           0    .0011948   .00145395   .00183727   .00211495   .00255212   .00437295
      8.wave  -.00212785  -.00265911  -.00312339  -.00220668           0           0   .00115644   .00150192   .00189425   .00210091   .00230689   .00266734
      9.wave  -.00260739  -.00309709  -.00371848  -.00519557           0           0   .00126547   .00151329    .0019506   .00215517   .00255566   .00253055
       _cons  -.00078645  -.00083466  -.00079861  -.00062604           0           0  -.00118036  -.00139556  -.00160746  -.00172157  -.00186009  -.00182735

                       8.          9.
                    wave        wave       _cons
      8.wave   .00476708
      9.wave   .00272558   .00479532
       _cons  -.00180037   -.0017412           0

.
. eststo women                                                                                                                                    // Saving the estimates

.
.
. // Estimates for men
. xtreg happy ib7.time_p i.wave if obs >= 2 & male == 1, fe vce(cluster id)               // Model for men


note: 10000.time_p omitted because of collinearity.


Fixed-effects (within) regression               Number of obs     =     10,556
Group variable: id                              Number of groups  =      1,709

R-squared:                                      Obs per group:
     Within  = 0.0090                                         min =          2
     Between = 0.0024                                         avg =        6.2
     Overall = 0.0006                                         max =          9

                                                F(23,1708)        =       3.16
corr(u_i, Xb) = -0.0733                         Prob > F          =     0.0000

(Std. err. adjusted for 1,709 clusters in id)
-------------+----------------------------------------------------------------
             |               Robust
        happy | Coefficient   std. err.       t    P>|t|      [95% con f. interval]
-------------+----------------------------------------------------------------
      time_p |
         -8  |    -1.48065    .3841394     -3.85    0.000     -2.234084    -.7272172
         -7  |   -.8390606    .2534522     -3.31    0.001      -1.33617    -.3419511
         -6  |   -.3679142    .1987772     -1.85    0.064     -.7577867     .0219583
         -5  |    -.376249    .1516282     -2.48    0.013     -.6736455    -.0788525
         -4  |   -.3849543    .1470876     -2.62    0.009     -.6734451    -.0964634
         -3  |   -.1573152    .1179961     -1.33    0.183     -.3887473     .0741169
         -2  |   -.0609504    .0979181     -0.62    0.534     -.2530025     .1311017
         +1  |   -.0221689    .0990392     -0.22    0.823     -.2164198      .172082
         +2  |   -.0249206    .1087722     -0.23    0.819     -.2382614     .1884203
         +3  |   -.0942046    .1233846     -0.76    0.445     -.3362056     .1477963
         +4  |    -.057291    .1436179     -0.40    0.690     -.3389765     .2243945
         +5  |   -.2089543    .1534413     -1.36    0.173     -.5099069     .0919984
         +6  |    .0559563    .1761931      0.32    0.751     -.2896207     .4015333
         +7  |   -.0082875    .2129387     -0.04    0.969     -.4259356     .4093606
         +8  |    .2595905    .2728776      0.95    0.342     -.2756191     .7948001
      10000  |           0   (omitted)
             |
        wave |
  2 2009/10  |    .1504852    .0416534      3.61    0.000      .0687882     .2321823
  3 2010/11  |   -.0335583     .046355     -0.72    0.469     -.1244768     .0573602
  4 2011/12  |   -.1037848    .0537059     -1.93    0.053      -.209121     .0015515
  5 2012/13  |   -.1014501    .0532383     -1.91    0.057     -.2058691      .002969
  6 2013/14  |   -.0809339    .0568564     -1.42    0.155     -.1924495     .0305816
  7 2014/15  |   -.1076942    .0618469     -1.74    0.082     -.2289978     .0136094
  8 2015/16  |   -.1472565    .0633281     -2.33    0.020     -.2714654    -.0230477
  9 2016/17  |    -.189609     .066617     -2.85    0.004     -.3202684    -.0589495
             |
       _cons |    7.651098     .035605    214.89    0.000      7.581264     7.720932
-------------+----------------------------------------------------------------
     sigma_u |  1.2822175
     sigma_e |  1.2019623
         rho |  .53227283   (fraction of variance due to u_i)
-------------+----------------------------------------------------------------

.
. tempname bmat                                                                                                                                   // Set the constant to 0...

. matrix `bmat' = e(b)                                                                                                                    // ... and use as the reference category

. matrix `bmat'[1,27]=0

. erepost b = `bmat'

. mat list e(b)

e(b)[1,27]
             0.          1.          2.          3.          4.          5.          6.         7b.          8.          9.         10.         11.
        time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p
y1  -1.4806505  -.83906057  -.36791419  -.37624902  -.38495428  -.15731519  -.06095042           0  -.02216886  -.02492058  -.09420462    -.057291

            12.         13.         14.         15.     10000o.         1b.          2.          3.          4.          5.          6.          7.
        time_p      time_p      time_p      time_p      time_p        wave        wave        wave        wave        wave        wave        wave
y1  -.20895427   .05595632  -.00828749   .25959049           0           0   .15048523   -.0335583  -.10378477  -.10145007  -.08093394  -.10769418

             8.          9.
          wave        wave       _cons
y1  -.14725655  -.18960896           0

.
. tempname Vmat                                                                                                                                   // Reference category does not need a confidence interval

. matrix `Vmat' = e(V)                                                                                                                    // Set respective value in covariance matrix to 0

. matrix `Vmat'[27,27]=0

. erepost V = `Vmat'

. mat list e(V)

symmetric e(V)[27,27]
                       0.          1.          2.          3.          4.          5.          6.         7b.          8.          9.         10.         11.
                  time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p      time_p
    0.time_p   .14756305
    1.time_p   .02641238   .06423802
    2.time_p   .01578655   .00964344   .03951238
    3.time_p   .01468157   .00702425    .0144079    .0229911
    4.time_p   .01227396   .00421048   .01078655   .01005073   .02163477
    5.time_p   .00903849   .00448303   .00638419   .01052795   .00734691   .01392308
    6.time_p   .00326167   .00259171   .00587092   .00639106   .00516362   .00594162   .00958796
   7b.time_p           0           0           0           0           0           0           0           0
    8.time_p  -.00001328    .0022753   .00250622   .00276172   .00061067   .00252096    .0017701           0   .00980876
    9.time_p   .00190183  -.00081356   .00311007   .00335175   .00177296   .00311486   .00214884           0   .00453001    .0118314
   10.time_p   .00182609   .00033326   .00279326   .00367037   .00228387   .00348589   .00400645           0   .00353246   .00473032   .01522377
   11.time_p   .00180823   .00032279   .00286605   .00450971   .00301437   .00387782   .00303034           0   .00438736   .00375092   .00744735    .0206261
   12.time_p   .00150612   .00025192   .00268046   .00377795   .00298859   .00309621   .00376431           0   .00408711   .00539797   .00881841   .01142683
   13.time_p   .00180896   .00051965   .00298802   .00401911   .00301951   .00349574   .00358926           0   .00496082   .00626801   .01207693   .01079638
   14.time_p   .00033965  -.00073532   .00131537   .00219976   .00156348   .00229811   .00121166           0   .00470973   .00393145   .00808703   .01047303
   15.time_p   .00093573   -.0000505   .00143546   .00238288   .00162908    .0022628   .00223266           0   .00557593   .00512316   .01031868   .00534374
10000o.tim~p           0           0           0           0           0           0           0           0           0           0           0           0
     1b.wave           0           0           0           0           0           0           0           0           0           0           0           0
      2.wave   .00175802   .00020869   .00004499    .0005374   .00025201   .00015418    .0001398           0  -.00045991  -.00022109  -.00014446  -.00029984
      3.wave   .00212287   .00100724   .00065076   .00061531   .00025657  -.00001443   .00010732           0  -.00038461  -.00061006  -.00028416  -.00010424
      4.wave   .00283618   .00131077   .00106553   .00083476   .00105915   .00007984  -.00008447           0  -.00052361  -.00078533  -.00077232  -.00063469
      5.wave   .00337796   .00177725   .00159115   .00145411   .00085642   .00033301     .000237           0  -.00046309  -.00065496  -.00095076  -.00114186
      6.wave   .00351336   .00185249   .00158745   .00137411   .00117602   .00043092   .00010349           0  -.00049751  -.00023851  -.00062422  -.00140432
      7.wave   .00320911    .0027109   .00178207   .00145037   .00130926   .00060165   .00019362           0  -.00035071  -.00077374  -.00130983  -.00132531
      8.wave   .00357315   .00256387   .00156714   .00167479   .00094721   .00068555   .00050998           0  -.00042321  -.00132921  -.00089363  -.00143649
      9.wave   .00302598   .00200896   .00218523   .00184164    .0012455   .00097649   .00056153           0  -.00078514  -.00076545  -.00135035  -.00157548
       _cons  -.00321611   -.0018175  -.00189305  -.00189796  -.00143652  -.00106837  -.00079075           0  -.00020486  -.00014905   -.0002487  -.00017346

                      12.         13.         14.         15.     10000o.         1b.          2.          3.          4.          5.          6.          7.
                  time_p      time_p      time_p      time_p      time_p        wave        wave        wave        wave        wave        wave        wave
   12.time_p   .02354423
   13.time_p   .01237113   .03104401
   14.time_p   .01506072   .01430369   .04534288
   15.time_p   .01570176   .00907184   .02012735   .07446221
10000o.tim~p           0           0           0           0           0
     1b.wave           0           0           0           0           0           0
      2.wave  -.00016593  -.00003087  -.00012633  -.00022978           0           0   .00173501
      3.wave  -.00014319  -.00015217  -.00011792  -.00038208           0           0   .00116074   .00214878
      4.wave  -.00073286  -.00031076  -.00065477  -.00053954           0           0   .00108866   .00148109   .00288432
      5.wave  -.00108308  -.00078146  -.00096545  -.00157307           0           0   .00106709   .00137281   .00176567   .00283431
      6.wave  -.00079166  -.00069855   -.0006231  -.00066869           0           0   .00108742   .00134879   .00174889    .0019333   .00323265
      7.wave  -.00190419  -.00140403  -.00135118  -.00184273           0           0   .00104914   .00133437   .00172043   .00203929    .0022427   .00382504
      8.wave  -.00175749  -.00240636  -.00330719  -.00233126           0           0   .00107648   .00133673   .00159677    .0018903   .00196676   .00242668
      9.wave  -.00202793  -.00274911  -.00296738  -.00429442           0           0   .00116253   .00140297    .0017268   .00204426   .00208193     .002422
       _cons   -.0002058  -.00034117  -.00003788   .00012119           0           0  -.00101319  -.00122057  -.00142482  -.00148226  -.00154931  -.00162723

                       8.          9.
                    wave        wave       _cons
      8.wave   .00401045
      9.wave   .00291273   .00443782
       _cons  -.00159806  -.00168915           0

.
. eststo men                                                                                                                                              // Saving the estimates

.
.
.
. // Graph for both women and men
. #delimit ;
delimiter now ;
. coefplot  (women) (men), vertical yline(0)                                                                              // Graphing the trajectories from 2 models
>                   drop(1b.wave 2.wave 3.wave 4.wave 5.wave 6.wave 7.wave 8.wave 9.wave)
>                   order(0.time_p 1.time_p 2.time_p 3.time_p 4.time_p 5.time_p 6.time_p
>                   _cons 8.time_p 9.time_p 10.time_p 11.time_p 12.time_p 13.time_p 14.time_p 15.time_p)
>                   coeflabels(_cons = "-1")
>                   yscale(range(-1.5 1))
>                   ylab(-1.5(.5)1)
>                   xtitle(Years before/after marriage)
>                   ytitle(Happiness)
>                   recast(line) noci                                                                                                             // Simple lines for ease of demonstration
>                   ;

. #delimit cr
delimiter now cr
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
end of do-file

. exit, clear