[ede2d4]: / R / postestimation.R

Download this file

447 lines (423 with data), 16.5 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
##=============================================================================
##
## Copyright (c) 2019 Marco Colombo
##
## Parts of the code are based on https://avehtari.github.io/bayes_R2/bayes_R2.html
## Portions copyright (c) 2019 Aki Vehtari, Andrew Gelman, Ben Goodrich, Jonah Gabry
##
## Parts of the code are based on https://github.com/stan-dev/rstanarm
## Portions copyright (c) 2015, 2016, 2017 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, see <http://www.gnu.org/licenses/>.
##
##=============================================================================
#' Pointwise log-likelihood
#'
#' Compute the pointwise log-likelihood.
#'
#' @param object An object of class `hsstan`.
#' @param newdata Optional data frame to use to evaluate the log-likelihood.
#' If `NULL` (default), the model matrix is used.
#' @param ... Currently ignored.
#'
#' @return
#' A matrix of size `S` by `N`, where `S` is number of draws from the posterior
#' distribution, and `N` is the number of data points.
#'
#' @examples
#' \dontshow{utils::example("hsstan", echo=FALSE)}
#' # continued from ?hsstan
#' log_lik(hs.biom)
#'
#' @importFrom rstantools log_lik
#' @importFrom stats rbinom rnorm
#' @method log_lik hsstan
#' @aliases log_lik
#' @export log_lik
#' @export
log_lik.hsstan <- function(object, newdata=NULL, ...) {
if (is.null(newdata))
newdata <- object$data
y <- validate.outcome(newdata[[object$model.terms$outcome]])
mu <- posterior_linpred(object, newdata, transform=TRUE)
if (!is.logistic(object))
sigma <- as.matrix(object$stanfit, pars="sigma")
llkfun <- ifelse(is.logistic(object),
function(x) stats::dbinom(y[x], 1, mu[, x], log=TRUE),
function(x) stats::dnorm(y[x], mu[, x], sigma, log=TRUE))
llk <- cbind(sapply(1:ncol(mu), llkfun))
return(llk)
}
#' Posterior uncertainty intervals
#'
#' Compute posterior uncertainty intervals for `hsstan` objects.
#'
#' @param object An object of class `hsstan`.
#' @param pars Names of parameters for which posterior intervals should be
#' returned, which can be specified as regular expressions. If `NULL`
#' (default) then this refers to the set of predictors used in the model.
#' @param prob A value between 0 and 1 indicating the desired probability
#' to be covered by the uncertainty intervals (0.95, by default).
#' @param ... Currently ignored.
#'
#' @return
#' A matrix with lower and upper interval bounds as columns and as many rows
#' as selected parameters.
#'
#' @examples
#' \dontshow{utils::example("hsstan", echo=FALSE)}
#' # continued from ?hsstan
#' posterior_interval(hs.biom)
#'
#' @importFrom rstantools posterior_interval
#' @method posterior_interval hsstan
#' @aliases posterior_interval
#' @export posterior_interval
#' @export
posterior_interval.hsstan <- function(object, pars=NULL, prob=0.95, ...) {
validate.samples(object)
validate.probability(prob)
pars <- get.pars(object, pars)
post.matrix <- as.matrix(object$stanfit, pars=pars)
rstantools::posterior_interval(post.matrix, prob=prob)
}
#' Posterior distribution of the linear predictor
#'
#' Extract the posterior draws of the linear predictor, possibly transformed
#' by the inverse-link function.
#'
#' @param object An object of class `hsstan`.
#' @param transform Whether the linear predictor should be transformed using
#' the inverse-link function (`FALSE` by default).
#' @param newdata Optional data frame containing the variables to use to
#' predict. If `NULL` (default), the model matrix is used. If
#' specified, its continuous variables should be standardized, since
#' the model coefficients are learnt on standardized data.
#' @param ... Currently ignored.
#'
#' @return
#' A matrix of size `S` by `N`, where `S` is the number of draws from the
#' posterior distribution of the (transformed) linear predictor, and `N` is
#' the number of data points.
#'
#' @examples
#' \dontshow{utils::example("hsstan", echo=FALSE)}
#' # continued from ?hsstan
#' posterior_linpred(hs.biom)
#'
#' @importFrom rstantools posterior_linpred
#' @method posterior_linpred hsstan
#' @aliases posterior_linpred
#' @export posterior_linpred
#' @export
posterior_linpred.hsstan <- function(object, transform=FALSE,
newdata=NULL, ...) {
validate.samples(object)
newdata <- validate.newdata(object, newdata)
pars <- grep("^beta_", object$stanfit@model_pars, value=TRUE)
post.matrix <- as.matrix(object$stanfit, pars=pars)
linear.predictor <- multiplyABt(post.matrix, newdata)
if (!transform)
return(linear.predictor)
return(object$family$linkinv(linear.predictor))
}
#' Posterior predictive distribution
#'
#' Draw from the posterior predictive distribution of the outcome.
#'
#' @param object An object of class `hsstan`.
#' @param newdata Optional data frame containing the variables to use to
#' predict. If `NULL` (default), the model matrix is used. If
#' specified, its continuous variables should be standardized, since
#' the model coefficients are learnt on standardized data.
#' @param nsamples A positive integer indicating the number of posterior samples
#' to use. If `NULL` (default) all samples are used.
#' @param seed Optional integer defining the seed for the pseudo-random number
#' generator.
#' @param ... Currently ignored.
#'
#' @return
#' A matrix of size `S` by `N`, where `S` is the number of simulations from
#' the posterior predictive distribution, and `N` is the number of data points.
#'
#' @examples
#' \dontshow{utils::example("hsstan", echo=FALSE)}
#' # continued from ?hsstan
#' posterior_predict(hs.biom)
#'
#' @importFrom rstantools posterior_predict
#' @importFrom stats rbinom rnorm
#' @method posterior_predict hsstan
#' @aliases posterior_predict
#' @export posterior_predict
#' @export
posterior_predict.hsstan <- function(object, newdata=NULL, nsamples=NULL,
seed=NULL, ...) {
validate.samples(object)
if (!is.null(nsamples))
validate.positive.scalar(nsamples, "nsamples", int=TRUE)
## extract a random subset of the posterior samples
if (!is.null(seed))
set.seed(seed)
num.samples <- nsamples(object)
samp <- sample(num.samples, min(num.samples, nsamples))
nsamples <- length(samp)
## generate the posterior predictions
mu <- posterior_linpred(object, newdata, transform=TRUE)[samp, , drop=FALSE]
nobs <- ncol(mu)
if (is.logistic(object))
pp <- t(sapply(1:nsamples, function(z) rbinom(nobs, 1, mu[z, ])))
else {
sigma <- as.matrix(object$stanfit, pars="sigma")[samp, , drop=FALSE]
pp <- t(sapply(1:nsamples, function(z) rnorm(nobs, mu[z, ], sigma[z])))
}
return(pp)
}
#' Posterior measures of performance
#'
#' Compute the log-likelihood and a relevant measure of performance (R-squared
#' or AUC) from the posterior samples.
#'
#' @param obj An object of class `hsstan` or `kfold`.
#' @param prob Width of the posterior interval (0.95, by default). It is
#' ignored if `summary=FALSE`.
#' @param sub.idx Vector of indices of observations in the dataset to be used
#' in computing the performance measures. If `NULL` (default), all
#' observations in the dataset are used.
#' @param summary Whether a summary of the distribution of the performance
#' measure should be returned rather than the pointwise values
#' (`TRUE` by default).
#' @param cores Number of cores to use for parallelization (the value of
#' `options("mc.cores")` by default).
#'
#' @return
#' The mean, standard deviation and posterior interval of the performance
#' measure (R-squared or AUC) if `summary=TRUE`, or a vector of values
#' of the performance measure with length equal to the size of the posterior
#' sample if `summary=FALSE`. Attribute `type` reports whether the performance
#' measures are cross-validated or not. If `sub.idx` is not `NULL`, attribute
#' `subset` reports the index of observations used in the computations.
#'
#' @examples
#' \donttest{
#' \dontshow{utils::example("hsstan", echo=FALSE)}
#' # continued from ?hsstan
#' posterior_performance(hs.biom, cores=1)
#' }
#'
#' @export
posterior_performance <- function(obj, prob=0.95, sub.idx=NULL, summary=TRUE,
cores=getOption("mc.cores", 1)) {
if (inherits(obj, "hsstan")) {
obj <- list(fits=array(list(fit=obj, test.idx=1:nrow(obj$data)), c(1, 2)),
data=obj$data)
colnames(obj$fits) <- c("fit", "test.idx")
} else if (inherits(obj, c("kfold", "loo"))) {
if (is.null(obj[["fits"]]))
stop("No fitted models found, run 'kfold' with store.fits=TRUE.")
} else
stop("Not an 'hsstan' or 'kfold' object.")
if (is.null(sub.idx)) {
sub.idx <- 1:nrow(obj$data)
used.subset <- FALSE
} else {
validate.indices(sub.idx, nrow(obj$data), "sub.idx")
sub.idx <- sort(sub.idx)
used.subset <- length(sub.idx) < nrow(obj$data)
}
validate.samples(obj$fits[[1]])
validate.probability(prob)
logistic <- is.logistic(obj$fits[[1]])
num.folds <- nrow(obj$fits)
## loop over the folds
y <- mu <- llk <- NULL
for (fold in 1:num.folds) {
hs <- obj$fits[[fold]]
test.idx <- intersect(obj$fits[, "test.idx"][[fold]], sub.idx)
if (length(test.idx) == 0)
next
newdata <- obj$data[test.idx, ]
y <- c(y, obj$data[test.idx, hs$model.terms$outcome])
mu <- cbind(mu, posterior_linpred(hs, newdata=newdata, transform=TRUE))
llk <- cbind(llk, log_lik(hs, newdata=newdata))
}
if (used.subset && logistic && length(unique(y)) != 2)
stop("'sub.idx' must contain both outcome classes.")
if (logistic) {
par.auc <- function(i)
as.numeric(pROC::roc(y, mu[i, ], direction="<", quiet=TRUE)$auc)
if (.Platform$OS.type != "windows") {
out <- parallel::mclapply(X=1:nrow(mu), mc.cores=cores,
mc.preschedule=TRUE, FUN=par.auc)
} else { # windows
cl <- parallel::makePSOCKcluster(cores)
on.exit(parallel::stopCluster(cl))
out <- parallel::parLapply(X=1:nrow(mu), cl=cl, fun=par.auc)
}
} else {
out <- pmax(fastCor(y, mu), 0)^2
}
out <- cbind(perf=unlist(out), llk=rowSums(llk))
colnames(out)[1] <- ifelse(logistic, "auc", "r2")
if (summary)
out <- posterior_summary(out, prob)
attr(out, "type") <- paste0(if(num.folds == 1) "non ", "cross-validated")
if (used.subset)
attr(out, "subset") <- sub.idx
return(out)
}
#' Predictive information criteria for Bayesian models
#'
#' Compute an efficient approximate leave-one-out cross-validation
#' using Pareto smoothed importance sampling (PSIS-LOO), or the widely
#' applicable information criterion (WAIC), also known as the Watanabe-Akaike
#' information criterion.
#'
#' @param x An object of class `hsstan`.
#' @param cores Number of cores used for parallelisation (the value of
#' `options("mc.cores")` by default).
#' @param ... Currently ignored.
#'
#' @return
#' A `loo` object.
#'
#' @examples
#' \dontshow{utils::example("hsstan", echo=FALSE)}
#' \dontshow{oldopts <- options(mc.cores=2)}
#' # continued from ?hsstan
#' loo(hs.biom)
#' waic(hs.biom)
#' \dontshow{options(oldopts)}
#'
#' @importFrom loo loo
#' @method loo hsstan
#' @aliases loo
#' @export loo
#' @export
loo.hsstan <- function(x, cores=getOption("mc.cores"), ...) {
validate.samples(x)
llk <- log_lik(x)
chain.id <- rep(1:ncol(x$stanfit), each=nrow(x$stanfit))
r.eff <- loo::relative_eff(exp(llk), chain_id=chain.id, cores=cores)
loo <- suppressWarnings(loo::loo(llk, r_eff=r.eff, cores=cores))
return(loo)
}
#' @rdname loo.hsstan
#' @importFrom loo waic
#' @method waic hsstan
#' @aliases waic
#' @export waic
#' @export
waic.hsstan <- function(x, cores=getOption("mc.cores"), ...) {
validate.samples(x)
llk <- log_lik(x)
waic <- suppressWarnings(loo::waic(llk, cores=cores))
return(waic)
}
#' Bayesian and LOO-adjusted R-squared
#'
#' Compute the Bayesian and the LOO-adjusted R-squared from the posterior
#' samples. For Bayesian R-squared it uses the modelled residual variance
#' (rather than the variance of the posterior distribution of the residuals).
#' The LOO-adjusted R-squared uses Pareto smoothed importance sampling LOO
#' residuals and Bayesian bootstrap.
#'
#' @param object An object of class `hsstan`.
#' @param prob Width of the posterior interval (0.95, by default). It is
#' ignored if `summary=FALSE`.
#' @param summary Whether a summary of the distribution of the R-squared
#' should be returned rather than the pointwise values (`TRUE` by
#' default).
#' @param ... Currently ignored.
#'
#' @return
#' The mean, standard deviation and posterior interval of R-squared if
#' `summary=TRUE`, or a vector of R-squared values with length equal to
#' the size of the posterior sample if `summary=FALSE`.
#'
#' @references
#' Andrew Gelman, Ben Goodrich, Jonah Gabry and Aki Vehtari (2019),
#' R-squared for Bayesian regression models,
#' _The American Statistician_, 73 (3), 307-309.
#' \doi{10.1080/00031305.2018.1549100}
#'
#' Aki Vehtari, Andrew Gelman, Ben Goodrich and Jonah Gabry (2019),
#' Bayesian R2 and LOO-R2.
#' \url{https://avehtari.github.io/bayes_R2/bayes_R2.html}
#'
#' @examples
#' \dontshow{utils::example("hsstan", echo=FALSE)}
#' \dontshow{oldopts <- options(mc.cores=2)}
#' # continued from ?hsstan
#' bayes_R2(hs.biom)
#' loo_R2(hs.biom)
#' \dontshow{options(oldopts)}
#'
#' @importFrom rstantools bayes_R2
#' @method bayes_R2 hsstan
#' @aliases bayes_R2
#' @export bayes_R2
#' @export
bayes_R2.hsstan <- function(object, prob=0.95, summary=TRUE, ...) {
validate.samples(object)
validate.probability(prob)
mu <- posterior_linpred(object, transform=TRUE)
var.mu <- apply(mu, 1, stats::var)
if (is.logistic(object))
sigma2 <- rowMeans(mu * (1 - mu))
else
sigma2 <- drop(as.matrix(object$stanfit, pars="sigma"))^2
R2 <- var.mu / (var.mu + sigma2)
if (summary)
R2 <- vector.summary(R2, prob)
return(R2)
}
#' @rdname bayes_R2.hsstan
#' @importFrom rstantools loo_R2
#' @method loo_R2 hsstan
#' @aliases loo_R2
#' @export loo_R2
#' @export
loo_R2.hsstan <- function(object, prob=0.95, summary=TRUE, ...) {
validate.samples(object)
validate.probability(prob)
y <- object$data[[object$model.terms$outcome]]
mu <- posterior_linpred(object, transform=TRUE)
ll <- log_lik(object)
S <- nrow(mu)
N <- ncol(mu)
chains <- object$stanfit@sim$chains
r.eff <- loo::relative_eff(exp(ll), chain_id=rep(1:chains, each=S / chains))
psis <- suppressWarnings(loo::psis(-ll, r_eff=r.eff))
mu.loo <- loo::E_loo(mu, psis, log_ratios=-ll)$value
err.loo <- mu.loo - y
## set the random seed as the seed used in the first chain and ensure
## the old RNG state is restored on exit
rng.state.old <- .Random.seed
on.exit(assign(".Random.seed", rng.state.old, envir=.GlobalEnv))
set.seed(object$stanfit@stan_args[[1]]$seed)
## dirichlet weights for bayesian bootstrap
exp.draws <- matrix(stats::rexp(S * N, rate=1), nrow=S, ncol=N)
dw <- exp.draws / rowSums(exp.draws)
var.y <- (rowSums(sweep(dw, 2, y^2, FUN = "*")) -
rowSums(sweep(dw, 2, y, FUN = "*"))^2) * (N / (N - 1))
var.err.loo <- (rowSums(sweep(dw, 2, err.loo^2, FUN = "*")) -
rowSums(sweep(dw, 2, err.loo, FUN = "*")^2)) * (N / (N - 1))
R2 <- 1 - var.err.loo / var.y
R2[R2 < -1] <- -1
R2[R2 > 1] <- 1
if (summary)
R2 <- vector.summary(R2, prob)
return(R2)
}