JosiahParry / sfdep

A tidy interface for spatial dependence
https://sfdep.josiahparry.com/
GNU General Public License v3.0
121 stars 5 forks source link

Error in stopifnot() with local moran #33

Closed eforsyth closed 1 year ago

eforsyth commented 1 year ago

Hi Josiah,

I'm a spdep user who just discovered this useful looking package so thought I'd gave it a quick go. While looking to apply a simple LISA assessment to some of my data I encountered an error with local_moran() that is reproducible with the Guerry data.

guerry <- sfdep::guerry %>% 
  st_set_crs(27572) %>% 
  select(code_dept, crime_pers, crime_prop)

guerry_nb <- guerry %>% 
  mutate(nb = st_contiguity(geometry),
         wt = st_weights(nb))

guerry_lisa <- guerry_nb %>% 
  mutate(local_moran_crime_pers = local_moran(crime_pers, nb, wt, nsim = 199), # error occurs here
         local_moran_crime_prop = local_moran(crime_prop, nb, wt, nsim = 199)) # tried with another column as well, just in case

It returns this error for both Guerry and my own data:

Error in `stopifnot()`:
! Problem while computing `local_moran_crime_pers = local_moran(crime_pers, nb, wt, nsim = 199)`.
Caused by error in `names(object) <- nm`:
! 'names' attribute [9] must be the same length as the vector [8]

In case there was any mix-up with rgeoda package, I also tried using sfdep::local_moran(), to no avail.

eforsyth commented 1 year ago

I also just had a quick go copy/pasting the Guerry example from the readme and received the same error. Plotting the spatial lag worked as expected but hit the same error with local_moran().

JosiahParry commented 1 year ago

I cannot reproduce the error—though it is familiar to an issue I saw recently creating listw objects without a style on the weights. Can you provide a reproducible example? The following code chunk produced the below output.

reprex::reprex({
  library(sf)
  library(sfdep)
  library(dplyr)

  sfdep::guerry %>% 
    st_set_crs(27572) %>% 
    select(code_dept, crime_pers, crime_prop) %>% 
    mutate(nb = st_contiguity(geometry),
           wt = st_weights(nb),
           local_moran_crime_pers = local_moran(crime_pers, nb, wt, nsim = 199))
})
library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.2.3, PROJ 7.2.1; sf_use_s2() is TRUE
library(sfdep)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

sfdep::guerry %>% 
  st_set_crs(27572) %>% 
  select(code_dept, crime_pers, crime_prop) %>% 
  mutate(nb = st_contiguity(geometry),
         wt = st_weights(nb),
         local_moran_crime_pers = local_moran(crime_pers, nb, wt, nsim = 199))
#> Simple feature collection with 85 features and 6 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 47680 ymin: 1703258 xmax: 1031401 ymax: 2677441
#> Projected CRS: NTF (Paris) / Lambert zone II
#> # A tibble: 85 × 7
#>    code_dept crime_pers crime_prop                  geometry nb    wt    local…¹
#>  * <fct>          <int>      <int>        <MULTIPOLYGON [m]> <nb>  <lis>   <dbl>
#>  1 01             28870      15890 (((801150 2092615, 80066… <int> <dbl>  0.522 
#>  2 02             26226       5521 (((729326 2521619, 72932… <int> <dbl>  0.828 
#>  3 03             26747       7925 (((710830 2137350, 71174… <int> <dbl>  0.804 
#>  4 04             12935       7289 (((882701 1920024, 88240… <int> <dbl>  0.742 
#>  5 05             17488       8174 (((886504 1922890, 88573… <int> <dbl>  0.231 
#>  6 07              9474      10263 (((747008 1925789, 74663… <int> <dbl>  0.839 
#>  7 08             35203       8847 (((818893 2514767, 81861… <int> <dbl>  0.623 
#>  8 09              6173       9597 (((509103 1747787, 50882… <int> <dbl>  1.65  
#>  9 10             19602       4086 (((775400 2345600, 77506… <int> <dbl> -0.0198
#> 10 11             15647      10431 (((626230 1810121, 62626… <int> <dbl>  0.695 
#> # … with 75 more rows, 11 more variables: local_moran_crime_pers$eii <dbl>,
#> #   $var_ii <dbl>, $z_ii <dbl>, $p_ii <dbl>, $p_ii_sim <dbl>,
#> #   $p_folded_sim <dbl>, $skewness <dbl>, $kurtosis <dbl>, $mean <fct>,
#> #   $median <fct>, $pysal <fct>, and abbreviated variable name
#> #   ¹​local_moran_crime_pers$ii
#> # ℹ Use `print(n = ...)` to see more rows, and `colnames()` to see all variable names

Created on 2022-09-08 by the reprex package (v2.0.1)

eforsyth commented 1 year ago

Sorry, should've added the reprex to start with.

reprex::reprex({
  library(sf)
  library(sfdep)
  library(dplyr)

  guerry <- sfdep::guerry %>% 
    st_set_crs(27572) %>% 
    select(code_dept, crime_pers, crime_prop)

  guerry_nb <- guerry %>% 
    mutate(nb = st_contiguity(geometry),
           wt = st_weights(nb, style = "W")) # explicitly added the style following your comment

  guerry_lisa <- guerry_nb %>% 
    mutate(local_moran_crime_pers = local_moran(crime_pers, nb, wt, nsim = 199))
  })
library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
library(sfdep)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

guerry <- sfdep::guerry %>% 
  st_set_crs(27572) %>% 
  select(code_dept, crime_pers, crime_prop)

guerry_nb <- guerry %>% 
  mutate(nb = st_contiguity(geometry),
         wt = st_weights(nb, style = "W"))

guerry_lisa <- guerry_nb %>% 
  mutate(local_moran_crime_pers = local_moran(crime_pers, nb, wt, nsim = 199))
#> Error in `stopifnot()`:
#> ! Problem while computing `local_moran_crime_pers =
#>   local_moran(crime_pers, nb, wt, nsim = 199)`.
#> Caused by error in `names(object) <- nm`:
#> ! 'names' attribute [9] must be the same length as the vector [8]

As you weren't able to reproduce the error, I tried it on three other machines where it worked correctly in all instances (using my code above, your reprex, and the readme example). Maybe some sort of package clash on my particular machine?

JosiahParry commented 1 year ago

@eforsyth three other machines?! That's dedication. Can you provide the output of sessionInfo() perhaps you're rununing an old version of the package or dplyr. Can you try

guerry_nb <- guerry %>% 
  mutate(nb = st_contiguity(geometry),
         wt = st_weights(nb, style = "W"))

local_moran(guerry_nb$crime_pers, guerry_nb$nb, guerry_nb$wt)

?

eforsyth commented 1 year ago

I thought I had updated all packages before opening this issue, but apparently it didn't take for whatever reason(s). After updating all packages correctly (!), everything works as expected. My bad!

reprex::reprex({
  library(sf)
  library(sfdep)
  library(dplyr)

  guerry_nb <- guerry %>% 
  mutate(nb = st_contiguity(geometry),
         wt = st_weights(nb, style = "W"))

local_moran(guerry_nb$crime_pers, guerry_nb$nb, guerry_nb$wt)
})
  library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE
  library(sfdep)
  library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

  guerry_nb <- guerry %>% 
  mutate(nb = st_contiguity(geometry),
         wt = st_weights(nb, style = "W"))

local_moran(guerry_nb$crime_pers, guerry_nb$nb, guerry_nb$wt)
#>              ii           eii       var_ii        z_ii        p_ii p_ii_sim
#> 1   0.522264520 -2.282117e-02 0.3625034937  0.90533369 0.365288634    0.364
#> 2   0.828016509  8.625564e-03 0.1361509940  2.22065326 0.026374455    0.024
#> 3   0.803539971  1.727066e-02 0.1477115701  2.04580422 0.040775637    0.036
#> 4   0.741889657 -1.541478e-02 0.2429853142  1.53631577 0.124460954    0.132
#> 5   0.231187184  1.879696e-02 0.0387605799  1.07879613 0.280678620    0.324
#> 6   0.838909799 -2.964767e-02 0.2930159758  1.60454872 0.108593155    0.124
#> 7   0.622605215 -2.173891e-02 1.3427941404  0.55604911 0.578177280    0.572
#> 8   1.646789439 -9.065398e-02 1.0280698083  1.71356020 0.086609527    0.060
#> 9  -0.019758073  9.819049e-05 0.0004678125 -0.91803998 0.358597935    0.360
#> 10  0.695261432 -1.448846e-02 0.0757497444  2.57878269 0.009914914    0.004
#> 11  1.728228681 -4.546853e-02 0.3803243563  2.87608961 0.004026354    0.008
#> 12  0.831530992 -1.712603e-02 0.2676692347  1.64033661 0.100935197    0.080
#> 13 -0.216110795 -2.085022e-03 0.0332692767 -1.17339543 0.240637267    0.232
#> 14  0.294784885 -9.534953e-03 0.0123837485  2.73466544 0.006244371    0.004
#> 15 -0.187033812 -1.324179e-02 0.0974057924 -0.55684900 0.577630602    0.628
#> 16 -0.047760501 -1.606668e-03 0.0059822900 -0.59672473 0.550691176    0.568
#> 17  0.252948279 -4.885183e-03 0.0142795657  2.15765389 0.030954752    0.028
#> 18  0.055058947 -1.548146e-02 0.0757212462  0.25634749 0.797682532    0.772
#> 19  0.872358990 -7.789831e-02 0.3962237592  1.50963152 0.131137473    0.144
#> 20  0.845313303 -6.547763e-02 0.4653370413  1.33516484 0.181822401    0.176
#> 21  0.544842074 -2.240884e-02 0.8372715295  0.61992913 0.535304450    0.536
#> 22 -0.051046792  8.300838e-04 0.0073157944 -0.60651713 0.544171413    0.564
#> 23 -0.689130071 -1.730870e-02 0.7082542405 -0.79828731 0.424703783    0.444
#> 24  0.686933419 -2.110252e-02 0.1687586136  1.72354382 0.084790227    0.088
#> 25 -0.106623626 -1.461993e-02 0.0816009519 -0.32207548 0.747395513    0.740
#> 26  0.039027064 -5.794621e-03 0.0068636492  0.54101667 0.588496095    0.624
#> 27  1.129525109 -2.700108e-02 0.8640151816  1.24421314 0.213421129    0.252
#> 28  1.182045274 -3.847974e-02 0.1569548319  3.08077045 0.002064658    0.004
#> 29  0.163959819 -1.078285e-03 0.0056644514  2.19283169 0.028319508    0.040
#> 30  0.069859097  2.332562e-04 0.0056220603  0.92858721 0.353103050    0.336
#> 31  0.021778578  1.901107e-02 0.0765754118  0.01000102 0.992020470    1.000
#> 32  1.012163106 -1.586665e-02 0.2312211455  2.13792228 0.032523052    0.016
#> 33  0.279597476 -8.150080e-03 0.0135438984  2.47251992 0.013416424    0.028
#> 34  0.428197017  1.841788e-02 0.5052247371  0.57651093 0.564269887    0.572
#> 35 -0.102477591  3.319689e-03 0.0023215371 -2.19576920 0.028108467    0.032
#> 36  0.015833610  5.833066e-03 0.0044998301  0.14908213 0.881488830    0.904
#> 37  0.561451620 -3.293733e-02 0.1401182141  1.58790102 0.112308723    0.132
#> 38 -0.027912803  8.080371e-03 0.0239652193 -0.23250347 0.816146989    0.812
#> 39  0.112523886 -9.634936e-05 0.0059032024  1.46579283 0.142704734    0.160
#> 40 -0.083982431 -1.748769e-02 0.1475651711 -0.17309933 0.862573349    0.860
#> 41  0.285172503  3.819343e-03 0.0566859781  1.18171834 0.237317480    0.268
#> 42 -0.049545329  2.806279e-03 0.0021776064 -1.12186434 0.261920126    0.256
#> 43 -0.016027706  7.243099e-03 0.0145919424 -0.19264372 0.847238006    0.832
#> 44  0.840170458 -5.958729e-02 0.6900004221  1.08318071 0.278728220    0.260
#> 45 -0.162657608 -3.788817e-03 0.0300788297 -0.91602668 0.359652902    0.344
#> 46  1.616626620 -3.778179e-02 0.5639722036  2.20299687 0.027594969    0.020
#> 47  0.401435540 -4.102856e-03 0.2419565721  0.82444800 0.409685052    0.376
#> 48  0.872608337  3.462221e-02 0.5563742607  1.12344888 0.261246900    0.248
#> 49 -0.471949223  6.970301e-03 0.0586279356 -1.97792692 0.047936952    0.076
#> 50  0.265466518  6.780869e-03 0.1311468479  0.71432098 0.475028719    0.484
#> 51  1.441699896 -2.394203e-02 0.2704078256  2.81850139 0.004824840    0.008
#> 52 -0.383762557 -3.207865e-02 0.2177313772 -0.75368867 0.451036188    0.496
#> 53  0.392685859  1.088614e-02 0.0754285564  1.39016934 0.164477460    0.176
#> 54  0.319995929 -3.632002e-03 0.0609279621  1.31110559 0.189822091    0.204
#> 55  0.069614844  3.268483e-02 0.5902451531  0.04806879 0.961661417    0.956
#> 56  0.410365993  5.700990e-04 0.0902211610  1.36431105 0.172469689    0.184
#> 57  0.988650255 -3.674872e-02 0.2696658186  1.97460362 0.048313148    0.060
#> 58  0.205022085 -2.693496e-02 0.1888435834  0.53377281 0.593498728    0.652
#> 59  0.723039856 -3.483876e-02 0.2374919410  1.55516049 0.119907851    0.140
#> 60  0.608607689 -9.919500e-03 0.0955521665  2.00096054 0.045396642    0.060
#> 61 -0.179708687  6.433892e-04 0.0231366546 -1.18568920 0.235745060    0.268
#> 62  0.232341838 -1.442439e-02 0.0693710966  0.93690689 0.348806452    0.340
#> 63  0.287892902 -5.473042e-02 0.3851411239  0.55208631 0.580889223    0.588
#> 64  2.274753084  1.973812e-02 1.5288044677  1.82378423 0.068184699    0.080
#> 65  0.539130024  3.135126e-03 0.2838978652  1.00595741 0.314436060    0.324
#> 66  1.051798136 -1.983423e-02 1.4680454457  0.88445567 0.376450281    0.400
#> 67 -0.131392170 -6.809601e-05 0.0069111662 -1.57967840 0.114180535    0.132
#> 68  0.138190992 -3.307947e-03 0.0211186746  0.97368807 0.330211440    0.348
#> 69  1.046159211 -3.441684e-04 0.1834975449  2.44301147 0.014565274    0.020
#> 70  1.253265453 -2.721616e-02 0.6382018890  1.60285525 0.108966615    0.116
#> 71  0.299578515 -6.403811e-03 0.3395420152  0.52510914 0.599507320    0.636
#> 72 -0.169627902 -1.784115e-03 0.0147751474 -1.38082729 0.167332072    0.176
#> 73 -0.042159110  4.179474e-03 0.0108046777 -0.44579671 0.655744102    0.652
#> 74  0.036829436 -2.163083e-02 0.1752899102  0.13963116 0.888951421    0.896
#> 75 -0.055736811  6.376289e-03 0.0084513592 -0.67564711 0.499264726    0.488
#> 76  1.180394095 -1.234250e-01 0.6851030626  1.57521329 0.115207221    0.124
#> 77  0.782521216 -1.014849e-02 0.1789624563  1.87374849 0.060965099    0.072
#> 78  0.529916604 -1.713127e-02 0.0909580577  1.81386411 0.069698649    0.064
#> 79  0.861411539 -7.327856e-03 0.2994813005  1.58746683 0.112406953    0.112
#> 80  0.895182573  5.938533e-03 0.1339875850  2.42934214 0.015126250    0.012
#> 81  0.025800751 -1.120000e-03 0.0038851969  0.43189745 0.665815949    0.680
#> 82 -0.330384096 -2.821850e-02 0.0804457570 -1.06535279 0.286716313    0.308
#> 83 -0.310472511 -1.871905e-02 0.0457980957 -1.36330276 0.172787112    0.184
#> 84  0.001292507  2.005703e-03 0.0041847559 -0.01102489 0.991203592    0.980
#> 85 -0.126709946 -1.991933e-03 0.0151911150 -1.01189245 0.311589477    0.344
#>    p_folded_sim      skewness     kurtosis      mean    median     pysal
#> 1         0.182  0.0193411097 -0.122208273 High-High High-High High-High
#> 2         0.012 -0.0109319199 -0.289870122 High-High High-High High-High
#> 3         0.018  0.1788234657  0.170805358 High-High High-High High-High
#> 4         0.066 -0.0334039216 -0.354790183   Low-Low   Low-Low   Low-Low
#> 5         0.162 -0.0750309081 -0.446044093   Low-Low   Low-Low   Low-Low
#> 6         0.062 -0.0572211136 -0.005831860   Low-Low   Low-Low   Low-Low
#> 7         0.286 -0.0321503166 -0.242719732 High-High High-High High-High
#> 8         0.030 -0.1977298559 -0.210516409   Low-Low   Low-Low   Low-Low
#> 9         0.180 -0.1255602015 -0.166912953  Low-High High-High  Low-High
#> 10        0.002 -0.2465115875 -0.269237208   Low-Low   Low-Low   Low-Low
#> 11        0.004 -0.0339426945 -0.120468264   Low-Low   Low-Low   Low-Low
#> 12        0.040 -0.1289448703 -0.022779065   Low-Low   Low-Low   Low-Low
#> 13        0.116  0.0466240649 -0.180117292  Low-High  Low-High  Low-High
#> 14        0.002 -0.1206006615 -0.163007777   Low-Low   Low-Low   Low-Low
#> 15        0.314  0.3562752322  0.092094287  High-Low  High-Low  High-Low
#> 16        0.284 -0.1305508726 -0.042850370  Low-High  Low-High  Low-High
#> 17        0.014  0.1875452282 -0.273600059 High-High High-High High-High
#> 18        0.386 -0.1364147024 -0.049624248   Low-Low   Low-Low   Low-Low
#> 19        0.072  0.0508943338 -0.081149120 High-High High-High High-High
#> 20        0.088  0.2492454690 -0.213807480 High-High High-High High-High
#> 21        0.268  0.0861600399 -0.087862674 High-High High-High High-High
#> 22        0.282  0.1674452740  0.101630820  High-Low  High-Low  High-Low
#> 23        0.222 -0.1839227891 -0.476547087  Low-High  Low-High  Low-High
#> 24        0.044 -0.0146318039 -0.339914975   Low-Low   Low-Low   Low-Low
#> 25        0.370 -0.0794943861 -0.148973784  Low-High   Low-Low  Low-High
#> 26        0.312 -0.0616924547 -0.127974827 High-High  High-Low High-High
#> 27        0.126  0.0205282528 -0.516424446 High-High High-High High-High
#> 28        0.002 -0.0486417067 -0.289049667   Low-Low   Low-Low   Low-Low
#> 29        0.020  0.1026226970 -0.079172296   Low-Low   Low-Low   Low-Low
#> 30        0.168 -0.1582736178 -0.204158225   Low-Low   Low-Low   Low-Low
#> 31        0.500 -0.0296021327 -0.325002266  High-Low  High-Low High-High
#> 32        0.008 -0.2214810865 -0.186253968   Low-Low   Low-Low   Low-Low
#> 33        0.014  0.1249103960  0.075422460 High-High High-High High-High
#> 34        0.286  0.0818498804 -0.148674321 High-High High-High High-High
#> 35        0.016 -0.0335252081 -0.244021412  Low-High High-High  Low-High
#> 36        0.452 -0.1383585310 -0.319218156   Low-Low   Low-Low   Low-Low
#> 37        0.066  0.1115983405  0.034577659 High-High High-High High-High
#> 38        0.406 -0.2736432359 -0.210114324  Low-High   Low-Low  Low-High
#> 39        0.080  0.1317190228 -0.054834336 High-High High-High High-High
#> 40        0.430  0.0725148780  0.001677947  High-Low  High-Low  High-Low
#> 41        0.134  0.0140163554 -0.248862041   Low-Low   Low-Low   Low-Low
#> 42        0.128 -0.0973721860 -0.138807753  Low-High High-High  Low-High
#> 43        0.416 -0.0999646102 -0.167155525   Low-Low   Low-Low  Low-High
#> 44        0.130 -0.0810902363 -0.228886822   Low-Low   Low-Low   Low-Low
#> 45        0.172 -0.0477920324 -0.069974194  High-Low  High-Low  High-Low
#> 46        0.010 -0.1098802922  0.098983650   Low-Low   Low-Low   Low-Low
#> 47        0.188 -0.0639907079 -0.074653826 High-High High-High High-High
#> 48        0.124 -0.0046427883 -0.126554331 High-High High-High High-High
#> 49        0.038 -0.1967588401  0.230963390  Low-High  Low-High  Low-High
#> 50        0.242 -0.1792651217 -0.220535593 High-High High-High High-High
#> 51        0.004 -0.0203015502  0.035842143 High-High High-High High-High
#> 52        0.248  0.0934566666 -0.394643105  High-Low  High-Low  High-Low
#> 53        0.088 -0.1176310532  0.007062574 High-High High-High High-High
#> 54        0.102  0.1954887091 -0.182899997 High-High High-High High-High
#> 55        0.478 -0.2094105712 -0.133451497   Low-Low   Low-Low   Low-Low
#> 56        0.092  0.2476141586  0.022249550 High-High High-High High-High
#> 57        0.030  0.0339922295 -0.112605585 High-High High-High High-High
#> 58        0.326 -0.0006951971 -0.039743702 High-High  High-Low High-High
#> 59        0.070  0.1980756027  0.052009622 High-High High-High High-High
#> 60        0.030  0.2516472589 -0.256408770 High-High High-High High-High
#> 61        0.134 -0.0342826249 -0.075899688  Low-High  Low-High  Low-High
#> 62        0.170 -0.1191325325 -0.085775927   Low-Low   Low-Low   Low-Low
#> 63        0.294  0.1027470474 -0.295350151   Low-Low   Low-Low   Low-Low
#> 64        0.040 -0.1696685789 -0.277198018   Low-Low   Low-Low   Low-Low
#> 65        0.162 -0.1089101500 -0.299093773   Low-Low   Low-Low   Low-Low
#> 66        0.200 -0.1528244107 -0.333491230   Low-Low   Low-Low   Low-Low
#> 67        0.066 -0.0712826286 -0.346602879  Low-High High-High  Low-High
#> 68        0.174  0.0826554906 -0.268472298 High-High High-High High-High
#> 69        0.010  0.1596515384 -0.320451331 High-High High-High High-High
#> 70        0.058  0.1702509672  0.207277379 High-High High-High High-High
#> 71        0.320 -0.1075313072 -0.316979391   Low-Low   Low-Low   Low-Low
#> 72        0.088  0.0024365251 -0.081740054  Low-High  Low-High  Low-High
#> 73        0.326  0.1346875309  0.175975727  High-Low  High-Low  High-Low
#> 74        0.448 -0.0116495866 -0.557233553   Low-Low   Low-Low   Low-Low
#> 75        0.244 -0.0961202969  0.008658497  Low-High  Low-High  Low-High
#> 76        0.062  0.1039308651 -0.178596538 High-High High-High High-High
#> 77        0.036  0.1808753991  0.056002805   Low-Low   Low-Low   Low-Low
#> 78        0.032  0.0312442426 -0.429543982   Low-Low   Low-Low   Low-Low
#> 79        0.056 -0.0585124662 -0.304859767   Low-Low   Low-Low   Low-Low
#> 80        0.006 -0.1053024760 -0.286047879   Low-Low   Low-Low   Low-Low
#> 81        0.340  0.1409353367 -0.035024224 High-High High-High High-High
#> 82        0.154 -0.0587071080 -0.201426423  Low-High  Low-High  Low-High
#> 83        0.092 -0.0581686674 -0.381502683  Low-High  Low-High  Low-High
#> 84        0.490 -0.1149523862 -0.036042936   Low-Low  High-Low   Low-Low
#> 85        0.172  0.0115709402 -0.166278839  Low-High  Low-High  Low-High

And with my original attempt.

reprex::reprex({
  library(sf)
  library(sfdep)
  library(dplyr)

  sfdep::guerry %>% 
    st_set_crs(27572) %>% 
    select(code_dept, crime_pers, crime_prop) %>% 
    mutate(nb = st_contiguity(geometry),
           wt = st_weights(nb),
           local_moran_crime_pers = local_moran(crime_pers, nb, wt, nsim = 199))
})
library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE
library(sfdep)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

sfdep::guerry %>% 
  st_set_crs(27572) %>% 
  select(code_dept, crime_pers, crime_prop) %>% 
  mutate(nb = st_contiguity(geometry),
         wt = st_weights(nb),
         local_moran_crime_pers = local_moran(crime_pers, nb, wt, nsim = 199))
#> Simple feature collection with 85 features and 6 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 47680 ymin: 1703258 xmax: 1031401 ymax: 2677441
#> Projected CRS: NTF (Paris) / Lambert zone II
#> # A tibble: 85 × 7
#>    code_dept crime_pers crime_prop                  geometry nb    wt    local…¹
#>  * <fct>          <int>      <int>        <MULTIPOLYGON [m]> <nb>  <lis>   <dbl>
#>  1 01             28870      15890 (((801150 2092615, 80066… <int> <dbl>  0.522 
#>  2 02             26226       5521 (((729326 2521619, 72932… <int> <dbl>  0.828 
#>  3 03             26747       7925 (((710830 2137350, 71174… <int> <dbl>  0.804 
#>  4 04             12935       7289 (((882701 1920024, 88240… <int> <dbl>  0.742 
#>  5 05             17488       8174 (((886504 1922890, 88573… <int> <dbl>  0.231 
#>  6 07              9474      10263 (((747008 1925789, 74663… <int> <dbl>  0.839 
#>  7 08             35203       8847 (((818893 2514767, 81861… <int> <dbl>  0.623 
#>  8 09              6173       9597 (((509103 1747787, 50882… <int> <dbl>  1.65  
#>  9 10             19602       4086 (((775400 2345600, 77506… <int> <dbl> -0.0198
#> 10 11             15647      10431 (((626230 1810121, 62626… <int> <dbl>  0.695 
#> # … with 75 more rows, 11 more variables: local_moran_crime_pers$eii <dbl>,
#> #   $var_ii <dbl>, $z_ii <dbl>, $p_ii <dbl>, $p_ii_sim <dbl>,
#> #   $p_folded_sim <dbl>, $skewness <dbl>, $kurtosis <dbl>, $mean <fct>,
#> #   $median <fct>, $pysal <fct>, and abbreviated variable name
#> #   ¹​local_moran_crime_pers$ii
JosiahParry commented 1 year ago

Wonderful! Thanks for the update!