# install and load packages. 
pacman::p_load(rgdal, raster, ggplot2, spatstat, plotrix, fields,leaflet, maptools, RColorBrewer,lattice, geoR, plotrix, car, sp, spdep, ape, pgirmess, splancs, smacpod)
# retrieve data source and administrative area map. 
eth_malaria_data <- read.csv("https://raw.githubusercontent.com/HughSt/HughSt.github.io/master/course_materials/week1/Lab_files/Data/mal_data_eth_2009_no_dups.csv",
                            header=T)

eth_adm_1 <- raster::getData("GADM", country="ETH", level=1) 
# create map with prevalence values. 
pal = colorNumeric("Oranges", eth_malaria_data$pr_pr)
leaflet(eth_malaria_data) %>% addTiles() %>% 
  addPolygons(data=eth_adm_1, color = "black", 
                        weight = 1, fillOpacity = 0.1) %>% 
  addCircleMarkers(~longitude, ~latitude, fillOpacity=.75,
   fillColor= ~pal(pf_pr), radius=~pf_pr*100, stroke=TRUE, weight=1) %>% 
  addLegend(pal = pal, values = ~pf_pr, title = "Malaria prevalence, 2009.", position = "bottomleft") %>% 
  setView(37.2193,7.7477, zoom = 5)
# explore distribution of prevalence values. 
hist(eth_malaria_data$pf_pr, xlab = "Prevalence", main = "")

# use logit transformation on distribution to support parametric statistical testing. 
eth_malaria_data$prev_adj <- eth_malaria_data$pf_pr + .001
eth_malaria_data$log_odds <- logit(eth_malaria_data$pf_pr)
## Warning in logit(eth_malaria_data$pf_pr): proportions remapped to (0.025, 0.975)
hist(eth_malaria_data$log_odds, xlab = "Log odds", main = "")

# create distance matrix from latitude and longitude values. 
eth_samp_dists <- as.matrix(dist(cbind(eth_malaria_data$longitude, eth_malaria_data$latitude)))

# verify matrix dimensions identical to lat/long vector length. 
dim(eth_samp_dists) 
## [1] 203 203
# invert matrix values to map weights related to proximity. 
samp_dists_inv <- 1/eth_samp_dists

# overwrite 0 values along the diagonal of matrix since 1/0 = inf.
diag(samp_dists_inv) <- 0  

# compute Moran's I autocorrelation coefficient of x with a logit of the prevalence data and the inversely weighted distance values.  
moran_i_one <- ape::Moran.I(eth_malaria_data$log_odds, samp_dists_inv)   
moran_i_one
## $observed
## [1] 0.1847263
## 
## $expected
## [1] -0.004950495
## 
## $sd
## [1] 0.02282913
## 
## $p.value
## [1] 0

Notice a positive value in $observed, indicating the presence of spatial autocorrelation, or spatial clustering in these data.

Measure autocorrelation at differing spatial lags:

# ascertain maximum distance between points. 
max_dist<-max(dist(cbind(eth_malaria_data$longitude, eth_malaria_data$latitude)))
max_dist
## [1] 7.957449
# bind latitude and longitude point data vector into object.
xy=cbind(eth_malaria_data$longitude, eth_malaria_data$latitude)

# create correlogram
pgi_cor <- pgirmess::correlog(coords=xy, z=eth_malaria_data$log_odds, 
                              method="Moran", nbclass=10) 
pgi_cor
## Moran I statistic 
##       dist.class        coef      p.value    n
##  [1,]  0.3979675  0.22708898 3.307121e-20 3904
##  [2,]  1.1937029  0.02187041 7.781197e-02 5192
##  [3,]  1.9894379 -0.07975471 9.999763e-01 5680
##  [4,]  2.7851728  0.01896800 1.192675e-01 4976
##  [5,]  3.5809077 -0.06374242 9.983910e-01 5078
##  [6,]  4.3766427 -0.07540964 9.999799e-01 5896
##  [7,]  5.1723776 -0.05037439 9.869326e-01 4890
##  [8,]  5.9681126 -0.01292578 6.005353e-01 3694
##  [9,]  6.7638475 -0.01687454 5.884774e-01 1412
## [10,]  7.5595825 -0.01881338 4.486059e-01  284

Check the table above to evaluate coefficients, significant p-value of coefficients, and bin values. With a global approach to the data points together, we see that one distance class is returns as statistically significant for positive autocorrelation.

# plot correlogram
plot(pgi_cor)

We see evidence of positive sptial autocorrelation (clustering) in the correlogram above. One statistically significant coefficient is visible here within the first distance class, at about .4 decimal degrees of distance, approximately 44km. At other distances, Moran’s I value decreases sharply, as well as becoming fairly indistinguishable from a null hypothesis indicating spatial randomness.

Compare correlogram to variogram measurements:

# create geo file object
eth_malaria_data_geo<-as.geodata(eth_malaria_data[,c("longitude","latitude","log_odds")])

# binned variogram with max distance
vario<-variog(eth_malaria_data_geo,max.dist=7.96,
              uvec=seq(0.4121237,7.1595572,l=10))
## variog: computing omnidirectional variogram
# check bin width of variogram. 
min(vario$n) > 30
## [1] TRUE
# plot variogram.
par(mfrow=c(1,1))
plot(vario)

Plotting a variogram with the same data reveals an inverse sort of distribution to the correlogram

Calculate Moran’s I with a binary distance matrix:

# create spatial object with xy coordinate object.
coords <-raster::coordinates(xy)
id_s <- row.names(as.data.frame(coords))

# create matrix of points with "k=1" number of nearest neighbors to return. 
neigh_nb<- spdep::knn2nb(knearneigh(coords, k=1, 
                                    longlat = TRUE), 
                                    row.names=id_s)  

# assign one neighbor to each and calculate distance. 
dsts <- unlist(spdep::nbdists(neigh_nb,coords)) 
# maximum distance to assign K =1 neighbors.
max_one_nn <- max(dsts)
max_one_nn
## [1] 0.6581074

Here we create different neighbor structures through toggling parameters in a dnearneigh() function.

# shift neighbor structure by extending the upper distance bound by a factor of 2.
neigh_kd_one <-dnearneigh(coords,d1=0, d2=max_one_nn, row.names=id_s) 
neigh_kd_two <-dnearneigh(coords,d1=0, d2=max_one_nn * 2, row.names=id_s) 

# verify reciprocal pathways between all points connected.
nb_one<-list(d1=neigh_kd_one, d2=neigh_kd_two) 
sapply(nb_one, function(x) spdep::is.symmetric.nb(x, verbose=F, force=T))
##   d1   d2 
## TRUE TRUE
# plot both the neigh_kd_one and neigh_kd_two structures.
par(mfrow=c(2,1), mar= c(1, 0, 1, 0))

plot(xy, pch=16)
plot(neigh_kd_one, coords, col="green",add=T)

plot(xy, pch=16)
plot(neigh_kd_two, coords,col="green", add=T)

Assign weights to the differing neighbor structures and compute a Moran Test on both:

# assign weights to the first neighbor structure.
weights_nnb_one <- spdep::nb2listw(neigh_kd_one, style="W")   
weights_nnb_one 
## Characteristics of weights list object:
## Neighbour list object:
## Number of regions: 203 
## Number of nonzero links: 2956 
## Percentage nonzero weights: 7.17319 
## Average number of links: 14.56158 
## 
## Weights style: W 
## Weights constants summary:
##     n    nn  S0       S1       S2
## W 203 41209 203 46.03848 823.6546
# assign weights to the second neighbor structure.
weights_nnb_two <- spdep::nb2listw(neigh_kd_two, style="W")   
weights_nnb_two 
## Characteristics of weights list object:
## Neighbour list object:
## Number of regions: 203 
## Number of nonzero links: 7264 
## Percentage nonzero weights: 17.62722 
## Average number of links: 35.78325 
## 
## Weights style: W 
## Weights constants summary:
##     n    nn  S0       S1       S2
## W 203 41209 203 13.41184 820.1892
# compare Moran's I returns on both neighbor structures. 
moran_test_weights_one <- spdep::moran.test(eth_malaria_data$log_odds , listw=weights_nnb_one)

moran_test_weights_two <- spdep::moran.test(eth_malaria_data$log_odds , listw=weights_nnb_two)

moran_test_weights_one
## 
##  Moran I test under randomisation
## 
## data:  eth_malaria_data$log_odds  
## weights: weights_nnb_one    
## 
## Moran I statistic standard deviate = 8.8841, p-value < 2.2e-16
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic       Expectation          Variance 
##      0.2709168871     -0.0049504950      0.0009642047
moran_test_weights_two
## 
##  Moran I test under randomisation
## 
## data:  eth_malaria_data$log_odds  
## weights: weights_nnb_two    
## 
## Moran I statistic standard deviate = 11.237, p-value < 2.2e-16
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic       Expectation          Variance 
##      0.1724323990     -0.0049504950      0.0002491987

The first, less densly connected neighborhood structure yeilds a stronger coefficient for spatial autocorrelation than the second. Both measures can be considered statistically significant, though the first structure demonstrates less in the way of standard deviation.

Exploring a Local Indicator of Spatial Autocorrelation (LISA):

# functiion to return local Moran's I. 
local_i <- spdep::localmoran(eth_malaria_data$log_odds, weights_nnb_two)     

# verbose return of local Moran's I for each point.
coef <- printCoefmat(data.frame(local_i[id_s,], row.names=row.names(coords), check.names=FALSE))
##                Ii       E.Ii     Var.Ii    Z.Ii Pr(z > 0)    
##   [1,] -0.0299369 -0.0049505  0.0351739 -0.1332  0.552993    
##   [2,]  0.0339043 -0.0049505  0.0489775  0.1756  0.430316    
##   [3,]  0.0741931 -0.0049505  0.0369516  0.4117  0.340273    
##   [4,] -0.0262312 -0.0049505  0.0369516 -0.1107  0.544075    
##   [5,] -0.2164017 -0.0049505  0.0522847 -0.9247  0.822451    
##   [6,] -0.0529488 -0.0049505  0.0489775 -0.2169  0.585851    
##   [7,]  0.0548720 -0.0049505  0.0522847  0.2616  0.396806    
##   [8,]  0.0559470 -0.0049505  0.0778397  0.2183  0.413608    
##   [9,]  0.0374661 -0.0049505  0.0388987  0.2151  0.414859    
##  [10,]  0.0559470 -0.0049505  0.0778397  0.2183  0.413608    
##  [11,] -0.0703870 -0.0049505  0.1085058 -0.1987  0.578733    
##  [12,] -0.0248379 -0.0049505  0.0335443 -0.1086  0.543234    
##  [13,]  0.2553825 -0.0049505  0.0710250  0.9768  0.164324    
##  [14,] -0.0924528 -0.0049505  0.0778397 -0.3136  0.623099    
##  [15,]  0.0352637 -0.0049505  0.0603162  0.1637  0.434967    
##  [16,]  0.0523675 -0.0049505  0.0560327  0.2421  0.404335    
##  [17,] -0.1213995 -0.0049505  0.0320451 -0.6505  0.742319    
##  [18,]  0.0523675 -0.0049505  0.0560327  0.2421  0.404335    
##  [19,] -0.1428681 -0.0049505  0.0388987 -0.6993  0.757812    
##  [20,] -0.0299369 -0.0049505  0.0351739 -0.1332  0.552993    
##  [21,]  0.0522978 -0.0049505  0.0860173  0.1952  0.422620    
##  [22,]  0.3301325 -0.0049505  0.0320451  1.8719  0.030613 *  
##  [23,] -0.1128293 -0.0049505  0.0293798 -0.6294  0.735449    
##  [24,]  0.7881091 -0.0049505  0.0293798  4.6268 1.857e-06 ***
##  [25,] -0.1312758 -0.0049505  0.0351739 -0.6736  0.749706    
##  [26,] -0.0080840 -0.0049505  0.0281899 -0.0187  0.507445    
##  [27,]  0.0548720 -0.0049505  0.0522847  0.2616  0.396806    
##  [28,]  1.5896611 -0.0049505  0.0522847  6.9738 1.543e-12 ***
##  [29,] -0.1216679 -0.0049505  0.0335443 -0.6373  0.738027    
##  [30,]  0.0333360 -0.0049505  0.0489775  0.1730  0.431326    
##  [31,] -0.0246972 -0.0049505  0.0335443 -0.1078  0.542929    
##  [32,] -0.2164017 -0.0049505  0.0522847 -0.9247  0.822451    
##  [33,] -0.0722409 -0.0049505  0.0560327 -0.2843  0.611899    
##  [34,] -0.0482933 -0.0049505  0.0410404 -0.2139  0.584707    
##  [35,]  0.0252770 -0.0049505  0.0351739  0.1612  0.435979    
##  [36,]  0.4725888 -0.0049505  0.0320451  2.6676  0.003819 ** 
##  [37,]  0.0656782 -0.0049505  0.0560327  0.2984  0.382709    
##  [38,]  0.0241953 -0.0049505  0.0351739  0.1554  0.438251    
##  [39,]  0.0771803 -0.0049505  0.0148039  0.6750  0.249831    
##  [40,]  0.0771803 -0.0049505  0.0148039  0.6750  0.249831    
##  [41,] -0.2441972 -0.0049505  0.0217646 -1.6217  0.947566    
##  [42,]  0.0771803 -0.0049505  0.0148039  0.6750  0.249831    
##  [43,]  0.0774917 -0.0049505  0.0144215  0.6865  0.246197    
##  [44,]  0.0774917 -0.0049505  0.0144215  0.6865  0.246197    
##  [45,]  0.0702445 -0.0049505  0.0233223  0.4924  0.311224    
##  [46,]  0.0777907 -0.0049505  0.0140543  0.6979  0.242608    
##  [47,]  0.0777907 -0.0049505  0.0140543  0.6979  0.242608    
##  [48,]  0.0777907 -0.0049505  0.0140543  0.6979  0.242608    
##  [49,]  0.0924392 -0.0049505  0.0180035  0.7258  0.233972    
##  [50,]  0.0924392 -0.0049505  0.0174811  0.7366  0.230685    
##  [51,]  0.0777907 -0.0049505  0.0140543  0.6979  0.242608    
##  [52,]  0.0777907 -0.0049505  0.0140543  0.6979  0.242608    
##  [53,]  0.0741285 -0.0049505  0.0185520  0.5806  0.280760    
##  [54,]  0.0731648 -0.0049505  0.0197356  0.5560  0.289090    
##  [55,]  0.0777907 -0.0049505  0.0140543  0.6979  0.242608    
##  [56,] -0.2422340 -0.0049505  0.0210507 -1.6354  0.949021    
##  [57,]  0.0777907 -0.0049505  0.0140543  0.6979  0.242608    
##  [58,]  0.0765169 -0.0049505  0.0156187  0.6519  0.257242    
##  [59,]  0.0924392 -0.0049505  0.0174811  0.7366  0.230685    
##  [60,]  0.0786198 -0.0049505  0.0130360  0.7319  0.232100    
##  [61,]  0.0788757 -0.0049505  0.0127217  0.7432  0.228679    
##  [62,]  0.0786198 -0.0049505  0.0130360  0.7319  0.232100    
##  [63,]  0.0786198 -0.0049505  0.0130360  0.7319  0.232100    
##  [64,]  0.0765169 -0.0049505  0.0156187  0.6519  0.257242    
##  [65,]  0.0791224 -0.0049505  0.0124188  0.7544  0.225297    
##  [66,]  0.0774917 -0.0049505  0.0144215  0.6865  0.246197    
##  [67,]  0.0924392 -0.0049505  0.0144215  0.8110  0.208690    
##  [68,]  0.0924392 -0.0049505  0.0137015  0.8320  0.202702    
##  [69,]  0.0795896 -0.0049505  0.0118449  0.7768  0.218645    
##  [70,]  0.0795896 -0.0049505  0.0118449  0.7768  0.218645    
##  [71,]  0.0924392 -0.0049505  0.0130360  0.8530  0.196834    
##  [72,]  0.0924392 -0.0049505  0.0121267  0.8844  0.188244    
##  [73,]  0.0924392 -0.0049505  0.0133623  0.8425  0.199753    
##  [74,]  0.0924392 -0.0049505  0.0133623  0.8425  0.199753    
##  [75,]  0.0806259 -0.0049505  0.0105722  0.8323  0.202625    
##  [76,]  0.0809850 -0.0049505  0.0118449  0.7896  0.214881    
##  [77,]  0.0809950 -0.0049505  0.0101188  0.8544  0.196443    
##  [78,]  0.0864595 -0.0049505  0.0108101  0.8792  0.189651    
##  [79,]  0.0760255 -0.0049505  0.0105722  0.7875  0.215483    
##  [80,]  0.0808134 -0.0049505  0.0103419  0.8433  0.199519    
##  [81,]  0.0806727 -0.0049505  0.0105722  0.8327  0.202496    
##  [82,]  0.0755594 -0.0049505  0.0099026  0.8090  0.209243    
##  [83,]  0.0796406 -0.0049505  0.0118449  0.7772  0.218507    
##  [84,]  0.0034245 -0.0049505  0.0096929  0.0851  0.466104    
##  [85,] -0.2233089 -0.0049505  0.0241741 -1.4044  0.919902    
##  [86,]  0.0924392 -0.0049505  0.0410404  0.4807  0.315352    
##  [87,] -0.0992319 -0.0049505  0.0115728 -0.8764  0.809596    
##  [88,] -0.0959832 -0.0049505  0.0113099 -0.8560  0.803998    
##  [89,]  0.0560601 -0.0049505  0.0197356  0.4343  0.332039    
##  [90,] -0.0028139 -0.0049505  0.0118449  0.0196  0.492169    
##  [91,] -0.1660939 -0.0049505  0.0169830 -1.2365  0.891869    
##  [92,]  0.0696417 -0.0049505  0.0241741  0.4798  0.315701    
##  [93,]  0.0654199 -0.0049505  0.0293798  0.4105  0.340701    
##  [94,] -0.2163642 -0.0049505  0.0210507 -1.4571  0.927460    
##  [95,]  0.0654199 -0.0049505  0.0293798  0.4105  0.340701    
##  [96,] -0.2251872 -0.0049505  0.0217646 -1.4928  0.932261    
##  [97,] -0.2661712 -0.0049505  0.0250809 -1.6494  0.950471    
##  [98,]  0.0761631 -0.0049505  0.0160533  0.6402  0.261023    
##  [99,]  0.0708972 -0.0049505  0.0225205  0.5054  0.306632    
## [100,]  0.0839998 -0.0049505  0.0160533  0.7020  0.241325    
## [101,]  0.0838080 -0.0049505  0.0165076  0.6908  0.244838    
## [102,]  0.0839998 -0.0049505  0.0160533  0.7020  0.241325    
## [103,]  0.0838080 -0.0049505  0.0165076  0.6908  0.244838    
## [104,]  0.0834717 -0.0049505  0.0180035  0.6590  0.254949    
## [105,]  0.0777907 -0.0049505  0.0140543  0.6979  0.242608    
## [106,]  0.0726439 -0.0049505  0.0203754  0.5436  0.293359    
## [107,] -0.1094842 -0.0049505  0.0118449 -0.9605  0.831594    
## [108,] -0.2080182 -0.0049505  0.0203754 -1.4226  0.922576    
## [109,] -0.1882836 -0.0049505  0.0180035 -1.3664  0.914086    
## [110,] -0.2444373 -0.0049505  0.0233223 -1.5682  0.941581    
## [111,] -0.1332399 -0.0049505  0.0137015 -1.0960  0.863458    
## [112,] -0.1332399 -0.0049505  0.0137015 -1.0960  0.863458    
## [113,]  0.0251242 -0.0049505  0.0210507  0.2073  0.417894    
## [114,]  3.1333457 -0.0049505  0.0306612 17.9225 < 2.2e-16 ***
## [115,] -0.2912152 -0.0049505  0.0260481 -1.7737  0.961943    
## [116,]  0.0116612 -0.0049505  0.0260481  0.1029  0.459011    
## [117,]  3.4720552 -0.0049505  0.0281899 20.7090 < 2.2e-16 ***
## [118,]  0.0190047 -0.0049505  0.0233223  0.1569  0.437677    
## [119,]  1.2679721 -0.0049505  0.0210507  8.7734 < 2.2e-16 ***
## [120,]  3.1623196 -0.0049505  0.0260481 19.6244 < 2.2e-16 ***
## [121,]  0.5093979 -0.0049505  0.0174811  3.8902 5.008e-05 ***
## [122,]  0.0663849 -0.0049505  0.0603162  0.2905  0.385732    
## [123,]  2.1359869 -0.0049505  0.0250809 13.5186 < 2.2e-16 ***
## [124,]  2.8603588 -0.0049505  0.0241741 18.4288 < 2.2e-16 ***
## [125,]  4.8339350 -0.0049505  0.0250809 30.5544 < 2.2e-16 ***
## [126,]  2.3864468 -0.0049505  0.0233223 15.6591 < 2.2e-16 ***
## [127,] -0.1771017 -0.0049505  0.0225205 -1.1472  0.874340    
## [128,]  0.5916841 -0.0049505  0.0197356  4.2470 1.083e-05 ***
## [129,] -0.2104459 -0.0049505  0.0197356 -1.4628  0.928235    
## [130,]  1.9090316 -0.0049505  0.0522847  8.3705 < 2.2e-16 ***
## [131,]  0.0681219 -0.0049505  0.0560327  0.3087  0.378776    
## [132,]  1.1186638 -0.0049505  0.0191286  8.1241 2.253e-16 ***
## [133,] -0.2143493 -0.0049505  0.0225205 -1.3954  0.918546    
## [134,]  0.6054720 -0.0049505  0.0210507  4.2072 1.293e-05 ***
## [135,] -0.1303095 -0.0049505  0.0335443 -0.6845  0.753157    
## [136,] -0.1213995 -0.0049505  0.0320451 -0.6505  0.742319    
## [137,] -0.1212299 -0.0049505  0.0320451 -0.6496  0.742013    
## [138,] -0.1213995 -0.0049505  0.0320451 -0.6505  0.742319    
## [139,] -0.2333390 -0.0049505  0.0410404 -1.1274  0.870208    
## [140,] -0.1700051 -0.0049505  0.0434076 -0.7922  0.785883    
## [141,] -0.1568829 -0.0049505  0.0410404 -0.7500  0.773364    
## [142,]  0.0149900 -0.0049505  0.0434076  0.0957  0.461876    
## [143,]  0.0106873 -0.0049505  0.0460379  0.0729  0.470950    
## [144,]  0.0924392 -0.0049505  0.0603162  0.3965  0.345850    
## [145,]  0.0706220 -0.0049505  0.0460379  0.3522  0.362339    
## [146,]  0.0490670 -0.0049505  0.0335443  0.2949  0.384022    
## [147,]  0.0490670 -0.0049505  0.0335443  0.2949  0.384022    
## [148,]  0.0490670 -0.0049505  0.0335443  0.2949  0.384022    
## [149,]  0.0508019 -0.0049505  0.0320451  0.3114  0.377731    
## [150,]  0.0753649 -0.0049505  0.0351739  0.4282  0.334238    
## [151,]  0.0312079 -0.0049505  0.0489775  0.1634  0.435108    
## [152,]  0.0376533 -0.0049505  0.0434076  0.2045  0.418987    
## [153,]  0.0428710 -0.0049505  0.0388987  0.2425  0.404209    
## [154,]  0.0728037 -0.0049505  0.0410404  0.3838  0.350559    
## [155,]  0.0924392 -0.0049505  0.0306612  0.5562  0.289042    
## [156,]  0.0924392 -0.0049505  0.0369516  0.5066  0.306205    
## [157,]  0.0711956 -0.0049505  0.0281899  0.4535  0.325085    
## [158,]  0.0924392 -0.0049505  0.0369516  0.5066  0.306205    
## [159,]  0.0924392 -0.0049505  0.0281899  0.5801  0.280940    
## [160,]  0.0861606 -0.0049505  0.0250809  0.5753  0.282542    
## [161,]  0.0805713 -0.0049505  0.0241741  0.5500  0.291143    
## [162,]  0.0831765 -0.0049505  0.0180035  0.6568  0.255656    
## [163,]  0.0833970 -0.0049505  0.0174811  0.6682  0.252001    
## [164,]  0.0836073 -0.0049505  0.0169830  0.6795  0.248396    
## [165,]  0.0838080 -0.0049505  0.0165076  0.6908  0.244838    
## [166,]  0.0838080 -0.0049505  0.0165076  0.6908  0.244838    
## [167,]  0.0839998 -0.0049505  0.0160533  0.7020  0.241325    
## [168,]  0.0841833 -0.0049505  0.0156187  0.7132  0.237857    
## [169,]  0.0833970 -0.0049505  0.0174811  0.6682  0.252001    
## [170,]  0.0836073 -0.0049505  0.0169830  0.6795  0.248396    
## [171,]  0.0833970 -0.0049505  0.0174811  0.6682  0.252001    
## [172,]  0.0833970 -0.0049505  0.0174811  0.6682  0.252001    
## [173,]  0.0836073 -0.0049505  0.0169830  0.6795  0.248396    
## [174,]  0.0836073 -0.0049505  0.0169830  0.6795  0.248396    
## [175,]  0.0833970 -0.0049505  0.0174811  0.6682  0.252001    
## [176,]  0.0836073 -0.0049505  0.0169830  0.6795  0.248396    
## [177,]  0.0838080 -0.0049505  0.0165076  0.6908  0.244838    
## [178,] -0.2873323 -0.0049505  0.0169830 -2.1669  0.984877    
## [179,]  0.0839998 -0.0049505  0.0160533  0.7020  0.241325    
## [180,]  0.0839998 -0.0049505  0.0160533  0.7020  0.241325    
## [181,]  0.0839998 -0.0049505  0.0160533  0.7020  0.241325    
## [182,]  0.0839998 -0.0049505  0.0160533  0.7020  0.241325    
## [183,]  0.0839998 -0.0049505  0.0160533  0.7020  0.241325    
## [184,]  0.0839998 -0.0049505  0.0160533  0.7020  0.241325    
## [185,]  0.0839998 -0.0049505  0.0160533  0.7020  0.241325    
## [186,]  0.0839998 -0.0049505  0.0160533  0.7020  0.241325    
## [187,]  0.0839998 -0.0049505  0.0160533  0.7020  0.241325    
## [188,]  0.0836073 -0.0049505  0.0169830  0.6795  0.248396    
## [189,]  0.0839998 -0.0049505  0.0160533  0.7020  0.241325    
## [190,]  0.0839998 -0.0049505  0.0160533  0.7020  0.241325    
## [191,]  0.0839998 -0.0049505  0.0160533  0.7020  0.241325    
## [192,]  0.0838080 -0.0049505  0.0165076  0.6908  0.244838    
## [193,]  0.0836073 -0.0049505  0.0169830  0.6795  0.248396    
## [194,]  0.0836073 -0.0049505  0.0169830  0.6795  0.248396    
## [195,]  0.0833970 -0.0049505  0.0174811  0.6682  0.252001    
## [196,]  0.0827015 -0.0049505  0.0191286  0.6338  0.263121    
## [197,]  0.0831765 -0.0049505  0.0180035  0.6568  0.255656    
## [198,]  0.0829449 -0.0049505  0.0185520  0.6453  0.259362    
## [199,]  0.0801885 -0.0049505  0.0250809  0.5376  0.295428    
## [200,]  0.0797801 -0.0049505  0.0260481  0.5250  0.299795    
## [201,]  0.0924392 -0.0049505  0.0281899  0.5801  0.280940    
## [202,]  0.0924392 -0.0049505  0.0281899  0.5801  0.280940    
## [203,]  0.0924392 -0.0049505  0.0351739  0.5193  0.301782    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# plot to visualize distribution of points in reference to local moran calculation with diamond shapes indicating outliers.
nci <- spdep::moran.plot(eth_malaria_data$log_odds, listw=weights_nnb_two, 
          xlab="Log prevalence", ylab="Spatially lagged log prev", labels=T, pch=16, col="grey")
text(c(3,3, -5,-5),c(0.9, -1.9,0.9,-1.9), c("High-High", "High-Low", "Low-High", "Low-Low"), cex=0.8)

# identify, count outliers landing 2 sigmas away from origin. 
infl<-nci$is_inf==T 
sum(infl==T) 
## [1] 23
# create vector of logged prevalance values.
x <-eth_malaria_data$log_odds

# converting values to classes.
lhx <-raster::cut(x, breaks=c(min(x), mean(x), max(x)), labels=c("L", "H"), include.lowest=T)
wx <-stats::lag(weights_nnb_two, eth_malaria_data$log_odds)
lhwx <- raster::cut(wx, breaks=c(min(wx), mean(wx), max(wx)), labels=c("L", "H"), include.lowest=T)

# compute interaction of factors.
lhlh <- interaction(lhx,lhwx,infl,drop=T)

# assign names to bolean returns of class measurment. 
names <- rep("none", length(lhlh))
names[lhlh=="L.L.TRUE"]<-"Low with Low"
names[lhlh=="H.L.TRUE"]<-"High with Low"
names[lhlh=="L.H.TRUE"]<-"Low with High"
names[lhlh=="H.H.TRUE"]<-"High with High"
# create data frame binding names to class values.
eth_malaria_local_m <- as.data.frame(cbind(xy,names))
colnames(eth_malaria_local_m)<-c("longitude", "latitude", "names")

# map longitude and latitude coordinates on to newly generated data.
eth_malaria_local_m[c("longitude", "latitude")] <- 
  lapply(eth_malaria_local_m[c("longitude", "latitude")], function(x) as.numeric(as.character(x)) )
# palette.
factpal <- colorFactor(c( "cyan4","coral4","coral","cyan","lightgrey"), names)

# Map local Moran's I with cluster Relationship factors.
leaflet(eth_malaria_local_m) %>% addTiles() %>% 
  addCircleMarkers(~longitude, ~latitude, fillOpacity=.75, color= ~factpal(names), radius=4, stroke=TRUE, weight=1) %>% 
  addLegend(pal = factpal, values = ~names, title="Clustering Classes")

~fin