-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
stan_betareg.fit.R
216 lines (195 loc) · 8.87 KB
/
stan_betareg.fit.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
# Part of the rstanarm package for estimating model parameters
# Copyright (C) 2013, 2014, 2015, 2016 Trustees of Columbia University
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#' @rdname stan_betareg
stan_betareg.fit <- function (x, y, z, weights = rep(1, NROW(x)), offset = rep(0, NROW(x)),
link = c("logit", "probit", "cloglog", "cauchit", "log", "loglog"),
link.phi = c("log", "identity", "sqrt"), ...,
prior = normal(), prior_intercept = normal(),
prior_z = normal(), prior_intercept_z = normal(),
prior_ops = prior_options(), prior_PD = FALSE,
algorithm = c("sampling", "optimizing", "meanfield", "fullrank"),
adapt_delta = NULL, QR = FALSE, sparse = FALSE, Z_true) {
# lots of tedious but simple stuff including standata which is a big list to pass to data {}
# process the prior information like stan_glm.fit() does
algorithm <- match.arg(algorithm)
# no family argument
famname <- "beta"
is_beta <- is.beta(famname)
# link for X variables
link <- match.arg(link)
supported_links <- c("logit", "probit", "cloglog", "cauchit", "log", "loglog")
link_num <- which(supported_links == link)
if (!length(link))
stop("'link' must be one of ", paste(supported_links, collapse = ", "))
# link for Z variables
link.phi <- match.arg(link.phi)
supported_phi_links <- c("log", "identity", "sqrt")
link_num_phi <- which(supported_phi_links == link.phi)
if (!length(link_num_phi))
stop("'link' must be one of ", paste(supported_phi_links, collapse = ", "))
if (Z_true == 0) {
link_num_phi <- 0
}
# useless assignments to pass R CMD check
has_intercept <- min_prior_scale <- prior_df <- prior_df_for_intercept <-
prior_dist <- prior_dist_for_intercept <- prior_mean <- prior_mean_for_intercept <-
prior_scale_for_dispersion <- scaled <- NULL
x_stuff <- center_x(x, sparse)
for (i in names(x_stuff)) # xtemp, xbar, has_intercept
assign(i, x_stuff[[i]])
nvars <- ncol(xtemp)
z_stuff <- center_x(z, sparse)
ztemp <- z_stuff$xtemp
zbar <- z_stuff$xbar
has_intercept_z <- z_stuff$has_intercept
nvars_z <- ncol(ztemp)
if (Z_true == 0) {
has_intercept_z <- FALSE
}
# if (nvars_z == 0 && has_intercept_z == 1) {
# Z_true <- 0
# }
for (i in names(prior_ops)) # scaled, min_prior_dispersion, prior_scale_for_dispersion
assign(i, prior_ops[[i]])
ok_dists <- nlist("normal", student_t = "t", "cauchy", "hs", "hs_plus")
ok_intercept_dists <- ok_dists[1:3]
# prior distributions (handle_glm_prior() from data_block.R)
prior_stuff <- handle_glm_prior(prior, nvars, link, default_scale = 2.5)
for (i in names(prior_stuff)) # prior_{dist, mean, scale, df}
assign(i, prior_stuff[[i]])
prior_intercept_stuff <- handle_glm_prior(prior_intercept, nvars = 1, default_scale = 10,
link, ok_dists =
nlist("normal", student_t = "t", "cauchy"))
names(prior_intercept_stuff) <- paste0(names(prior_intercept_stuff), "_for_intercept")
for (i in names(prior_intercept_stuff)) # prior_{dist, mean, scale, df}_for_intercept
assign(i, prior_intercept_stuff[[i]])
# prior distributions for parameters on z variables
prior_stuff_z <- handle_glm_prior(prior_z, nvars_z, link = link.phi, default_scale = 2.5)
for (i in names(prior_stuff_z))
assign(paste0(i,"_z"), prior_stuff_z[[i]])
prior_intercept_stuff_z <- handle_glm_prior(prior_intercept_z, nvars = 1, link = link.phi,
default_scale = 10,
ok_dists = nlist("normal", student_t = "t", "cauchy"))
names(prior_intercept_stuff_z) <- paste0(names(prior_intercept_stuff_z),"_for_intercept_z")
for (i in names(prior_intercept_stuff_z))
assign(paste0(i), prior_intercept_stuff_z[[i]])
if (nvars_z == 0) {
prior_mean_z <- double()
prior_scale_z <- double()
prior_df_z <- integer()
}
# create entries in the data block of the .stan file
standata <- nlist(
N = nrow(xtemp), K = ncol(xtemp), xbar = as.array(xbar), dense_X = !sparse, # TRUE, sparse = FALSE,
X = array(xtemp, dim = c(1L, dim(xtemp))),
nnz_X = 0L,
w_X = double(),
v_X = integer(),
u_X = integer(),
y = y,
prior_PD, has_intercept, family = 4L, link = link_num,
prior_dist, prior_mean, prior_scale = as.array(pmin(.Machine$double.xmax, prior_scale)), prior_df,
prior_dist_for_intercept, prior_mean_for_intercept = c(prior_mean_for_intercept),
prior_scale_for_intercept = min(.Machine$double.xmax, prior_scale_for_intercept),
prior_df_for_intercept = c(prior_df_for_intercept),
prior_scale_for_dispersion = prior_scale_for_dispersion %ORifINF% 0,
has_weights = length(weights) > 0, weights = double(),
has_offset = length(offset) > 0, offset = double(),
t = 0L,
p = integer(),
l = integer(),
q = 0L,
len_theta_L = 0L, shape = double(), scale = double(),
len_concentration = 0L, concentration = double(),
len_regularization = 0L, regularization = double(),
num_non_zero = 0L,
w = double(),
v = integer(),
u = integer(),
z_dim = nvars_z, # ncol(z),
link_phi = link_num_phi,
betareg_z = array(ztemp, dim = c(dim(ztemp))),
has_intercept_z,
zbar = array(zbar),
prior_dist_z, prior_mean_z, prior_scale_z = as.array(pmin(.Machine$double.xmax, prior_scale_z)), prior_df_z,
prior_dist_for_intercept_z, prior_mean_for_intercept_z = c(prior_mean_for_intercept_z),
prior_scale_for_intercept_z = min(.Machine$double.xmax, prior_scale_for_intercept_z),
prior_df_for_intercept_z = c(prior_df_for_intercept_z)
)
# call stan() to draw from posterior distribution
stanfit <- stanmodels$continuous
if (Z_true == 1) {
pars <- c(if (has_intercept) "alpha", "beta", "omega_int", "omega", "mean_PPD")
}
else {
pars <- c(if (has_intercept) "alpha", "beta", "dispersion", "mean_PPD")
}
if (algorithm == "optimizing") {
out <- optimizing(stanfit, data = standata, draws = 1000, constrained = TRUE, ...)
out$par <- out$par[!grepl("eta_z", names(out$par))] # might need fixing
out$theta_tilde <- out$theta_tilde[,!grepl("eta_z", colnames(out$theta_tilde))] # might need fixing
new_names <- names(out$par)
mark <- grepl("^beta\\[[[:digit:]]+\\]$", new_names)
new_names[mark] <- colnames(xtemp)
new_names[new_names == "alpha[1]"] <- "(Intercept)"
if (Z_true == 1) {
new_names[new_names == "omega_int[1]"] <- "(phi)_(Intercept)"
mark_z <- grepl("^omega\\[[[:digit:]]+\\]$", new_names) # "^omega\\[[[:digit:]]+\\]$"
new_names[mark_z] <- paste0("(phi)_", colnames(ztemp))
}
else {
new_names[new_names == "dispersion"] <- "(phi)"
}
names(out$par) <- new_names
colnames(out$theta_tilde) <- new_names
out$stanfit <- suppressMessages(sampling(stanfit, data = standata, chains = 0))
return(out)
}
else {
if (algorithm == "sampling") {
sampling_args <- set_sampling_args(
object = stanfit,
prior = prior,
user_dots = list(...),
user_adapt_delta = adapt_delta,
data = standata,
pars = pars,
show_messages = FALSE)
stanfit <- do.call(sampling, sampling_args)
}
else if (algorithm == "meanfield") { # FIXME
stanfit <- rstan::vb(stanfit, pars = pars, data = standata,
algorithm = algorithm, init = 0.001, ...)
}
else if (algorithm == "fullrank") { # FIXME
stanfit <- rstan::vb(stanfit, pars = pars, data = standata,
algorithm = algorithm, init = 0.001, ...)
}
if (Z_true == 1) {
new_names <- c(if (has_intercept) "(Intercept)", colnames(xtemp),
if (has_intercept_z) "(phi)_(Intercept)", paste0("(phi)_", colnames(ztemp)),
"mean_PPD", "log-posterior")
}
else {
new_names <- c(if (has_intercept) "(Intercept)",
colnames(xtemp),
"(phi)", "mean_PPD", "log-posterior")
}
stanfit@sim$fnames_oi <- new_names
return(stanfit)
}
}