-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.R
232 lines (181 loc) · 7.04 KB
/
Main.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
## R code for assignment EBS2072 ##
## Tobias Schnabel ##
## i6255807 ##
rm(list = ls(all = TRUE)) #CLEAR ALL
####Housekeeping####
packages <- c("tidyverse", "broom", "ggpubr", "stargazer", "caret", "rstan",
"rstanarm", "kableExtra", "bayesplot", "coda", "ISLR")
#Comment in lines below to Install packages not yet installed
# installed_packages <- packages %in% rownames(installed.packages())
# if (any(installed_packages == FALSE)) {
# install.packages(packages[!installed_packages])
# }
#load packages
invisible(lapply(packages, library, character.only = TRUE))
#set options
options(digits = 3)
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
#load Credit Card Default Data Set
attach(Default)
#record start time
start.time = Sys.time()
# tidy
#make factors numerical
data = Default %>%
mutate(default=ifelse(default=="No", 0,1)) %>%
mutate(student=ifelse(student=="No", 0,1))
#df summary statistics
stargazer(data, type = "text")
#generate datasets with fewer obs to compare models
intrain_1k = caret::createDataPartition(
y = data$default,
p = 0.1,
list = F
)
intrain_5k = caret::createDataPartition(
y = data$default,
p = 0.5,
list = F
)
subset1 = data[intrain_1k,]
subset2 = data[intrain_5k,]
#verify proportions
props = rbind(table(subset1$default)[2]/table(subset1$default)[1],
table(subset2$default)[2]/table(subset2$default)[1],
table(data$default)[2]/table(data$default)[1])
nrows = rbind(nrow(subset1), nrow(subset2), nrow(data))
#create matrix to export later
data_integrity = as.matrix(cbind(nrows, props))
rownames(data_integrity) = c("Subset 1", "Subset 2", "Original Data")
colnames(data_integrity) = c("n_obs", "Proportion of Defaults")
print(data_integrity)
#estimate logit baseline
form = formula(default ~ student + balance + income)
baseline = glm(form, data = data, family = binomial(link = "logit"))
tidy(baseline)
####flat priors####
#fit stan model
flat.fit = stan_glm(default ~ student + balance + income, data = data,
family = binomial(link = "logit"), y = T,
algorithm = "sampling",
warmup = 1000, iter = 10000, chains = 4, refresh = 10000)
#generate yrep for this prior
yrep.flat = posterior_predict(flat.fit, draws = 1000)
#extract posterior
posterior.flat = as.matrix(flat.fit)
#generate tidy df for use with ggplot
plotposterior.flat = as.data.frame(flat.fit) %>%
reshape2::melt(measure.vars = 1:4)
####strong / data-driven priors####
tidy(baseline)
#set data-driven priors: means and SD taken from baseline logit output
data_driven_prior = normal(location = c(0.5, -0.1, -0.011),
scale = c(0.236, 0.000232, 0.00000820), autoscale = F)
#estimate models with data-driven / strong prior
#fit strong priors (data-driven) on ***FULL data set***
strong.fit = stan_glm(default ~ student + balance + income, data = data,
family = binomial(link = "logit"), y = T,
algorithm = "sampling",
prior = data_driven_prior,
warmup = 1000, iter = 10000, chains = 4, refresh = 10000)
yrep.strong = posterior_predict(strong.fit, draws = 1000) #gen yrep
posterior.strong = as.matrix(strong.fit) #extract posterior
#generate tidy df for ggplot
plotposterior.strong = as.data.frame(strong.fit) %>%
reshape2::melt(measure.vars = 1:4)
#fit strong priors (data-driven) on ***SUBSET 1***
strong.fit.s1 = stan_glm(default ~ student + balance + income, data = subset1,
family = binomial(link = "logit"), y = T,
algorithm = "sampling",
prior = data_driven_prior,
warmup = 1000, iter = 10000, chains = 4, refresh = 0)
yrep.strong.s1 = posterior_predict(strong.fit.s1, draws = 1000) #gen yrep
posterior.strong.s1 = as.matrix(strong.fit.s1) #extract posterior
#fit strong priors (data-driven) on ***SUBSET 2***
strong.fit.s2 = stan_glm(default ~ student + balance + income, data = subset2,
family = binomial(link = "logit"), y = T,
algorithm = "sampling",
prior = data_driven_prior,
warmup = 1000, iter = 10000, chains = 4, refresh = 0)
yrep.strong.s2 = posterior_predict(strong.fit.s2, draws = 1000) #gen yrep
posterior.strong.s2 = as.matrix(strong.fit.s2) #extract posterior
####COMPARE RESULTS####
#monitor results
monitor(posterior.flat)
#look at flat priors
prior_summary(flat.fit)
#look at strong priors
prior_summary(strong.fit)
#monitor results
monitor(posterior.strong)
cat("", sep = "\n") # print empty line for readability
#monitor results subset 1
monitor(posterior.strong.s1)
cat("", sep = "\n") # print empty line for readability
#monitor results subset 2
monitor(posterior.strong.s2)
cat("", sep = "\n") # print empty line for readability
#Geweke Test
geweke.diag(posterior.flat)
geweke.diag(posterior.strong)
geweke.diag(posterior.strong.s1)
geweke.diag(posterior.strong.s2)
#Geweke plots
geweke.plot(as.mcmc(posterior.flat))
geweke.plot(as.mcmc(posterior.strong))
geweke.plot(as.mcmc(posterior.strong.s1))
geweke.plot(as.mcmc(posterior.strong.s2))
#compare 10-fold cv with diff sample sizes, this way of performing 10-fold cv
#adds an attribute to each model
#*****NOTE*****this will take quite a while to run
#I would recommend lowering k to 3-4 unless strong compute is available
# reason why I chose k = 10: my machine has 10 cores, which this function utilizes
# reason why not LOOCV: too computationally expensive
flat.fit$loo = kfold(flat.fit, k = nrow(data))
strong.fit$loo = kfold(strong.fit, k = nrow(data))
strong.fit.s1$loo = kfold(strong.fit.s1, k = 10)
strong.fit.s2$loo = kfold(strong.fit.s2, k = nrow(subset2))
#compare
loocv.comp = loo_compare(flat.fit, strong.fit)
strong.fit.s1$loo
strong.fit.s2$loo
####Graphical PPC####
# do plots
#define custom functions for plots below
prop_zero <- function(x) mean(x == 0)
prop_one <- function(x) mean(x == 1)
source('Plots.R')
####Housekeeping Pt2####
#export plots and Tables and copy code files
if (Sys.info()[7] == "ts") {
#this code only executes on my machine to prevent errors
source('Tables.R')
source('Plot.Export.R')
setwd('/Users/ts/Git/ise')
#copy code files to overleaf
file.copy('Main.R', '/Users/ts/Library/CloudStorage/Dropbox/Apps/Overleaf/ISE_Assignment/Code', overwrite = T)
file.copy('scrap_file.R', '/Users/ts/Library/CloudStorage/Dropbox/Apps/Overleaf/ISE_Assignment/Code', overwrite = T)
file.copy('Final_Assignment_Schnabel.stan', '/Users/ts/Library/CloudStorage/Dropbox/Apps/Overleaf/ISE_Assignment/Code', overwrite = T)
#copy bib of packages and dependencies
knitr::write_bib(c(.packages()),
"/Users/ts/Dropbox/Apps/Overleaf/ISE_Assignment/packages.bib")
}
#record end time
end.time = Sys.time()
print("Time Elapsed: ")
print(end.time-start.time)
####Show Plots####
#display plots (run each line to show plots, might take a few seconds)
phf
dof
dodf
propcomp
denscomp
discretedenscomp
rhatcomp
neffcomp
acfcomp
do_sample_comp
#Baseline Regression Diagnostic Plots
plot(baseline)