local.sp.runs.test: A function to calculate the local spatial runs tests.

Description Usage Arguments Details Value Control arguments Author(s) See Also Examples

View source: R/local.sp.runs.test.R

Description

This function calculates the local spatial runs tests for each localization.

Usage

1
2
3
local.sp.runs.test(formula = NULL, data = NULL, na.action, fx = NULL,
distr = "asymptotic",listw = listw, alternative = "two.sided" , nsim = NULL,
control = list())

Arguments

formula

An (optional) formula with the factor included in data

data

An (optional) data frame or a sf object containing the variable to testing for.

fx

An (optional) factor of observations with the same length as the neighbors list in listw

distr

a character string specifying the distribution "asymptotic" (default) or "bootstrap"

listw

A neighbours list of the class knn or nb. Alternatively a matrix W que indique el orden de cada $m_i-entorno$ (por ejemplo de inversa distancia). Para calcular el numero de rachas en cada $m_i-entorno$ debe establecerse un orden, por ejemplo del vecino más próximo al más lejano.

alternative

a character string specifying the alternative hypothesis, must be one of "two.sided" (default), "greater" or "less".

nsim

Default value is NULL to obtain the asymptotic version of the local test. To obtain the boots version the number of permutations to obtain the statistics.

Details

The object listw can be the class:

knn Un objeto tipo knn obtenida utilizando el criterio del vecino más próximo, obtenida por ejemplo con knearneigh
nb Un objeto tipo nb obtenido con spdep::poly2nb.
matrix Una matriz indicando el orden de vecindad de cada $m_i-entorno$. Por ejemplo basada en inversa de la distancia con cierto punto de corte.

Value

The output is an object of the class localsrq

local.SRQ A matrix with

runs.i número de rachas en la localización i.
E.i expectation of local local runs statistic.
Sd.i standard deviate of local runs statistic.
z.value standard value of local runs statistic (only for asymptotic version).
p.value p-value of local local runs statistic (only for asymptotic version).
zseudo.value standard value of local runs statistic (only for boots version).
pseudo.value p-value of local local runs statistic (only for boots version).

MeanNeig Mean of run.i
MaxNeig Max of run.i
listw the object listw
alternative a character string describing the alternative hypothesis

Control arguments

seedinit Numerical value for the seed in boot version. Default value seedinit = 123

Author(s)

Fernando López fernando.lopez@upct.es
Román Mínguez roman.minguez@uclm.es
Antonio Páez paezha@gmail.com
Manuel Ruiz manuel.ruiz@upct.es

@references

See Also

sp.runs.test, dgp.spq

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Case 1: Local SRQ test based on knn
rm(list = ls())
N <- 100
cx <- runif(N)
cy <- runif(N)
x <- cbind(cx,cy)
listw <- knearneigh(cbind(cx,cy), k = 10)
p <- c(1/6,3/6,2/6)
rho <- 0.5
fx <- dgp.spq(p = p, listw = listw, rho = rho)
# Asymtotic version
lsrq <- local.sp.runs.test(fx = fx, listw = listw, alternative = "less")
print(lsrq)
plot(lsrq, sig = 0.05)
# Asymtotic version
lsrq <- local.sp.runs.test(fx = fx, listw = listw, alternative = "two.sided", distr ="bootstrap", nsim = 399)
print(lsrq)
plot(lsrq, sig = 0.1)

# Case 2:Fastfood example. sf (points)
rm(list = ls())
data("FastFood")
x <- cbind(FastFood.sf$Lat,FastFood.sf$Lon)
listw <- spdep::knearneigh(x, k = 10)
formula <- ~ Type
lsrq <- local.sp.runs.test(formula = formula, data = FastFood.sf, listw = listw)
print(lsrq)
plot(lsrq, sf = FastFood.sf, sig = 0.05)

# Case 3: With a sf object (poligons)
rm(list = ls())
fname <- system.file("shape/nc.shp", package="sf")
nc <- st_read(fname)
listw <- spdep::poly2nb(as(nc,"Spatial"), queen = FALSE)
p <- c(1/6,3/6,2/6)
rho = 0.5
nc$fx <- dgp.spq(p = p, listw = listw, rho = rho)
plot(nc["fx"])
formula <- ~ fx
lsrq <- local.sp.runs.test(formula = formula, data = nc, listw = listw)
print(lsrq)
plot(lsrq, sf = nc)
# Version boot
lsrq <- local.sp.runs.test(formula = formula, data = nc, listw = listw, distr ="bootstrap", nsim = 399)
print(lsrq)
plot(lsrq, sf = nc)

# Case 4: With isolated areas
rm(list = ls())
data(Spain)
listw <- spdep::poly2nb(as(spain.sf,"Spatial"), queen = FALSE)
plot(spain.sf["MenWoman"])
formula <- ~ MenWoman
lsrq <- local.sp.runs.test(formula = formula, data = spain.sf, listw = listw)
print(lsrq)
plot(lsrq, sf = spain.sf, sig = 0.1)
# Boots Version
lsrq <- local.sp.runs.test(formula = formula, data = spain.sf, listw = listw, distr ="bootstrap", nsim = 199)
print(lsrq)
plot(lsrq, sf = spain.sf, sig = 0.10)

# Case 5: SRQ test based on a distance matrix (inverse distance)
rm(list = ls())
N <- 100
cx <- runif(N)
cy <- runif(N)
coor <- as.data.frame(cbind(cx,cy))
coor <- st_as_sf(coor,coords = c("cx","cy"))
n = dim(coor)[1]
dis <- 1/matrix(as.numeric(st_distance(coor,coor)), ncol = n, nrow = n)
diag(dis) <- 0
dis <- (dis < quantile(dis,.10))*dis
p <- c(1/6,3/6,2/6)
rho <- 0.5
fx <- dgp.spq(p = p, listw = dis, rho = rho)
lsrq <- local.sp.runs.test(fx = fx, listw = dis)
print(lsrq)
plot(lsrq, coor = cbind(cx,cy), sig = 0.05)
lsrq <- local.sp.runs.test(fx = fx, listw = dis, data = )
print(lsrq)
plot(lsrq, sf = coor)
# Version boots
lsrq <- local.sp.runs.test(fx = fx, listw = dis, data = coor, distr ="bootstrap", nsim = 299)
print(lsrq)
plot(lsrq, sf = coor)

# SRQ test based on inverse distance
rm(list = ls())
data("FastFood")
n = dim(FastFood.sf)[1]
dis <- 1000000/matrix(as.numeric(st_distance(FastFood.sf,FastFood.sf)),ncol=n,nrow=n)
diag(dis) <- 0
dis <- (dis < quantile(dis,.01))*dis
formula <- ~ Type
lsrq <- local.sp.runs.test(formula = formula, data = FastFood.sf, listw = dis)
print(lsrq)
plot(lsrq, sf = FastFood.sf)

paezha/spqdata documentation built on Dec. 22, 2021, 5:24 a.m.