-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathmakeSubSample.Rmd
111 lines (87 loc) · 3.53 KB
/
makeSubSample.Rmd
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
From PracticalDataScienceWithR2nd/PDSwR2/PUMS/ss16pus.RDS
Making the subset of data used for Chapter 8 Linear regression example scenario
* Restrict data to full-time employees between 20 and 50 years of age,
with income between $1000 and #250,000
From Data Dictionary
* PINCP = total person income
* ESR = employment status recode
* PERNP = total person's earnings
* WKHP = usual hours worked per week over the last 12 months
* AGEP = age
* SCHL = educational attainment.
* SEX = sex.
* COW = class of worker
```{r}
dpus = readRDS("ss16pus.RDS")
dim(dpus)
# change the numeric columns to numeric
for(col in c('PINCP', 'PERNP', 'WKHP', 'AGEP')) {
dpus[[col]] = as.numeric(dpus[[col]])
}
summary(dpus[, wrapr::qc(ESR, PINCP, PERNP, WKHP, AGEP, COW, SCHL)])
# build indicator for a very restricted subset
dpus$stdworker <- with(dpus,
(ESR == "Civilian employed, at work") &
(PINCP > 1000) & (PINCP<=250000) & # personal income between $1000 - $250,000
(PERNP > 1000) & (PERNP<=250000) & # total earnings between $1000 - $250,000
(!is.na(WKHP)) & (WKHP >= 40) & # full time (more than 40 hrs/week)
(AGEP >= 20) & (AGEP <= 50) & # between 20 and 50 years old
(!(COW %in% c("Unemployed and last worked 5 years ago or earlier or never worked",
"Working without pay in family business or farm"))) &
(!is.na(COW)) &
(!is.na(SCHL)) )
# get a very restricted subset
psub = subset(dpus, stdworker)
dim(psub)
```
Fix up factors
```{r}
# mostly to make this consistent with already existing code.
psub$SEX <- relevel(as.factor(psub$SEX),'Male')
# make the class of worker categories shorter
summary(as.factor(psub$COW))
(existingCOW <- sort(unique(psub$COW)) )
(shorterCOW <- c("Employee of a private for profit",
"Private not-for-profit employee",
"Federal government employee",
"Local government employee",
"Self employed incorporated",
"Self employed not incorporated",
"State government employee"
) )
names(shorterCOW) <- existingCOW; print(shorterCOW)
psub$COW <- as.factor(shorterCOW[psub$COW])
summary(psub$COW)
# simplify the educational levels
summary(as.factor(psub$SCHL))
(existingSCHL <- sort(unique(psub$SCHL)) )
(shorterSCHL <- c( c("some college credit, no degree",
"no high school diploma",
"Associate's degree",
"Bachelor's degree",
"Doctorate degree",
"GED or alternative credential"),
rep("no high school diploma", 12),
c("Master's degree",
"no high school diploma",
"no high school diploma",
"Professional degree",
"Regular high school diploma",
"some college credit, no degree")) )
names(shorterSCHL) <- existingSCHL; print(shorterSCHL)
SCHLlevels <- c(
"no high school diploma",
"Regular high school diploma",
"GED or alternative credential",
"some college credit, no degree",
"Associate's degree",
"Bachelor's degree",
"Master's degree",
"Professional degree",
"Doctorate degree")
shorter <- shorterSCHL[psub$SCHL]
psub$SCHL <- factor(shorter, levels=SCHLlevels)
summary(psub$SCHL)
# get a very restricted subset
saveRDS(psub, "psub.RDS")
```