-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathexercise_3b.R
68 lines (49 loc) · 1.95 KB
/
exercise_3b.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -----------------------------------------------------------------------------
# DESCRIPTION: THIS PROGRAM ILLUSTRATES HOW TO POOL MEPS LONGITUDINAL DATA
# FILES FROM DIFFERENT PANELS
#
# THE EXAMPLE USED IS PANELS 17-19 POPULATION AGE 26-30 WHO ARE UNINSURED BUT
# HAVE HIGH INCOME IN THE FIRST YEAR
#
# DATA FROM PANELS 17, 18, AND 19 ARE POOLED.
#
# INPUT FILES: (1) C:/MEPS/H183.ssp (PANEL 19 LONGITUDINAL FILE)
# (2) C:/MEPS/H172.ssp (PANEL 18 LONGITUDINAL FILE)
# (3) C:/MEPS/H164.ssp (PANEL 17 LONGITUDINAL FILE)
# -----------------------------------------------------------------------------
# Install and load libraries
# Can skip this part if already installed
install.packages("survey")
install.packages("foreign")
install.packages("dplyr")
# Run this part each time you re-start R
library(survey)
library(foreign)
library(dplyr)
# Set options to deal with lonely psu
options(survey.lonely.psu='adjust');
# Read in data
varlist = c("DUPERSID", "INSCOVY1", "INSCOVY2",
"LONGWT", "VARSTR", "VARPSU",
"POVCATY1", "AGEY1X", "PANEL")
h183 = read.xport("C:/MEPS/h183.ssp") %>% select(varlist) # Panel 19
h172 = read.xport("C:/MEPS/h172.ssp") %>% select(varlist) # Panel 18
h164 = read.xport("C:/MEPS/h164.ssp") %>% select(varlist) # Panel 17
# Stack longitudinal files and define pooled weight variable and subpop of interest
# subpop = age 26-30, uninsured, high income
pool = bind_rows(h183, h172, h164) %>%
mutate(poolwt = LONGWT / 3,
subpop = (26 <= AGEY1X & AGEY1X <= 30 & POVCATY1 == 5 & INSCOVY1 == 3))
pool %>%
filter(subpop) %>%
summary
head(pool)
# Define the survey design
mepsdsgn = svydesign(
id = ~VARPSU,
strata = ~VARSTR,
weights = ~poolwt,
data = pool,
nest = TRUE)
# INSURANCE STATUS IN THE SECOND YEAR FOR THOSE W/ AGE=26-30, UNINSURED WHOLE YEAR, AND HIGH INCOME IN THE FIRST YEAR'
svymean(~as.factor(INSCOVY2), design = subset(mepsdsgn, subpop))