Skip to content

Commit

Permalink
fix docs, update repo status
Browse files Browse the repository at this point in the history
  • Loading branch information
helske committed Aug 27, 2024
1 parent 7bc926f commit 69eec3b
Show file tree
Hide file tree
Showing 36 changed files with 289 additions and 280 deletions.
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: walker
Type: Package
Title: Bayesian Generalized Linear Models with Time-Varying Coefficients
Version: 1.0.7
Version: 1.0.9
Description: Efficient Bayesian generalized linear models with time-varying coefficients
as in Helske (2022, <doi:10.1016/j.softx.2022.101016>). Gaussian, Poisson, and binomial
observations are supported. The Markov chain Monte Carlo (MCMC) computations are done using
Expand All @@ -23,11 +23,11 @@ Suggests:
rmarkdown (>= 0.8.1),
testthat
Depends:
bayesplot,
R (>= 3.4.0),
Rcpp (>= 0.12.9),
rstan (>= 2.26.0)
Imports:
bayesplot,
coda,
dplyr,
Hmisc,
Expand All @@ -49,8 +49,9 @@ LinkingTo:
SystemRequirements: GNU make
Biarch: true
VignetteBuilder: knitr
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
ByteCompile: true
URL: https://github.com/helske/walker
BugReports: https://github.com/helske/walker/issues
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
16 changes: 8 additions & 8 deletions R/fitted.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#' Extract Fitted Values of Walker Fit
#'
#' Returns fitted values (posterior means) from output of \code{walker} or \code{walker_glm}.
#' Returns fitted values (posterior means) from output of `walker` or `walker_glm`.
#'
#' @export
#' @importFrom stats fitted sd
#' @name fitted.walker_fit
#' @param object Output of \code{walker} or \code{walker_glm}.
#' @param summary If \code{TRUE} (default), return summary statistics. Otherwise returns samples.
#' @param object Output of `walker` or `walker_glm`.
#' @param summary If `TRUE` (default), return summary statistics. Otherwise returns samples.
#' @param ... Ignored.
#' @return If \code{summary=TRUE}, matrix containing summary statistics of fitted values.
#' @return If `summary=TRUE`, matrix containing summary statistics of fitted values.
#' Otherwise a matrix of samples.
fitted.walker_fit <- function(object, summary = TRUE, ...) {

Expand All @@ -31,13 +31,13 @@ fitted.walker_fit <- function(object, summary = TRUE, ...) {

#' Extract Coeffients of Walker Fit
#'
#' Returns the time-varying regression coefficients from output of \code{walker} or \code{walker_glm}.
#' Returns the time-varying regression coefficients from output of `walker` or `walker_glm`.
#'
#' @export
#' @importFrom stats coef
#' @name coef.walker_fit
#' @param object Output of \code{walker} or \code{walker_glm}.
#' @param summary If \code{TRUE} (default), return summary statistics. Otherwise returns samples.
#' @param object Output of `walker` or `walker_glm`.
#' @param summary If `TRUE` (default), return summary statistics. Otherwise returns samples.
#' @param transform Optional vectorized function for transforming the coefficients (for example exp).
#' @param ... Ignored.
#' @return Time series containing coefficient values.
Expand Down Expand Up @@ -67,4 +67,4 @@ coef.walker_fit <- function(object, summary = TRUE, transform = identity, ...)
} else {
coef_data
}
}
}
14 changes: 7 additions & 7 deletions R/lfo.R
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#' Leave-Future-Out Cross-Validation
#'
#' Estimates the leave-future-out (LFO) information criterion for \code{walker} and \code{walker_glm} models.
#' Estimates the leave-future-out (LFO) information criterion for `walker` and `walker_glm` models.
#'
#' The LFO for non-Gaussian models is (currently) based on the corresponding Gaussian approximation and
#' not the importance sampling corrected true posterior.
#'
#' @export
#' @importFrom loo psis pareto_k_values weights.importance_sampling
#' @param object Output of \code{walker} or \code{walker_glm}.
#' @param object Output of `walker` or `walker_glm`.
#' @param L Positive integer defining how many observations should be used for the initial fit.
#' @param exact If \code{TRUE}, computes exact 1-step predictions by re-estimating the model repeatedly.
#' If \code{FALSE} (default), uses approximate method based on Bürkner, Gabry and Vehtari (2020).
#' @param verbose If \code{TRUE} (default), print the progress of the LFO computations to the console.
#' @param exact If `TRUE`, computes exact 1-step predictions by re-estimating the model repeatedly.
#' If `FALSE` (default), uses approximate method based on Bürkner, Gabry and Vehtari (2020).
#' @param verbose If `TRUE` (default), print the progress of the LFO computations to the console.
#' @param k_thres Threshold for the pareto k estimate triggering refit. Default is 0.7.
#' @references Paul-Christian Bürkner, Jonah Gabry & Aki Vehtari (2020).
#' Approximate leave-future-out cross-validation for Bayesian time series models,
#' Journal of Statistical Computation and Simulation, 90:14, 2499-2523, DOI: 10.1080/00949655.2020.1783262.
#' @return List with components \code{ELPD} (Expected log predictive density), \code{ELPDs} (observation-specific ELPDs),
#' \code{ks} (Pareto k values in case of approximation was used), and \code{refits} (time points where model was re-estimated)
#' @return List with components `ELPD` (Expected log predictive density), `ELPDs` (observation-specific ELPDs),
#' `ks` (Pareto k values in case of approximation was used), and `refits` (time points where model was re-estimated)
#' @examples
#' \dontrun{
#' fit <- walker(Nile ~ -1 +
Expand Down
12 changes: 6 additions & 6 deletions R/plot_coefs.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#' Posterior predictive check for walker object
#'
#' Plots sample quantiles from posterior predictive sample.
#' See \code{\link{ppc_ribbon}} for details.
#' See [bayesplot::ppc_ribbon()] for details.
#'
#' @importFrom dplyr group_by summarise
#' @importFrom rlang .data
#' @importFrom stats quantile time update.formula drop.terms
#' @import ggplot2
#' @import bayesplot
#' @param object An output from \code{\link{walker}}.
#' @param object An output from [walker()].
#' @param level Level for intervals. Default is 0.05, leading to 90\% intervals.
#' @param alpha Transparency level for \code{geom_ribbon}.
#' @param transform Optional vectorized function for transforming the coefficients (for example \code{exp}).
#' @param scales Should y-axis of the panels be \code{"fixed"} (default) or \code{"free"}?
#' @param alpha Transparency level for [ggplot2::geom_ribbon()].
#' @param transform Optional vectorized function for transforming the coefficients (for example `exp`).
#' @param scales Should y-axis of the panels be `"fixed"` (default) or `"free"`?
#' @param add_zero Logical, should a dashed line indicating a zero be included?
#' @export
plot_coefs <- function(object, level = 0.05, alpha = 0.33, transform = identity, scales = "fixed", add_zero = TRUE){
Expand Down Expand Up @@ -58,4 +58,4 @@ plot_coefs <- function(object, level = 0.05, alpha = 0.33, transform = identity,
)
if (add_zero) p <- p + geom_hline(yintercept = 0, linetype = "dashed")
p
}
}
8 changes: 4 additions & 4 deletions R/plot_fit.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#' Plot the fitted values and sample quantiles for a walker object
#'
#' @param object An output from \code{\link{walker}} or \code{\link{walker_glm}}.
#' @param object An output from [walker()] or [walker_glm()].
#' @param level Level for intervals. Default is 0.05, leading to 90\% intervals.
#' @param alpha Transparency level for \code{geom_ribbon}.
#' @param ... Further arguments to \code{\link{ppc_ribbon}}.
#' @param alpha Transparency level for [ggplot2::geom_ribbon()].
#' @param ... Further arguments to [bayesplot::ppc_ribbon()].
#' @export
plot_fit <- function(object, level = 0.05, alpha = 0.33, ...){

Expand All @@ -18,4 +18,4 @@ plot_fit <- function(object, level = 0.05, alpha = 0.33, ...){
x = as.numeric(time(object$y))[noNA], ...) +
theme(legend.position = "none") +
scale_x_continuous(name = "time")
}
}
12 changes: 6 additions & 6 deletions R/plot_predict.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#' Prediction intervals for walker object
#'
#' Plots sample quantiles and posterior means of the predictions
#' of the \code{predict.walker_fit} output.
#' of the `predict.walker_fit` output.
#'
#' @importFrom ggplot2 ggplot facet_wrap geom_ribbon geom_line
#' @importFrom bayesplot color_scheme_get theme_default
#' @param object An output from \code{\link{predict.walker_fit}}.
#' @param draw_obs Either \code{"response"}, \code{"mean"}, or \code{"none"},
#' where \code{"mean"} is response variable divided by number of trials or exposures
#' @param object An output from [predict.walker_fit()].
#' @param draw_obs Either `"response"`, `"mean"`, or `"none"`,
#' where `"mean"` is response variable divided by number of trials or exposures
#' in case of binomial/poisson models.
#' @param level Level for intervals. Default is 0.05, leading to 90\% intervals.
#' @param alpha Transparency level for \code{\link{geom_ribbon}}.
#' @param alpha Transparency level for [ggplot2::geom_ribbon()].
#' @export
#' @examples
#' set.seed(1)
Expand Down Expand Up @@ -104,4 +104,4 @@ plot_predict <- function(object, draw_obs = NULL, level = 0.05, alpha = 0.33){
aes_(~x, ~y, alpha = 1), inherit.aes = FALSE)
}
p
}
}
10 changes: 5 additions & 5 deletions R/pp_check.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#' Plots sample quantiles from posterior predictive sample.
#'
#' @details
#' For other types of posterior predictive checks for example with \code{bayesplot},
#' you can extract the variable \code{yrep} from the output, see examples.#'
#' For other types of posterior predictive checks for example with `bayesplot`,
#' you can extract the variable `yrep` from the output, see examples.
#'
#' @importFrom bayesplot pp_check
#' @param object An output from \code{\link{walker}}.
#' @param ... Further parameters to \code{\link{ppc_ribbon}}.
#' @param object An output from [walker()].
#' @param ... Further parameters to [bayesplot::ppc_ribbon()].
#' @export
#' @examples
#' \dontrun{
Expand All @@ -34,4 +34,4 @@ pp_check.walker_fit <- function(object, ...){
yrep = y_rep,
x = as.numeric(time(object$y)),
...)
}
}
16 changes: 8 additions & 8 deletions R/predict.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#' Predictions for walker object
#'
#' Given the new covariate data and output from \code{walker},
#' Given the new covariate data and output from `walker`,
#' obtain samples from posterior predictive distribution for future time points.
#'
#' @importFrom stats deltat tsp
#' @param object An output from \code{\link{walker}} or \code{\link{walker_glm}}.
#' @param newdata A \code{data.frame} containing covariates used for prediction.
#' @param object An output from [walker()] or [walker_glm()].
#' @param newdata A `data.frame` containing covariates used for prediction.
#' @param u For Poisson model, a vector of future exposures i.e. E(y) = u*exp(x*beta).
#' For binomial, a vector containing the number of trials for future time points. Defaults 1.
#' @param type If \code{"response"} (default for Gaussian model), predictions are on the response level
#' @param type If `"response"` (default for Gaussian model), predictions are on the response level
#' (e.g., number of successes for Binomial case, and for Gaussian case the observational
#' level noise is added to the mean predictions).
#' If \code{"mean"} (default for non-Gaussian case), predict means (e.g., success probabilities in Binomial case).
#' If \code{"link"}, predictions for non-Gaussian models are returned before applying the inverse of the link-function.
#' If `"mean"` (default for non-Gaussian case), predict means (e.g., success probabilities in Binomial case).
#' If `"link"`, predictions for non-Gaussian models are returned before applying the inverse of the link-function.
#' @param ... Ignored.
#' @return A list containing samples from posterior predictive distribution.
#' @method predict walker_fit
#' @seealso \code{\link{plot_predict}} for example.
#' @seealso [plot_predict()] for example.
#' @export
predict.walker_fit <- function(object, newdata, u,
type = ifelse(object$distribution == "gaussian", "response", "mean"), ...){
Expand Down Expand Up @@ -106,4 +106,4 @@ predict.walker_fit <- function(object, newdata, u,
length = nrow(pred$y_new)), iter = 1:ncol(pred$y_new))

pred
}
}
16 changes: 8 additions & 8 deletions R/predict_counterfactual.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#' Predictions for walker object
#'
#' Given the new covariate data and output from \code{walker},
#' Given the new covariate data and output from `walker`,
#' obtain samples from posterior predictive distribution for counterfactual case,
#' i.e. for past time points with different covariate values.
#'
#' @importFrom stats deltat tsp rpois plogis rbinom
#' @param object An output from \code{\link{walker}} or \code{\link{walker_glm}}.
#' @param newdata A \code{data.frame} containing covariates used for prediction.
#' @param object An output from [walker()] or [walker_glm()].
#' @param newdata A `data.frame` containing covariates used for prediction.
#' Should have equal number of rows as the original data
#' @param u For Poisson model, a vector of exposures i.e. E(y) = u*exp(x*beta).
#' For binomial, a vector containing the number of trials. Defaults 1.
#' @param summary If \code{TRUE} (default), return summary statistics. Otherwise returns samples.
#' @param type If \code{"response"} (default for Gaussian model), predictions are on the response level
#' @param summary If `TRUE` (default), return summary statistics. Otherwise returns samples.
#' @param type If `"response"` (default for Gaussian model), predictions are on the response level
#' (e.g., number of successes for Binomial case, and for Gaussian case the observational
#' level noise is added to the mean predictions).
#' If \code{"mean"} (default for non-Gaussian case), predict means (e.g., success probabilities in Binomial case).
#' If \code{"link"}, predictions for non-Gaussian models are returned before applying the inverse of the link-function.
#' @return If \code{summary=TRUE}, time series containing summary statistics of predicted values.
#' If `"mean"` (default for non-Gaussian case), predict means (e.g., success probabilities in Binomial case).
#' If `"link"`, predictions for non-Gaussian models are returned before applying the inverse of the link-function.
#' @return If `summary=TRUE`, time series containing summary statistics of predicted values.
#' Otherwise a matrix of samples from predictive distribution.
#' @export
#' @examples
Expand Down
16 changes: 8 additions & 8 deletions R/print_fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#' Prints the summary information of time-invariant model parameters. In case of non-Gaussian models,
#' results based on approximate model are returned with a warning.
#'
#' @param x An output from \code{\link{walker}} or \code{\link{walker_glm}}.
#' @param ... Additional arguments to \code{\link{print.stanfit}}.
#' @param x An output from [walker()] or [walker_glm()].
#' @param ... Additional arguments to [rstan::print.stanfit()].
#' @method print walker_fit
#' @export
print.walker_fit <- function(x, ...) {
Expand All @@ -17,11 +17,11 @@ print.walker_fit <- function(x, ...) {
#'
#' Creates a data.frame object from the output of walker fit.
#'
#' @param x An output from \code{\link{walker}} or \code{\link{walker_glm}}.
#' @param row.names \code{NULL} (default) or a character vector giving the row names
#' @param x An output from [walker()] or [walker_glm()].
#' @param row.names `NULL` (default) or a character vector giving the row names
#' for the data frame.
#' @param optional Ignored (part of generic \code{as.data.frame} signature).
#' @param type Either \code{tiv} (time-invariant parameters) or \code{tv} (time-varying coefficients).
#' @param optional Ignored (part of generic `as.data.frame` signature).
#' @param type Either `tiv` (time-invariant parameters) or `tv` (time-varying coefficients).
#' @param ... Ignored.
#' @method as.data.frame walker_fit
#' @export
Expand Down Expand Up @@ -75,8 +75,8 @@ as.data.frame.walker_fit <- function(x, row.names = NULL, optional = FALSE, typ
#'
#' Return summary information of time-invariant model parameters.
#'
#' @param object An output from \code{\link{walker}} or \code{\link{walker_glm}}.
#' @param type Either \code{tiv} (time-invariant parameters, the default) or \code{tv} (time-varying coefficients).
#' @param object An output from [walker()] or [walker_glm()].
#' @param type Either `tiv` (time-invariant parameters, the default) or `tv` (time-varying coefficients).
#' @param ... Ignored.
#' @importFrom Hmisc wtd.mean wtd.var wtd.quantile
#' @importFrom coda spectrum0.ar
Expand Down
6 changes: 3 additions & 3 deletions R/rw.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Construct a first-order random walk component
#'
#' Auxiliary function used inside of the formula of \code{walker}.
#' Auxiliary function used inside of the formula of `walker`.
#'
#' @export
#' @param formula Formula for RW1 part of the model. Only right-hand-side is used.
Expand Down Expand Up @@ -42,7 +42,7 @@ rw1 <- function(formula, data, beta, sigma = c(2, 0.0001), gamma = NULL) {
}
#' Construct a second-order random walk component
#'
#' Auxiliary function used inside of the formula of \code{walker}.
#' Auxiliary function used inside of the formula of `walker`.
#'
#' @export
#' @param formula Formula for RW2 part of the model. Only right-hand-side is used.
Expand Down Expand Up @@ -86,4 +86,4 @@ rw2 <- function(formula, data, beta, sigma = c(2, 0.0001), nu, gamma = NULL) {
list(xreg = xreg, beta = beta,
sigma = sigma, nu = nu, gamma = gamma)

}
}
2 changes: 1 addition & 1 deletion R/test_args.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ check_gamma <- function(x, name) {
"defining the shape and rate for the Gamma prior the parameter ", name, ". "))
if(!all(x > 0))
stop(paste0("Both parameters of the Gamma prior for the parameter ", name, " should be positive. "))
}
}
Loading

0 comments on commit 69eec3b

Please sign in to comment.