Labor Economics

### Navigation
Click on a task in the content table in order to jump to it.

Click on the hyperlinked parts of the task text to get back to the table of content.

## Content
- **[Introduction](#intro)**

- **[Homework 1](#homework1): Get the data**

- **[Homework 2](#homework2): Linkage validation**

- **[Task 1: Insight & descriptive statistics](#task1)**

    - **[1.1 How many unique respondents are introduced into the panel each year?](#task1.1)**

    - **[1.2 What is the average yearly share of female respondents overall? How does the average annual share evolve from 2000-2020?](#task1.2)**

    - **[1.3 What is the average yearly share of respondents holding at least a college degree overall? How does the average annual share evolve from 2000-2020?](#task1.3)**

    - **[1.4 Plot the distribution of hourly wages for the years 2001, 2009 and 2018.](#task1.4)**

    - **[1.5 Is the data representative of the distribution of the population across all 50 U.S. states in 2017?](#task1.5)**

    - **[1.6 You have the task to create statistics of employment status, occupation, wage and education.
You are also asked to do this analysis for the subpopulations defined by age, race, sex and citizenship.
How would you proceed?](#task1.6)**

- **[Task 2: Effects of wage on hours worked (intensive margin)](#task2)**

    - **[2.1 Examine the effect of wage on hours worked (intensive margin of labour supply) using an OLS.](#task2.1)**

    - **[2.2 Determine the correlation between wages and hours worked. What is the difference between OLS and correlation?](#task2.2)**

    - **[2.3 Add age, sex and citizenship to your model. Include the log of wage and hours. Interpret the results of a multivariate regression.](#task2.3)**

    - **[2.4 Run the following multivariate regression:

\\( \log hours = \beta_0 + \beta_1 \log wage_i + \beta_2 age_i + \beta_3 educ2_i + \beta_4 sex_i + \beta_5 (educ2_i \times sex_i)\\);
 with educ2 being a dummy variable for a respondent holding at least a college degree.
Interpret your results.](#task2.4)**

- **[Task 3: Visualisation of employment flows](#task3)**

    - **[3.1 Derive information on monthly employment flows. Disregard marginal in- and outflows. Do so for every month in your data.](#task3.1)**

    - **[3.2 Find out the monthly flows for the U.S. for a period of years covered by your cps sample aswell. Try to visualise the monthly flows](#task3.2)**

    - **[3.3 Calculate the monthly unemployment rate for for the months from 2018 onwards. Plot the time series.](#task3.3)**

    - **[3.4 Create a time series for the mean age of people being unemployed. What effect on the mean age did the Covid crisis show?](#task3.4)**

- **[Task 4: Minimum wages and labor supply - a DiD-approach](#task4)**

    - **[4.1 Massachusetts increased its state level minimum wage from 10 \\$ to 11 \\$ per hour in 2017 (effective from 01.01.2017). The neighbouring New Hampshire kept its minimum wage at the federal level, i.e. 7.25 \\$ per hour. Using the CPS data set, run a DiD analysis to scrutinize the effect of the minimum wage increase on labour supply in Massachusetts (STATECENSUS == 14) relative to New Hampshire (STATECENSUS == 12).
](#task4.1)**

    -**[Task 4.2 Repeat the analysis above for two or more neighbouring state of your choice for a suitable date.](#task4.2)**

-**[Task 5 The effect of maximum duration of unemployment benefits on labor supply - Regression Discontinuity Design](#task5)**

    - **[5.1 In 2017, the maximum duration of unemployment benefits was
- 12 weeks for Gerogia & Nevada,
- 13 weeks for Missouri,
- 14 weeks for Hawaii,
- 16 weeks for Kentucky,
- 20 weeks for Arizona, Minnesota & South Dakota,
- 21 weeks for Indiana,
- 28 weeks for Nebraska,
- 30 weeks for Maryland
- and 26 weeks in all other states.

Use the complete dataset of IPUMS CPS for 2017.
 Create a dummy variable indicating an individual being unemployed longer than the maximum duration of benefits.](#task5.1)**

    - **[5.2 Have a look at the sanity of the data at hand.
 For every individual check the distance in weeks between two consecutive interviews as well as the difference between $durunemp_{t}$ and $durunemp_{t+1}$. What does strike your eye?](#task5.2)**

    - **[5.3 Create variables indicating whether an individual was employed or unemployed at the next interview.

Create another variable indicating whether a person might not have been unemployed between the interviews.](#task5.3)**

    - **[5.4 How many observations of unemployed individuals are still eligible for unemployment benefits?
 Find a way to detect the number of unemployment spells for an individual throughout her survey lifetime.](#task5.4)**

    - **[5.5 Create a running variable indicating the time in weeks an individual is away from her unemployment benefits to expire.
Considering maximum duration of continuous unemployment, how many individuals were still eligible for benefits?](#task5.5)**

    - **[5.6 Perform an analysis of the effect of the expiration of unemployment benefits on the employment. Use a regression discontinuity design for linear fit and polynomial fit (2nd and 3rd degree).](#task5.6)**

### [Exam](#content)
- project work:
    - by yourself or in a group
    - presentation at the end of the term
    - brief written summary of your work, 3-5 pages

### Exercise class
- biweekly
- working on data
- small exercises
- working on your own project

### Tools
- Stata

Feel free to use any code you like for your own analysis (R, SAS, Stata, Python, ...). However, since we had to decide
on a tool to be used in this class, I am going to work with Stata.

### Data
- Current Population Survey (U.S.)
- CPS-Supplements

## Stata

getting started
-   https://ell.stackexchange.com/questions/91623/difference-between-by-and-via
List of helpful links for beggining to work with stata

- https://www.stata.com/features/
List of features in Stata

- vast amount of literature and tutorials available online

## Current Population Survey

- cps homepage
- started in 1940, open data, free access
- monthly survey of ~ 60.000 U.S. households
- participants: over 15 years old, not part of the armed forces, not working in institutions such as prisons, long-term care hospitals and nursing homes
- conducted by United States Census Bureau, commissioned by Bureau of Labor Statistics

- questionnaire covers demographic and economic (especially labor force) topics
- expanded by regular (at least yearly) supplements, such as
    - Social and Economic Supplement
    - Computer and Internet Use
    - Tobacco Use
    - Civic Engagement
    - ...

    (complete list of supplements: LINK)

- process of data collection (monthly data):
    - households are interviewed in the week of the 19th of a month
    - revisited for three following months
    - revisited again in the next year and the very same four months

4-8-4 pattern

## IPUMS

- problem: CPS solely assigns unique identifiers to households, not to individuals
    - i.e. the same household is revisited seven times - regardless of which family or individual answers the door
- longitudinal research on an individual level complicated, but not impossible

- linking items via matching making use of variables such as household identifier, line number within household and demographic characteristics (e.g. sex, age, race)

    - Drew, J.A., Flood, S. and Warren, J.R. (2014) "Making full use of the longitudinal design of the Current Population Survey:
    Methods for linking records across 16 months", Journal of Economic and Social Measurement, 39, pp. 121-144.
 DOI 10.3233/JEM-140388
- accordingly prepared microdata available in the Integrated Public Use Microdata Series (IPUMS-CPS) LINK

## [Homework 1](#content)

Visit https://cps.ipums.org/cps/ and create an account. Register with any valid e-mail adress, preferably your uni-mail adress. Select all the variables and samples potentially relevant to your project.
Use the desciprtion of the variables to decide which to include in your extract. Try to minimise the number of variables in your extract.
Submit your request. It may take some time for your extract to be prepared and ready for download. The download link will be provided to you via e-mail once the data is processed.

Hint #1: If you already stumbled upon research papers dealing with questions similar to your project and working with CPS-data, you may include the author's choice of variables in your set as well.

Hint #2: take your time. This task is not trivial. Your work depends on the data at hand. Carefully chosen variables suited for answering a particular question facilitate your research.

- We confine our analysis to the years from 2000 onwards
- Choose your variables wisely: The number of variables inflates the size of the data set. E.g. 80 variables across monthly data between 2000 and 2019 constitute more than 7 gb of data
- adding supplements you receive data sets as large as 50 gb and more

- the bigger your extract,
    - the more time it takes the IPUMS server to process your request (from several hours to possibly several days)
    - the more time it takes you to download it
    - the more time and processing power is required to modify and analyse the data
- Good practice: Select cases to reduce the size, drop redundant variables (e.g. recodes), do not include every supplement (since for most individuals in the monthly survey the values are blank anyway)

## [Homework](#content)

Validate the CPSID linkages following the hints in the description of the variable. (LINK to description)

It is recommended to verify the linkages by checking the consistency of the variables AGE, RACE and SEX.
For a linkage to be valid, a CPSID should have a unique gender and race. Though age is not a static variable, it should be in a certain range for the survey lifetime of an individual, i.e. between $AGE_{i, t=1} = AGE_{i, t=0} + d$ with $d \in [0, 2]$.

Hint: This is not an easy task to do. You might come up with an own approach outside of Stata, you might be able to write proper code in Stata.
Furthermore, you have to consider whether you want to check the linkages for two consecutive months only or if you want to check the whole survey lifetime of a participant.

Here is my approach for validating the linkages between two consecutive months:

```stata
* loading data
cd "C:\Users\Hannes\Documents"
use "session01.dta", clear

* Creating lead variables
bysort cpsidp (mish): gen lead_mish = mish[_n+1]
bysort cpsidp (mish): gen lead_age = age[_n+1] if lead_mish - mish == 1
bysort cpsidp (mish): gen lead_race = race[_n+1] if lead_mish - mish == 1
bysort cpsidp (mish): gen lead_sex = sex[_n+1] if lead_mish - mish == 1
```

    C:\Users\Hannes\Documents

    (1,773,756 missing values generated)

    (1,993,760 missing values generated)

    (1,993,760 missing values generated)

    (1,993,760 missing values generated)

```stata
* Creating lead variables
bysort cpsidp (mish): gen lag_mish = mish[_n-1]
bysort cpsidp (mish): gen lag_age = age[_n-1] if mish - lag_mish == 1
bysort cpsidp (mish): gen lag_race = race[_n-1] if mish - lag_mish == 1
bysort cpsidp (mish): gen lag_sex = sex[_n-1] if mish - lag_mish == 1
```

    (1,773,756 missing values generated)

    (1,993,760 missing values generated)

    (1,993,760 missing values generated)

    (1,993,760 missing values generated)

```stata
* validation
gen linked_f = .m
replace linked_f = 1 if lead_mish - mish == 1 & lead_race == race & ///
lead_sex == sex & lead_age - age >= 0 & lead_age - age <= 1
```

    lead_mish not found

    r(111);
    r(111);

```stata
* flag for linked backwards and/or forwards
gen linked = linked_f == 1 | linked_b == 1
```

```stata
tab linked
```

         linked |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              0 |    197,314        1.74        1.74
              1 | 11,157,023       98.26      100.00
    ------------+-----------------------------------
          Total | 11,354,337      100.00

What makes the linking invalid for those 197,5 k observations?

```stata
gen age_diff = lead_age - age
tab age_diff if linked == 0 & !inrange(age_diff, 0, 2)
```

    (1,993,760 missing values generated)

       age_diff |      Freq.     Percent        Cum.
    ------------+-----------------------------------
            -52 |          1        0.00        0.00
            -51 |          2        0.01        0.01
            -50 |          4        0.01        0.02
            -49 |          2        0.01        0.03
            -48 |          3        0.01        0.03
            -47 |          4        0.01        0.05
            -46 |          6        0.02        0.06
            -45 |          9        0.03        0.09
            -44 |         17        0.05        0.14
            -43 |         12        0.03        0.17
            -42 |         17        0.05        0.22
            -41 |         20        0.06        0.27
            -40 |         22        0.06        0.34
            -39 |         30        0.09        0.42
            -38 |         38        0.11        0.53
            -37 |         28        0.08        0.61
            -36 |         43        0.12        0.73
            -35 |         49        0.14        0.87
            -34 |         42        0.12        0.99
            -33 |         53        0.15        1.14
            -32 |         58        0.16        1.30
            -31 |         63        0.18        1.48
            -30 |         75        0.21        1.70
            -29 |         78        0.22        1.92
            -28 |         95        0.27        2.19
            -27 |         93        0.26        2.45
            -26 |        105        0.30        2.75
            -25 |        103        0.29        3.04
            -24 |        105        0.30        3.34
            -23 |        132        0.37        3.71
            -22 |        147        0.42        4.13
            -21 |        160        0.45        4.58
            -20 |        204        0.58        5.16
            -19 |        189        0.54        5.69
            -18 |        192        0.54        6.24
            -17 |        203        0.58        6.81
            -16 |        216        0.61        7.43
            -15 |        241        0.68        8.11
            -14 |        268        0.76        8.87
            -13 |        308        0.87        9.74
            -12 |        330        0.94       10.68
            -11 |        354        1.00       11.68
            -10 |        632        1.79       13.47
             -9 |        518        1.47       14.94
             -8 |        607        1.72       16.66
             -7 |        665        1.89       18.55
             -6 |        729        2.07       20.61
             -5 |      1,339        3.80       24.41
             -4 |      1,283        3.64       28.05
             -3 |      1,812        5.14       33.18
             -2 |      2,689        7.62       40.80
             -1 |      6,466       18.33       59.13
              3 |      2,292        6.50       65.63
              4 |      1,598        4.53       70.16
              5 |      1,762        4.99       75.15
              6 |      1,012        2.87       78.02
              7 |        876        2.48       80.51
              8 |        688        1.95       82.46
              9 |        625        1.77       84.23
             10 |        788        2.23       86.46
             11 |        482        1.37       87.83
             12 |        340        0.96       88.79
             13 |        321        0.91       89.70
             14 |        316        0.90       90.60
             15 |        297        0.84       91.44
             16 |        227        0.64       92.08
             17 |        208        0.59       92.67
             18 |        224        0.63       93.31
             19 |        217        0.62       93.92
             20 |        221        0.63       94.55
             21 |        171        0.48       95.03
             22 |        175        0.50       95.53
             23 |        139        0.39       95.92
             24 |        144        0.41       96.33
             25 |        114        0.32       96.66
             26 |        127        0.36       97.02
             27 |        117        0.33       97.35
             28 |        106        0.30       97.65
             29 |         99        0.28       97.93
             30 |        102        0.29       98.22
             31 |         89        0.25       98.47
             32 |         72        0.20       98.67
             33 |         66        0.19       98.86
             34 |         60        0.17       99.03
             35 |         49        0.14       99.17
             36 |         46        0.13       99.30
             37 |         49        0.14       99.44
             38 |         29        0.08       99.52
             39 |         22        0.06       99.58
             40 |         29        0.08       99.67
             41 |         28        0.08       99.74
             42 |         33        0.09       99.84
             43 |         13        0.04       99.88
             44 |         15        0.04       99.92
             45 |          8        0.02       99.94
             46 |          8        0.02       99.96
             47 |          4        0.01       99.97
             48 |          3        0.01       99.98
             49 |          1        0.00       99.99
             50 |          4        0.01      100.00
             51 |          1        0.00      100.00
    ------------+-----------------------------------
          Total |     35,278      100.00

```stata
numlabel, add
tab sex lead_sex if linked == 0 & sex ~= lead_sex
```

               |       lead_sex
           sex |         1          2 |     Total
    -----------+----------------------+----------
       1. male |         0      1,059 |     1,059
     2. female |     1,328          0 |     1,328
    -----------+----------------------+----------
         Total |     1,328      1,059 |     2,387

```stata
tab race lead_race if linked == 0 & race ~= lead_race
```

                          |                  lead_race
                     race |       100        200        300        650 |     Total
    ----------------------+--------------------------------------------+----------
               100. white |         0        688        132         47 |     1,573
         200. black/negro |       643          0         17          8 |       789
    300. american indian/ |       151         22          0          0 |       226
    650. asian or pacific |        82          9          1          0 |       476
          651. asian only |       355         63          8          0 |       471
    652. hawaiian/pacific |        43         11          2          0 |        91
         801. white-black |        45         22          2          0 |        79
    802. white-american i |        59          4          4          0 |        74
         803. white-asian |        16          0          0          0 |        24
    804. white-hawaiian/p |         2          1          0          0 |         7
    805. black-american i |         4          1          0          0 |         8
         806. black-asian |         0          1          0          0 |         9
    808. american indian- |         0          0          0          0 |         1
    809. asian-hawaiian/p |         2          0          0          0 |         8
    810. white-black-amer |         1          0          0          0 |         3
    811. white-black-asia |         0          0          0          0 |         1
    812. white-american i |         1          0          0          0 |         1
    813. white-asian-hawa |         1          0          0          0 |         3
    816. white-black--haw |         0          0          0          0 |         1
    820. two or three rac |         0          0          0          0 |         9
    ----------------------+--------------------------------------------+----------
                    Total |     1,405        822        166         55 |     3,854

                          |                  lead_race
                     race |       651        652        801        802 |     Total
    ----------------------+--------------------------------------------+----------
               100. white |       371         52         84        136 |     1,573
         200. black/negro |        64          4         25          3 |       789
    300. american indian/ |        13          0          1         35 |       226
    650. asian or pacific |       336         27          0          1 |       476
          651. asian only |         0         16          5          0 |       471
    652. hawaiian/pacific |        31          0          0          0 |        91
         801. white-black |         2          0          0          4 |        79
    802. white-american i |         5          0          2          0 |        74
         803. white-asian |         5          1          0          1 |        24
    804. white-hawaiian/p |         2          0          0          0 |         7
    805. black-american i |         1          0          1          0 |         8
         806. black-asian |         8          0          0          0 |         9
    808. american indian- |         1          0          0          0 |         1
    809. asian-hawaiian/p |         4          0          0          0 |         8
    810. white-black-amer |         0          0          2          0 |         3
    811. white-black-asia |         0          0          1          0 |         1
    812. white-american i |         0          0          0          0 |         1
    813. white-asian-hawa |         0          1          0          0 |         3
    816. white-black--haw |         0          0          0          0 |         1
    820. two or three rac |         0          0          0          0 |         9
    ----------------------+--------------------------------------------+----------
                    Total |       843        101        121        180 |     3,854

                          |                  lead_race
                     race |       803        804        805        806 |     Total
    ----------------------+--------------------------------------------+----------
               100. white |        37          8          5          1 |     1,573
         200. black/negro |         4          0         14          0 |       789
    300. american indian/ |         0          0          2          0 |       226
    650. asian or pacific |         7          5          0          0 |       476
          651. asian only |        16          0          1          1 |       471
    652. hawaiian/pacific |         0          3          0          0 |        91
         801. white-black |         2          0          0          0 |        79
    802. white-american i |         0          0          0          0 |        74
         803. white-asian |         0          0          0          0 |        24
    804. white-hawaiian/p |         0          0          0          0 |         7
    805. black-american i |         0          0          0          0 |         8
         806. black-asian |         0          0          0          0 |         9
    808. american indian- |         0          0          0          0 |         1
    809. asian-hawaiian/p |         0          0          0          0 |         8
    810. white-black-amer |         0          0          0          0 |         3
    811. white-black-asia |         0          0          0          0 |         1
    812. white-american i |         0          0          0          0 |         1
    813. white-asian-hawa |         0          1          0          0 |         3
    816. white-black--haw |         0          0          0          0 |         1
    820. two or three rac |         0          0          0          0 |         9
    ----------------------+--------------------------------------------+----------
                    Total |        66         17         22          2 |     3,854

                          |                  lead_race
                     race |       807        808        809        810 |     Total
    ----------------------+--------------------------------------------+----------
               100. white |         0          2          3          1 |     1,573
         200. black/negro |         2          0          0          2 |       789
    300. american indian/ |         0          1          0          0 |       226
    650. asian or pacific |         0          0          4          0 |       476
          651. asian only |         0          0          4          1 |       471
    652. hawaiian/pacific |         0          0          1          0 |        91
         801. white-black |         0          0          0          1 |        79
    802. white-american i |         0          0          0          0 |        74
         803. white-asian |         0          0          0          0 |        24
    804. white-hawaiian/p |         0          0          0          0 |         7
    805. black-american i |         0          0          0          1 |         8
         806. black-asian |         0          0          0          0 |         9
    808. american indian- |         0          0          0          0 |         1
    809. asian-hawaiian/p |         0          0          0          0 |         8
    810. white-black-amer |         0          0          0          0 |         3
    811. white-black-asia |         0          0          0          0 |         1
    812. white-american i |         0          0          0          0 |         1
    813. white-asian-hawa |         0          0          0          0 |         3
    816. white-black--haw |         1          0          0          0 |         1
    820. two or three rac |         0          0          1          0 |         9
    ----------------------+--------------------------------------------+----------
                    Total |         3          3         13          6 |     3,854

                          |                  lead_race
                     race |       811        812        813        815 |     Total
    ----------------------+--------------------------------------------+----------
               100. white |         3          0          2          0 |     1,573
         200. black/negro |         1          1          0          0 |       789
    300. american indian/ |         0          0          0          1 |       226
    650. asian or pacific |         0          1          1          0 |       476
          651. asian only |         0          0          1          0 |       471
    652. hawaiian/pacific |         0          0          0          0 |        91
         801. white-black |         0          0          1          0 |        79
    802. white-american i |         0          0          0          0 |        74
         803. white-asian |         0          1          0          0 |        24
    804. white-hawaiian/p |         0          0          2          0 |         7
    805. black-american i |         0          0          0          0 |         8
         806. black-asian |         0          0          0          0 |         9
    808. american indian- |         0          0          0          0 |         1
    809. asian-hawaiian/p |         0          0          2          0 |         8
    810. white-black-amer |         0          0          0          0 |         3
    811. white-black-asia |         0          0          0          0 |         1
    812. white-american i |         0          0          0          0 |         1
    813. white-asian-hawa |         0          0          0          0 |         3
    816. white-black--haw |         0          0          0          0 |         1
    820. two or three rac |         0          0          8          0 |         9
    ----------------------+--------------------------------------------+----------
                    Total |         4          3         17          1 |     3,854

                          |       lead_race
                     race |       820        830 |     Total
    ----------------------+----------------------+----------
               100. white |         1          0 |     1,573
         200. black/negro |         0          1 |       789
    300. american indian/ |         0          0 |       226
    650. asian or pacific |         2          0 |       476
          651. asian only |         0          0 |       471
    652. hawaiian/pacific |         0          0 |        91
         801. white-black |         0          0 |        79
    802. white-american i |         0          0 |        74
         803. white-asian |         0          0 |        24
    804. white-hawaiian/p |         0          0 |         7
    805. black-american i |         0          0 |         8
         806. black-asian |         0          0 |         9
    808. american indian- |         0          0 |         1
    809. asian-hawaiian/p |         0          0 |         8
    810. white-black-amer |         0          0 |         3
    811. white-black-asia |         0          0 |         1
    812. white-american i |         0          0 |         1
    813. white-asian-hawa |         0          0 |         3
    816. white-black--haw |         0          0 |         1
    820. two or three rac |         0          0 |         9
    ----------------------+----------------------+----------
                    Total |         3          1 |     3,854

```stata
by cpsidp: egen maxmish = max(mish)
tab maxmish
```

        maxmish |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              1 |      8,880        0.08        0.08
              2 |     28,653        0.25        0.33
              3 |     62,278        0.55        0.88
              4 |    700,541        6.17        7.05
              5 |    123,755        1.09        8.14
              6 |    170,636        1.50        9.64
              7 |    302,570        2.66       12.31
              8 |  9,957,024       87.69      100.00
    ------------+-----------------------------------
          Total | 11,354,337      100.00

```stata
egen tag_help = tag(cpsidp month year)
egen obs = total(tag_help), by(cpsidp)
drop tag_help

tab obs if obs == 1 & obs ~= maxmish

```

            obs |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              1 |     19,057      100.00      100.00
    ------------+-----------------------------------
          Total |     19,057      100.00

```stata
gen mish_delta = lead_mish - mish
tab mish_delta if mish_delta ~= 1 & linked == 0
```

    (1,773,756 missing values generated)

     mish_delta |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              2 |     44,397       71.52       71.52
              3 |      9,370       15.09       86.61
              4 |      6,290       10.13       96.74
              5 |      1,237        1.99       98.74
              6 |        517        0.83       99.57
              7 |        267        0.43      100.00
    ------------+-----------------------------------
          Total |     62,078      100.00

So why do we make such an effort looking at invalid linkages?

Because you ought to know the data you are about to dismiss as well as the data you would like to workt with.
Checking you data for sanity might reduce the risk of measurement errors (wrong observations).
Apparently the concept of CPSIDP is not as trustworthy as one would have thought beforehand.

Now that we have our data set, we can start working with it.

# [Insight & descriptive statistics (Task 1)](#content)

### 1.1 [How many unique respondents are introduced into the panel each year?](#content)

```stata
* count unique new individuals by year
sort year month
egen tag_help = tag(cpsidp)
egen num_entries = total(tag_help), by(year)
tabdisp year, c(num_entries)
```

    -----------------------
    survey    |
    year      | num_entries
    ----------+------------
         2000 |      118318
         2001 |       90452
         2002 |       88235
         2003 |       85354
         2004 |       85452
         2005 |       88015
         2006 |       85214
         2007 |       84712
         2008 |       83922
         2009 |       85156
         2010 |       84026
         2011 |       82859
         2012 |       82899
         2013 |       80171
         2014 |       82359
         2015 |       83782
         2016 |       80282
         2017 |       77176
         2018 |       74552
         2019 |       70910
         2020 |       53685
         2021 |       26225
    -----------------------

### [1.2 What is the average yearly share of female respondents overall? How does the average annual share evolve from 2000-2020?](#content)

```stata
* share of female responses

tab sex
replace sex = sex - 1
label define sex 0 "male" 1 "female"
label value sex sex

* share overall
mean sex

* or simply
tab sex

* share per year
bysort year: egen fem = mean(sex)
table year, content(max fem)
* or
tab year sex, row
```

        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
             sex | 11,354,337    1.514263    .4997965          1          2

    (11,354,337 real changes made)

    Mean estimation                   Number of obs   = 11,354,337

    --------------------------------------------------------------
                 |       Mean   Std. Err.     [95% Conf. Interval]
    -------------+------------------------------------------------
             sex |   .5142631   .0001483      .5139724    .5145539
    --------------------------------------------------------------

    ----------------------
    survey    |
    year      |   max(fem)
    ----------+-----------
         2000 |   .5150207
         2001 |    .515788
         2002 |   .5150558
         2003 |    .515885
         2004 |   .5148885
         2005 |   .5155335
         2006 |   .5166667
         2007 |   .5157396
         2008 |   .5139523
         2009 |   .5114723
         2010 |   .5131696
         2011 |    .515455
         2012 |   .5148475
         2013 |   .5144394
         2014 |   .5130008
         2015 |   .5133336
         2016 |   .5131161
         2017 |   .5143098
         2018 |   .5136473
         2019 |   .5123831
         2020 |   .5126786
         2021 |   .5099174
    ----------------------

    +----------------+
    | Key            |
    |----------------|
    |   frequency    |
    | row percentage |
    +----------------+

        survey |          sex
          year |      male     female |     Total
    -----------+----------------------+----------
          2000 |   195,532    207,644 |   403,176
               |     48.50      51.50 |    100.00
    -----------+----------------------+----------
          2001 |   257,579    274,376 |   531,955
               |     48.42      51.58 |    100.00
    -----------+----------------------+----------
          2002 |   281,561    299,044 |   580,605
               |     48.49      51.51 |    100.00
    -----------+----------------------+----------
          2003 |   281,372    299,837 |   581,209
               |     48.41      51.59 |    100.00
    -----------+----------------------+----------
          2004 |   273,908    290,721 |   564,629
               |     48.51      51.49 |    100.00
    -----------+----------------------+----------
          2005 |   271,854    289,287 |   561,141
               |     48.45      51.55 |    100.00
    -----------+----------------------+----------
          2006 |   272,846    291,663 |   564,509
               |     48.33      51.67 |    100.00
    -----------+----------------------+----------
          2007 |   271,411    289,054 |   560,465
               |     48.43      51.57 |    100.00
    -----------+----------------------+----------
          2008 |   272,943    288,613 |   561,556
               |     48.60      51.40 |    100.00
    -----------+----------------------+----------
          2009 |   276,748    289,746 |   566,494
               |     48.85      51.15 |    100.00
    -----------+----------------------+----------
          2010 |   275,194    290,083 |   565,277
               |     48.68      51.32 |    100.00
    -----------+----------------------+----------
          2011 |   269,893    287,110 |   557,003
               |     48.45      51.55 |    100.00
    -----------+----------------------+----------
          2012 |   268,463    284,895 |   553,358
               |     48.52      51.48 |    100.00
    -----------+----------------------+----------
          2013 |   264,413    280,139 |   544,552
               |     48.56      51.44 |    100.00
    -----------+----------------------+----------
          2014 |   258,617    272,425 |   531,042
               |     48.70      51.30 |    100.00
    -----------+----------------------+----------
          2015 |   253,397    267,282 |   520,679
               |     48.67      51.33 |    100.00
    -----------+----------------------+----------
          2016 |   254,819    268,548 |   523,367
               |     48.69      51.31 |    100.00
    -----------+----------------------+----------
          2017 |   249,060    263,736 |   512,796
               |     48.57      51.43 |    100.00
    -----------+----------------------+----------
          2018 |   239,464    252,903 |   492,367
               |     48.64      51.36 |    100.00
    -----------+----------------------+----------
          2019 |   229,355    241,004 |   470,359
               |     48.76      51.24 |    100.00
    -----------+----------------------+----------
          2020 |   190,645    200,565 |   391,210
               |     48.73      51.27 |    100.00
    -----------+----------------------+----------
          2021 |   106,146    110,442 |   216,588
               |     49.01      50.99 |    100.00
    -----------+----------------------+----------
         Total | 5,515,220  5,839,117 |11,354,337
               |     48.57      51.43 |    100.00

If you trying to calculate the share of a variable, yet it is not binary coded, you can try different approaches.

tab gives you pretty much everything you want:

>tab year sex, row

If you want to store the share as a ne variable, you have to come up with something different.

Let us generate a variable for the share of unique female respondents:

```stata
egen fem_counter = tag(year cpsidp) if sex == 1
egen fem_total = total(fem_counter), by(year)
egen both_counter = tag(year cpsidp)
egen respondents_total = total(both_counter), by(year)
gen fem_share = fem_total / respondents_total
```

```stata
save "workfile01.dta", replace
```

    file workfile01.dta saved

```stata
collapse fem_share, by(year)
twoway (line fem_share year, sort), ytitle(share) xtitle(year) title(Share of female respondents) yscale(range(0.00 1.00)) ylabel(0(0.1)1)
```

```stata
use "workfile01.dta", clear
```

I strongly recommend to drop the auxiliary variables after that, so that the table stays clear and tidy.

```stata
drop fem_* both_* respondents_*
```

### [1.3 What is the average yearly share of respondents holding at least a college degree overall? How does the average annual share evolve from 2000-2020?](#content)

For the average share of respondents holding a college degree you have to calculated the share of respondents instead the share of responses. You also have to keep in mind that individuals are paticipating up to four times a year in the survey.
The following code considers this as well as the fact that a person can obtain college degree between the waves of interviews. The highes degree obtained within a year will be counted.

```stata
tab educ
gen college = .
replace college = 1 if inlist(educ, 125, 124, 092, 123, 111)

egen tag_college = tag(year cpsidp college)
egen tag_respondents = tag(year cpsidp)

egen college_total = total(tag_college), by(year)
egen respondents_total = total(tag_respondents), by(year)

gen college_share = college_total / respondents_total
```

              educational attainment recode |      Freq.     Percent        Cum.
    ----------------------------------------+-----------------------------------
                            1. niu or blank |          9        0.00        0.00
                       2. none or preschool |     29,001        0.26        0.26
                   10. grades 1, 2, 3, or 4 |     55,654        0.49        0.75
                          20. grades 5 or 6 |    124,978        1.10        1.85
                          30. grades 7 or 8 |    221,665        1.95        3.80
                                40. grade 9 |    335,882        2.96        6.76
                               50. grade 10 |    414,008        3.65       10.40
                               60. grade 11 |    447,258        3.94       14.34
                 71. 12th grade, no diploma |    165,357        1.46       15.80
      73. high school diploma or equivalent |  3,219,208       28.35       44.15
             81. some college but no degree |  2,086,324       18.37       62.53
    91. associate's degree, occupational/vo |    506,645        4.46       66.99
    92. associate's degree, academic progra |    545,433        4.80       71.79
                     111. bachelor's degree |  2,106,549       18.55       90.34
                       123. master's degree |    798,942        7.04       97.38
            124. professional school degree |    150,658        1.33       98.71
                      125. doctorate degree |    146,766        1.29      100.00
    ----------------------------------------+-----------------------------------
                                      Total | 11,354,337      100.00

    (11,354,337 missing values generated)

    (3,748,348 real changes made)

```stata
save "workfile01.dta", replace
```

    file workfile01.dta saved

```stata
collapse college_share, by(year)
twoway (line college_share year, sort), ytitle(share) xtitle(year) title(Share of respondents holding at least a college degree) yscale(range(0.00 1.00)) ylabel(0(0.1)1)
```

```stata
use "workfile01.dta", clear
```

```stata
drop college* respondents*
```

### [1.4 Plot the distribution of hourly wages for the years 2001, 2009 and 2018.](#content)

```stata
mvdecode(hourwage), mv(999.99)

qui histogram hourwage if year==2001, name(h2001) yscale(range(0 .1)) ylabel(0(0.02)0.1)
qui histogram hourwage if year==2009, name(h2009) yscale(range(0 .1)) ylabel(0(0.02)0.1)
qui histogram hourwage if year==2018, name(h2018) yscale(range(0 .1)) ylabel(0(0.02)0.1)
graph combine h2001 h2009 h2018, col(3)
```

### [1.5 Is the data representative of the distribution of the population across all 50 U.S. states in 2017?](#content)

```stata
tab statecensus
```

                        state (census code) |      Freq.     Percent        Cum.
    ----------------------------------------+-----------------------------------
                                  11. maine |    171,775        1.51        1.51
                          12. new hampshire |    208,615        1.84        3.35
                                13. vermont |    161,983        1.43        4.78
                          14. massachusetts |    206,391        1.82        6.59
                           15. rhode island |    163,331        1.44        8.03
                            16. connecticut |    200,132        1.76        9.80
                               21. new york |    508,971        4.48       14.28
                             22. new jersey |    249,338        2.20       16.47
                           23. pennsylvania |    358,675        3.16       19.63
                                   31. ohio |    323,555        2.85       22.48
                                32. indiana |    185,484        1.63       24.12
                               33. illinois |    362,981        3.20       27.31
                               34. michigan |    289,975        2.55       29.87
                              35. wisconsin |    202,315        1.78       31.65
                              41. minnesota |    229,674        2.02       33.67
                                   42. iowa |    183,425        1.62       35.29
                               43. missouri |    174,191        1.53       36.82
                           44. north dakota |    153,968        1.36       38.18
                           45. south dakota |    158,552        1.40       39.57
                               46. nebraska |    164,934        1.45       41.03
                                 47. kansas |    159,595        1.41       42.43
                               51. delaware |    152,790        1.35       43.78
                               52. maryland |    210,504        1.85       45.63
                   53. district of columbia |    145,550        1.28       46.91
                               54. virginia |    225,874        1.99       48.90
                          55. west virginia |    170,770        1.50       50.41
                         56. north carolina |    241,747        2.13       52.54
                         57. south carolina |    154,789        1.36       53.90
                                58. georgia |    228,556        2.01       55.91
                                59. florida |    463,151        4.08       59.99
                               61. kentucky |    155,967        1.37       61.36
                              62. tennessee |    169,203        1.49       62.85
                                63. alabama |    166,799        1.47       64.32
                            64. mississippi |    138,288        1.22       65.54
                               71. arkansas |    145,579        1.28       66.82
                              72. louisiana |    156,549        1.38       68.20
                               73. oklahoma |    145,215        1.28       69.48
                                  74. texas |    574,448        5.06       74.54
                                81. montana |    143,027        1.26       75.80
                                  82. idaho |    147,604        1.30       77.10
                                83. wyoming |    152,347        1.34       78.44
                               84. colorado |    209,008        1.84       80.28
                             85. new mexico |    133,566        1.18       81.46
                                86. arizona |    153,674        1.35       82.81
                                   87. utah |    155,571        1.37       84.18
                                 88. nevada |    165,476        1.46       85.64
                             91. washington |    201,853        1.78       87.42
                                 92. oregon |    165,215        1.46       88.87
                             93. california |    952,149        8.39       97.26
                                 94. alaska |    148,309        1.31       98.57
                                 95. hawaii |    162,899        1.43      100.00
    ----------------------------------------+-----------------------------------
                                      Total | 11,354,337      100.00

```stata
egen distinct2017 = tag(cpsidp year) if year == 2017
tab statecensus if distinct2017 == 1
```

                        state (census code) |      Freq.     Percent        Cum.
    ----------------------------------------+-----------------------------------
                                  11. maine |      1,399        0.89        0.89
                          12. new hampshire |      2,136        1.36        2.26
                                13. vermont |      2,058        1.31        3.57
                          14. massachusetts |      3,368        2.15        5.72
                           15. rhode island |      1,436        0.92        6.64
                            16. connecticut |      1,602        1.02        7.66
                               21. new york |      6,411        4.09       11.75
                             22. new jersey |      3,165        2.02       13.78
                           23. pennsylvania |      4,433        2.83       16.61
                                   31. ohio |      4,024        2.57       19.17
                                32. indiana |      2,568        1.64       20.81
                               33. illinois |      4,561        2.91       23.73
                               34. michigan |      3,556        2.27       26.00
                              35. wisconsin |      2,151        1.37       27.37
                              41. minnesota |      2,077        1.33       28.70
                                   42. iowa |      1,758        1.12       29.82
                               43. missouri |      2,238        1.43       31.25
                           44. north dakota |      2,239        1.43       32.68
                           45. south dakota |      1,710        1.09       33.77
                               46. nebraska |      1,934        1.23       35.00
                                 47. kansas |      1,965        1.25       36.26
                               51. delaware |      1,712        1.09       37.35
                               52. maryland |      2,031        1.30       38.65
                   53. district of columbia |      2,646        1.69       40.34
                               54. virginia |      2,914        1.86       42.20
                          55. west virginia |      3,453        2.20       44.40
                         56. north carolina |      3,531        2.25       46.66
                         57. south carolina |      2,411        1.54       48.20
                                58. georgia |      3,560        2.27       50.47
                                59. florida |      6,820        4.35       54.82
                               61. kentucky |      2,024        1.29       56.12
                              62. tennessee |      3,014        1.92       58.04
                                63. alabama |      3,209        2.05       60.09
                            64. mississippi |      2,881        1.84       61.93
                               71. arkansas |      2,767        1.77       63.70
                              72. louisiana |      3,525        2.25       65.95
                               73. oklahoma |      2,258        1.44       67.39
                                  74. texas |      9,006        5.75       73.14
                                81. montana |      2,903        1.85       74.99
                                  82. idaho |      2,394        1.53       76.52
                                83. wyoming |      2,245        1.43       77.95
                               84. colorado |      1,882        1.20       79.15
                             85. new mexico |      2,710        1.73       80.88
                                86. arizona |      2,342        1.50       82.38
                                   87. utah |      2,340        1.49       83.87
                                 88. nevada |      1,945        1.24       85.12
                             91. washington |      2,846        1.82       86.93
                                 92. oregon |      2,321        1.48       88.41
                             93. california |     13,892        8.87       97.28
                                 94. alaska |      1,957        1.25       98.53
                                 95. hawaii |      2,296        1.47      100.00
    ----------------------------------------+-----------------------------------
                                      Total |    156,624      100.00

Compare the frequencies with official figures, which you can finde here:
census.gov

### [1.6 You have the task to create statistics of employment status, occupation, wage and education.
You are also asked to do this analysis for the subpopulations defined by age, race, sex and citizenship.
 How would you proceed?](#content)

The idea of this task is to rise awareness of the different scale of measurements present in this data set.
E.g., there is no point in calculating 5 point statistics for a nominal variable. Frequency tables are a helpful start for a data analysis.

There are different ways in stata to create meaningful statistics. When doing so for various subpopulations, you should avoid to render the statistics to granular, i.e. analsing too many groups. The more result tables you have, the harder it gets to compare them.

```stata
/* There exist a variety of commands that yield a quick overview of the data as well as some related basic statistics
and further information.*/
describe
```

```stata
codebook
```

```stata
tab year
```

```stata
summarize age, detail
```

```stata
// Create frequency tables using the tabulate command
tabulate educ sex, row
```

```stata
numlabel, add
tab educ
```

Value 92 represents associate's degrees. Those degrees academic degrees in the US and in other countries who introduced it. However, in most countries of the EU those degrees are not considered academic.
Now we have to decide whether to label those degrees as academic college degrees or not.
I choose not to do so, since the results of my analysis would be hard to compare with the situation in other countries where such a degree is unknown.

If you happen to work with this variable in your project, you may decide to label associate's degrees differently. However you proceed, please elaborate on your decision.

```stata
/* create new variable college that is 0 for every degree below college, 1 for every degree equivalent
or above college and .m for missing values */

gen college = 0
replace college = 1 if educ > 92
replace college = .m if educ == 1
```

```stata
tab sex college, row
```

```stata
bysort sex college: tab occ empstat, row
```

```stata
numlabel, add
tabulate faminc
```

```stata
mvdecode(faminc), mv(996 997 999)
```

```stata
tabstat faminc, by(year) statistics(p25, median, p75)
```

```stata
graph bar (count), over(faminc) over(year, label(angle(v))) asyvars stack percent
```

# [2. Effects of wage on hours worked (intensive margin)](#content)

```stata
*load data
cd "C:\Users\Hannes\Documents"
use "session01.dta", clear
```

    C:\Users\Hannes\Documents

```stata
gen hwage_r = round(hourwage, 1)
```

```stata
tab hwage_r
```

 IPUM's description of hourwage:
>HOURWAGE reports how much the respondent earned per hour in the current job, for those workers paid an hourly wage (and coded as "2" in PAIDHOUR). Amounts are expressed as they were reported to the interviewer; users must adjust for inflation using Consumer Price Index adjustment factors. Researchers should use the EARNWT weight with this variable.

 Users should note that HOURWAGE originally had two implied decimal places, but was revised so that the command files provided by IPUMS divide HOURWAGE by 100.

 HOURWAGE is one of the Outgoing Rotation/Earner Study questions.

```stata
summarize hourwage, detail
```

The 10th percentile is 999.99, which means "not in universe". Thus we are lacking information on hourly wage for at least 90% of the respondents.
The variable for wages in topcoded for the sake of discretion: Every wage that would result in a yearly income of more than \$ 100,000 (1985-2002) and \\$ 150,000 (since 2003) is coded as 99.99 in hourwage.

```stata
mvdecode(hourwage), mv(999.99)
```

        hourwage: 10334665 missing values generated

```stata
summarize hourwage, detail
```

So how many topcoded values do we have in hourwage?

```stata
tab hourwage if hourwage == 99.99
```

    hourly wage |      Freq.     Percent        Cum.
    ------------+-----------------------------------
          99.99 |        147      100.00      100.00
    ------------+-----------------------------------
          Total |        147      100.00

```stata
sum hourwage if hourwage < 99.99
```

```stata
mvdecode(hourwage), mv(99.99)
```

        hourwage: 147 missing values generated

```stata
tab month if !missing(hourwage)
```

### [2.1 Examine the effect of wage on hours worked (intensive margin of labour supply) using an OLS.](#content)

```stata
numlabel , add
tabulate uhrswork1
```

uhrswork1 = 999 denotes missing values. But how do we handle varying hours (i.e. uhrswork1=997)? In a way we are lacking information here as well.

```stata
tab empstat
```

From our understanding individuals with $empstat \in [1, 12]$ should work at least some hours a week.

```stata
tab uhrswork1 if inrange(empstat, 1, 12)
```

       hours usually |
     worked per week |
         at main job |      Freq.     Percent        Cum.
    -----------------+-----------------------------------
          0. 0 hours |      6,588        0.09        0.09
                   1 |      3,188        0.04        0.14
                   2 |      7,959        0.11        0.25
                   3 |      7,698        0.11        0.36
                   4 |     13,467        0.19        0.54
                   5 |     17,100        0.24        0.78
                   6 |     15,871        0.22        1.00
                   7 |      6,084        0.09        1.09
                   8 |     32,912        0.46        1.55
                   9 |      5,634        0.08        1.63
                  10 |     65,531        0.92        2.54
                  11 |      2,631        0.04        2.58
                  12 |     38,263        0.53        3.12
                  13 |      4,294        0.06        3.18
                  14 |      7,845        0.11        3.29
                  15 |     82,197        1.15        4.43
                  16 |     41,570        0.58        5.02
                  17 |      6,080        0.08        5.10
                  18 |     18,750        0.26        5.36
                  19 |      3,857        0.05        5.42
                  20 |    269,134        3.76        9.18
                  21 |      6,728        0.09        9.27
                  22 |     10,534        0.15        9.42
                  23 |      6,799        0.10        9.51
                  24 |     68,599        0.96       10.47
                  25 |    125,220        1.75       12.22
                  26 |      7,552        0.11       12.33
                  27 |      8,527        0.12       12.45
                  28 |     21,295        0.30       12.75
                  29 |      3,968        0.06       12.80
                  30 |    223,764        3.13       15.93
                  31 |      2,277        0.03       15.96
                  32 |     99,667        1.39       17.35
                  33 |      9,441        0.13       17.49
                  34 |      8,833        0.12       17.61
                  35 |    227,885        3.19       20.79
                  36 |     83,390        1.17       21.96
                  37 |     39,541        0.55       22.51
                  38 |     87,401        1.22       23.73
                  39 |      9,936        0.14       23.87
                  40 |  3,957,809       55.32       79.19
                  41 |      4,113        0.06       79.25
                  42 |     35,985        0.50       79.75
                  43 |     18,161        0.25       80.01
                  44 |     20,602        0.29       80.30
                  45 |    315,324        4.41       84.70
                  46 |     10,018        0.14       84.84
                  47 |      7,996        0.11       84.95
                  48 |     56,433        0.79       85.74
                  49 |      3,654        0.05       85.79
                  50 |    506,182        7.08       92.87
                  51 |        952        0.01       92.88
                  52 |      7,930        0.11       92.99
                  53 |      3,187        0.04       93.04
                  54 |      4,298        0.06       93.10
                  55 |    107,358        1.50       94.60
                  56 |      9,850        0.14       94.74
                  57 |      1,450        0.02       94.76
                  58 |      2,914        0.04       94.80
                  59 |        440        0.01       94.80
                  60 |    231,627        3.24       98.04
                  61 |        207        0.00       98.04
                  62 |      1,082        0.02       98.06
                  63 |      1,036        0.01       98.07
                  64 |        916        0.01       98.09
                  65 |     26,040        0.36       98.45
                  66 |      1,588        0.02       98.47
                  67 |        393        0.01       98.48
                  68 |        828        0.01       98.49
                  69 |        165        0.00       98.49
                  70 |     45,307        0.63       99.13
                  71 |         80        0.00       99.13
                  72 |      7,385        0.10       99.23
                  73 |        139        0.00       99.23
                  74 |        239        0.00       99.24
                  75 |      6,575        0.09       99.33
                  76 |        287        0.00       99.33
                  77 |        516        0.01       99.34
                  78 |        447        0.01       99.34
                  79 |         66        0.00       99.35
                  80 |     23,988        0.34       99.68
                  81 |         66        0.00       99.68
                  82 |        210        0.00       99.68
                  83 |         78        0.00       99.69
                  84 |      6,603        0.09       99.78
                  85 |      1,331        0.02       99.80
                  86 |        176        0.00       99.80
                  87 |        124        0.00       99.80
                  88 |        205        0.00       99.80
                  89 |         43        0.00       99.80
                  90 |      4,008        0.06       99.86
                  91 |        205        0.00       99.86
                  92 |         65        0.00       99.86
                  93 |         43        0.00       99.86
                  94 |        101        0.00       99.87
                  95 |        271        0.00       99.87
                  96 |        624        0.01       99.88
                  97 |         30        0.00       99.88
                  98 |        573        0.01       99.89
                  99 |      8,066        0.11      100.00
    -----------------+-----------------------------------
               Total |  7,154,399      100.00

uhrswork1 = 997 affects approximately 7.6 % of all respondents having a job. We are going to exclude 997 an 999 from the analysis, since we are interested in effects on the intensive margin.
Keep in mind that the missing of data (or in our case a varying in hours) could yield some important implications as well.

To elaborate on that, let us have a brief look at missing values.

#### Missing Completely At Random (MCAR)
The occurence of a missing value in Y does not depend on realisations of any other variable $X_i$ nor does it depend on other realisations of Y itself.

e.g.
- transmission errors

#### Missing At Random (MAR)
The occurence of a missing value can be fully explained by the other variables $X_i$.

e.g.
- males are less likely to state existing mental problems - regardless of whether they have mentral problems or not

#### Missing Not At Random (MNAR)
The occurence of a missing value in Y does depend on realisations of Y itself and can also depend on realisations of other variables $X_i$.

e.g.
- People with high income are less likely to state their income.

Keep in mind that the missing of data might hold some information as well.

```stata
mvdecode(uhrswork1), mv(999 997)
```

```stata
reg uhrswork1 hourwage [pweight=earnwt]
```

    (sum of wgt is 8,740,213,304.43)

    Linear regression                               Number of obs     =    952,574
                                                    F(1, 952572)      =   22347.36
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.0501
                                                    Root MSE          =     9.6503

    ------------------------------------------------------------------------------
                 |               Robust
       uhrswork1 |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
        hourwage |   .2388114   .0015975   149.49   0.000     .2356803    .2419425
           _cons |   32.61111   .0278884  1169.34   0.000     32.55645    32.66577
    ------------------------------------------------------------------------------

OLS minimizes
$$\sum_{i=1}^n (y_i - (\alpha1 + \beta x_i))^2$$
Introducing a weight $w_i$ (probability weight) for every observation:
$$\sum_{i=1}^n w_i^2 (y_i - (\alpha1 + \beta x_i))^2$$
$$\sum_{i=1}^n (w_iy_i - (\alpha w_i + \beta w_i x_i))^2$$

### [2.2 Determine the correlation between wages and hours worked. What is the difference between OLS and correlation?](#content)

```stata
corr uhrswork1 hourwage
```

    (obs=955,096)

                 | uhrswo~1 hourwage
    -------------+------------------
       uhrswork1 |   1.0000
        hourwage |   0.2343   1.0000

The default correlation command does not feature pweights. However, for point estimates of the correlation it is recommended to use aweight instead - though it delivers bogus p-values.
https://www.stata.com/support

```stata
corr uhrswork1 hourwage [aweight=earnwt]
display round(r(rho)^2, .0001)
```

    (sum of wgt is 8,740,213,304.43)
    (obs=952,574)

                 | uhrswo~1 hourwage
    -------------+------------------
       uhrswork1 |   1.0000
        hourwage |   0.2239   1.0000

    .0501

### [2.3 Add age, sex and citizenship to your model. Include the log of wage and hours. Interpret the results of a multivariate regression.](#content)

```stata
numlabel, add
tab citizen
```

                    citizenship status |      Freq.     Percent        Cum.
    -----------------------------------+-----------------------------------
                        1. born in u.s |  9,742,527       85.80       85.80
              2. born in u.s. outlying |     58,904        0.52       86.32
    3. born abroad of american parents |    100,987        0.89       87.21
                4. naturalized citizen |    636,648        5.61       92.82
                      5. not a citizen |    815,271        7.18      100.00
    -----------------------------------+-----------------------------------
                                 Total | 11,354,337      100.00

```stata
recode citizen (1/4 = 1) (5 = 0), gen (ctz)
label define citizen 0 "not a citizen" 1 "citizen"
label value ctz citizen
```

    (1611810 differences between citizen and ctz)

```stata
tab ctz, missing
```

        RECODE of |
          citizen |
     (citizenship |
          status) |      Freq.     Percent        Cum.
    --------------+-----------------------------------
    not a citizen |    815,271        7.18        7.18
          citizen | 10,539,066       92.82      100.00
    --------------+-----------------------------------
            Total | 11,354,337      100.00

```stata
hist age
```

    (bin=70, start=15, width=.74285714)

In your own data extract, you might notice the accumulation at ages 80 and 85?

The description of the variable age on the ipums website holds the following:

>Top codes:

1962-1987: 99+ coded as 99

1988-2001: 90+ coded as 90

2002-2003: 80+ coded as 80

2004-onward: 80-84 coded as 80, 85+ coded as 85

Does this affect our analysis? How many people aged 80 and above still participate in the job market?

(First guess: Not that much.)

```stata
* creating logs
gen log_uhrswork1 = log(uhrswork1)
gen log_hourwage = log(hourwage)
```

    (4,206,526 missing values generated)

    (10,334,812 missing values generated)

```stata
* linear regression
reg log_uhrswork1 log_hourwage age i.sex i.ctz [pweight=earnwt]
```

    (sum of wgt is 8,735,618,218.398)

    Linear regression                               Number of obs     =    952,041
                                                    F(4, 952036)      =   19380.34
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.1133
                                                    Root MSE          =      .3694

    ------------------------------------------------------------------------------
                 |               Robust
    log_uhrswo~1 |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
    log_hourwage |   .1672318   .0010219   163.65   0.000     .1652289    .1692347
             age |    .003772   .0000397    95.09   0.000     .0036942    .0038497
                 |
             sex |
      2. female  |  -.1185369   .0008945  -132.51   0.000    -.1202902   -.1167837
                 |
             ctz |
     1. citizen  |  -.0798118    .001299   -61.44   0.000    -.0823578   -.0772658
           _cons |    3.08505   .0029665  1039.98   0.000     3.079236    3.090864
    ------------------------------------------------------------------------------

Note: Stata created a appropriate cutoff for the dummies i.sex by itself. No need to transform the variable into a binary 0-1 format.

Instead of creating a new dummy variable for citizen you can also tell stata where to set the cutoff for a dummy in the regression by typing **i5.citizen**.
Stata will construct a dummy variable that is one for citizen greater or equal 5 and zero otherwise.
Note that this is the exact opposite definition of the dummy variable we have created.

```stata
reg log_uhrswork1 log_hourwage age i.sex i5.citizen [pweight=earnwt]
```

    (sum of wgt is 8,735,618,218.398)

    Linear regression                               Number of obs     =    952,041
                                                    F(4, 952036)      =   19380.34
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.1133
                                                    Root MSE          =      .3694

    -------------------------------------------------------------------------------
                  |               Robust
    log_uhrswork1 |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    --------------+----------------------------------------------------------------
     log_hourwage |   .1672318   .0010219   163.65   0.000     .1652289    .1692347
              age |    .003772   .0000397    95.09   0.000     .0036942    .0038497
                  |
              sex |
       2. female  |  -.1185369   .0008945  -132.51   0.000    -.1202902   -.1167837
                  |
          citizen |
    5. not a c..  |   .0798118    .001299    61.44   0.000     .0772658    .0823578
            _cons |   3.005238   .0030812   975.36   0.000     2.999199    3.011277
    -------------------------------------------------------------------------------

The absolute value of the citizen dummy is the same as for the former regression, only the sign of the coefficient, the t-value and the confidence interval changes.
Because of the changing reference level of both models (1 denotes two different things in citizen2 and i5.citizen) the intercept is slightly different, too.

**[Run the following multivariate regression:](#content)

\\( \log hours = \beta_0 + \beta_1 \log wage_i + \beta_2 age_i + \beta_3 educ2_i + \beta_4 sex_i + \beta_5 (educ2_i \times sex_i)\\);
 with educ2 being a dummy variable for a respondent holding at least a college degree.
Interpret your results.**

```stata
numlabel, add
tab educ
```

              educational attainment recode |      Freq.     Percent        Cum.
    ----------------------------------------+-----------------------------------
                            1. niu or blank |          9        0.00        0.00
                       2. none or preschool |     29,001        0.26        0.26
                   10. grades 1, 2, 3, or 4 |     55,654        0.49        0.75
                          20. grades 5 or 6 |    124,978        1.10        1.85
                          30. grades 7 or 8 |    221,665        1.95        3.80
                                40. grade 9 |    335,882        2.96        6.76
                               50. grade 10 |    414,008        3.65       10.40
                               60. grade 11 |    447,258        3.94       14.34
                 71. 12th grade, no diploma |    165,357        1.46       15.80
      73. high school diploma or equivalent |  3,219,208       28.35       44.15
             81. some college but no degree |  2,086,324       18.37       62.53
    91. associate's degree, occupational/vo |    506,645        4.46       66.99
    92. associate's degree, academic progra |    545,433        4.80       71.79
                     111. bachelor's degree |  2,106,549       18.55       90.34
                       123. master's degree |    798,942        7.04       97.38
            124. professional school degree |    150,658        1.33       98.71
                      125. doctorate degree |    146,766        1.29      100.00
    ----------------------------------------+-----------------------------------
                                      Total | 11,354,337      100.00

```stata
recode educ (1 = .m) (092 111 123 124 125 = 1) (else = 0), gen(educ2)
```

    (11354337 differences between educ and educ2)

```stata
label define educ2 .m "missing answer" 0 "no college" 1 "college"
label value educ2 educ2
```

```stata
tab educ2, missing
```

    RECODE of educ |
      (educational |
        attainment |
           recode) |      Freq.     Percent        Cum.
    ---------------+-----------------------------------
        no college |  7,605,980       66.99       66.99
           college |  3,748,348       33.01      100.00
    missing answer |          9        0.00      100.00
    ---------------+-----------------------------------
             Total | 11,354,337      100.00

```stata
reg log_uhrswork1 log_hourwage age i.sex##i.educ2 [pweight=earnwt]
```

    (sum of wgt is 8,735,618,218.398)

    Linear regression                               Number of obs     =    952,041
                                                    F(5, 952035)      =   15926.79
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.1128
                                                    Root MSE          =     .36952

    ------------------------------------------------------------------------------
                 |               Robust
    log_uhrswo~1 |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
    log_hourwage |   .1770907   .0010586   167.29   0.000     .1750159    .1791655
             age |   .0037393   .0000399    93.65   0.000     .0036611    .0038176
                 |
             sex |
      2. female  |  -.1105777   .0010262  -107.75   0.000    -.1125891   -.1085663
                 |
           educ2 |
     1. college  |  -.0389158   .0013397   -29.05   0.000    -.0415415   -.0362901
                 |
       sex#educ2 |
      2. female #|
     1. college  |  -.0246875   .0020473   -12.06   0.000    -.0287002   -.0206748
                 |
           _cons |   2.996977   .0030552   980.95   0.000     2.990989    3.002965
    ------------------------------------------------------------------------------

ceteris paribus:

\\( \beta_3 educ2_i + \beta_4 sex_i + \beta_5 (educ2_i \times sex_i) \\)

Plugging in the estimates for the coefficients and calculating the total coefficient for all four subpopulations defined by the two binary dummy variables *sex* and *college* yields:

male and no college ==>  \\( \beta_3*0 + \beta_4*0 + \beta_5*(0 \times 0) \\)

0 * (-.0389158) + 0 * (-.1105777) -.0246875 * (0x0) = 0 (reference group)

male and college ==> \\( \beta_3*1 + \beta_4*0 + \beta_5*(1 \times 0) \\)

1 * (-.0389158) + 0 * (-.1105777) -.0246875 * (1x0) = -.0389158

female and no college ==> \\( \beta_3*0 + \beta_4*1 + \beta_5*(0 \times 1) \\)

0 * (-.0389158) + 1 * (-.1105777) -.0246875 * (0x1) = -.1105777

female and collge ==>  \\( \beta_3*1 + \beta_4*1 + \beta_5*(1 \times 1) \\)

1 * (-.0389158) + 1 * (-.1105777) -.0246875 * (1x1) = -.174181

The coefficients of the dummy variable depict the effect of being a male holding a college degree as well as of being a female holding or not holding a college degree relative to being a male not holding a college degree.

Pay attention to the reference group when interpreting your results

```stata
twoway (lfit uhrswork1 hourwage if sex == 1 & educ2 == 0) (lfit uhrswork1 hourwage if sex == 1 & educ2 == 1) (lfit uhrswork1 hourwage if sex == 2 & educ2 == 0) (lfit uhrswork1 hourwage if sex == 2 & educ2 == 1), legend(cols(2) order(1 "male, no college" 2 "male, college" 3 "female, no college" 4 "female, no college")) title("Regression function fit", color(gs0))
```

```stata
reg uhrswork1 log_hourwage age i.sex##i.educ2 [pweight=earnwt]
```

    (sum of wgt is 8,740,213,304.43)

    Linear regression                               Number of obs     =    952,574
                                                    F(5, 952568)      =   21161.61
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.1380
                                                    Root MSE          =      9.193

    ------------------------------------------------------------------------------
                 |               Robust
       uhrswork1 |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
    log_hourwage |   4.994455   .0263433   189.59   0.000     4.942823    5.046087
             age |   .0913921   .0009462    96.58   0.000     .0895375    .0932467
                 |
             sex |
      2. female  |  -3.386369   .0252816  -133.95   0.000     -3.43592   -3.336818
                 |
           educ2 |
     1. college  |   -1.15064   .0375782   -30.62   0.000    -1.224292   -1.076988
                 |
       sex#educ2 |
      2. female #|
     1. college  |  -.5551601   .0519744   -10.68   0.000    -.6570283    -.453292
                 |
           _cons |   21.80574   .0731417   298.13   0.000     21.66239     21.9491
    ------------------------------------------------------------------------------

```stata
reg log_uhrswork1 hourwage age i.sex##i.educ2 [pweight=earnwt]
```

    (sum of wgt is 8,735,618,218.398)

    Linear regression                               Number of obs     =    952,041
                                                    F(5, 952035)      =   11881.97
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.0889
                                                    Root MSE          =     .37445

    ------------------------------------------------------------------------------
                 |               Robust
    log_uhrswo~1 |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
        hourwage |   .0063358   .0000632   100.31   0.000      .006212    .0064596
             age |   .0046301   .0000404   114.56   0.000     .0045509    .0047094
                 |
             sex |
      2. female  |   -.127375   .0010303  -123.62   0.000    -.1293944   -.1253555
                 |
           educ2 |
     1. college  |  -.0280367   .0013698   -20.47   0.000    -.0307215   -.0253519
                 |
       sex#educ2 |
      2. female #|
     1. college  |  -.0098348   .0020711    -4.75   0.000     -.013894   -.0057755
                 |
           _cons |   3.330031    .001883  1768.44   0.000      3.32634    3.333721
    ------------------------------------------------------------------------------

```stata
reg uhrswork1 hourwage age i.sex##i.educ2 [pweight=earnwt]
```

    (sum of wgt is 8,740,213,304.43)

    Linear regression                               Number of obs     =    952,574
                                                    F(5, 952568)      =   15812.80
                                                    Prob > F          =     0.0000
                                                    R-squared         =     0.1101
                                                    Root MSE          =     9.3406

    ------------------------------------------------------------------------------
                 |               Robust
       uhrswork1 |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
        hourwage |   .1860219    .001619   114.90   0.000     .1828486    .1891952
             age |   .1152636   .0009575   120.38   0.000     .1133869    .1171403
                 |
             sex |
      2. female  |  -3.837657   .0255038  -150.47   0.000    -3.887644   -3.787671
                 |
           educ2 |
     1. college  |  -.8898273    .038427   -23.16   0.000     -.965143   -.8145116
                 |
       sex#educ2 |
      2. female #|
     1. college  |  -.1451342   .0527644    -2.75   0.006    -.2485507   -.0417178
                 |
           _cons |   31.13315   .0448581   694.04   0.000     31.04523    31.22107
    ------------------------------------------------------------------------------

\\( y = \beta_0 + \beta_1 x \\)

| **y / x** | **level** | **log** |
| :---        |    :----:   |          ---: |
| **level** | change in x by 1 unit changes y by \\( \beta_1 \\) |  change in x by 1 % changes y by
 \\( \beta_1 / 100\\) units |
| **log** | change in x by 1 unit changes y
 **approximately** by  \\( \beta_1 \times 100 \% \\) | change in x by 1 % changes y by \\( \beta_1 \% \\)|

## [3. Employment Flows](#content)

### Group Work (15-20 min)

**The statistics of the Bureau of Labor Statistics base on data from the CPS.**

**[Derive information on monthly employment flows. Disregard marginal in- and outflows.](#content)**
**Do so for every month in your data.**

from Imgflip Meme Generator

```stata
cd "C:\Users\Hannes\Documents"
use "session01.dta", clear
```

    C:\Users\Hannes\Documents

```stata
numlabel, add
tab empstat, missing
```

                     employment status |      Freq.     Percent        Cum.
    -----------------------------------+-----------------------------------
                       1. armed forces |     50,414        0.44        0.44
                           10. at work |  7,451,380       65.63       66.07
    12. has job, not at work last week |    299,849        2.64       68.71
    21. unemployed, experienced worker |    406,801        3.58       72.29
            22. unemployed, new worker |     39,493        0.35       72.64
              32. nilf, unable to work |    588,222        5.18       77.82
                       34. nilf, other |  1,778,746       15.67       93.49
                     36. nilf, retired |    739,432        6.51      100.00
    -----------------------------------+-----------------------------------
                                 Total | 11,354,337      100.00

```stata
recode empstat (1/12 = 1) (21 22 = 2) (32/36 = 3), gen(emp2)
label define emp2 1 "employed" 2 "unemployed" 3 "not in labor force"
label value emp2 emp2
```

    (11303923 differences between empstat and emp2)

```stata
tab emp2
```

     RECODE of empstat |
           (employment |
               status) |      Freq.     Percent        Cum.
    -------------------+-----------------------------------
              employed |  7,801,643       68.71       68.71
            unemployed |    446,294        3.93       72.64
    not in labor force |  3,106,400       27.36      100.00
    -------------------+-----------------------------------
                 Total | 11,354,337      100.00

```stata
bysort cpsidp (mish): gen lead_mish = mish[_n + 1]
bysort cpsidp (mish): gen lead_emp = emp2[_n + 1] if lead_mish - mish == 1
```

    (1,773,756 missing values generated)

    (1,993,760 missing values generated)

```stata
gen empflow = emp2 * 10 + lead_emp
replace empflow = emp2 * 1000 + 999 if missing(lead_emp)
label define empflow 11 "EE" 12 "EU" 13 "EN" 21 "UE" 22 "UU" 23 "UN" 31 "NE" 32 "NU" 33 "NN" 1999 "E." 2999 "U." 3999 "N."
label value empflow empflow
tab empflow
```

    (1,993,760 missing values generated)

    (1,993,760 real changes made)

        empflow |      Freq.     Percent        Cum.
    ------------+-----------------------------------
             EE |  6,162,616       54.28       54.28
             EU |     88,037        0.78       55.05
             EN |    197,102        1.74       56.79
             UE |     95,966        0.85       57.63
             UU |    184,199        1.62       59.25
             UN |     84,685        0.75       60.00
             NE |    179,394        1.58       61.58
             NU |     83,560        0.74       62.32
             NN |  2,285,018       20.12       82.44
             E. |  1,353,888       11.92       94.36
             U. |     81,444        0.72       95.08
             N. |    558,428        4.92      100.00
    ------------+-----------------------------------
          Total | 11,354,337      100.00

```stata
tab month empflow if year == 2020
```

                 |                   empflow
           month |        EE         EU         EN         UE |     Total
    -------------+--------------------------------------------+----------
      1. january |    20,794        234        601        225 |    38,391
     2. february |    19,648        353        697        194 |    38,412
        3. march |    16,075      1,746      1,061        184 |    34,763
        4. april |    15,014        490        483        934 |    33,541
          5. may |    14,684        351        437        837 |    32,119
         6. june |    14,229        303        457        563 |    30,236
         7. july |    14,744        198        509        508 |    29,965
       8. august |    15,163        220        554        414 |    30,231
    9. september |    15,967        221        471        329 |    31,793
     10. october |    16,036        183        507        286 |    31,641
    11. november |    15,248        209        475        211 |    30,659
    12. december |    13,360        181        387        164 |    29,459
    -------------+--------------------------------------------+----------
           Total |   190,962      4,689      6,639      4,849 |   391,210

                 |                   empflow
           month |        UU         UN         NE         NU |     Total
    -------------+--------------------------------------------+----------
      1. january |       334        205        544        182 |    38,391
     2. february |       355        176        509        160 |    38,412
        3. march |       333        297        396        187 |    34,763
        4. april |     1,102        363        615        421 |    33,541
          5. may |       885        302        620        390 |    32,119
         6. june |       744        292        513        310 |    30,236
         7. july |       719        264        518        234 |    29,965
       8. august |       588        261        472        223 |    30,231
    9. september |       550        268        571        232 |    31,793
     10. october |       523        214        469        194 |    31,641
    11. november |       476        204        440        184 |    30,659
    12. december |       424        180        320        147 |    29,459
    -------------+--------------------------------------------+----------
           Total |     7,033      3,026      5,987      2,864 |   391,210

                 |                   empflow
           month |        NN         E.         U.         N. |     Total
    -------------+--------------------------------------------+----------
      1. january |     8,208      4,793        218      2,053 |    38,391
     2. february |     7,863      5,832        208      2,417 |    38,412
        3. march |     7,279      4,820        201      2,184 |    34,763
        4. april |     7,441      3,918        628      2,132 |    33,541
          5. may |     6,736      4,162        561      2,154 |    32,119
         6. june |     6,080      4,189        485      2,071 |    30,236
         7. july |     6,181      3,891        428      1,771 |    29,965
       8. august |     6,408      3,870        309      1,749 |    30,231
    9. september |     6,754      4,183        303      1,944 |    31,793
     10. october |     6,714      4,297        275      1,943 |    31,641
    11. november |     6,327      4,484        264      2,137 |    30,659
    12. december |     5,693      5,679        348      2,576 |    29,459
    -------------+--------------------------------------------+----------
           Total |    81,684     54,118      4,228     25,131 |   391,210

**[Find out the monthly flows for the U.S. for a period of years covered by your cps sample aswell.](#content)

Adequate data is provided by the Bureau of Labor Statistics. LINK

Try to visualise the monthly flows.**

**You can also continue to work on your CPS data instead.**

*Note: Not all tasks have to be solved using Stata. Especially when working with smaller data sets, it is much more convenient to explore and modify data using a spreadsheet program.*

See my example here: Link to Google Slides

Related Spreadsheet

```stata
save "session02.dta", replace
```

    file session02.dta saved

```stata
collapse (count) countflow = cpsidp, by(year month empflow)
```

```stata
gen date = ym(year,month)
format date %tmMCY
```

```stata
twoway (line countflow date if empflow == 11, lpattern(solid) lcolor(green)) ///
(line countflow date if empflow == 12, lpattern(solid) lcolor(red)) ///
(line countflow date if empflow == 13, lpattern(solid) lcolor(black)) ///
(line countflow date if empflow == 21, lpattern(dash) lcolor(green)) ///
(line countflow date if empflow == 22, lpattern(dash) lcolor(red)) ///
(line countflow date if empflow == 23, lpattern(dash) lcolor(black)) ///
(line countflow date if empflow == 31, lpattern(dot) lcolor(green)) ///
(line countflow date if empflow == 32, lpattern(dot) lcolor(red)) ///
(line countflow date if empflow == 33, lpattern(dot) lcolor(black)), ///
legend(cols(2) order(1 "EE" 2 "EU" 3 "EN" 4 "UE" 5 "UU" 6 "UN" 7 "NE" 8 "NU" 9 "NN")) ///
title("labor force flows", color(gs0))
```

### Job Separation Rate
The Job Separation Rate is the ratio of the number of individuals formerly being employed that become unemployed and the number of employed individuals in the reporting month.

\\(seprate_{t+1} = \frac{n_{EU, t}}{n_{E, t}}\\)

The number of employed individuals within a survey month equals the number of inividuals experiencing "EE", "EU", "EN" and "E.", i.e. inlist(empflow, 11, 12, 13, 1999)

\\(seprate_{t+1} = \frac{n_{EU,t}}{n_{EE,t}+n_{EU,t}+n_{EN,t}+n_{E.,t}}\\)

```stata
* # total employed
bysort date: egen tot_empl = total(countflow) if inlist(empflow, 11, 12, 13, 1999)

* # total separations
bysort date: egen tot_sep = total(countflow) if empflow == 12

* ratio
gen seprate = tot_sep / tot_empl
```

    variable tot_empl already defined

    r(110);
    r(110);

```stata
sum seprate
```

        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
         seprate |        259    .0112869    .0046523   .0056074   .0736647

```stata
twoway (line seprate date, lpattern(solid) lcolor(green)), title("job separation rate", color(gs0))
```

### Job Finding Rate
The Job Finding Rate is the ratio of the number of individuals formerly being unemployed that become employed and the number of unemployed individuals in the reporting month.

\\(findrate_{t+1} = \frac{n_{UE, t}}{n_{U, t}}\\)

The number of unemployed individuals within a survey month equals the number of inividuals experiencing "UE", "UU", "UN" and "U.", i.e. inlist(empflow, 21, 22, 23, 2999)

\\(findrate_{t+1} = \frac{n_{UE,t}}{n_{UE,t}+n_{UU,t}+n_{UN,t}+n_{U.,t}}\\)

```stata
* # total unemployed
bysort date: egen tot_unemp = total(countflow) if inlist(empflow, 21, 22, 23, 2999)

* # total job matches
bysort date: egen tot_find = total(countflow) if empflow == 21

* ratio
gen findrate = tot_find / tot_unemp
```

    (2,074 missing values generated)

    (2,852 missing values generated)

    (2,852 missing values generated)

```stata
twoway (line seprate date, lpattern(solid) lcolor(green)) ///
(line findrate date, lpattern(dot) lcolor(red)), ///
title("job separation rate & job finding rate", color(gs0))
```

```stata
misstable summarize
```

                                                                   Obs<.
                                                    +------------------------------
                   |                                | Unique
          Variable |     Obs=.     Obs>.     Obs<.  | values        Min         Max
      -------------+--------------------------------+------------------------------
          tot_empl |     2,074               1,037  |    257      14116       35182
           tot_sep |     2,852                 259  |    184        105        1746
           seprate |     2,852                 259  |    259   .0056074    .0736647
         tot_unemp |     2,074               1,037  |    244        699        3243
          tot_find |     2,852                 259  |    190        145         934
          findrate |     2,852                 259  |    258   .1269231    .3397762
      -----------------------------------------------------------------------------

Using the Hodrick-Prescott filter to decompose the flows into a cylclical and a trend component with a smoothing parameter of 14400 for monhtly data.

```stata
collapse (mean) seprate = seprate findrate = findrate, by(date)
```

```stata
tsset date, monthly
```

            time variable:  date, January2000 to August2021
                    delta:  1 month

```stata
tsfilter hp ct_sep = seprate, trend(trend_sep) smooth(14400)
tsfilter hp ct_find = findrate, trend(trend_find) smooth(14400)
```

```stata
twoway (line ct_sep date, lpattern(solid) lcolor(green)) ///
(line ct_find date, lpattern(dot) lcolor(red)), ///
legend(rows(2)) title("job separation rate & job finding rate, cyclical component", color(gs0))
```

```stata
twoway (line trend_sep date, lpattern(solid) lcolor(green)) ///
(line trend_find date, lpattern(dot) lcolor(red)), ///
legend(rows(2)) title("job separation rate & job finding rate, trend component", color(gs0))
```

```stata
bysort date sex: egen total_empflow = total(countflow)
gen share = countflow / total_empflow
```

```stata
tsfilter hp ct=share, trend(trendvar) smooth(14400)
```

**[Calculate the monthly unemployment rate for for the months from 2018 onwards.](#content)**

**Plot the time series.**

```stata
use "session02.dta", clear
```

```stata
gen date = ym(year,month)
format date %tmMCY
```

```stata
collapse (count) frequency = cpsidp, by(date year month emp2)
```

```stata
bysort date: egen total_frequency = total(frequency)
```

```stata
bysort date: egen total_laborforce = total(frequency) if emp2 < 3
```

    (260 missing values generated)

```stata
gen ushare = frequency / total_laborforce if emp2 == 2
```

    (520 missing values generated)

```stata
twoway (line ushare date if year >= 2018)
```

**[Add a line for the share of individuas not being part of the labor force in the same plot.](#content)**

```stata
gen nshare = frequency / total_frequency if emp2 == 3
```

    (520 missing values generated)

```stata
twoway (line ushare date if year >= 2018) (line nshare date if year >= 2018)
```

**[Create a time series for the mean age of people being unemployed. What effect on the mean age did the Covid crisis show?](#content)**

```stata
use "session02.dta", clear
```

```stata
gen date = ym(year,month)
format date %tmMCY
```

```stata
collapse (mean) mean_age = age (count) count_in = cpsidp, by(year month date emp2)
```

```stata
twoway (line mean_age date if emp2 == 2)
```

```stata
twoway (line mean_age date if emp2 == 1) (line mean_age date if emp2 == 2) (line mean_age date if emp2 == 3)
```

```stata
tsset emp2 date, monthly
```

           panel variable:  emp2 (strongly balanced)
            time variable:  date, January2000 to August2021
                    delta:  1 month

```stata
tsfilter hp ct=mean_age, trend(trendvar) smooth(14400)
```

```stata
twoway (line trendvar date if emp2 == 2)
```

```stata
twoway (line ct date if emp2 == 2)
```

```stata
gen tmp_var = count_in * mean_age
bysort date: egen total_in = total(count_in)
bysort date: egen total_age = total(tmp_var)
gen mean_age_t = total_age / total_in

drop tmp_var total_in total_age
```

```stata
twoway (line mean_age_t date)
```

```stata
gen age_diff = mean_age - mean_age_t
```

```stata
twoway (line age_diff date)
```

```stata
twoway (line age_diff date if year >= 2018)
```

## [Task 4](#content)
## Minimum wages and labour supply
### A Difference-in-difference-approach (DiD)

## [Task 4.1](#content)

Massachusetts increased its state level minimum wage from 10 \\$ to 11 \\$ per hour in 2017 (effective from 01.01.2017).

The neighbouring New Hampshire kept its minimum wage at the federal level, i.e. 7.25 \\$ per hour.

Using the CPS data set, run a DiD analysis to scrutinize the effect of the minimum wage increase on labour supply in Massachusetts (STATECENSUS == 14) relative to New Hampshire (STATECENSUS == 12).

Data on minimum wages in the US: Historical table  minimum wage per state

Introduction to DiD: https://mixtape.scunning.com/difference-in-differences.html

## Difference-in-Difference (DiD)

|  | Treatment | Control |
|-|:-:|:-:|
| t=0 | **Y**T0 | **Y**C0 |
| t=1 | **Y**T1 | **Y**C1 |

Difference before the treatment: **Y**T0 - **Y**C0
Difference after the treatment: **Y**T1 - **Y**C1
Difference-in-differences: (**Y**T1 - **Y**C1) - (**Y**T0 - **Y**C0)

The idea behind this: The difference before the treatment captures the *normal* difference, the difference after the treatment captures the normal difference and the causal effect.

The naive evaluation of the DiD would simply be the equation given above. Note that you could also subtract the treatment effect for the control group from the treatment effect of the treatment group to obtain the same estimator, i.e.

(**Y**T1 - **Y**C1) - (**Y**T0 - **Y**C0) = (**Y**T1 - **Y**T0) - (**Y**C1 - **Y**C0)

Let us consider the plain graphic example given above: Two groups of patients receive different medicamentation: The one group is treated with a new drug (treatment group), the other one is treated conventionally. The mortality of both groups differ before the treatment. This could be due to sample selection, e.g. because the researchers and physicians were afraid of severe side effects they chose younger and more robust patients to be treated.

For the DiD to be applicable, the commmon trend assumptions have to be met: In absence of any policy difference or treatment, bot groups would have evolved in parrallel. This means that in absence of the treatment we would expect the development of the mortality for both groups to be parallel.

This assumptions is easiest to be checked graphically by visualising trends in the variable of interest.

The treatment effect is measured by analysing the development of the differences in mortality between both groups.

The mortality rates beforehand (T0 - C0) give us information about how the difference in mortality between the two groups would have been without treatment (T1 - B = T0 - C0). This scenario is represented by the dashed orange line.

Because of the treatment, the difference in mortality actually became T1 - C1. Hence, the treatment created a difference in differences of (T1 - C1) - (T0 - C0), which can be imagined as the vertical distance between the imaginary point B and C1 (B-C1).

Most basic example:

\\[ Y_{it} = \beta_0 + \beta_1 \text{TREAT}_i  + \beta_2 \text{POST}_t  + \beta_{3rDD} ( \text{TREAT} \times \text{POST}) +\epsilon_{st} \\]

\\( \text{TREAT} \dots\\) dummy for treatment group

\\( \text{POST} \dots\\) dummy for treatment time

\\( \text{TREAT} \times \text{POST} \dots\\) interaction term, indicates observations of treatment group after the treatment

```stata
cd "C:\Users\Hannes\Documents"
use "cps_00028.dta", clear
numlabel, add
tab statecensus
```

    C:\Users\Hannes\Documents

                        state (census code) |      Freq.     Percent        Cum.
    ----------------------------------------+-----------------------------------
                                  11. maine |    221,336        1.24        1.24
                          12. new hampshire |    295,050        1.65        2.88
                                13. vermont |    245,069        1.37        4.25
                          14. massachusetts |    324,094        1.81        6.06
                           15. rhode island |    219,778        1.23        7.29
                            16. connecticut |    274,086        1.53        8.82
                               21. new york |    727,864        4.07       12.89
                             22. new jersey |    354,383        1.98       14.87
                           23. pennsylvania |    510,357        2.85       17.72
                                   31. ohio |    471,823        2.64       20.35
                                32. indiana |    288,189        1.61       21.96
                               33. illinois |    519,371        2.90       24.86
                               34. michigan |    412,841        2.31       27.17
                              35. wisconsin |    294,209        1.64       28.81
                              41. minnesota |    322,879        1.80       30.61
                                   42. iowa |    263,761        1.47       32.09
                               43. missouri |    274,788        1.53       33.62
                           44. north dakota |    253,042        1.41       35.04
                           45. south dakota |    239,054        1.34       36.37
                               46. nebraska |    251,507        1.40       37.78
                                 47. kansas |    242,364        1.35       39.13
                               51. delaware |    233,194        1.30       40.43
                               52. maryland |    311,361        1.74       42.17
                   53. district of columbia |    254,535        1.42       43.59
                               54. virginia |    356,334        1.99       45.58
                          55. west virginia |    301,241        1.68       47.26
                         56. north carolina |    386,950        2.16       49.43
                         57. south carolina |    261,404        1.46       50.89
                                58. georgia |    391,527        2.19       53.07
                                59. florida |    743,912        4.15       57.23
                               61. kentucky |    241,905        1.35       58.58
                              62. tennessee |    298,938        1.67       60.25
                                63. alabama |    291,533        1.63       61.88
                            64. mississippi |    263,605        1.47       63.35
                               71. arkansas |    268,818        1.50       64.85
                              72. louisiana |    312,417        1.74       66.60
                               73. oklahoma |    248,097        1.39       67.98
                                  74. texas |    998,022        5.57       73.56
                                81. montana |    268,730        1.50       75.06
                                  82. idaho |    259,316        1.45       76.50
                                83. wyoming |    252,640        1.41       77.92
                               84. colorado |    296,576        1.66       79.57
                             85. new mexico |    245,611        1.37       80.94
                                86. arizona |    259,501        1.45       82.39
                                   87. utah |    271,656        1.52       83.91
                                 88. nevada |    246,105        1.37       85.28
                             91. washington |    325,311        1.82       87.10
                                 92. oregon |    267,669        1.49       88.60
                             93. california |  1,549,450        8.65       97.25
                                 94. alaska |    220,721        1.23       98.48
                                 95. hawaii |    271,538        1.52      100.00
    ----------------------------------------+-----------------------------------
                                      Total | 17,904,462      100.00

```stata
keep if inlist(statecensus, 12, 14)
```

    (17,285,318 observations deleted)

```stata
sum year
```

```stata
gen qdate = qofd(dofm(ym(year, month)))
format qdate %tq
```

```stata
tab qdate
```

          qdate |      Freq.     Percent        Cum.
    ------------+-----------------------------------
         2010q1 |     14,373        2.32        2.32
         2010q2 |     15,083        2.44        4.76
         2010q3 |     14,874        2.40        7.16
         2010q4 |     14,440        2.33        9.49
         2011q1 |     14,245        2.30       11.79
         2011q2 |     14,593        2.36       14.15
         2011q3 |     14,800        2.39       16.54
         2011q4 |     14,658        2.37       18.91
         2012q1 |     14,140        2.28       21.19
         2012q2 |     14,200        2.29       23.49
         2012q3 |     14,517        2.34       25.83
         2012q4 |     14,512        2.34       28.17
         2013q1 |     14,252        2.30       30.48
         2013q2 |     13,747        2.22       32.70
         2013q3 |     13,888        2.24       34.94
         2013q4 |     13,751        2.22       37.16
         2014q1 |     13,608        2.20       39.36
         2014q2 |     13,521        2.18       41.54
         2014q3 |     13,497        2.18       43.72
         2014q4 |     13,463        2.17       45.90
         2015q1 |     13,181        2.13       48.02
         2015q2 |     12,585        2.03       50.06
         2015q3 |     12,402        2.00       52.06
         2015q4 |     13,048        2.11       54.17
         2016q1 |     13,049        2.11       56.28
         2016q2 |     12,604        2.04       58.31
         2016q3 |     12,931        2.09       60.40
         2016q4 |     13,229        2.14       62.54
         2017q1 |     13,111        2.12       64.65
         2017q2 |     12,993        2.10       66.75
         2017q3 |     12,713        2.05       68.81
         2017q4 |     12,952        2.09       70.90
         2018q1 |     12,549        2.03       72.92
         2018q2 |     12,735        2.06       74.98
         2018q3 |     12,600        2.04       77.02
         2018q4 |     12,526        2.02       79.04
         2019q1 |     12,295        1.99       81.03
         2019q2 |     12,116        1.96       82.98
         2019q3 |     12,175        1.97       84.95
         2019q4 |     12,194        1.97       86.92
         2020q1 |     11,638        1.88       88.80
         2020q2 |     10,744        1.74       90.53
         2020q3 |     10,924        1.76       92.30
         2020q4 |     11,076        1.79       94.09
         2021q1 |     10,942        1.77       95.85
         2021q2 |     11,202        1.81       97.66
         2021q3 |     10,937        1.77       99.43
         2021q4 |      3,531        0.57      100.00
    ------------+-----------------------------------
          Total |    619,144      100.00

Variable HOURWAGE - hourly wage
https://cps.ipums.org/cps-action/variables/HOURWAGE#codes_section

The variable is topcoded.

> For the years 2003 forward, for usual hours worked <29, the topcode is \\$99.99, and for usual hours worked 29+, the topcode is \\$2885.07/(usual hours worked).

```stata
mvdecode(uhrsworkorg), mv(999)
```

     uhrsworkorg: 590003 missing values generated

```stata
gen wage_topcoded = 0 if !missing(hourwage)
replace wage_topcoded = 1 if uhrsworkorg < 29 & hourwage == 99.99
replace wage_topcoded = 1 if uhrsworkorg >= 29 & hourwage == round(2885.07/uhrsworkorg, 0.01)
```

    (5 real changes made)

    (11 real changes made)

```stata
tab hourwage uhrsworkorg if wage_topcoded == 1
```

        hourly | usual hours worked per week, outgoing rotation groups
          wage |         8         14         15         20         25 |     Total
    -----------+-------------------------------------------------------+----------
         30.69 |         0          0          0          0          0 |         1
         36.06 |         0          0          0          0          0 |         2
         48.08 |         0          0          0          0          0 |         8
         99.99 |         1          1          1          1          1 |         5
    -----------+-------------------------------------------------------+----------
         Total |         1          1          1          1          1 |        16

               |   usual hours worked per week,
        hourly |     outgoing rotation groups
          wage |        60         80         94 |     Total
    -----------+---------------------------------+----------
         30.69 |         0          0          1 |         1
         36.06 |         0          2          0 |         2
         48.08 |         8          0          0 |         8
         99.99 |         0          0          0 |         5
    -----------+---------------------------------+----------
         Total |         8          2          1 |        16

So we have 16 topcoded entries for hourwage in our data.

```stata
sum hourwage
mvdecode(hourwage), mv(999.99)
sum hourwage
```

        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
        hourwage |    619,144    937.0221    240.5184          1     999.99

        hourwage: 579425 missing values generated

        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
        hourwage |     39,719    18.43993    11.06415          1      99.99

Let's have a look at the usual hours worked at the main job at the wage rate of HOURWAGE.

UHRSWORKORG
https://cps.ipums.org/cps-action/variables/UHRSWORKORG#codes_section

Edit: We are disregarding UHRSWORKORG for this task. If you want to relate the labor supply to a specific wage for an individual, you should use UHRSWORKORG. But we are merely interested in the overall effect of a minimum wage increase, so we are going to use UHRSWORKT - the usual total hours worked in a week.

```stata
mvdecode(uhrsworkt), mv(997 999)
sum uhrsworkt
```

       uhrsworkt: 322625 missing values generated

        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
       uhrsworkt |    296,519    38.97833    12.06778          0        159

```stata
*gen topcode_value = round(2885.07 / uhrsworkorg, 0.01) if uhrsworkorg >= 29
*replace topcode_value = 99.99 if uhrsworkorg < 29

*** uhrsworkorg is not part of our analysis
```

```stata
tabstat hourwage if inlist(statecensus, 12, 14) & inrange(year, 2016, 2017) & wage_topcoded == 0, ///
by(statecensus) statistics(count, mean, median, min, max)
```

    Summary for variables: hourwage
         by categories of: statecensus (state (census code))

         statecensus |         N      mean       p50       min       max
    -----------------+--------------------------------------------------
    12. new hampshir |      2946   18.1349    15.115      1.01     99.99
    14. massachusett |      3728  19.22948     15.27       1.2        99
    -----------------+--------------------------------------------------
               Total |      6674  18.74632     15.25      1.01     99.99
    --------------------------------------------------------------------

```stata
egen hwmeant = mean(hourwage) if wage_topcoded == 0, by(qdate statecensus)

gen hwmean12 = .m
replace hwmean12 = hwmeant if statecensus == 12

gen hwmean14 = .m
replace hwmean14 = hwmeant if statecensus == 14
```

    (16 missing values generated)

    (619,144 missing values generated)

    (295,050 real changes made, 7 to missing)

    (619,144 missing values generated)

    (324,094 real changes made, 9 to missing)

```stata
qui graph twoway line hwmean12 hwmean14 qdate, legend(size(small))  xlabel(, angle(vertical)) sort
qui graph export "G:\Meine Ablage\Uni Rostock\Lehrveranstaltungen\WiSe 2020 21\Labor Economics (Master)\Exercise\img\hwmean12.png"
```

    (file G:\Meine Ablage\Uni Rostock\Lehrveranstaltungen\WiSe 2020 21\Labor Economi
    > cs (Master)\Exercise\img\hwmean12.png written in PNG format)

```stata
egen hwmedian = median(hourwage) if wage_topcoded == 0, by(qdate statecensus)

gen hwmedian12 = .m
replace hwmedian12 = hwmedian if statecensus == 12

gen hwmedian14 = .m
replace hwmedian14 = hwmedian if statecensus == 14
```

    (16 missing values generated)

    (619,144 missing values generated)

    (295,050 real changes made, 7 to missing)

    (619,144 missing values generated)

    (324,094 real changes made, 9 to missing)

```stata
qui graph twoway line hwmedian12 hwmedian14 qdate, legend(size(small))  xlabel(, angle(vertical)) sort
qui graph export "G:\Meine Ablage\Uni Rostock\Lehrveranstaltungen\WiSe 2020 21\Labor Economics (Master)\Exercise\img\hwmedian.png"
```

    (file G:\Meine Ablage\Uni Rostock\Lehrveranstaltungen\WiSe 2020 21\Labor Economi
    > cs (Master)\Exercise\img\hwmedian.png written in PNG format)

```stata
egen hworkp50 = median(uhrsworkt), by(qdate statecensus)

gen hworkp5012 = .m
replace hworkp5012 = hworkp50 if statecensus == 12

gen hworkp5014 = .m
replace hworkp5014 = hworkp50 if statecensus == 14
```

    (619,144 missing values generated)

    (295,050 real changes made)

    (619,144 missing values generated)

    (324,094 real changes made)

```stata
qui graph twoway line hworkp5012 hworkp5014 qdate, legend(size(small)) xlabel(, angle(vertical)) sort
qui graph export "G:\Meine Ablage\Uni Rostock\Lehrveranstaltungen\WiSe 2020 21\Labor Economics (Master)\Exercise\img\hworkp50.png"
```

    (file G:\Meine Ablage\Uni Rostock\Lehrveranstaltungen\WiSe 2020 21\Labor Economi
    > cs (Master)\Exercise\img\hworkp50.png written in PNG format)

```stata
egen hworkmean = mean(uhrsworkt), by(qdate statecensus)

gen hworkmean12 = .m
replace hworkmean12 = hworkmean if statecensus == 12

gen hworkmean14 = .m
replace hworkmean14 = hworkmean if statecensus == 14
```

    (619,144 missing values generated)

    (295,050 real changes made)

    (619,144 missing values generated)

    (324,094 real changes made)

```stata
qui graph twoway line hworkmean12 hworkmean14 qdate, legend(size(small)) xlabel(, angle(vertical)) sort
qui graph export "G:\Meine Ablage\Uni Rostock\Lehrveranstaltungen\WiSe 2020 21\Labor Economics (Master)\Exercise\img\hworkmean.png"
```

```stata
tab statecensus
```

                        state (census code) |      Freq.     Percent        Cum.
    ----------------------------------------+-----------------------------------
                          12. new hampshire |    295,050       47.65       47.65
                          14. massachusetts |    324,094       52.35      100.00
    ----------------------------------------+-----------------------------------
                                      Total |    619,144      100.00

```stata
egen hwp25 = pctile(hourwage), by(qdate statecensus) p(25)

gen hwp2512 = .m
replace hwp2512 = hwp25 if statecensus == 12

gen hwp2514 = .m
replace hwp2514 = hwp25 if statecensus == 14
```

    (619,144 missing values generated)

    (295,050 real changes made)

    (619,144 missing values generated)

    (324,094 real changes made)

```stata
qui graph twoway line hwp2512 hwp2514 qdate, legend(size(small)) xlabel(, angle(vertical)) sort
qui graph export "G:\Meine Ablage\Uni Rostock\Lehrveranstaltungen\WiSe 2020 21\Labor Economics (Master)\Exercise\img\hwp25.png"
```

```stata
egen hwp10 = pctile(hourwage), by(qdate statecensus) p(10)

gen hwp1012 = .m
replace hwp1012 = hwp10 if statecensus == 12

gen hwp1014 = .m
replace hwp1014 = hwp10 if statecensus == 14
```

    (619,144 missing values generated)

    (295,050 real changes made)

    (619,144 missing values generated)

    (324,094 real changes made)

```stata
qui graph twoway line hwp1012 hwp1014 qdate, legend(size(small)) xlabel(, angle(vertical)) sort
qui graph export "G:\Meine Ablage\Uni Rostock\Lehrveranstaltungen\WiSe 2020 21\Labor Economics (Master)\Exercise\img\hwp10.png"
```

Define the treatment date as well as the treatment group.

```stata
gen post14 = 0
replace post14 = 1 if year == 2017

gen treat14 = 0
replace treat14 = 1 if statecensus == 14
```

    (51,769 real changes made)

    (324,094 real changes made)

```stata
reg uhrsworkt i.treat14 i.post14 i.treat14#i.post14 if inlist(statecensus, 12, 14) & inrange(year, 2016, 2017)
```

          Source |       SS           df       MS      Number of obs   =    51,076
    -------------+----------------------------------   F(3, 51072)     =      0.81
           Model |  352.798159         3  117.599386   Prob > F        =    0.4891
        Residual |  7432254.49    51,072  145.525033   R-squared       =    0.0000
    -------------+----------------------------------   Adj R-squared   =   -0.0000
           Total |  7432607.28    51,075  145.523393   Root MSE        =    12.063

    ------------------------------------------------------------------------------
       uhrsworkt |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
       1.treat14 |   -.180106   .1543337    -1.17   0.243    -.4826017    .1223897
        1.post14 |   .0173384   .1689218     0.10   0.918    -.3137501    .3484268
                 |
         treat14#|
          post14 |
            1 1  |   .0290606   .2179739     0.13   0.894    -.3981705    .4562917
                 |
           _cons |   39.25552   .1192699   329.13   0.000     39.02175    39.48929
    ------------------------------------------------------------------------------

```stata
margins i.post14#i.treat14
```

    Adjusted predictions                            Number of obs     =     51,076
    Model VCE    : OLS

    Expression   : Linear prediction, predict()

    ------------------------------------------------------------------------------
                 |            Delta-method
                 |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
          post14#|
         treat14 |
            0 0  |   39.25552   .1192699   329.13   0.000     39.02175    39.48929
            0 1  |   39.07542   .0979468   398.95   0.000     38.88344    39.26739
            1 0  |   39.27286   .1196213   328.31   0.000      39.0384    39.50732
            1 1  |   39.12182   .0968735   403.84   0.000     38.93194    39.31169
    ------------------------------------------------------------------------------

```stata
qui marginsplot
qui graph export "G:\Meine Ablage\Uni Rostock\Lehrveranstaltungen\WiSe 2020 21\Labor Economics (Master)\Exercise\img\margins1.png"
```

### For wages <= 10th percentile

```stata
reg uhrsworkt i.treat14 i.post14 i.treat14#i.post14 if inlist(statecensus, 12, 14) & inrange(year, 2016, 2017) & (hourwage <= hwp1012 | hourwage <= hwp1014) & !missing(hourwage)
```

          Source |       SS           df       MS      Number of obs   =     6,264
    -------------+----------------------------------   F(3, 6260)      =      4.61
           Model |  1809.87695         3  603.292316   Prob > F        =    0.0032
        Residual |  819443.966     6,260  130.901592   R-squared       =    0.0022
    -------------+----------------------------------   Adj R-squared   =    0.0017
           Total |  821253.843     6,263  131.127869   Root MSE        =    11.441

    ------------------------------------------------------------------------------
       uhrsworkt |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
       1.treat14 |  -.6650665   .4065078    -1.64   0.102    -1.461961    .1318283
        1.post14 |   .6568202   .4373933     1.50   0.133    -.2006207    1.514261
                 |
         treat14#|
          post14 |
            1 1  |  -.6746551   .5830591    -1.16   0.247    -1.817651    .4683407
                 |
           _cons |   35.87641   .3040471   118.00   0.000     35.28038    36.47245
    ------------------------------------------------------------------------------

```stata
margins i.post14#i.treat14
```

    Adjusted predictions                            Number of obs     =      6,264
    Model VCE    : OLS

    Expression   : Linear prediction, predict()

    ------------------------------------------------------------------------------
                 |            Delta-method
                 |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
          post14#|
         treat14 |
            0 0  |   35.87641   .3040471   118.00   0.000     35.28038    36.47245
            0 1  |   35.21135   .2698222   130.50   0.000      34.6824    35.74029
            1 0  |   36.53323   .3144333   116.19   0.000     35.91684    37.14963
            1 1  |   35.19351   .2753925   127.79   0.000     34.65365    35.73337
    ------------------------------------------------------------------------------

```stata
qui marginsplot
qui graph export "G:\Meine Ablage\Uni Rostock\Lehrveranstaltungen\WiSe 2020 21\Labor Economics (Master)\Exercise\img\margins2.png"
```

```stata
tab statecensus
```

                        state (census code) |      Freq.     Percent        Cum.
    ----------------------------------------+-----------------------------------
                          12. new hampshire |    295,050       47.65       47.65
                          14. massachusetts |    324,094       52.35      100.00
    ----------------------------------------+-----------------------------------
                                      Total |    619,144      100.00

Let us have a look at the extensive margin of labour supply.

In a first analysis, let us look at the share of the labour force being employed.

```stata
gen employed = 1 if inrange(empstat, 10, 12)
replace employed = 0 if empstat > 12
```

    (298,625 missing values generated)

    (194,502 real changes made)

```stata
reg employed i.treat14 i.post14 i.treat14#i.post14 if inlist(statecensus, 12, 14) & inrange(year, 2016, 2017)
```

          Source |       SS           df       MS      Number of obs   =    86,933
    -------------+----------------------------------   F(3, 86929)     =     25.85
           Model |  18.1152273         3  6.03840908   Prob > F        =    0.0000
        Residual |  20302.3018    86,929   .23355039   R-squared       =    0.0009
    -------------+----------------------------------   Adj R-squared   =    0.0009
           Total |  20320.4171    86,932  .233750714   Root MSE        =    .48327

    ------------------------------------------------------------------------------
        employed |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
       1.treat14 |  -.0373972   .0047552    -7.86   0.000    -.0467173   -.0280772
        1.post14 |  -.0090599   .0052331    -1.73   0.083    -.0193167     .001197
                 |
         treat14#|
          post14 |
            1 1  |   .0189626   .0067136     2.82   0.005     .0058041    .0321212
                 |
           _cons |   .6489781   .0037088   174.98   0.000     .6417089    .6562474
    ------------------------------------------------------------------------------

```stata
margins i.post14#i.treat14
```

    Adjusted predictions                            Number of obs     =     86,933
    Model VCE    : OLS

    Expression   : Linear prediction, predict()

    ------------------------------------------------------------------------------
                 |            Delta-method
                 |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
          post14#|
         treat14 |
            0 0  |   .6489781   .0037088   174.98   0.000     .6417089    .6562474
            0 1  |   .6115809    .002976   205.51   0.000      .605748    .6174138
            1 0  |   .6399183   .0036919   173.33   0.000     .6326822    .6471544
            1 1  |   .6214837   .0029716   209.14   0.000     .6156593     .627308
    ------------------------------------------------------------------------------

```stata
qui marginsplot
qui graph export "G:\Meine Ablage\Uni Rostock\Lehrveranstaltungen\WiSe 2020 21\Labor Economics (Master)\Exercise\img\margins3.png"
```

```stata
egen mean_empl = mean(employed), by(statecensus qdate)
gen mean_empl12 = .m
replace mean_empl12 = mean_empl if statecensus == 12

gen mean_empl14 = .m
replace mean_empl14 = mean_empl if statecensus == 14
```

    (619,144 missing values generated)

    (295,050 real changes made)

    (619,144 missing values generated)

    (324,094 real changes made)

```stata
qui graph twoway line mean_empl14 mean_empl12 qdate, legend(size(small)) xlabel(, angle(vertical)) sort
qui graph export "G:\Meine Ablage\Uni Rostock\Lehrveranstaltungen\WiSe 2020 21\Labor Economics (Master)\Exercise\img\mean_emp.png
```

According to our plain analysis, there seems to be a positive effect on employment associated with the increase of the minimum wage in Massachusetts.
There are some ways to test the robustness of our analysis results.
First, we are going to replace the date of the treatment by a different date.

```stata
ssc install diff, replace
```

    checking diff consistency and verifying not already installed...
    all files already exist and are up to date.

```stata
diff employed, t(treat14) p(post)

```

    DIFFERENCE-IN-DIFFERENCES ESTIMATION RESULTS
    Number of observations in the DIFF-IN-DIFF: 515021
                Before         After
       Control: 228908         17135       246043
       Treated: 242530         26448       268978
                471438         43583
    --------------------------------------------------------
     Outcome var.   | emplo~d | S. Err. |   |t|   |  P>|t|
    ----------------+---------+---------+---------+---------
    Before          |         |         |         |
       Control      | 0.641   |         |         |
       Treated      | 0.604   |         |         |
       Diff (T-C)   | -0.037  | 0.001   | -26.28  | 0.000***
    After           |         |         |         |
       Control      | 0.640   |         |         |
       Treated      | 0.621   |         |         |
       Diff (T-C)   | -0.018  | 0.005   | 3.88    | 0.000***
                    |         |         |         |
    Diff-in-Diff    | 0.019   | 0.005   | 3.77    | 0.000***
    --------------------------------------------------------
    R-square:    0.00
    * Means and Standard Errors are estimated by linear regression
    **Inference: *** p<0.01; ** p<0.05; * p<0.1

```stata
gen post2 = 0
replace post2 = 1 if year == 2016
reg employed i.treat14 i.post2 i.treat14#i.post2 if inlist(statecensus, 12, 14) & inrange(year, 2015, 2016)
```

    (51,813 real changes made)

          Source |       SS           df       MS      Number of obs   =    86,175
    -------------+----------------------------------   F(3, 86171)     =     45.39
           Model |  31.9430405         3  10.6476802   Prob > F        =    0.0000
        Residual |  20214.4597    86,171  .234585414   R-squared       =    0.0016
    -------------+----------------------------------   Adj R-squared   =    0.0015
           Total |  20246.4027    86,174  .234947928   Root MSE        =    .48434

    -------------------------------------------------------------------------------
         employed |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    --------------+----------------------------------------------------------------
        1.treat14 |  -.0394845   .0047208    -8.36   0.000    -.0487372   -.0302317
          1.post2 |   .0075507   .0051385     1.47   0.142    -.0025208    .0176222
                  |
    treat14#post2 |
             1 1  |   .0020872    .006708     0.31   0.756    -.0110605    .0152349
                  |
            _cons |   .6414274    .003548   180.78   0.000     .6344733    .6483815
    -------------------------------------------------------------------------------

```stata
margins i.post2#i.treat14
```

    Adjusted predictions                            Number of obs     =     86,175
    Model VCE    : OLS

    Expression   : Linear prediction, predict()

    -------------------------------------------------------------------------------
                  |            Delta-method
                  |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
    --------------+----------------------------------------------------------------
    post2#treat14 |
             0 0  |   .6414274    .003548   180.78   0.000     .6344733    .6483815
             0 1  |    .601943   .0031141   193.30   0.000     .5958393    .6080466
             1 0  |   .6489781    .003717   174.60   0.000     .6416928    .6562635
             1 1  |   .6115809   .0029825   205.05   0.000     .6057351    .6174267
    -------------------------------------------------------------------------------

```stata
qui marginsplot
qui graph export "G:\Meine Ablage\Uni Rostock\Lehrveranstaltungen\WiSe 2020 21\Labor Economics (Master)\Exercise\img\margins4.png"
```

```stata
gen post3 = 0
replace post3 = 1 if year == 2018
reg employed i.treat14 i.post3 i.treat14#i.post3 if inlist(statecensus, 12, 14) & inrange(year, 2017, 2018)
```

    (50,410 real changes made)

          Source |       SS           df       MS      Number of obs   =    85,740
    -------------+----------------------------------   F(3, 85736)     =     12.13
           Model |  8.42934549         3  2.80978183   Prob > F        =    0.0000
        Residual |  19857.9865    85,736  .231617832   R-squared       =    0.0004
    -------------+----------------------------------   Adj R-squared   =    0.0004
           Total |  19866.4158    85,739  .231708042   Root MSE        =    .48127

    -------------------------------------------------------------------------------
         employed |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    --------------+----------------------------------------------------------------
        1.treat14 |  -.0184346   .0047196    -3.91   0.000     -.027685   -.0091842
          1.post3 |   .0084229   .0052262     1.61   0.107    -.0018204    .0186662
                  |
    treat14#post3 |
             1 1  |   .0079049   .0067232     1.18   0.240    -.0052725    .0210822
                  |
            _cons |   .6399183   .0036766   174.05   0.000     .6327122    .6471244
    -------------------------------------------------------------------------------

```stata
margins i.post3#i.treat14
```

    Adjusted predictions                            Number of obs     =     85,740
    Model VCE    : OLS

    Expression   : Linear prediction, predict()

    -------------------------------------------------------------------------------
                  |            Delta-method
                  |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
    --------------+----------------------------------------------------------------
    post3#treat14 |
             0 0  |   .6399183   .0036766   174.05   0.000     .6327122    .6471244
             0 1  |   .6214837   .0029593   210.01   0.000     .6156835    .6272839
             1 0  |   .6483412   .0037143   174.55   0.000     .6410612    .6556211
             1 1  |   .6378114   .0030216   211.08   0.000      .631889    .6437338
    -------------------------------------------------------------------------------

```stata
qui marginsplot
qui graph export "G:\Meine Ablage\Uni Rostock\Lehrveranstaltungen\WiSe 2020 21\Labor Economics (Master)\Exercise\img\margins5.png"
```

In 2016 Massachusetts increased its state minimum wage level by 1 \\$ (9 to 10), whereas New Hampshire kept its minimum wage at 7.25 \\$.

Our analysis shows no statistical significant effect of the treatment. C.p., we would expect a similiar effect as in our analysis above.

A pseudo treatment for 2018, when in fact the minimum wage was not increased in neither of both states, shows no statistical effect of the treatment on the treatment group.
As for the increase in minimum wages from 2016 to 2017, our model rather supports the notion of a positive effect of an increase in minimum wages on the extensive labour supply.

```stata
clear
```

## [Task 4.2](#content)

Repeat the analysis above for two or more neighbouring state of your choice for a suitable date.

## [Task 5](#content)

### The effect of maximum duration of unemployment benefits on labor supply
 Regression Discontinuity Design

For this task we are going to employ a Regression Discontinuity approach.

A short introduction can be found here: https://mixtape.scunning.com/regression-discontinuity.html

A more sophisticated introduction into different aspects of RnD can be found here:
Cattaneo, Idrobo, Titiunik (2019) A Practical Introduction to Regression Discontinuity Designs: Foundations;
https://arxiv.org/pdf/1911.09511.pdf

The first 3 tasks are designated to scrutinizing preparing the data set for the analysis. These are exhausting, yet important tasks.

We are going to have a brief look at a simple sharp regression discontinuity design. "Sharp" means that every individual in the sample is affected by the measure of interest. "Fuzzy" on the other hand means that the probability of being treated significantly increases around a cutoff, but that there still exist untreated individuals on the right of the cutoff as well as treated individuals on the left of the cutoff. See literature above for more details.

As the name suggests, regression discontinuity analyses possinle discountinuities around a threshold. This threshold could be any treatment, e.g. policy measures. The design enables us to conduct counterfactual analyis when a control group is missing.
 In many scenarios we are lackking a control group, because the measure affects everybody. E.g. an minimum wage increase (should) apply to every worker.

In absence of a control group we assume that the two groups of individuals near noth sides of the cutoff are on average similar to each other and that we do not have discontinuities in covariates around the cutoff. To give just one example: Let us imagine that from 01.01.2021 on every German citizen is entitled to receive unconditional basic income. You are interested in the effect of this policy measure on labor supply. Since the unconditional basic income applies to everybody, you are missing a control group. So you decide to employ a regression discontinuity design. In order for possible effects on the labor supply to be attributed to the policy measure, you need to make sure that nothing else happens around the cutoff 01.01.2021. For example, the share of male individuals in a sample could also experience a signficant change around the cutoff or the wage distribution could change crossing the cutoff.
You can easily verify that covariates affecting the outcome are balanced around the cutoff by running regression discontinuity analysis around the cutoff for them as well.

A basic model might look like that:

\\[ y_i = \beta_0 + \beta_1 (x_i - c) + \beta_2 D_i + \beta_3 \left(D_i \times (x_i - c)\right)  \\]

The running variable \\( x_i \\) represents the distance to the treatment and eventually crosses a threshold c, which indicates wheter the treatment took place. \\( D_i \\) is an indicator variable for the treatment.
\\[
  \begin{equation}
    D=
    \begin{cases}
      0 & \text{if}\ x_i < 01.01.2021, \\
      1 & \text{if} x_i >= 01.01.2021
    \end{cases}
  \end{equation}
\\]

\\( \beta_2 D_i \\) represents the difference in levels of the fitted lines on the left and on the right of the cutoff. The coefficient \\( \beta_2 \\) could be interpreted as an additional intercept introduced by the treatment.

\\( \beta_3 \left(D_i \times (x_i - c)\right) \\) introduces the possibility of different slopes of the fitted line on the left and on the right of the cutoff.

The bandwidth (\\( h \\)) of the running variable \\(x_i\\) you choose for the analysis affects the results of the simple regression. A high bandwidth (more data points, farther from cutoff) is associated wth a high bias and low variance. A low bandwidth (fewer data points, closer to cutoff) on the other hand is associated with a low bias and a high variance.
The proximity to the cutoff can affect the size of the treatment effect as well.

The three lovely hand-drawn examples below show you how the bandwidth affects the size of the treatment effect.

If we employ a polynomial fit the treatment effect might disappear completely.

There exists different appraches to choose the optimal bandwith.
I am using a command of the package rdrobust.
The command *rdbwselect* with the specification *bwselect(mserd)* yields an estimate for a mean squared error optimal bandwidth for a linear fit *p(1)*, i.e. the bandwidth that minimizes the MSE.

## [Task 5.1](#content)

In 2017, the maximum duration of unemployment benefits was
- 12 weeks for Gerogia & Nevada,
- 13 weeks for Missouri,
- 14 weeks for Hawaii,
- 16 weeks for Kentucky,
- 20 weeks for Arizona, Minnesota & South Dakota,
- 21 weeks for Indiana,
- 28 weeks for Nebraska,
- 30 weeks for Maryland

and 26 weeks in all other states.

Use the complete dataset of IPUMS CPS for 2017.

Create a dummy variable indicating an individual being unemployed longer than the maximum duration of benefits.

```stata
* Loading the data
cd "C:\Users\Hannes\Documents"
use "cps_00028.dta", clear
keep if year == 2017
numlabel, add
tab statecensus
```

```stata
* maximum duration of unemployment benefits in weeks
gen maxui = 26
replace maxui = 12 if inlist(statecensus, 58, 88)
replace maxui = 13 if statecensus == 43
replace maxui = 14 if statecensus == 95
replace maxui = 16 if statecensus == 61
replace maxui = 20 if inlist(statecensus, 86, 41, 45)
replace maxui = 21 if statecensus == 21
replace maxui = 28 if statecensus == 46
replace maxui = 30 if statecensus == 52

```

```stata
tab maxui
```

```stata
** unemployment benefits expired?
mvdecode(durunemp), mv(999)
gen expui = .m
replace expui = 0 if durunemp <= maxui & inlist(empstat, 21, 22)
replace expui = 1 if durunemp > maxui & inlist(empstat, 21, 22)
tab expui, missing
```

## [Task 5.2](#content)

Have a look at the sanity of the data at hand.

For every individual check the distance in weeks between two consecutive interviews as well as the difference between $durunemp_{t}$ and $durunemp_{t+1}$. What does strike your eye?

```stata
tab durunemp
```

```stata
numlabel, add
tab empstat if !missing(durunemp)
```

So far, so good: Almost only unemployed workers report durations of unemployment. The 13 missing employment status are most likely unemployed, too. Since we only have observations where the employment status is not defined, we are going to drop these information.

```stata
mvdecode(empstat), mv(0)
```

```stata
hist durunemp
```

Problem: The majority of individuals are reported several times in the CPS within one year.

```stata
** cpsidp occurence per year
egen tag = tag(cpsid mish)
egen occurence = total(tag), by(cpsid)
drop tag
tab occurence
```

The majority of survey participants are interviewed 4 times in 2017.

Let us have a look at how stable the employment status for unemployed individuals is throughout the year 2017.

```stata
** give next mish for individual
bysort cpsidp (mish): gen lead_mish = mish[_n+1]

** Employment status of unemployed individuals in the next interview
bysort cpsidp (mish): gen empstat_t = empstat[_n+1]
replace empstat_t = . if missing(lead_mish)

** Generate a variable for calendar week
gen cw = week(mdy(month, 19, year))
bysort cpsidp (mish): gen cw_t = cw[_n+1]

** duration of unemployment in next interview
bysort cpsidp (mish): gen durunemp_t = durunemp[_n+1]
```

```stata
tab empstat empstat_t if empstat != empstat_t
```

```stata
tab empstat empstat_t if empstat == empstat_t
```

At leas 92k times we have changes in the employment status.

Let us now try to detect whether an individual had been continously unemployed or not.

```stata
gen diff_cw = cw_t - cw
gen diff_du = durunemp_t - durunemp
gen diff_cw_du = diff_cw - diff_du
gen con_unemp = .

** Does the difference between the differences in durunemp and cw indicate continous unemployment?
replace con_unemp = 1 if inrange(diff_du, diff_cw - 1, diff_cw + 1)
replace con_unemp = 0 if !inrange(diff_du, diff_cw - 1, diff_cw + 1) & !missing(durunemp)
```

The idea of the snippet above is to check whether individuals who happen to have multiple appearances in the sample in the year 2017 were continuously unemployed during the time in between the interviews.

If so, then

\\[ durunemp_{mish=t+1} - durunemp_{mish=t} \in [CW_{mish=t+1} - CW_{mish=t} - 1, CW_{mish=t+1} - CW_{mish=t} + 1] \\]

```stata
tab con_unemp
```

```stata
tab diff_cw_du
```

Interesting finding: While we might argue that a difference of -2 weeks could be due to individuals incorrectly recalling their duration of unemployment in weeks, it is harder to justify a difference of -4 weeks and more similarly.

What could be other reasons for that?

Did we stumbled upon a bug in the dataset?

Does the linkage by IPUMS yield false ties?

Most likely individuals simply recall the time being unemployed wrongly. This would explain some range of the differences, but still the gap should be near 0 - not 100 weeks.

Especially the negative differences do not make sense, since this would mean that the duration of the unemployment increased faster than time passes. We are going to exclude such observations from our analysis, but we will do so with some tolerance.

```stata
keep if diff_cw_du >= -1
```

But how about positive values of *diff_cw_du*? They could imply that an individual was employed in between the interviews. However, this difference would be limited by the maximum interview break length of 8 months.

```stata
tab diff_du diff_cw if diff_cw_du > 0
```

\\[\text{diff_du} < 0 \Rightarrow du_{t+1} < du_{t} \\]
This could mean that a person was not unemployed in between both periods.

\\[\text{diff_du} > 0 \Rightarrow du_{t+1} > du_{t} \\]
This is what we would expect to see for continuing unemployment. However, we cannot reject the possibility of breaks from unemployment.

\\[\text{diff_du} = 0 \Rightarrow du_{t+1} = du_{t+1} \\]
This would make sense if \\( du_{t+1}, du_{t+1} \leq \text{diff_cw} \\), because otherwise this would indicate an error in reporting.

e.g.

$du_{t+1}, du_{t+1} = 3 \Rightarrow \text{diff_du} = 0 $ would be fine,

$du_{t+1}, du_{t+1} = 33 \Rightarrow \text{diff_du} = 0 $ would not.

We are going to keep the osbervations of unemployed individuals for which
\\[ durunemp_{mish=t+1} - durunemp_{mish=t} \leq (cw_{mish=t+1} - cw_{mish=t}) + 1\\]
\\[\Leftrightarrow \text{diff_cu} \leq \text{diff_cw}+1^* \\]
and
\\[ durunemp_{mish=t+1}, durunemp_{mish=t} \leq \text{diff_cw} \quad \forall \quad \text{diff_du} = 0. \\]

\* *Note that we already excluded negative cases, so we can concentrate on the positive gaps here.*

```stata
keep if (diff_du ~= 0 & diff_du <= diff_cw + 1) | missing(diff_du) | (diff_du == 0 & durunemp < diff_cw & durunemp_t < diff_cw)
```

If a person managed to switch its employment status away from unemployment in between interview periods after a long time of unemployment, but ends up in unemployment at the time of the next interview then this could lead to
\\[durunemp_{t+1} \leq durunemp_{t}. \\]
This implies
\\[durunemp_{t+1} - durunemp_{t} \leq 0, \\]
so that
\\[cw_{t+1} - cw_{t} - durunemp_{t+1} + durunemp_{t} \geq 0. \\]
diff_cw_du can even exceed diff_cw.

```stata
tab diff_cw_du if diff_cw < diff_cw_du
```

Let us have a look at the reasons for being unemployed in the next interview month (since we are dealing with leading variables).

```stata
bysort cpsidp (mish): gen whyunemp_t = whyunemp[_n+1]
tab whyunemp
tab whyunemp_t con_unemp
```

We cannot give an answer to the question of why unemployment seems to be discontinuous for most unemployed units with the data at hand.

Bear this findings in mind while continuing to work on our problem.

## [Task 5.3](#content)
Create variables indicating whether an individual was employed or unemployed at the next interview.

Create another variable indicating whether a person might not have been unemployed between the interviews.

```stata
** unemployed in next period?
gen unemployed_t = 1 if inlist(empstat_t, 21, 22)
replace unemployed_t = 0 if !inlist(empstat_t, 21, 22)

** employed in next period?
gen employed_t = 0 if !inlist(empstat_t, 10, 12)
replace employed_t = 1 if inlist(empstat_t, 10, 12)

** unemployed between interviews?
gen unemp_bw = .
replace unemp_bw = 1 if unemployed_t == 1
replace unemp_bw = 0 if diff_du < diff_cw - 1
                                          ** adding some tolerance, so that the durationunemployment
                                          ** can deviate up to 2 weeks from the difference in calender weeks
```

```stata
table unemployed_t unemp_bw employed_t, content(freq)
```

```stata
** distribution of unemployed wrt max duration of unemployment in 2017
hist durunemp if unemployed_t == 0 | (inlist(empstat, 21, 22) & missing(lead_mish))
```

## [Task 5.4](#content)
How many observations of unemployed individuals are still eligible for unemployment benefits?

Find a way to detect the number of unemployment spells for an individual throughout her survey lifetime.

```stata
tab expui
```

75.26 % of all observations for unemployed individuals lie within the range of weeks for which unemployment benefits were granted.

**continuous unemployment**

We have been looking into this earlier. A respondent could experience multiple spells of unemployment throughout the survey. We have to find a crude measure for the approximate number of such spells.

```stata
** break in unemployment
bysort cpsidp (mish): gen empstat_y = empstat[_n-1]
bysort cpsidp (mish): gen durunemp_y = durunemp[_n-1]
bysort cpsidp (mish): gen cw_y = cw[_n-1]
```

```stata
** first occurence in 2017?
egen min_mish = min(mish), by(cpsidp)

** dummy for new spell of unemployment
gen new_u_spell = .m

* unemployed yestrday and unemployed today, no strong indicators of break from unenmployment, i.e. diff_du == diff_cw
replace new_u_spell = 0 if inlist(empstat_y, 21, 22) & inlist(empstat, 21, 22) & durunemp - durunemp_y == (cw - cw_y)

* unemployed today, indicator of break from unemployment, i.e. diff_du < diff_cw
replace new_u_spell = 1 if inlist(empstat, 21, 22) & durunemp - durunemp_y < (cw - cw_y) & durunemp < (cw - cw_y)

* very first observation of an individual, that happens to be unemployed at this point of time
replace new_u_spell = 1 if mish == min_mish & inlist(empstat, 21, 22)

* unemployed today, but not unemployed yesterday
replace new_u_spell = 1 if mish > min_mish & !inlist(empstat_y, 21, 22) & inlist(empstat, 21, 22)

tab new_u_spell
```

```stata
* total of spells
egen num_u_spells = total(new_u_spell), by(cpsidp)
* cumulative sum of spells over months
bysort cpsidp (mish): gen sum_u_spells = sum(new_u_spell)
tab num_u_spells
```

```stata
tab sum_u_spells
```

The table above gives the number of rows of oberservations per number of unemployment spells. However, remember that indiviuals appear more than once in the data. If we are interested in the number of distinct individuals per maximum number of unemployment spell, we have to do the following:

```stata
egen tag_n_spells = tag(cpsidp sum_u_spells)
tab sum_u_spells if tag_n_spells == 1
```

The table above yields the number of individuals per number of unemployment spells in 2017.

According to our identification of breaks in unemployment there were 25 individuals that at least reentered the state of unemployment twice. Does this seem to be plausible to you?

```stata
tab sum_u_spell num_u_spell if expui == 0
```

```stata
tab sum_u_spell num_u_spell if expui == 1
```

## [Task 5.5](#content)
Create a running variable indicating the time in weeks an individual is away from her unemployment benefits to expire.
Considering maximum duration of continuous unemployment, how many individuals were still eligible for benefits?

```stata
** weeks until unemployment benefits expire
gen dis_maxui = durunemp - maxui
```

```stata
hist dis_maxui
```

```stata
egen max_mish = max(mish), by(cpsidp)
```

```stata
tab dis_maxui if unemployed_t == 0 | (mish == max_mish & inrange(unemployed, 21, 22))
```

~ 73% of unemployed individuals are still eligble for unemployment benefits in their last appearance as unemployed in the survey in 2017 (with regard to the duration of their unemployment).

```stata
tab lead_mish if expui == 1
```

```stata
table lead_mish expui con_unemp if unemployed_t == 1, content(freq)
```

```stata
gen dis_mish = lead_mish - mish
tab dis_mish
```

Since we conition our estimation of continuity on calendar week, we do not care about the acutal distance between two consecutive interviews.

How about jobs that were taken up and were lost again between interviews?

$durunemp_{mish+1}< cw_{mish+1} - cw_{mish}$ could indicate such an event.

```stata
** employed between interviews if duration of unemployment is shorter than the time passed between the interviews
replace employed_t = 1 if durunemp_t < diff_cw & inlist(empstat, 21, 22)
```

```stata
tab employed_t
```

```stata
table expui unemployed_t employed_t if inlist(empstat, 21, 22), content(freq)
```

The last column indicates cases where an unemployment spell might have been not continuous.m

## [Task 5.6](#content)
Perform an analysis of the effect of the expiration of unemployment benefits on the employment.
Use a regression discontinuity design for linear fit and polynomial fit (2nd and 3rd degree)

Now we have to install a package for the RDD.

We are going to install the latest version from the default repository.

```stata
search rdrobust
```

What is the running variable?

What determines the threshold?

Which bandwidth are we going to choose?

Do we have a sharp or a fuzzy RD-Design?

```stata
** running variable: weeks to expiration of unemployment benefits
gen rv = durunemp - maxui
```

```stata
** treatment variable: unemloyment benefits expired
gen ubex = 0 if rv < 0
replace ubex = 1 if rv >= 0
```

```stata
scatter ubex rv, xline(0)
```

Since the treatment is merely a dummy for exceding the limit of the duration of unemployment benefits, we are dealing with a sharp regression discontinuity design.

Crossing the threshold perfectly assigns each observation to the treatment group. We cannot observe two different treatment states for one x value.

## Linear fit

The command *rdbwselect* with the specification *bwselect(mserd)* yields an estimate for a mean squared error optimal bandwidth for a linear fit *p(1)*, i.e. the bandwidth that minimizes the MSE.

$$MSE(h) = 𝔼 \left[ ( \hat{ \tau_{SRD}}-\tau_{SRD})^2 \right] = 𝔼\left[ \left( (\hat{\mu}_+ - \mu_+) - (\hat{\mu}_- - \mu_-) \right)^2 \right]$$

$$h^* = arg \min_h MSE(h)$$

A vivid discussion on the advantages and disadvantages of different bandwidth selecting methods has emerged. It is always good practice to inform yourself on the latest developments with regard to an econometric or other analytical approach before starting to employ a standard model.

For the purpose of demonstration, we are going to stick to the textbook approach.

```stata
rdbwselect employed_t rv, bwselect(mserd) p(1) q(2)
```

```stata
gen tmp_y = employed_t if inrange(rv, -7, 7)
gen tmp_x = rv if inrange(rv, -7, 7)

su tmp_x

rdrobust tmp_y tmp_x, bwselect(mserd) p(1) h(7) c(0)
```

```stata
rdplot tmp_y tmp_x, genvars ci(95) p(1) binselect(es) vce(robust)
```

The following snippets are more complex than they have to be. This is due to Jupyter not displaying the output of

> rdplot y x

properly. So I had to use the source code for rdplot.

You can find information on the package here: https://rdpackages.github.io/rdrobust/.

```stata
reg tmp_y tmp_x ubex, robust
```

```stata
gen rdplot_hat_y_0 = _b[_cons] + (tmp_x * _b[tmp_x])
gen rdplot_hat_y_1 = _b[_cons] + (tmp_x * _b[tmp_x]) + (ubex * _b[ubex])
```

```stata
twoway (rcap rdplot_ci_l rdplot_ci_r rdplot_mean_bin, color(gs11)) ///
(scatter rdplot_mean_y rdplot_mean_bin, sort msize(small)  mcolor(gs10)) ///
(line rdplot_hat_y_0 tmp_x if tmp_x<=0, lcolor(black) sort lwidth(medthin) lpattern(solid)) ///
(line rdplot_hat_y_1 tmp_x if tmp_x>=0, lcolor(black) sort lwidth(medthin) lpattern(solid)), ///
xline(0, lcolor(black) lwidth(medthin)) xscale(r(-7 7) noextend) xlabel(-7(1)7) ///
legend(cols(2) order(1 "Sample average within bin" 2 "linear fit" )) title("Regression function fit", color(gs0))
```

### Polynomial fit of order 2

```stata
gen tmp_x2 = rv * rv if inrange(rv, -7, 7)
reg tmp_y tmp_x tmp_x2 ubex, robust
```

```stata
gen rdplot_hat_y_2 = _b[_cons] + (tmp_x * _b[tmp_x]) + (tmp_x2 * _b[tmp_x2])
gen rdplot_hat_y_3 = _b[_cons] + (tmp_x * _b[tmp_x]) + (tmp_x2 * _b[tmp_x2]) + (ubex * _b[ubex])
```

```stata
twoway (rcap rdplot_ci_l rdplot_ci_r rdplot_mean_bin, color(gs11)) ///
(scatter rdplot_mean_y rdplot_mean_bin, sort msize(small)  mcolor(gs10)) ///
(line rdplot_hat_y_2 tmp_x if tmp_x<=0, lcolor(red) sort lwidth(medthin) lpattern(solid)) ///
(line rdplot_hat_y_3 tmp_x if tmp_x>=0, lcolor(red) sort lwidth(medthin) lpattern(solid)) ///
(line rdplot_hat_y_0 tmp_x if tmp_x<=0, lcolor(black) sort lwidth(medthin) lpattern(solid)) ///
(line rdplot_hat_y_1 tmp_x if tmp_x>=0, lcolor(black) sort lwidth(medthin) lpattern(solid)), ///
xline(0, lcolor(black) lwidth(medthin)) xscale(r(-7 7) noextend) xlabel(-7(1)7) ///
legend(cols(2) order(1 "Sample average within bin" 2 "linear fit" )) title("Regression function fit", color(gs0))
```

### Polynomial fit of order 3

```stata
gen tmp_x3 = tmp_x2 * rv if inrange(rv, -7, 7)
reg tmp_y tmp_x tmp_x2 tmp_x3 ubex, robust
gen rdplot_hat_y_4 = _b[_cons] + (tmp_x * _b[tmp_x]) + (tmp_x2 * _b[tmp_x2])
gen rdplot_hat_y_5 = _b[_cons] + (tmp_x * _b[tmp_x]) + (tmp_x2 * _b[tmp_x2]) + (ubex * _b[ubex])
```

```stata
twoway (rcap rdplot_ci_l rdplot_ci_r rdplot_mean_bin, color(gs11)) ///
(scatter rdplot_mean_y rdplot_mean_bin, sort msize(small)  mcolor(gs10)) ///
(line rdplot_hat_y_4 tmp_x if tmp_x<=0, lcolor(green) sort lwidth(medthin) lpattern(solid)) ///
(line rdplot_hat_y_5 tmp_x if tmp_x>=0, lcolor(green) sort lwidth(medthin) lpattern(solid)) ///
(line rdplot_hat_y_2 tmp_x if tmp_x<=0, lcolor(red) sort lwidth(medthin) lpattern(solid)) ///
(line rdplot_hat_y_3 tmp_x if tmp_x>=0, lcolor(red) sort lwidth(medthin) lpattern(solid)) ///
(line rdplot_hat_y_0 tmp_x if tmp_x<=0, lcolor(black) sort lwidth(medthin) lpattern(solid)) ///
(line rdplot_hat_y_1 tmp_x if tmp_x>=0, lcolor(black) sort lwidth(medthin) lpattern(solid)), ///
xline(0, lcolor(black) lwidth(medthin)) xscale(r(-7 7) noextend) xlabel(-7(1)7) ///
legend(cols(2) order(1 "Sample average within bin" 2 "linear fit" )) title("Regression function fit", color(gs0))
```

**Why did I use the predictions of y from a regression to plot the lines?**

As I said, I am not able to render the output of *rdplot* inside this notebook properly.

The actual fitted lines of *rdplot* are slightly different to my predictions of y due to differences in the variance-covariance estimation employed in both the regression in *rdplot* and my *reg y x, robust*.