Skip to content

Commit

Permalink
rebuild from rstantools
Browse files Browse the repository at this point in the history
  • Loading branch information
ConnorDonegan committed Dec 27, 2021
1 parent 0a48bd3 commit 9365a03
Show file tree
Hide file tree
Showing 195 changed files with 3,726 additions and 10,482 deletions.
6 changes: 3 additions & 3 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
^rcppExports.cpp$
^stanExports_*

^\.travis\.yml$
rccpExports.cpp
stanExports_*
^data-raw$
.Rhistory
README.Rmd
Expand All @@ -18,5 +19,4 @@ tests/testthat/test-rstan-conformity-results
NEWS.md
index.html
cran-comments.md

^CRAN-RELEASE$
15 changes: 10 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
src/geostan.so
src/geostan.dll
^rcppExports.cpp$
^stanExports_*
!man/geostan-internal.Rd
!man/figures/logo.png
man/*.Rd
.Rhistory
*~
inst/doc
cran-comments.md
*~$
cran-comments.md

src/geostan.so
src/geostan.dll
man/*.Rd
.Rhistory
cran-comments.md
51 changes: 0 additions & 51 deletions .travis.yml

This file was deleted.

18 changes: 7 additions & 11 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ Description: For Bayesian inference with spatial data, provides exploratory anal
License: GPL (>= 3)
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
Biarch: true
Depends:
R (>= 3.4.0)
Imports:
Imports:
spdep (>= 1.1-8),
sf,
ggplot2 (>= 3.0.0),
Expand All @@ -33,20 +35,14 @@ Imports:
utils,
Matrix (>= 1.3),
Rcpp (>= 0.12.0),
RcppParallel,
RcppParallel (>= 5.0.1),
rstan (>= 2.18.1),
rstantools (>= 2.0.0)
rstantools (>= 2.1.1)
LinkingTo:
BH (>= 1.66.0),
Rcpp (>= 0.12.0),
RcppEigen (>= 0.3.3.3.0),
RcppParallel (>= 5.0.1),
rstan (>= 2.18.1),
StanHeaders (>= 2.18.0),
RcppParallel
Suggests:
testthat,
knitr,
rmarkdown
StanHeaders (>= 2.18.0)
SystemRequirements: GNU make
RoxygenNote: 7.1.1
VignetteBuilder: knitr
595 changes: 0 additions & 595 deletions LICENSE.md

This file was deleted.

2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

geostan now supports Poisson models with censored count data, a common problem in public health research where small area disease and mortality counts are censored below a threshold value. Model for censored outcome data can now be implemented using the `censor_point` argument found in all of the model fitting functions (stan_glm, stan_car, stan_esf, stan_icar).

## measurement error models improved
## Measurement error models improved

The measurement error models have been updated in three important respects:

Expand Down
30 changes: 15 additions & 15 deletions R/convenience-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ lisa <- function(x, w, type = TRUE) {
#' @param shape An object of class \code{sf} or another spatial object coercible to \code{sf} with \code{sf::st_as_sf} such as \code{SpatialPolygonsDataFrame}.
#' @param name The name to use on the plot labels; default to "y" or, if \code{y} is a \code{geostan_fit} object, to "Residuals".
#' @param plot If \code{FALSE}, return a list of \code{gg} plots.
#' @param mc Character string indicating how to plot the residual Moran coefficient: if `mc = "scatter"`, then \code{\link[geostan]{moran_plot}} will be used with the marginal residuals; if `mc = "hist"`, then a histogram of Moran coefficient values will be returned, where each plotted value represents the degree of residual autocorrelation in a draw from the join posterior distribution of model parameters.
#'
#' @param mc_style Character string indicating how to plot the residual Moran coefficient (only used if `y` is a fitted model): if `mc = "scatter"`, then \code{\link[geostan]{moran_plot}} will be used with the marginal residuals; if `mc = "hist"`, then a histogram of Moran coefficient values will be returned, where each plotted value represents the degree of residual autocorrelation in a draw from the join posterior distribution of model parameters.
#'
#' @param style Style of connectivity matrix; if `w` is not provided, `style` is passed to \code{\link[geostan]{shape2mat}} and defaults to "W" for row-standardized.
#' @param w An optional spatial connectivity matrix; if not provided, one will be created using \code{\link[geostan]{shape2mat}}.
Expand Down Expand Up @@ -368,7 +369,7 @@ sp_diag <- function(y,
shape,
name = "y",
plot = TRUE,
mc = c("scatter", "hist"),
mc_style = c("scatter", "hist"),
style = c("W", "B"),
w = shape2mat(shape, match.arg(style)),
binwidth = function(x) 0.5 * sd(x),
Expand All @@ -393,14 +394,14 @@ sp_diag.geostan_fit <- function(y,
shape,
name = "Residual",
plot = TRUE,
mc = c("scatter", "hist"),
mc_style = c("scatter", "hist"),
style = c("W", "B"),
w = shape2mat(shape, match.arg(style)),
binwidth = function(x) 0.5 * stats::sd(x),
rates = TRUE,
size = 0.15,
...) {
mc <- match.arg(mc)
mc_style <- match.arg(mc_style, c("scatter", "hist"))
if (!inherits(shape, "sf")) shape <- sf::st_as_sf(shape)
outcome <- y$data[,1]
fits <- fitted(y, summary = TRUE, rates = rates)
Expand Down Expand Up @@ -432,13 +433,9 @@ sp_diag.geostan_fit <- function(y,
label = signs::signs) +
theme_void()
# residual autocorrelation

R <- residuals(y, summary = FALSE)
R.mc <- apply(R, 1, mc, w = w)
if (length(unique(R.mc)) == 1) {
g.mc <- moran_plot(R[1,], w, xlab = name)
}
if (mc == "scatter") {
if (mc_style == "scatter") {
g.mc <- moran_plot(marginal_residual, w, xlab = name)
} else {
R.mc.mu <- mean(R.mc)
Expand All @@ -453,6 +450,9 @@ sp_diag.geostan_fit <- function(y,
x = "Residual MC",
subtitle = paste0("MC (mean) = ", round(R.mc.mu, 2)))
}
if (length(unique(R.mc)) == 1) {
g.mc <- moran_plot(R[1,], w, xlab = name)
}
if (plot) {
return( gridExtra::grid.arrange(ovf, g.mc, map.y, ncol = 3) )
} else {
Expand Down Expand Up @@ -512,7 +512,7 @@ sp_diag.numeric <- function(y,
#' @param probs Lower and upper quantiles of the credible interval to plot.
#' @param plot If \code{FALSE}, return a list of \code{ggplot}s and a \code{data.frame} with the raw data values alongside a posterior summary of the modeled variable.
#'
#' @param mc Character string indicating how to plot the Moran coefficient for the delta values: if `mc = "scatter"`, then \code{\link[geostan]{moran_plot}} will be used with the marginal residuals; if `mc = "hist"`, then a histogram of Moran coefficient values will be returned, where each plotted value represents the degree of residual autocorrelation in a draw from the join posterior distribution of delta values.
#' @param mc_style Character string indicating how to plot the Moran coefficient for the delta values: if `mc = "scatter"`, then \code{\link[geostan]{moran_plot}} will be used with the marginal residuals; if `mc = "hist"`, then a histogram of Moran coefficient values will be returned, where each plotted value represents the degree of residual autocorrelation in a draw from the join posterior distribution of delta values.
#'
#' @param size Size of points and lines, passed to \code{geom_pointrange}.
#' @param index Integer value; use this if you wish to identify observations with the largest `n=index` absolute Delta values; data on the top `n=index` observations ordered by absolute Delta value will be printed to the console and the plots will be labeled with the indices of the identified observations.
Expand Down Expand Up @@ -570,7 +570,7 @@ me_diag <- function(fit,
shape,
probs = c(0.025, 0.975),
plot = TRUE,
mc = c("scatter", "hist"),
mc_style = c("scatter", "hist"),
size = 0.25,
index = 0,
style = c("W", "B"),
Expand All @@ -580,7 +580,7 @@ me_diag <- function(fit,
stopifnot(length(varname) == 1)
if (!varname %in% colnames(fit$data)) stop("varname is not found in colnames(fit$data). Provide the name of the variable as it appears in the model formula")
if (!inherits(shape, "sf")) shape <- sf::st_as_sf(shape)
mc <- match.arg(mc)
mc_style <- match.arg(mc_style, c("scatter", "hist"))
x.raw <- as.numeric(fit$data[,varname])
probs = sort(probs)
width = paste0(100 * (probs[2] - probs[1]), "%")
Expand Down Expand Up @@ -624,11 +624,11 @@ me_diag <- function(fit,
theme_classic()
delta.mat <- t(apply(x.samples, 1, .resid, y = x.raw))
df$Delta <- apply(delta.mat, 2, mean)
if (mc == "scatter") {
D.mc <- apply(delta.mat, 1, mc, w = w)
D.mc.mu <- mean(D.mc)
if (mc_style == "scatter") {
g.mc <- moran_plot(df$Delta, w, xlab = bquote(hat(Delta)))
} else {
D.mc <- apply(delta.mat, 1, mc, w = w)
D.mc.mu <- mean(D.mc)
g.mc <- ggplot() +
geom_histogram(aes(D.mc),
binwidth = binwidth(as.numeric(D.mc)),
Expand Down
44 changes: 15 additions & 29 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,38 @@ knitr::opts_chunk$set(

<img src="man/figures/logo.png" align="right" width="160" />

# geostan
## geostan: Bayesian spatial analysis

The **geostan** R package supports a complete spatial analysis
workflow with hierarchical Bayesian models (HBMs) for areal
data and a variety of functions for visualizing spatial data and model results.
data, including a variety of functions for visualizing spatial data and model results.

The package is designed primarily to support public health research with spatial data;
see the [**surveil**](https://connordonegan.github.io/surveil) R package for time series analysis of public health surveillance data.

**geostan** is an interface to [**Stan**](https://mc-stan.org), a state-of-the-art platform for Bayesian inference.

### Disease mapping and spatial regression

Model small-area incidence rates with mortality or disease data recorded across areal units like counties or census tracts.
Model small-area incidence rates with mortality or disease data recorded across areal units like states, counties, or census tracts.

### Observational uncertainty

Incorporate information on data reliability into any **geostan** model. Built specifically for American Community Survey (ACS) data.
Incorporate information on data reliability, such as standard errors of American Community Survey estimates, into any **geostan** model.

### Censored observations

Vital statistics and disease surveillance systems like CDC Wonder censor case counts that fall below a threshold number; **geostan** can provide probability distributions for disease or mortality risk across all areas, censored or not.

### Spatial analysis tools

Tools for visualizing and measuring spatial autocorrelation and map patterns, for exploratory analysis and model diagnostics.
Tools for visualizing and measuring spatial autocorrelation and map patterns, for exploratory analysis and model diagnostics. Visual diagnostics also support the evaluation of survey data quality and observational error models.

### The RStan ecosystem

Compatible with a suite of high-quality R packages for Bayesian inference and model evaluation.

### Custom Stan models
### Custom spatial models

Tools for building custom spatial models in [Stan](https://mc-stan.org/).

Expand All @@ -54,26 +63,3 @@ if (!require(drat)) install.packages("drat")
drat::addRepo("connordonegan")
install.packages("geostan")
```

## Resources

For demonstration analyses and some additional discussion, see the vignettes on the package [website](https://connordonegan.github.io/geostan/) and the package help pages (e.g., run `?stan_car` in R).

## Citation

* Donegan, Connor (2021). geostan: Bayesian Spatial Analysis. R package Version 0.1.1 https://connordonegan.github.io/geostan/

* Donegan, Connor, Yongwan Chun, and Daniel A. Griffith. Modeling community health with areal data: Bayesian inference with survey standard errors and spatial structure. International Journal of Environmental Research and Public Health 18.13 (2021): 6856. DOI: 10.3390/ijerph18136856


**geostan** is an interface to **Stan**:

* Carpenter B., Gelman A., Hoffman M. D., Lee D., Goodrich B., Betancourt M., Brubaker M., Guo J., Li P., and Riddell A. (2017). Stan: A probabilistic programming language. Journal of Statistical Software. 76(1). DOI: 10.18637/jss.v076.i01

## Credit

The **geostan** package was built with the help of **rstantools**:

* Gabry, Jonah, Ben Goodrich, and Martin Lysy (2021). rstantools: Tools for Developing R Packages Interfacing with 'Stan'. R package version 2.1.1 https://mc-stan.org/rstantools/index.html


34 changes: 11 additions & 23 deletions README.html

Large diffs are not rendered by default.

Loading

0 comments on commit 9365a03

Please sign in to comment.