Skip to content

Commit

Permalink
clean docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ConnorDonegan committed Sep 18, 2022
1 parent 6a5c2d9 commit 62070ec
Show file tree
Hide file tree
Showing 20 changed files with 381 additions and 98 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ tests/testthat/test-rstan-conformity-results
index.html
cran-comments.md
^CRAN-RELEASE$
LICENSE.md
File renamed without changes.
13 changes: 10 additions & 3 deletions R/convenience-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ sp_diag.geostan_fit <- function(y,
w = shape2mat(shape, match.arg(style)),
binwidth = function(x) 0.5 * stats::sd(x, na.rm = TRUE),
rates = TRUE,
size = 0.15,
size = 0.05,
...) {
mc_style <- match.arg(mc_style, c("scatter", "hist"))
if (!inherits(shape, "sf")) shape <- sf::st_as_sf(shape)
Expand Down Expand Up @@ -231,7 +231,7 @@ sp_diag.geostan_fit <- function(y,
marginal_residual <- apply(residuals(y, summary = FALSE, ...), 2, mean, na.rm = TRUE)
map.y <- ggplot(shape) +
geom_sf(aes(fill = marginal_residual),
lwd = 0.05,
lwd = .01,
col = "gray20") +
scale_fill_gradient2(name = name,
label = signs::signs) +
Expand Down Expand Up @@ -979,7 +979,14 @@ prep_icar_data <- function(C, scale_factor = NULL) {
#' @md
#' @importFrom rstan extract_sparse_parts
#' @importFrom Matrix isSymmetric Matrix rowSums summary
prep_car_data <- function(A, style = c("WCAR", "ACAR", "DCAR"), k = 1, gamma = 0, lambda = TRUE, cmat = TRUE, stan_fn = ifelse(style == "WCAR", "wcar_normal_lpdf", "car_normal_lpdf")) {
prep_car_data <- function(A,
style = c("WCAR", "ACAR", "DCAR"),
k = 1,
gamma = 0,
lambda = TRUE,
cmat = TRUE,
stan_fn = ifelse(style == "WCAR", "wcar_normal_lpdf", "car_normal_lpdf")
) {
style = match.arg(style)
if (style != "WCAR" & stan_fn == "wcar_normal_lpdf") stop("wcar_normal_lpdf only works with style = 'WCAR'.")
stopifnot(inherits(A, "matrix") | inherits(A, "Matrix"))
Expand Down
4 changes: 2 additions & 2 deletions R/moran.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ mc <- function(x, w, digits = 3, warn = TRUE, na.rm = FALSE) {
check_sa_data(x, w)
na_idx <- which(is.na(x))
if (na.rm == TRUE && length(na_idx) > 0) {
if (warn) message(length(na_idx), " NA values found in x. They will be removed from the data before calculating the Moran coefficient. If matrix w was row-standardized, it may not longer be. To address this, you can use a binary connectivity matrix, using style = 'B' in shape2mat.")
if (warn) message(length(na_idx), " NA values found in x; they will be removed from the data before calculating the Moran coefficient. If matrix w was row-standardized, it may not longer be. You may want to use a binary connectivity matrix by using style = 'B' in shape2mat.")
x <- x[-na_idx]
w <- w[-na_idx, -na_idx]
}
Expand Down Expand Up @@ -101,7 +101,7 @@ moran_plot <- function(x, w, xlab = "x (centered)", ylab = "Spatial Lag", pch =
na_idx <- which(is.na(x))
if (length(na_idx) > 0) {
if (na.rm == TRUE) {
message(length(na_idx), " NA values found in x. They will be dropped from the data before creating the Moran plot. If matrix w was row-standardized, it no longer is. To address this, you can use a binary connectivity matrix, using style = 'B' in shape2mat.")
message(length(na_idx), " NA values found in x; they will be dropped from the data before creating the Moran plot. If matrix w was row-standardized, it no longer is. You may want to use a binary connectivity matrix using style = 'B' in shape2mat.")
x <- x[-na_idx]
w <- w[-na_idx, -na_idx]
} else {
Expand Down
4 changes: 2 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ data(georgia)
The `sp_diag` function provides visual summaries of spatial data, including a histogram, Moran scatter plot, and map:

```{r fig.width = 8}
sp_diag(georgia$rate.female, georgia)
A <- shape2mat(georgia, style = "B")
sp_diag(georgia$rate.female, georgia, w = A)
```

There are three censored observations in the `georgia` female mortality data, which means there were 9 or fewer deaths in those counties. The following code fits a spatial conditional autoregressive (CAR) model to female county mortality data. By using the `censor_point` argument we include our information on the censored observations to obtain results for all counties:

```{r}
A <- shape2mat(georgia, style = "B")
cars <- prep_car_data(A)
fit <- stan_car(deaths.female ~ offset(log(pop.at.risk.female)),
censor_point = 9,
Expand Down
83 changes: 38 additions & 45 deletions README.html

Large diffs are not rendered by default.

27 changes: 10 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,17 @@ Load the package and the `georgia` county mortality data set (ages

``` r
library(geostan)
#> This is geostan version 0.3.0
#>
#> Attaching package: 'geostan'
#> The following object is masked from 'package:base':
#>
#> gamma
data(georgia)
```

The `sp_diag` function provides visual summaries of spatial data,
including a histogram, Moran scatter plot, and map:

``` r
sp_diag(georgia$rate.female, georgia)
A <- shape2mat(georgia, style = "B")
sp_diag(georgia$rate.female, georgia, w = A)
#> 3 NA values found in x. They will be dropped from the data before creating the Moran plot. If matrix w was row-standardized, it no longer is. To address this, you can use a binary connectivity matrix, using style = 'B' in shape2mat.
#> Warning: Removed 3 rows containing non-finite values (stat_bin).
#> Warning: Removed 3 rows containing non-finite values (`stat_bin()`).
```

<img src="man/figures/README-unnamed-chunk-3-1.png" style="display: block; margin: auto;" />
Expand All @@ -98,9 +93,7 @@ include our information on the censored observations to obtain results
for all counties:

``` r
A <- shape2mat(georgia, style = "B")
cars <- prep_car_data(A)
#> as(<ngCMatrix>, "dgCMatrix") is deprecated since Matrix 1.5-0; do as(., "dMatrix") instead
#> Range of permissible rho values: -1.661134 1
fit <- stan_car(deaths.female ~ offset(log(pop.at.risk.female)),
censor_point = 9,
Expand Down Expand Up @@ -132,7 +125,7 @@ diagnostics for spatial models:
``` r
sp_diag(fit, georgia, w = A)
#> 3 NA values found in x. They will be dropped from the data before creating the Moran plot. If matrix w was row-standardized, it no longer is. To address this, you can use a binary connectivity matrix, using style = 'B' in shape2mat.
#> Warning: Removed 3 rows containing missing values (geom_pointrange).
#> Warning: Removed 3 rows containing missing values (`geom_pointrange()`).
```

<img src="man/figures/README-unnamed-chunk-5-1.png" style="display: block; margin: auto;" />
Expand All @@ -150,20 +143,20 @@ print(fit)
#> Spatial method (outcome): CAR
#> Likelihood function: poisson
#> Link function: log
#> Residual Moran Coefficient: 0.0014105
#> WAIC: 1291.3
#> Residual Moran Coefficient: 0.00053825
#> WAIC: 1291.44
#> Observations: 159
#> Data models (ME): none
#> Inference for Stan model: foundation.
#> 4 chains, each with iter=2000; warmup=1000; thin=1;
#> post-warmup draws per chain=1000, total post-warmup draws=4000.
#>
#> mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
#> intercept -4.671 0.004 0.107 -4.855 -4.718 -4.672 -4.625 -4.478 845 1.004
#> car_rho 0.926 0.001 0.057 0.780 0.897 0.939 0.968 0.996 3130 0.999
#> car_scale 0.456 0.001 0.035 0.392 0.431 0.455 0.479 0.532 3233 1.001
#> intercept -4.674 0.002 0.083 -4.826 -4.717 -4.674 -4.633 -4.510 2199 1.002
#> car_rho 0.921 0.001 0.057 0.781 0.891 0.932 0.963 0.994 3783 1.000
#> car_scale 0.458 0.001 0.034 0.395 0.434 0.456 0.480 0.531 4130 1.000
#>
#> Samples were drawn using NUTS(diag_e) at Tue Sep 13 08:59:48 2022.
#> Samples were drawn using NUTS(diag_e) at Sat Sep 17 21:02:38 2022.
#> For each parameter, n_eff is a crude measure of effective sample size,
#> and Rhat is the potential scale reduction factor on split chains (at
#> convergence, Rhat=1).
Expand Down
Loading

0 comments on commit 62070ec

Please sign in to comment.