forked from paulgp/applied-methods-phd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlikelihood_3.R
More file actions
178 lines (160 loc) · 6.7 KB
/
Copy pathlikelihood_3.R
File metadata and controls
178 lines (160 loc) · 6.7 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
library(tidyverse)
library(stats)
library(broom)
library(estimatr)
library(haven)
dblue = rgb(0, 114, 178, max = 255)
dred = rgb(213,94,0, max = 255)
acs_data <- read_csv("~/Dropbox/Papers/GGP_HoldingLength/data/usa_00014.csv")
acs_data <- usa_00014
corelogic_data = read_dta("~/Dropbox/Papers/GGP_HoldingLength/data/holding_data_sample100k.dta")
sample = acs_data %>% filter(YEAR == 2019) %>% select(OWNERSHP, AGE, RACE, INCTOT, HHWT)
sample_10k = sample %>% sample_n(10000) %>% mutate(owner = OWNERSHP == 1,
age = AGE,
age_sq = age^2,
income = INCTOT / 10000) %>%
filter(age > 18) %>% filter(INCTOT > 0)
ggplot(data = corelogic_data %>% filter(!is.na(duration))) +
geom_histogram(aes(x = duration/12, after_stat(density)), fill=dblue,) +
labs(x = "Duration in years",
y = "",
subtitle = "Distribution of housing duration") +
theme_minimal() +
theme(text = element_text(size=24)) +
xlim(0,30)
ggsave("images/housing_duration_full.png")
ggplot(data = corelogic_data %>% filter(!is.na(duration)),
aes(x = duration/12)) +
geom_step(aes(y = 1 - ..y..), stat='ecdf') +
labs(x = "Duration in years",
y = "",
subtitle = "Survival probability") +
theme_minimal() +
theme(text = element_text(size=24)) +
xlim(0,30)
ggsave("images/housing_duration_full_naive_survival.png")
library(survminer)
library(condsurv)
library(survival)
model = survfit(Surv(time, status) ~ 1,
data = corelogic_data %>% filter(!is.na(duration)) %>%
mutate(time = duration/12) %>%
mutate(status = case_when(fym == 692 ~ 1,
TRUE ~ 2) ))
ggplot(data = corelogic_data %>% filter(!is.na(duration)),
aes(x = duration/12)) +
geom_step(aes(y = 1 - ..y..), stat='ecdf', color = dred) +
labs(x = "Duration in years",
y = "",
subtitle = "Survival probability") +
theme_minimal() +
theme(text = element_text(size=24)) +
xlim(0,30) +
geom_step(data = tibble(y = model$surv, x = model$time),
aes(y = y, x = x), color=dblue) +
annotate("text", x = 25, y = 0.38,
hjust = 0, size = 7, color = dblue,
label = "Kaplan-Meier") +
annotate("text", x = 25, y = 0.05,
hjust = 0, size = 7,
color = dred, label = "Unadjusted")
ggsave("images/housing_duration_full_naive_survival_v_KM.png")
model_boom = survfit(Surv(time, status) ~ 1,
data = corelogic_data %>% filter(!is.na(duration)) %>%
mutate(time = duration/12) %>%
mutate(status = case_when(fym == 692 ~ 1,
TRUE ~ 2) ) %>%
filter(year > 2000 & year < 2008))
model_bust = survfit(Surv(time, status) ~ 1,
data = corelogic_data %>% filter(!is.na(duration)) %>%
mutate(time = duration/12) %>%
mutate(status = case_when(fym == 692 ~ 1,
TRUE ~ 2) )%>%
filter(year > 2007 & year < 2011))
ggplot(data = corelogic_data %>% filter(!is.na(duration)),
aes(x = duration/12)) +
labs(x = "Duration in years",
y = "",
subtitle = "Survival probability") +
theme_minimal() +
theme(text = element_text(size=24)) +
xlim(0,18) +
geom_step(data = tibble(y = model_boom$surv, x = model_boom$time),
aes(y = y, x = x), color=dblue) +
geom_step(data = tibble(y = model_bust$surv, x = model_bust$time),
aes(y = y, x = x), color=dred) +
annotate("text", x = 15, y = 0.48,
hjust = 0, size = 7, color = dblue,
label = "Boom - 2000-2006") +
annotate("text", x = 10, y = 0.64,
hjust = 0, size = 7,
color = dred, label = "Bust - 2007-2010")
ggsave("images/housing_duration_survival_KM_boom_bust.png")
boom_data = tibble(y = model_boom$surv, x = model_boom$time) %>%
mutate(f = lag(y)-y) %>%
mutate(f = case_when(is.na(f) ~ 1-y,
TRUE ~ f)) %>%
mutate(h = f/y)
bust_data = tibble(y = model_bust$surv, x = model_bust$time) %>%
mutate(f = lag(y)-y) %>%
mutate(f = case_when(is.na(f) ~ 1-y,
TRUE ~ f)) %>%
mutate(h = f/y)
ggplot(data = corelogic_data %>% filter(!is.na(duration)),
aes(x = duration/12)) +
labs(x = "Duration in years",
y = "",
subtitle = "Hazard rate") +
theme_minimal() +
theme(text = element_text(size=24)) +
xlim(0,18) +
geom_step(data = boom_data,
aes(y = h, x = x), color=dblue) +
geom_step(data = bust_data,
aes(y = h, x = x), color=dred) +
annotate("text", x = 15, y = 0.0,
hjust = 0, size = 7, color = dblue,
label = "Boom - 2000-2006") +
annotate("text", x = 10, y = 0.0,
hjust = 0, size = 7,
color = dred, label = "Bust - 2007-2010")
ggsave("images/housing_duration_hazard_KM_boom_bust.png")
ggplot(data = corelogic_data %>% filter(!is.na(duration) & year >= 2010)) +
geom_histogram(aes(x = duration/12, after_stat(density)), fill=dblue,) +
labs(x = "Duration in years",
y = "",
subtitle = "Distribution of housing duration for 2010+ cohort") +
theme_minimal() +
theme(text = element_text(size=24)) +
xlim(0,30)
ggsave("images/housing_duration_2010plus.png")
ggplot(data = corelogic_data %>% filter(!is.na(duration) & year >= 2005)) +
geom_histogram(aes(x = duration/12, after_stat(density)), fill=dblue,) +
labs(x = "Duration in years",
y = "",
subtitle = "Distribution of housing duration for 2005+ cohort") +
theme_minimal() +
theme(text = element_text(size=24)) +
xlim(0,30)
ggsave("images/housing_duration_2005plus.png")
ggplot(data = corelogic_data %>% filter(!is.na(duration) & year >= 2000 & year <=2008)) +
geom_histogram(aes(x = duration/12, after_stat(density)), fill=dblue) +
labs(x = "Duration in years",
y = "",
subtitle = "Distribution of housing duration for 2000-2008 cohorts") +
theme_minimal() +
theme(text = element_text(size=24)) +
xlim(0,20) +
facet_wrap(~year)
ggsave("images/housing_duration_20002010_byyear.png")
ggplot(data = corelogic_data %>% filter(!is.na(duration) & year >= 2000 & year <=2008)) +
stat_ecdf(aes(x = duration/12, group=year, color=as.factor(year))) +
labs(x = "Duration in years",
y = "",
subtitle = "Distribution of housing duration for 2000-2008 cohorts",
color = "Purchase Year") +
theme_minimal() +
theme(text = element_text(size=24),
legend.position = c(0.2,.7)) +
xlim(0,20)
ggsave("images/housing_duration_20002010_byyear_cdf.png")