Skip to content

Commit

Permalink
version 0.1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
seananderson authored and cran-robot committed Mar 10, 2023
1 parent 23795e2 commit a28d827
Show file tree
Hide file tree
Showing 37 changed files with 2,884 additions and 462 deletions.
15 changes: 9 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Type: Package
Package: glmmfields
Title: Generalized Linear Mixed Models with Robust Random Fields for
Spatiotemporal Modeling
Version: 0.1.4
Version: 0.1.7
Authors@R:
c(person(given = c("Sean", "C."),
family = "Anderson",
Expand All @@ -26,20 +26,23 @@ BugReports: https://github.com/seananderson/glmmfields/issues
Depends: methods, R (>= 3.4.0), Rcpp (>= 0.12.18)
Imports: assertthat, broom, broom.mixed, cluster, dplyr (>= 0.8.0),
forcats, ggplot2 (>= 2.2.0), loo (>= 2.0.0), mvtnorm, nlme,
reshape2, rstan (>= 2.18.2), rstantools (>= 1.5.1), tibble
RcppParallel (>= 5.0.1), reshape2, rstan (>= 2.18.2),
rstantools (>= 2.1.1), tibble
Suggests: bayesplot, coda, knitr, parallel, rmarkdown, testthat,
viridis
LinkingTo: BH (>= 1.66.0), Rcpp (>= 0.12.8), RcppEigen (>= 0.3.3.3.0),
rstan (>= 2.18.2), StanHeaders (>= 2.18.0)
RcppParallel (>= 5.0.1), rstan (>= 2.18.2), StanHeaders (>=
2.18.0)
VignetteBuilder: knitr
Encoding: UTF-8
RoxygenNote: 7.1.1
RoxygenNote: 7.2.3
SystemRequirements: GNU make
NeedsCompilation: yes
Packaged: 2020-07-09 04:02:12 UTC; seananderson
Biarch: true
Packaged: 2023-03-10 22:07:14 UTC; seananderson
Author: Sean C. Anderson [aut, cre],
Eric J. Ward [aut],
Trustees of Columbia University [cph]
Maintainer: Sean C. Anderson <sean@seananderson.ca>
Repository: CRAN
Date/Publication: 2020-07-09 05:50:03 UTC
Date/Publication: 2023-03-10 22:50:02 UTC
62 changes: 31 additions & 31 deletions MD5
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
414ac92c4d39dfe29198ad2334503781 *DESCRIPTION
616360d02ede2594e93f3106dafe8ade *NAMESPACE
22e349f0bd517dda5e0ef6b4b97778a4 *NEWS.md
7efcb6bf4bf941a3d1aedf46febbce78 *DESCRIPTION
8795f9d3a3f1b84306311a834c03cb77 *NAMESPACE
0c7eaed6acf539c3e5b9df7c1cbdb71f *NEWS.md
591501246e19faa6fa46d5db1192581e *R/family.R
2d8fb55fd1f2692f66040d5f1ffe54bd *R/fitting.R
67f28947877adabcbe4ff5b5aac74471 *R/fitting.R
8f87959bc9c0025114d13f40dfe9775f *R/format_data.R
14808a5ce06013d76df7b23800b55f14 *R/glmmfields-package.R
2dc03779de722dbb3310a958e45954fe *R/methods.R
739b7006f78e5bfde7bb3ab137452a28 *R/plot.R
18958a60f2459df0cb85078849964848 *R/predict.R
7619bd7a570d76264705782d93b5df95 *R/predict.R
6d8331b4cc7b91617cc84979adde823f *R/priors.R
1aaa246c53120c557468a57d12f99a9d *R/sim.R
db68c0796882372fe8a4a15f76a00567 *R/stan_pars.R
1c8249eb55b244cf8fa0b573b65d1570 *R/stanmodels.R
aefbc5c00caad6ec520914cfe1c22bf3 *R/zzz.R
2f8bb1d2710cde6ce807fc55550b2116 *build/vignette.rds
88f64e7df66edcf1e4bd7b0abc3412ab *inst/CITATION
afe22da3c34faa9b829c4b8343ac88c5 *inst/doc/spatial-glms.R
f01f9b33ec98a387b544845a28fce364 *inst/doc/spatial-glms.Rmd
2a9a8b64416e0383297de594d22c88fc *inst/doc/spatial-glms.html
855c064dbf8deaff96c8dc8a87169109 *inst/include/meta_header.hpp
8e67b680b71478dbbce457b38476233d *R/stan_pars.R
61a18858d3a3157cf40c7748b37bd828 *R/stanmodels.R
27ddd17174166eae01f777cbe0add0b4 *build/vignette.rds
1cc60bdd8610449d30184e0bb374bf46 *configure
6bea7bdbd44ffa386677bfdec2d1984b *configure.win
c896d2e11c9aa7c025c3920b3e3eab66 *inst/CITATION
fef4c8abd269511372a9c0c2d624cff9 *inst/doc/spatial-glms.R
80c39d81a9aad4f507bae2d075e8d70c *inst/doc/spatial-glms.Rmd
bcf181f9a48a561e95f473215eb84537 *inst/doc/spatial-glms.html
855c064dbf8deaff96c8dc8a87169109 *inst/include/stan_meta_header.hpp
45cf4ce76e5498ffba0c24610f950a8f *inst/stan/glmmfields.stan
f7045c08b543061ac102cc09b86e4410 *inst/stan/include/license.stan
9e888023dd26dc4cb904d14bb220b66e *inst/test.R
5552da1f2ebaa0aa6058b938cdf3a9b4 *man/format_data.Rd
15120b78c6117c9c85e4b58822d40a6a *man/glmmfields-package.Rd
b7875aac043cdd89b09ae71dca1b42fb *man/glmmfields.Rd
683c5d8c9c52976eb0fc5221b422b83a *man/glmmfields.Rd
a954f93cc60a089aa929d518e48561fc *man/lognormal.Rd
c68ec5a804539c4cc2a08986cbd6ef26 *man/loo.Rd
fbcbd8c199641a9ac649d74b87782065 *man/nbinom2.Rd
0dc17c4537fcae4bc470927a533918d6 *man/plot.glmmfields.Rd
ce9e0a1ac1151a9d09838e8dc5db68f4 *man/predict.Rd
d628a28cee511d9f91a1c812ecc125a0 *man/predict.Rd
fd3ea29fba1004c2c821f73131a3a369 *man/priors.Rd
267fed93d852a526793e599be97302be *man/sim_glmmfields.Rd
8b990eb0ba0476def8eadcc3b8d7f284 *man/stan_pars.Rd
d7466e066fa9fd20977c3dfa70b8bb39 *man/tidy.Rd
e385b69646bcc4555858feb68eb1f52e *src/Makevars
2d00973d800c4813504154a0267fe2f8 *src/Makevars.win
d41d8cd98f00b204e9800998ecf8427e *src/Modules.cpp
bca3e8755f348f81552bb65fc6001da6 *src/init.cpp
d41d8cd98f00b204e9800998ecf8427e *src/stan_files/chunks/common_functions.stan
1343c4c6fedaed31056cbc7c04c44ab7 *src/stan_files/chunks/license.stan
63ea07fd8aa34d48b4cf4200dd01d67f *src/stan_files/glmmfields.stan
8eb94ab0ca38362bef9a1ae171d63fad *src/Makevars
457929755cc1db9abe969d16b0006005 *src/Makevars.win
a7eb8f56f4dd47984f7e5fbe5fdbefc6 *src/RcppExports.cpp
426f38a399735d960553185277d67eab *src/stanExports_glmmfields.cc
752d22017d797bec2d5a8e21e0cabd8d *src/stanExports_glmmfields.h
962816c110c4a3d50cb8c9729b10f47c *tests/testthat.R
c9fe58f78c0e1fb163fa157438c24461 *tests/testthat/test-families.R
96f72035beb3865d574119ce5f26dea6 *tests/testthat/test-fit-ar-processes.R
15e0d4e2be8f2cefac1f34cd0beee55e *tests/testthat/test-fit-basic.R
74b578b718b399130f4607057f10a2e4 *tests/testthat/test-fit-observation-models.R
0f41c6004b6f486a8b2c69200ae883f2 *tests/testthat/test-fit-with-predictors.R
01f37d955a32c5cbc8c1c310260abac7 *tests/testthat/test-fit-ar-processes.R
526ad88ece9fb60f761516ce5eea0fb7 *tests/testthat/test-fit-basic.R
c932386d8a86323560fbf97b4d09a5d2 *tests/testthat/test-fit-observation-models.R
674eab315a135d0751941b53c86106de *tests/testthat/test-fit-with-predictors.R
13a1b7fef17c2d9489425f98807cf33d *tests/testthat/test-format-data.R
a756eae3f79a7b4cf6b4936c21526943 *tests/testthat/test-predict.R
cd352df2d4e1216390b8219524833f26 *tests/testthat/test-predict.R
d2a1a8fe460992c766dbd3ba0abfbeaa *tests/testthat/test-priors.R
c9afa41370ddc60acc527cb96782da2d *tests/testthat/test-stations.R
34e6b12ba370464b64c80d303f379d7d *tools/make_cc.R
f01f9b33ec98a387b544845a28fce364 *vignettes/spatial-glms.Rmd
9605f4d3cf3f1b82de83b7aeb7839aa3 *tests/testthat/test-stations.R
80c39d81a9aad4f507bae2d075e8d70c *vignettes/spatial-glms.Rmd
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export(tidy)
import(Rcpp)
import(methods)
import(rstantools)
importFrom(RcppParallel,RcppParallelLibs)
importFrom(assertthat,assert_that)
importFrom(assertthat,is.count)
importFrom(assertthat,is.number)
Expand All @@ -44,6 +45,7 @@ importFrom(stats,gaussian)
importFrom(stats,median)
importFrom(stats,model.frame)
importFrom(stats,model.matrix)
importFrom(stats,model.offset)
importFrom(stats,model.response)
importFrom(stats,na.omit)
importFrom(stats,predict)
Expand Down
16 changes: 16 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# glmmfields 0.1.7

* Rebuild with newest rstantools to avoid CRAN NOTE about C++ version

# glmmfields 0.1.6

* Add back RcppParallel as an import

# glmmfields 0.1.5

* Fully delegate installation to rstantools (#15)

* Other minor fixes to pass R CMD check on R devel

* Add offset functionality

# glmmfields 0.1.4

* Make compatible with updated broom.mixed
Expand Down
21 changes: 19 additions & 2 deletions R/fitting.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
#' lognormal (specified via [lognormal()] as `lognormal(link = "log")`.
#' Besides the negative binomial and lognormal, other families are specified
#' as shown in \code{\link[stats]{family}}.
#' @param binomial_N A character object giving the optional name of the column containing
#' Binomial sample size. Leave as `NULL` to fit a spatial GLMM with sample sizes (N) = 1,
#' equivalent to bernoulli model.
#' @param covariance The covariance function of the Gaussian Process.
#' One of "squared-exponential", "exponential", or "matern".
#' @param matern_kappa Optional parameter for the Matern covariance function.
Expand Down Expand Up @@ -88,6 +91,7 @@
#' @param cluster The type of clustering algorithm used to determine the knot
#' locations. `"pam"` = [cluster::pam()]. The `"kmeans"`
#' algorithm will be faster on larger datasets.
#' @param offset An optional offset vector.
#' @param ... Any other arguments to pass to [rstan::sampling()].
#'
#' @details
Expand All @@ -104,9 +108,10 @@
#' @export
#' @importFrom rstan sampling vb
#' @import Rcpp
#' @importFrom stats dist model.frame model.matrix model.response rnorm runif
#' @importFrom stats dist model.frame model.matrix model.response rnorm runif model.offset
#' @importFrom assertthat assert_that is.count is.number
#' @importFrom stats gaussian na.omit
#' @importFrom RcppParallel RcppParallelLibs
#'
#' @examples
#' \donttest{
Expand Down Expand Up @@ -158,6 +163,7 @@ glmmfields <- function(formula, data, lon, lat,
estimate_df = FALSE,
estimate_ar = FALSE,
family = gaussian(link = "identity"),
binomial_N = NULL,
covariance = c("squared-exponential", "exponential", "matern"),
matern_kappa = 0.5,
algorithm = c("sampling", "meanfield"),
Expand All @@ -167,6 +173,7 @@ glmmfields <- function(formula, data, lon, lat,
save_log_lik = FALSE,
df_lower_bound = 2,
cluster = c("pam", "kmeans"),
offset = NULL,
...) {

# argument checks:
Expand Down Expand Up @@ -215,11 +222,18 @@ glmmfields <- function(formula, data, lon, lat,
y <- model.response(mf, "numeric")
fixed_intercept <- ncol(X) == 0

if (is.null(offset)) offset <- rep(0, length(y))

if (is.null(time)) {
data$null_time_ <- 1
time <- "null_time_"
}

if (is.null(binomial_N)) {
data$null_N_ <- 1
binomial_N <- "null_N_"
}

if ("station" %in% names(list(...))) {
stop(
"The 'station' argument is no longer needed when calling glmmfields().",
Expand Down Expand Up @@ -255,6 +269,7 @@ glmmfields <- function(formula, data, lon, lat,
prior_rw_sigma = parse_t_prior(prior_rw_sigma),
prior_beta = parse_t_prior(prior_beta),
prior_phi = parse_t_prior(prior_phi),
offset = offset,
cov_func = switch(covariance,
exponential = 0L,
`squared-exponential` = 1L,
Expand All @@ -276,7 +291,8 @@ glmmfields <- function(formula, data, lon, lat,
matern_kappa = matern_kappa,
gp_sigma_scaling_factor = gp_sigma_scaling_factor,
nW = if (fixed_df_value[[1]] > 999 && !estimate_df) 0L else stan_data$nT,
df_lower_bound = df_lower_bound
df_lower_bound = df_lower_bound,
binomialN = data[,binomial_N]
)
)

Expand Down Expand Up @@ -314,6 +330,7 @@ glmmfields <- function(formula, data, lon, lat,
lon = lon, lat = lat,
time = time, year_re = year_re,
station = data_list$stationID, obs_model = obs_model,
offset = offset,
fixed_intercept = fixed_intercept, family = family
)
out <- structure(out, class = "glmmfields")
Expand Down
12 changes: 11 additions & 1 deletion R/predict.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#' "response" scale (Same as for [stats::predict.glm()]).
#' @param return_mcmc Logical. Should the full MCMC draws be returned for the
#' predictions?
#' @param offset Optional offset vector to be used in prediction.
#' @param iter Number of MCMC iterations to draw. Defaults to all.
#' @param ... Ignored currently
#'
Expand Down Expand Up @@ -68,7 +69,7 @@
#' )
#' pred_grid$prediction <- predict(m,
#' newdata = pred_grid, type = "response", iter = 100,
#' estimate_method = "median"
#' estimate_method = "median", offset = rep(0, nrow(pred_grid))
#' )$estimate
#'
#' ggplot(pred_grid, aes(lon, lat, fill = prediction)) +
Expand Down Expand Up @@ -127,6 +128,7 @@ predict.glmmfields <- function(object, newdata = NULL,
interval = c("confidence", "prediction"),
type = c("link", "response"),
return_mcmc = FALSE,
offset = NULL,
iter = "all", ...) {
estimate_method <- match.arg(estimate_method)
interval <- match.arg(interval)
Expand All @@ -143,6 +145,12 @@ predict.glmmfields <- function(object, newdata = NULL,

obs_model <- object$obs_model

if (is.null(newdata)) offset <- object$offset
if (is.null(offset) && !is.null(newdata)) offset <- rep(0, nrow(newdata))
if (!is.null(newdata) && !is.null(object$offset)) {
if (is.null(offset)) stop("Missing `offset` argument.", call. = FALSE)
}

# newdata is df with time, y, lon, lat
# if null, defaults to data used to fit model
if (is.null(newdata)) newdata <- object$data
Expand Down Expand Up @@ -265,6 +273,8 @@ predict.glmmfields <- function(object, newdata = NULL,
# generate (1) confidence intervals on mean or (2) prediction intervals
# including obs error

pred_values <- pred_values + offset

if (type == "response") {
# gamma or NB2 or poisson:
if (obs_model %in% c(0, 2, 5, 6)) pred_values <- exp(pred_values)
Expand Down
3 changes: 2 additions & 1 deletion R/stan_pars.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ stan_pars <- function(obs_error, estimate_df = TRUE, est_temporalRE = FALSE,
estimate_ar = FALSE, fixed_intercept = FALSE,
save_log_lik = FALSE) {
p <- c(
#"y_new",
"gp_sigma",
"gp_theta",
"B",
switch(obs_error[[1]], lognormal = "sigma", gaussian = "sigma",
gamma = "CV", nb2 = "nb2_phi"
gamma = "CV", nbinom2 = "nb2_phi"
),
"spatialEffectsKnots"
)
Expand Down
56 changes: 22 additions & 34 deletions R/stanmodels.R
Original file line number Diff line number Diff line change
@@ -1,37 +1,25 @@
# Part of the bayesdfa package for estimating model parameters
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# Generated by rstantools. Do not edit by hand.

# This file is only intended to be used during the installation process
# nocov start
MODELS_HOME <- "src"
if (!file.exists(MODELS_HOME)) MODELS_HOME <- sub("R$", "src", getwd())
# names of stan models
stanmodels <- c("glmmfields")

stan_files <- dir(file.path(MODELS_HOME, "stan_files"),
pattern = "stan$", full.names = TRUE)
stanmodels <- lapply(stan_files, function(f) {
model_cppname <- sub("\\.stan$", "", basename(f))
stanfit <- rstan::stanc(f, allow_undefined = TRUE,
obfuscate_model_name = FALSE)
stanfit$model_cpp <- list(model_cppname = stanfit$model_name,
# load each stan module
Rcpp::loadModule("stan_fit4glmmfields_mod", what = TRUE)

# instantiate each stanmodel object
stanmodels <- sapply(stanmodels, function(model_name) {
# create C++ code for stan model
stan_file <- if(dir.exists("stan")) "stan" else file.path("inst", "stan")
stan_file <- file.path(stan_file, paste0(model_name, ".stan"))
stanfit <- rstan::stanc_builder(stan_file,
allow_undefined = TRUE,
obfuscate_model_name = FALSE)
stanfit$model_cpp <- list(model_cppname = stanfit$model_name,
model_cppcode = stanfit$cppcode)
return(do.call(methods::new, args = c(stanfit[-(1:3)], Class = "stanmodel",
mk_cppmodule = function(x) get(paste0("model_", model_cppname)))))
}
)
names(stanmodels) <- sub("\\.stan$", "", basename(stan_files))
rm(MODELS_HOME)
# nocov end
# create stanmodel object
methods::new(Class = "stanmodel",
model_name = stanfit$model_name,
model_code = stanfit$model_code,
model_cpp = stanfit$model_cpp,
mk_cppmodule = function(x) get(paste0("rstantools_model_", model_name)))
})
4 changes: 0 additions & 4 deletions R/zzz.R

This file was deleted.

Binary file modified build/vignette.rds
Binary file not shown.
4 changes: 4 additions & 0 deletions configure
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Generated by rstantools. Do not edit by hand.

#! /bin/sh
"${R_HOME}/bin/Rscript" -e "rstantools::rstan_config()"
4 changes: 4 additions & 0 deletions configure.win
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Generated by rstantools. Do not edit by hand.

#! /bin/sh
"${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e "rstantools::rstan_config()"
Loading

0 comments on commit a28d827

Please sign in to comment.