library(deldir)
## deldir 0.1-16
set.seed(2018)
x <- runif(10, 0, 12)
y <- runif(10, 0, 6)
CEA <- runif(100, 3, 5)
cos <- rnorm(10, 2.5, 0.35)
plot(x, y, cex = cos, col='gray',pch=19)
x2 <- runif(100, 1, 12)
y2 <- runif(100, 1, 6)
points(x2, y2, pch = 19, col = 'red', 
       cex = CEA*0.3)

D <- deldir(x, y, sort = T, plotit = T, 
            digits = 4, z = cos)
## 
##      PLEASE NOTE:  The components "delsgs" and "summary" of the
##  object returned by deldir() are now DATA FRAMES rather than
##  matrices (as they were prior to release 0.0-18).
##  See help("deldir").
##  
##      PLEASE NOTE: The process that deldir() uses for determining
##  duplicated points has changed from that used in version
##  0.0-9 of this package (and previously). See help("deldir").

L <- tile.list(D)
G <- tile.centroids(L)
plot(L, close = T, 
     fillcol = gray.colors(cos),
     border = 0.2, showpoints = T, 
     number = T)
## Warning in seq.int(from = start^gamma, to = end^gamma, length.out = n):
## first element used of 'length.out' argument
points(G, pch = 20, col = 'lightblue')
points(x2, y2, pch = 19, col = 'red', 
       cex = CEA*0.2)
### Creating polygons
library(sp)

df_xy <- data.frame(x = x, y = y)
coordinates(df_xy) <- ~x+y
z <- deldir(x, y, plot = T, wl = 'triang',
            wp = 'none')

w <- tile.list(z)
Polys <- vector(mode = 'list',
                length = length(w))
for (i in seq(along = Polys)) {
  pcrds <- cbind(w[[i]]$x, w[[i]]$y)
  pcrds <- rbind(pcrds, pcrds[1,])
  Polys[[i]] <- Polygons(list(Polygon(pcrds)), 
                         ID = as.character(i))
}
SP <- SpatialPolygons(Polys)
plot(SP)
plot(df_xy, add = T, col = 'RED')

class(SP)
## [1] "SpatialPolygons"
## attr(,"package")
## [1] "sp"
### weight matrix
library(spdep)
## Loading required package: spData
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
## 
## Attaching package: 'spData'
## The following objects are masked _by_ '.GlobalEnv':
## 
##     x, y
## Loading required package: sf
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
Contnb <- poly2nb(SP, queen = F)
W = nb2listw(Contnb, glist = NULL, 
             style = 'W')
W$weights
## [[1]]
## [1] 0.25 0.25 0.25 0.25
## 
## [[2]]
## [1] 0.2 0.2 0.2 0.2 0.2
## 
## [[3]]
## [1] 0.5 0.5
## 
## [[4]]
## [1] 0.3333333 0.3333333 0.3333333
## 
## [[5]]
## [1] 0.3333333 0.3333333 0.3333333
## 
## [[6]]
## [1] 0.2 0.2 0.2 0.2 0.2
## 
## [[7]]
## [1] 0.25 0.25 0.25 0.25
## 
## [[8]]
## [1] 0.25 0.25 0.25 0.25
## 
## [[9]]
## [1] 0.5 0.5
## 
## [[10]]
## [1] 0.25 0.25 0.25 0.25
## 
## attr(,"mode")
## [1] "binary"
## attr(,"W")
## [1] TRUE
## attr(,"comp")
## attr(,"comp")$d
##  [1] 4 5 2 3 3 5 4 4 2 4
summary(W)
## Characteristics of weights list object:
## Neighbour list object:
## Number of regions: 10 
## Number of nonzero links: 36 
## Percentage nonzero weights: 36 
## Average number of links: 3.6 
## Link number distribution:
## 
## 2 3 4 5 
## 2 2 4 2 
## 2 least connected regions:
## 3 9 with 2 links
## 2 most connected regions:
## 2 6 with 5 links
## 
## Weights style: W 
## Weights constants summary:
##    n  nn S0       S1       S2
## W 10 100 10 5.838333 40.97389
class(W)
## [1] "listw" "nb"
plot(W, coordinates(SP))

dists <- as.matrix(dist(cbind(x, y)))
inv.d <- 1/dists
diag(inv.d) <- 0
View(inv.d)
sumd <- apply(inv.d, 1, sum)
w.d <- round((inv.d/sumd),2)
View(w.d)
L2 <- w.d%*%cos
cor(cos, L2)
##            [,1]
## [1,] -0.4117648
summary(w.d)
##        1               2               3               4         
##  Min.   :0.000   Min.   :0.000   Min.   :0.000   Min.   :0.0000  
##  1st Qu.:0.085   1st Qu.:0.090   1st Qu.:0.050   1st Qu.:0.0725  
##  Median :0.105   Median :0.130   Median :0.065   Median :0.0850  
##  Mean   :0.103   Mean   :0.129   Mean   :0.065   Mean   :0.1230  
##  3rd Qu.:0.120   3rd Qu.:0.155   3rd Qu.:0.070   3rd Qu.:0.1825  
##  Max.   :0.180   Max.   :0.270   Max.   :0.120   Max.   :0.3000  
##        5               6               7                8         
##  Min.   :0.000   Min.   :0.000   Min.   :0.0000   Min.   :0.0000  
##  1st Qu.:0.085   1st Qu.:0.100   1st Qu.:0.0525   1st Qu.:0.0525  
##  Median :0.110   Median :0.115   Median :0.0650   Median :0.0800  
##  Mean   :0.133   Mean   :0.125   Mean   :0.0690   Mean   :0.0990  
##  3rd Qu.:0.130   3rd Qu.:0.155   3rd Qu.:0.0800   3rd Qu.:0.1200  
##  Max.   :0.320   Max.   :0.220   Max.   :0.1500   Max.   :0.2500  
##        9              10       
##  Min.   :0.00   Min.   :0.000  
##  1st Qu.:0.03   1st Qu.:0.065  
##  Median :0.04   Median :0.085  
##  Mean   :0.04   Mean   :0.111  
##  3rd Qu.:0.04   3rd Qu.:0.155  
##  Max.   :0.09   Max.   :0.270