43
Tim Birkenbach (MEA) 19/10/2017 SHARE User Workshop Ljubljana 2017 P ANEL DATA ANALYSIS USING STATA

PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

Embed Size (px)

Citation preview

Page 1: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

Tim Birkenbach (MEA)

19/10/2017

SHARE User Workshop Ljubljana 2017

PANEL DATA ANALYSIS USING STATA

Page 2: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

2

(I) Basic panel commands in Stata • xtset • xtdescribe • reshape

(II)Panel analysis popular in Economics • Pooled OLS • Fixed-Effects Model & Difference-in-Difference • Random Effects Model

Outline

1

Page 3: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

3

xtset

2

Page 4: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

4

// declare panel data structure xtset panelvar timevar

xtset

3

Page 5: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

5 1

xtdescribe

Page 6: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

6

reshape

5

long

wide

Page 7: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

7

// reshape format: long -> wide reshape wide health, i(mergeid) j(wave)

reshape

1

Page 8: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

8

// reshape format: wide -> long reshape long health, i(mergeid) j(wave)

reshape

1

Page 9: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

9

gen original = 1 reshape wide health original, i(mergeid) j(wave) reshape long health original, i(mergeid) j(wave) keep if original == 1

reshape

1

Page 10: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

10

(I) Basic panel commands in Stata • xtset • xtdescribe • reshape

(II)Panel analysis popular in Economics • Pooled OLS • Fixed-Effects Model & Difference-in-Difference • Random Effects Model

Outline

9

Page 11: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

11

between & within estimator

Y (health)

t=1 t=2

between estimate

within estimate

R1

R1

R2

10

X (working)

X (retired)

Page 12: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

12

(I) Basic panel commands in Stata • xtset • xtdescribe • reshape

(II)Panel analysis popular in Economics • Pooled OLS • Fixed-Effects Model & Difference-in-Difference • Random Effects Model

Outline

11

Page 13: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

13

Pooled Ordinary Least Squares (POLS)

12

wave 4

wave 5

wave 6

+

+

wave 4

wave 5

wave 6 =

Page 14: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

14

Pooled Ordinary Least Squares (POLS)

t=1 t=2

between estimate

R2

R2

R3

13

R1

R3

R1

mean

mean

Y (health)

X (working)

X (retired)

Page 15: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

15

Pooled Ordinary Least Squares (POLS)

14

𝑦𝑦𝑖𝑖𝑖𝑖 = 𝛽𝛽0 + 𝛿𝛿𝑖𝑖 + 𝛽𝛽1𝑥𝑥𝑖𝑖𝑖𝑖 + 𝜀𝜀𝑖𝑖𝑖𝑖

time effect multiple lines per respondent i

𝜺𝜺 dependent ?

Page 16: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

16

Pooled Ordinary Least Squares (POLS)

15

𝜀𝜀 independent 𝜀𝜀 dependent

- cluster by respondent

- serial correlation

Page 17: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

17

// POLS reg health retired // + cluster robust inference reg health retired , cluster(id) // + linear time trend reg health retired wave , cluster(id) // + period effect reg health retired i.wave , cluster(id)

Pooled Ordinary Least Squares (POLS)

16

Page 18: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

18

Pooled Ordinary Least Squares (POLS)

1

Page 19: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

19

(I) Basic panel commands in Stata • xtset • xtdescribe • reshape

(II)Panel analysis popular in Economics • Pooled OLS • Fixed-Effects Model & Difference-in-Difference • Random Effects Model

Outline

18

Page 20: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

20

Fixed Effects Estimation (FE)

19

𝑦𝑦𝑖𝑖𝑖𝑖 = 𝛽𝛽1𝑥𝑥𝑖𝑖𝑖𝑖 + 𝑢𝑢𝑖𝑖 + 𝑒𝑒𝑖𝑖𝑖𝑖

𝑦𝑦𝑖𝑖𝑖𝑖 = 𝛽𝛽1𝑥𝑥𝑖𝑖𝑖𝑖 + 𝜀𝜀𝑖𝑖𝑖𝑖

𝑦𝑦𝑖𝑖𝑖𝑖 − 𝑦𝑦𝑖𝑖� = 𝛽𝛽1 𝑥𝑥𝑖𝑖𝑖𝑖 − 𝑥𝑥𝑖𝑖� + (𝑢𝑢𝑖𝑖 − 𝑢𝑢𝑖𝑖� ) + (𝑒𝑒𝑖𝑖𝑖𝑖 − 𝑒𝑒𝑖𝑖� )

�̈�𝑦𝑖𝑖𝑖𝑖 = 𝛽𝛽1�̈�𝑥𝑖𝑖𝑖𝑖 + �̈�𝑒𝑖𝑖𝑖𝑖

error decomposition

within transformstion

if 𝐶𝐶𝑜𝑜𝑜𝑜 𝑥𝑥𝑖𝑖𝑖𝑖, 𝜀𝜀𝑖𝑖𝑖𝑖 ≠ 0 for any t, POLS is biased

Page 21: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

21

within transformation

Fixed Effects Estimation (FE)

20

0

within transformation

Page 22: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

22

Fixed Effects Estimation (FE)

t=1 t=2

within estimate

R2

R2

R3

21

R1

R3

R1

Y (health)

X (working)

X (retired)

Page 23: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

23

Fixed Effects Estimation (FE)

22

// declare panel data structure xtset id wave // FE xtreg health retired , fe // + cluster robust inference xtreg health retired , fe cluster(id)

Page 24: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

24

Fixed Effects Estimation (FE)

23

Page 25: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

25

Difference-in-Difference (DID)

t=1 t=2

parallel trend assumption

24

R2 R2

R1

R1

R1

counterfactual

DID estimate

Y (health)

X (working)

X (retired)

Page 26: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

26

Difference-in-Difference (DID)

1

𝑦𝑦𝑖𝑖𝑖𝑖 = 𝛽𝛽1𝑥𝑥𝑖𝑖𝑖𝑖 + 𝛿𝛿𝑖𝑖 + 𝑢𝑢𝑖𝑖 + 𝑒𝑒𝑖𝑖𝑖𝑖

𝑦𝑦𝑖𝑖𝑖𝑖 = 𝛽𝛽1𝑥𝑥𝑖𝑖𝑖𝑖 + 𝛿𝛿𝑖𝑖 + 𝜀𝜀𝑖𝑖𝑖𝑖

𝑦𝑦𝑖𝑖𝑖𝑖 − 𝑦𝑦𝑖𝑖� = 𝛽𝛽1 𝑥𝑥𝑖𝑖𝑖𝑖 − 𝑥𝑥𝑖𝑖� + (𝛿𝛿𝑖𝑖 − 𝛿𝛿𝑖𝑖� ) + (𝑢𝑢𝑖𝑖 − 𝑢𝑢𝑖𝑖� ) + (𝑒𝑒𝑖𝑖𝑖𝑖 − 𝑒𝑒𝑖𝑖� )

�̈�𝑦𝑖𝑖𝑖𝑖 = 𝛽𝛽1�̈�𝑥𝑖𝑖𝑖𝑖 �̈�𝛿𝑖𝑖 + �̈�𝛿𝑖𝑖 + �̈�𝑒𝑖𝑖𝑖𝑖

error decomposition

within transformstion

period effect

Page 27: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

27

Difference-in-Difference (DID)

25

// declare panel data structure xtset id wave // DID xtreg health retired i.wave , fe // + cluster robust inference xtreg health retired i.wave , fe cluster(id)

Page 28: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

28

Difference-in-Difference (DID)

1

Page 29: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

29

Within models (pros & cons)

27

// pro: within models can overcome problems that arises from unobserved heterogeneity bias & attrition // contra: within models only focus on a small fraction of the variance in the data they usually have larger standard errors (lower efficiency) // contra: within models cannot incorporate time-constant variables directly e.g. gender, country, ethnical background

Page 30: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

30

(I) Basic panel commands in Stata • xtset • xtdescribe • reshape

(II)Panel analysis popular in Economics • Pooled OLS • Fixed-Effects Model & Difference-in-Difference • Random Effects Model

Outline

28

Page 31: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

31

Random Effects Estimation (RE)

29

𝜀𝜀𝑖𝑖𝑖𝑖 = 𝑢𝑢𝑖𝑖 + 𝑒𝑒𝑖𝑖𝑖𝑖

𝑦𝑦𝑖𝑖𝑖𝑖 = 𝛽𝛽0 + 𝛽𝛽1𝑥𝑥𝑖𝑖𝑖𝑖 + 𝜀𝜀𝑖𝑖𝑖𝑖

𝑦𝑦𝑖𝑖𝑖𝑖 − 𝜆𝜆𝑦𝑦𝑖𝑖� = 𝛽𝛽0 1 − 𝜆𝜆 + 𝛽𝛽1 𝑥𝑥𝑖𝑖𝑖𝑖 − 𝜆𝜆𝑥𝑥𝑖𝑖� + (𝜀𝜀𝑖𝑖𝑖𝑖 − 𝜆𝜆𝜀𝜀𝑖𝑖� )

error decomposition

quasi within transformstion

= FGLS

random effects assumption 𝐶𝐶𝑜𝑜𝑜𝑜 𝑥𝑥𝑖𝑖𝑖𝑖,𝑢𝑢𝑖𝑖 = 0 , 𝑡𝑡 = 1,2, … ,𝑇𝑇

𝜆𝜆 = 1 − 𝜎𝜎2𝑒𝑒

𝜎𝜎2𝑒𝑒 + 𝑇𝑇𝜎𝜎2𝑢𝑢

Page 32: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

32

within transformation

Random Effects Estimation (RE)

30

0

quasi-within transformation

𝜆𝜆

𝜆𝜆

Page 33: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

33

Random Effects Estimation (RE)

31

𝑦𝑦𝑖𝑖𝑖𝑖 − 𝜆𝜆𝑦𝑦𝑖𝑖� = 𝛽𝛽0 1 − 𝜆𝜆 + 𝛽𝛽1 𝑥𝑥𝑖𝑖𝑖𝑖 − 𝜆𝜆𝑥𝑥𝑖𝑖� + (𝜀𝜀𝑖𝑖𝑖𝑖 − 𝜆𝜆𝜀𝜀𝑖𝑖� )

If 𝜆𝜆 = 1, then RE = FE

If 𝜆𝜆 = 0, then RE = POLS

𝛽𝛽𝑅𝑅𝑅𝑅 lies in between 𝛽𝛽𝑃𝑃𝑃𝑃𝑃𝑃𝑃𝑃 & 𝛽𝛽𝐹𝐹𝑅𝑅

Page 34: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

34

Random Effects Estimation (RE)

32

// declare panel data structure xtset id wave // RE xtreg health retired , re // + time-constant explanatory variable xtreg health retired female , re // + cluster robust inference & period effect xtreg health retired female i.wave, re cluster(id)

Page 35: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

35

Random Effects Estimation (RE)

1

Page 36: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

36

Hausman test

34

𝐻𝐻0: 𝛽𝛽𝑅𝑅𝑅𝑅 − 𝛽𝛽𝐹𝐹𝑅𝑅 = 0

𝐻𝐻𝐴𝐴: 𝛽𝛽𝑅𝑅𝑅𝑅 − 𝛽𝛽𝐹𝐹𝑅𝑅 ≠ 0

random effects model

fixed effects model

Hausman test

Page 37: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

37

Hausman test

35

// fixed effects model xtreg health retired i.wave , fe estimates store fixed // random effects model xtreg health retired i.wave , re estimates store random // hausman test hausman fixed random

Page 38: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

38

Hausman test

36

fixed effects model

Page 40: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

40

Appendix

1

Page 41: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

41

// generate lagged variables gen lag1_health = l.health gen lag2_health = l2.health

time series operators

1

Page 42: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

42

// generate differenced variables gen diff1_health = d.health gen diff2_health = d2.health

time series operators

1

Page 43: PANEL DATA ANALYSIS - SHARE - Raziskava o zdravju, …€¦ · 2 (I) Basic panel commands in Stata • xtset • xtdescribe • reshape (II)Panel analysis popular in Economics •

43

Logit models

1

// Pooled logit logit goodhealth retired // declare panel data structure xtset id wave // FE logit xtlogit goodhealth retired , fe // RE logit xtlogit goodhealth retired , re

methodology &

interpretation quite complex !