-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrunningstancode_vlb1.Rmd
223 lines (154 loc) · 5.88 KB
/
runningstancode_vlb1.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
---
title: "Stan pt 3 -- R + Stan Code"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(rstan)
library(ggplot2)
library(tidyverse)
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
```
## Simulating Data
```{r, simulatedata, warning=FALSE, message=FALSE}
set.seed(17)
## hierarchical part
beta1 <- rnorm(20, 0, sd=0.5)
beta2 <- rnorm(20, 0, sd=0.5)
alpha <- numeric(3)
alpha[1] <- 0.1
alpha[2] <- -0.3
alpha[3] <- 0.01
## number of `sharks` = 20
sex <- rbinom(20, size=1, prob=0.5)
size <- rpois(20, lambda=220) ## in cm
y <- list()
for(n in 1:20){
y[[n]] <- data.frame(Shark = paste("Shark", n),
Size = size[n],
Sex = sex[n],
timecos = cos(2*pi*1:365/365),
timesin = sin(2*pi*1:365/365))%>%
mutate(Presence = rbinom(n = 365, size = 1,
prob = plogis(alpha[1] + alpha[2]*Sex + alpha[3]*(Size-220) + beta1[n]*timecos + beta2[n]*timesin)))
}
sharkfun <- dplyr::bind_rows(y)
```
## Running the Stan models
```{r}
data1 <- list(TT = dim(sharkfun)[1],
y= sharkfun$Presence)
fit1 <- stan(file = "logregmodel1.stan", data=data1)
fit1
```
```{r}
data2 <- list(TT = dim(sharkfun)[1],
y=sharkfun$Presence,
ncov = 2,
x = cbind(1, sharkfun$timecos, sharkfun$timesin))
fit2 <- stan(file = "logregmodel2.stan", data=data2)
```
```{r}
data3 <- list(TT = dim(sharkfun)[1],
y=sharkfun$Presence,
ncov = 2,
x = cbind(sharkfun$timecos, sharkfun$timesin),
bsize = sharkfun$Size - 220,
sex = sharkfun$Sex)
fit3 <- stan(file = "logregmodel3.stan", data=data3)
```
```{r}
data4 <- list(TT = dim(sharkfun)[1],
y=sharkfun$Presence,
sexmissing = as.numeric(sharkfun$Shark == "Shark 3"),
ncov = 2,
x = cbind(sharkfun$timecos, sharkfun$timesin),
bsize = sharkfun$Size - 220,
sex = sharkfun$Sex)
fit4 <- stan(file = "logregmodel4.stan", data=data4)
```
```{r}
data5 <- list(TT = dim(sharkfun)[1],
y=sharkfun$Presence,
ncov = 2,
x = cbind(sharkfun$timecos, sharkfun$timesin),
bsize = sharkfun$Size - 220,
sex = sharkfun$Sex,
nsharks = 20,
sharkid = rep(1:20, each=365))
fit5 <- stan(file="logregmodel5.stan", data=data5)
```
## projpred
```{r}
library(rstanarm)
library(projpred)
library(ggplot2)
library(bayesplot)
theme_set(theme_classic())
n <- 7300
D <- 5
p0 <- 2 # prior guess for the number of relevant variables
tau0 <- p0/(D-p0) * 1/sqrt(n)
# regularized horseshoe prior
prior_coeff <- hs(global_scale = tau0, slab_scale = 1)
#----------------------------------------------------------------------
## FITTING THE MODELS
logregcov.horseshoe <- stan_glm(formula = Presence ~ Sex + BSize + timecos + timesin,
family = binomial(link="logit"),
data = sharkfun%>%dplyr::select(-Shark)%>%mutate(BSize = Size - 220),
prior = hs(global_scale = 0.007802743,
slab_scale = 1))
#launch_shinystan(logregcov.horseshoe)
logregcov.defaultpriors <- stan_glm(formula = Presence ~ Sex + BSize + timecos + timesin,
family = binomial(link="logit"),
data = sharkfun%>%dplyr::select(-Shark)%>%mutate(BSize = Size - 220))
logregcov.n01 <- stan_glm(formula = Presence ~ Sex + BSize + timecos + timesin,
family = binomial(link="logit"),
data = sharkfun%>%dplyr::select(-Shark)%>%mutate(BSize = Size - 220),
prior = normal(0,1))
#----------------------------------------------------------------------
## K-FOLD CROSS-VALIDATION
cvs.horseshoe <- cv_varsel(logregcov.horseshoe,
method='forward',
cv_method='kfold', K=5)
cvplot.horseshoe <- varsel_plot(cvs.horseshoe,
stats=c('elpd', 'acc')) +
theme_minimal() + theme(text=element_text(size=15),
legend.position = "none") +
ggtitle("Classification Accuracy (5-fold CV)")
cvplot.horseshoe
suggest_size(cvs.horseshoe)
vs <- varsel(logregcov.horseshoe, method='forward')
vs$vind
varsel_plot(vs, stats=c('elpd', 'acc'), deltas=F)
#----------
cvs.horseshoe$vind
```
## Trying to have the covariates on approximately the same scale...let's see how that works out
```{r}
logregscaledcov.horseshoe <- stan_glm(formula = Presence ~ Sex + BSize + timecos + timesin,
family = binomial(link="logit"),
data = sharkfun%>%dplyr::select(-Shark)%>%mutate(BSize = (Size - 220)/30),
prior = hs(global_scale = 0.007802743,
slab_scale = 1))
#----------------------------------------------------------------------
## K-FOLD CROSS-VALIDATION
cvs.horseshoe2 <- cv_varsel(logregscaledcov.horseshoe,
method='forward',
cv_method='kfold', K=5)
cvplot.horseshoe2 <- varsel_plot(cvs.horseshoe2,
stats=c('elpd', 'acc')) +
theme_minimal() + theme(text=element_text(size=15),
legend.position = "none") +
ggtitle("Classification Accuracy (5-fold CV)")
cvplot.horseshoe2
suggest_size(cvs.horseshoe2)
vs2 <- varsel(logregscaledcov.horseshoe, method='forward')
vs2$vind
varsel_plot(vs2, stats=c('elpd', 'acc'), deltas=F)
#----------
cvs.horseshoe2$vind
```