-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitting_chains.Rmd
206 lines (146 loc) · 4.67 KB
/
fitting_chains.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
---
title: "Computation Time Verification & Fitting Chains for Data Analysis"
output:
pdf_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(LaplacesDemon)
library(BayesLogit)
library(Matrix)
library(tidyverse)
```
# Overview
This notebook was used to
1. get an approximate runtime required to fit the models on North Carolina data
2. fit models and also record the runtime for the S.Atlantic Census Division. These chains were saved and used for the data analysis (Section 6 of the manuscript)
This was run on a M1 Macbook Pro (2021).
Run on: `r Sys.Date()`.
The samplers are loaded here
```{r}
source("samplers/fh_fit.R")
source("samplers/dm_fit.R")
source("samplers/car_fit.R")
source("samplers/bym_fit.R")
source("samplers/ssd_fit.R")
```
# North Carolina
## Load Data
```{r warning=FALSE}
#north carolina
load("data/data_NC/data.RDA")
#data
covs = c("degree", "assistance", "no_car", "povPerc",
"white", "black", "native", "asian", "hispanic")
X <- model.matrix(~., all_data[, covs, drop=F])
n <- nrow(X); j <- ncol(X) # number of covariates INCLUDES intercept
#response (with transformation)
y <- all_data$rentBurden
d.var <- all_data$rentBurdenSE^2
y.star <- log(y)
d.star <- d.var / y^2
```
## Run samplers
```{r n_carolina}
set.seed(7)
# ---- Run Chains ----
start <- proc.time()
fh_res <- fh_fit(X, y.star, d.star, ndesired=2500, nburn=10000, nthin=1, verbose=FALSE)
fh_time <- proc.time() - start
start <- proc.time()
dm_res <- dm_fit(X, y.star, d.star, ndesired=2500, nburn=10000, nthin=1, verbose=FALSE)
dm_time <- proc.time() - start
start <- proc.time()
car_res <- car_fit(X, y.star, d.star, A, ndesired=2500, nburn=2000, nthin=1, verbose=FALSE)
car_time <- proc.time() - start
start <- proc.time()
bym_res <- bym_fit(X, y.star, d.star, A, ndesired=2500, nburn=2000, nthin=1, verbose=FALSE)
bym_time <- proc.time() - start
start <- proc.time()
new_res <- ssd_fit(X, y.star, d.star, A, ndesired=2500, nburn=2000, nthin=1, verbose=FALSE)
ssd_time <- proc.time() - start
```
Computation time **in seconds**:
```{r echo=FALSE}
print("--- FH Computation time ---")
fh_time
print("--- DM Computation time ---")
dm_time
print("--- CAR Computation time ---")
car_time
print("--- BYM Computation time ---")
bym_time
print("--- SSD Computation time ---")
ssd_time
```
# South Atlantic Census Division
## Load Data
```{r warning=FALSE, message=FALSE}
library(tidycensus)
#south atlantic census division
load("data/data_multi-state/data.RDA")
all_data = all_data %>% mutate(state_code=strtrim(all_data$fips, 2)) %>%
left_join(fips_codes %>%
select(state, state_code, state_name) %>%
unique.data.frame(), by="state_code")
#data
covs = c("degree", "povPerc", "black", "state")
X <- model.matrix(~., all_data[, covs, drop=F])
n <- nrow(X); j <- ncol(X) # number of covariates INCLUDES intercept
#response (with transformation)
y <- all_data$rentBurden
d.var <- all_data$rentBurdenSE^2
y.star <- log(y)
d.star <- d.var / y^2
```
## Run samplers
```{r s_atlantic}
set.seed(7)
# ---- Run Chains ----
start <- proc.time()
fh_res <- fh_fit(X, y.star, d.star, ndesired=2500, nburn=10000, nthin=1, verbose=FALSE)
fh_time <- proc.time() - start
start <- proc.time()
dm_res <- dm_fit(X, y.star, d.star, ndesired=2500, nburn=10000, nthin=1, verbose=FALSE)
dm_time <- proc.time() - start
start <- proc.time()
car_res <- car_fit(X, y.star, d.star, A, ndesired=2500, nburn=2000, nthin=1, verbose=FALSE)
car_time <- proc.time() - start
start <- proc.time()
bym_res <- bym_fit(X, y.star, d.star, A, ndesired=2500, nburn=2000, nthin=1, verbose=FALSE)
bym_time <- proc.time() - start
start <- proc.time()
new_res <- ssd_fit(X, y.star, d.star, A, ndesired=2500, nburn=2000, nthin=1, verbose=FALSE)
ssd_time <- proc.time() - start
```
Computation time **in seconds**:
```{r echo=FALSE}
print("--- FH Computation time ---")
fh_time
print("--- DM Computation time ---")
dm_time
print("--- CAR Computation time ---")
car_time
print("--- BYM Computation time ---")
bym_time
print("--- SSD Computation time ---")
ssd_time
```
Computation time in **minutes (spatial models)**:
```{r echo=FALSE}
print("--- CAR Computation time (minutes)---")
car_time/60
print("--- BYM Computation time (minutes)---")
bym_time/60
print("--- SSD Computation time (minutes)---")
ssd_time/60
```
## Save the chains for S.Atlantic Division
To be used for the data analysis.
```{r}
save(fh_res, file="data_analysis_chains/fh_res.RDA")
save(dm_res, file="data_analysis_chains/dm_res.RDA")
save(car_res, file="data_analysis_chains/car_res.RDA")
save(bym_res, file="data_analysis_chains/bym_res.RDA")
save(new_res, file="data_analysis_chains/new_res.RDA")
```