Skip to contents

Calculate the local Geary's C statistic for model residuals. ww_local_geary_c() returns the statistic itself, while ww_local_geary_pvalue() returns the associated p value. These functions are meant to help assess model predictions, for instance by identifying clusters of higher residuals than expected. For statistical testing and inference applications, use spdep::localC_perm() instead.

Usage

ww_local_geary_c(data, ...)

ww_local_geary_c_vec(truth, estimate, wt, na_rm = FALSE, ...)

ww_local_geary_pvalue(data, ...)

ww_local_geary_pvalue_vec(truth, estimate, wt = NULL, na_rm = FALSE, ...)

Arguments

data

A data.frame containing the columns specified by the truth and estimate arguments.

...

Additional arguments passed to spdep::localC() (for ww_local_geary_c()) or spdep::localC_perm() (for ww_local_geary_pvalue()).

truth

The column identifier for the true results (that is numeric). This should be an unquoted column name although this argument is passed by expression and supports quasiquotation (you can unquote column names). For _vec() functions, a numeric vector.

estimate

The column identifier for the predicted results (that is also numeric). As with truth this can be specified different ways but the primary method is to use an unquoted variable name. For _vec() functions, a numeric vector.

wt

A listw object, for instance as created with ww_build_weights(). For data.frame input, may also be a function that takes data and returns a listw object.

na_rm

A logical value indicating whether NA values should be stripped before the computation proceeds.

Value

A tibble with columns .metric, .estimator, and .estimate and nrow(data)

rows of values. For _vec() functions, a numeric vector of length(truth) (or NA).

Details

These functions can be used for geographic or projected coordinate reference systems and expect 2D data.

References

Anselin, L. 1995. Local indicators of spatial association, Geographical Analysis, 27, pp 93–115. doi: 10.1111/j.1538-4632.1995.tb00338.x.

Anselin, L. 2019. A Local Indicator of Multivariate Spatial Association: Extending Geary's C. Geographical Analysis, 51, pp 133-150. doi: 10.1111/gean.12164

Examples

guerry_model <- guerry
guerry_lm <- lm(Crm_prs ~ Litercy, guerry_model)
guerry_model$predictions <- predict(guerry_lm, guerry_model)

ww_local_geary_c(guerry_model, Crm_prs, predictions)
#> # A tibble: 85 × 3
#>    .metric       .estimator .estimate
#>    <chr>         <chr>          <dbl>
#>  1 local_geary_c standard       0.981
#>  2 local_geary_c standard       0.836
#>  3 local_geary_c standard       0.707
#>  4 local_geary_c standard       0.108
#>  5 local_geary_c standard       0.264
#>  6 local_geary_c standard       1.36 
#>  7 local_geary_c standard       3.64 
#>  8 local_geary_c standard       1.57 
#>  9 local_geary_c standard       0.867
#> 10 local_geary_c standard       0.737
#> # ℹ 75 more rows
ww_local_geary_pvalue(guerry_model, Crm_prs, predictions)
#> # A tibble: 85 × 3
#>    .metric            .estimator .estimate
#>    <chr>              <chr>          <dbl>
#>  1 local_geary_pvalue standard       0.217
#>  2 local_geary_pvalue standard       0.228
#>  3 local_geary_pvalue standard       0.179
#>  4 local_geary_pvalue standard       0.155
#>  5 local_geary_pvalue standard       0.295
#>  6 local_geary_pvalue standard       0.172
#>  7 local_geary_pvalue standard       0.440
#>  8 local_geary_pvalue standard       0.171
#>  9 local_geary_pvalue standard       0.753
#> 10 local_geary_pvalue standard       0.416
#> # ℹ 75 more rows

wt <- ww_build_weights(guerry_model)

ww_local_geary_c_vec(
  guerry_model$Crm_prs,
  guerry_model$predictions,
  wt = wt
)
#>  [1] 0.981119438 0.836402177 0.707464373 0.108332465 0.264075824 1.361485477
#>  [7] 3.641239412 1.571824022 0.867252524 0.737094462 0.573376555 0.001605731
#> [13] 1.891988440 1.152840284 1.029320931 0.297642850 1.219953394 1.934113868
#> [19] 1.632566652 0.441916658 5.202733790 0.921953310 3.084515822 0.237218594
#> [25] 1.346684045 1.051652204 0.419414691 0.217280214 0.794409207 0.243971372
#> [31] 0.376678958 0.139152907 0.711305633 3.096840680 1.974463944 0.922230710
#> [37] 1.032031759 0.339464386 0.933794842 1.910440700 0.937597672 0.625628647
#> [43] 0.376707677 2.692250283 1.288784962 0.798443065 1.671895951 1.310183326
#> [49] 2.347513577 0.845204889 0.302940809 2.291804447 0.881999216 0.412051312
#> [55] 2.006031605 0.561239582 0.375776092 1.853716391 1.191472387 1.146802970
#> [61] 1.857618679 0.149044974 0.614228825 0.755373475 1.287962784 1.447534518
#> [67] 1.236607966 0.962394651 0.338400653 1.914478855 0.641340157 2.146993342
#> [73] 0.703881855 1.417638272 0.692636715 1.765618175 0.246058853 0.700262130
#> [79] 0.002876896 0.057575267 0.420878038 2.025012395 2.525093274 1.053335832
#> [85] 1.030009749
ww_local_geary_pvalue_vec(
  guerry_model$Crm_prs,
  guerry_model$predictions,
  wt = wt
)
#>  [1] 0.23474514 0.21753355 0.16498361 0.11463282 0.31416100 0.14656747
#>  [7] 0.41580885 0.18440624 0.82547098 0.43890087 0.01878025 0.20159885
#> [13] 0.29696887 0.89603151 0.49738485 0.20757126 0.74602665 0.61187655
#> [19] 0.06108160 0.17775847 0.51966674 0.86254561 0.64501892 0.10934039
#> [25] 0.86169024 0.92992849 0.21565940 0.10106666 0.66605783 0.12770483
#> [31] 0.16184880 0.14642654 0.44407071 0.53788795 0.09491413 0.79791306
#> [37] 0.36892968 0.27266281 0.87241583 0.81298864 0.67737500 0.58148521
#> [43] 0.15231107 0.21953380 0.86390552 0.07249287 0.26748049 0.18097595
#> [49] 0.15800367 0.21253933 0.07147542 0.76459244 0.44154120 0.25012724
#> [55] 0.94542817 0.20942855 0.22734011 0.60804965 0.21291660 0.98490108
#> [61] 0.27740355 0.23532665 0.31561710 0.20878385 0.58089273 0.30732703
#> [67] 0.69922980 0.76961051 0.03518308 0.07048841 0.49706327 0.10230665
#> [73] 0.34476710 0.58937748 0.57341248 0.09425254 0.11593643 0.30026813
#> [79] 0.18232530 0.06863018 0.28494748 0.51828563 0.10712268 0.94110698
#> [85] 0.89742414