diff --git a/README.Rmd b/README.Rmd index d2b9627..1ac79d9 100644 --- a/README.Rmd +++ b/README.Rmd @@ -46,16 +46,18 @@ Using your R console, you can install geostan from CRAN: install.packages("geostan") ``` +For most users, installing from CRAN is the recommended way to obtain geostan. + ### From github -You can install geostan from github: +You can also install geostan from github: ```r if (!require('devtools')) install.packages('devtools') devtools::install_github("connordonegan/geostan") ``` -If you are using Windows and installing with `install_github`, you may need to install [Rtools](https://cran.r-project.org/bin/windows/Rtools/) first (this is not needed when installing from CRAN). To install Rtools: +This requires compilation of the Stan models. If you are using Windows and installing with `install_github`, you may need to install [Rtools](https://cran.r-project.org/bin/windows/Rtools/) first (this is not needed when installing from CRAN). To install Rtools: 1. Visit the Rtools site: https://cran.r-project.org/bin/windows/Rtools/ 2. Select the version that corresponds to the version of R that you have installed (e.g., R 4.3). diff --git a/README.html b/README.html index f1bae06..e9d0db3 100644 --- a/README.html +++ b/README.html @@ -623,11 +623,12 @@
Using your R console, you can install geostan from CRAN:
install.packages("geostan")
For most users, installing from CRAN is the recommended way to obtain geostan.
You can install geostan from github:
+You can also install geostan from github:
if (!require('devtools')) install.packages('devtools')
::install_github("connordonegan/geostan") devtools
If you are using Windows and installing with install_github
, you may need to install Rtools first (this is not needed when installing from CRAN). To install Rtools:
This requires compilation of the Stan models. If you are using Windows and installing with install_github
, you may need to install Rtools first (this is not needed when installing from CRAN). To install Rtools:
Load the package and the georgia
county mortality data set:
library(geostan)
-data(georgia)
This has county population and mortality data by sex for ages 55-64, and for the period 2014-2018. As is common for public access data, some of the observations missing because the CDC has censored them to protect privacy.
The sp_diag
function provides visual summaries of spatial data, including a histogram, Moran scatter plot, and map. The Moran scatter plot displays the values against a summary of their neighboring values, so that the slope of the regression line gives a measure of their degree of autocorrelation.
Here is a quick visual summary of crude female mortality rates (as deaths per 10,000):
@@ -662,8 +664,8 @@Mortality rates and other health statistics for counties are, in many cases, highly unstable estimates that cannot be relied upon for public advisories or inference (due to small population sizes). Hence, we need models to make inferences from small area data.
@@ -682,13 +684,16 @@Passing a fitted model to the sp_diag
function will return a set of diagnostics for spatial models:
sp_diag(fit, georgia)
#> 3 NA values found in x will be dropped from data x and from matrix w (nb: this disrupts row-standardization of w)
-#> Warning: Removed 3 rows containing missing values or values outside the
-#> scale range (`geom_pointrange()`).
The print
method returns a summary of the probability distributions for model parameters, as well as Markov chain Monte Carlo (MCMC) diagnostics from Stan (Monte Carlo standard errors of the mean se_mean
, effective sample size n_eff
, and the R-hat statistic Rhat
):
print(fit)
@@ -697,7 +702,7 @@ Usage
#> Likelihood: poisson
#> Link: log
#> Spatial method: CAR
-#> Residual Moran Coefficient: -0.0031375
+#> Residual Moran Coefficient: 0.0018845
#> Observations: 156
#>
#> Inference for Stan model: foundation.
@@ -705,11 +710,11 @@ Usage
#> post-warmup draws per chain=500, total post-warmup draws=2000.
#>
#> mean se_mean sd 2.5% 20% 50% 80% 97.5% n_eff Rhat
-#> intercept -4.677 0.003 0.092 -4.871 -4.734 -4.677 -4.619 -4.513 861 1.004
-#> car_rho 0.924 0.001 0.058 0.784 0.883 0.936 0.973 0.996 1606 0.999
-#> car_scale 0.456 0.001 0.036 0.391 0.424 0.454 0.486 0.532 1899 1.002
+#> intercept -4.660 0.010 0.124 -4.844 -4.728 -4.671 -4.614 -4.415 152 1.026
+#> car_rho 0.926 0.002 0.057 0.788 0.884 0.938 0.974 0.997 982 1.004
+#> car_scale 0.457 0.001 0.035 0.392 0.427 0.454 0.485 0.533 1869 1.001
#>
-#> Samples were drawn using NUTS(diag_e) at Wed Nov 13 16:09:50 2024.
+#> Samples were drawn using NUTS(diag_e) at Mon Dec 16 09:54:18 2024.
#> 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).
The mortality estimates are stored in the column named “mean”, and the limits of the 95% credible interval are found in the columns “2.5%” and “97.5%”. Here we create a map of estimates (with some help from sf
package):
library(sf)
-
-# put estimates into bins for map colors
- mortality_est$mean
- x <- quantile(x, probs = c(0, 0.2, 0.4, 0.6, 0.8, 1))
- brks <- cut(x, breaks = brks, include.lowest = TRUE)
- est_cut <-
- # assign colors to values
- as.numeric( est_cut )
- rank <- colorRampPalette( c("#5D74A5FF", "gray90", "#A8554EFF") )
- pal_fun <- pal_fun( max(rank) )
- pal <- pal[ rank ]
- colors <-
-# set plot margins
-par(mar=rep(1, 4))
- og=
-# get boundaries
- sf::st_geometry(georgia)
- geom <-
-# map estimates
-plot(geom,
-lwd = 0.2,
- col = colors)
-
-# legend
-legend("right",
-fill = pal,
- title = 'Mortality per 10,000',
- legend = levels(est_cut),
- bty = 'n'
-
- )
-mtext('County mortality rates per 10,000, Georgia women ages 55-64', side = 3, font = 2)
# reset margins
par(og)
par(og)
More details and demonstrations can be found in the package help pages and vignettes.
diff --git a/README.md b/README.md index 1cddde9..d867357 100644 --- a/README.md +++ b/README.md @@ -55,18 +55,22 @@ Using your R console, you can install geostan from CRAN: install.packages("geostan") ``` +For most users, installing from CRAN is the recommended way to obtain +geostan. + ### From github -You can install geostan from github: +You can also install geostan from github: ``` r if (!require('devtools')) install.packages('devtools') devtools::install_github("connordonegan/geostan") ``` -If you are using Windows and installing with `install_github`, you may -need to install [Rtools](https://cran.r-project.org/bin/windows/Rtools/) -first (this is not needed when installing from CRAN). To install Rtools: +This requires compilation of the Stan models. If you are using Windows +and installing with `install_github`, you may need to install +[Rtools](https://cran.r-project.org/bin/windows/Rtools/) first (this is +not needed when installing from CRAN). To install Rtools: 1. Visit the Rtools site:
install.packages("geostan")
+For most users, installing from CRAN is the recommended way to obtain geostan.
You can install geostan from github:
+You can also install geostan from github:
if (!require('devtools')) install.packages('devtools')
devtools::install_github("connordonegan/geostan")
If you are using Windows and installing with install_github
, you may need to install Rtools first (this is not needed when installing from CRAN). To install Rtools:
This requires compilation of the Stan models. If you are using Windows and installing with install_github
, you may need to install Rtools first (this is not needed when installing from CRAN). To install Rtools:
Load the package and the georgia
county mortality data set:
This has county population and mortality data by sex for ages 55-64, and for the period 2014-2018. As is common for public access data, some of the observations missing because the CDC has censored them to protect privacy.
The sp_diag
function provides visual summaries of spatial data, including a histogram, Moran scatter plot, and map. The Moran scatter plot displays the values against a summary of their neighboring values, so that the slope of the regression line gives a measure of their degree of autocorrelation.
Mortality rates and other health statistics for counties are, in many cases, highly unstable estimates that cannot be relied upon for public advisories or inference (due to small population sizes). Hence, we need models to make inferences from small area data.
The following code fits a spatial conditional autoregressive (CAR) model to female county mortality data. These models are used for estimating disease risk in small areas like counties, and for analyzing covariation of health outcomes with other area variables. The R syntax for fitting the models is similar to using lm
or glm
. We provide the population at risk (the denominator for mortality rates) as an offset term, using the log-transform.
Passing a fitted model to the sp_diag
function will return a set of diagnostics for spatial models:
sp_diag(fit, georgia)
#> 3 NA values found in x will be dropped from data x and from matrix w (nb: this disrupts row-standardization of w)
-#> Warning: Removed 3 rows containing missing values or values outside the
-#> scale range (`geom_pointrange()`).
The print
method returns a summary of the probability distributions for model parameters, as well as Markov chain Monte Carlo (MCMC) diagnostics from Stan (Monte Carlo standard errors of the mean se_mean
, effective sample size n_eff
, and the R-hat statistic Rhat
):
@@ -199,7 +204,7 @@Usage #> Likelihood: poisson #> Link: log #> Spatial method: CAR -#> Residual Moran Coefficient: -0.0031375 +#> Residual Moran Coefficient: 0.0018845 #> Observations: 156 #> #> Inference for Stan model: foundation. @@ -207,11 +212,11 @@
Usage #> post-warmup draws per chain=500, total post-warmup draws=2000. #> #> mean se_mean sd 2.5% 20% 50% 80% 97.5% n_eff Rhat -#> intercept -4.677 0.003 0.092 -4.871 -4.734 -4.677 -4.619 -4.513 861 1.004 -#> car_rho 0.924 0.001 0.058 0.784 0.883 0.936 0.973 0.996 1606 0.999 -#> car_scale 0.456 0.001 0.036 0.391 0.424 0.454 0.486 0.532 1899 1.002 +#> intercept -4.660 0.010 0.124 -4.844 -4.728 -4.671 -4.614 -4.415 152 1.026 +#> car_rho 0.926 0.002 0.057 0.788 0.884 0.938 0.974 0.997 982 1.004 +#> car_scale 0.457 0.001 0.035 0.392 0.427 0.454 0.485 0.533 1869 1.001 #> -#> Samples were drawn using NUTS(diag_e) at Wed Nov 13 16:09:50 2024. +#> Samples were drawn using NUTS(diag_e) at Mon Dec 16 09:54:18 2024. #> 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).
The mortality estimates are stored in the column named “mean”, and the limits of the 95% credible interval are found in the columns “2.5%” and “97.5%”. Here we create a map of estimates (with some help from sf
package):
library(sf)
+#> Linking to GEOS 3.10.2, GDAL 3.4.3, PROJ 8.2.1; sf_use_s2() is TRUE
# put estimates into bins for map colors
x <- mortality_est$mean
diff --git a/docs/reference/figures/README-unnamed-chunk-5-1.png b/docs/reference/figures/README-unnamed-chunk-5-1.png
index 85bcdff..d22bba5 100644
Binary files a/docs/reference/figures/README-unnamed-chunk-5-1.png and b/docs/reference/figures/README-unnamed-chunk-5-1.png differ
diff --git a/docs/reference/figures/README-unnamed-chunk-8-1.png b/docs/reference/figures/README-unnamed-chunk-8-1.png
index 665a79c..9d5b6f2 100644
Binary files a/docs/reference/figures/README-unnamed-chunk-8-1.png and b/docs/reference/figures/README-unnamed-chunk-8-1.png differ
diff --git a/docs/reference/figures/README-unnamed-chunk-9-1.png b/docs/reference/figures/README-unnamed-chunk-9-1.png
index 3ac77a8..b429ce0 100644
Binary files a/docs/reference/figures/README-unnamed-chunk-9-1.png and b/docs/reference/figures/README-unnamed-chunk-9-1.png differ
diff --git a/man/figures/README-unnamed-chunk-5-1.png b/man/figures/README-unnamed-chunk-5-1.png
index 85bcdff..d22bba5 100644
Binary files a/man/figures/README-unnamed-chunk-5-1.png and b/man/figures/README-unnamed-chunk-5-1.png differ
diff --git a/man/figures/README-unnamed-chunk-8-1.png b/man/figures/README-unnamed-chunk-8-1.png
index 665a79c..9d5b6f2 100644
Binary files a/man/figures/README-unnamed-chunk-8-1.png and b/man/figures/README-unnamed-chunk-8-1.png differ
diff --git a/man/figures/README-unnamed-chunk-9-1.png b/man/figures/README-unnamed-chunk-9-1.png
index 3ac77a8..b429ce0 100644
Binary files a/man/figures/README-unnamed-chunk-9-1.png and b/man/figures/README-unnamed-chunk-9-1.png differ