setwd ("D:/PASCASARJANA/Semester 3/3. SPASIAL/02. Praktikum/03")
library(spatstat)
## Warning: package 'spatstat' was built under R version 3.6.3
## Loading required package: spatstat.data
## Warning: package 'spatstat.data' was built under R version 3.6.3
## Loading required package: nlme
## Loading required package: rpart
## Warning: package 'rpart' was built under R version 3.6.3
## 
## spatstat 1.64-1       (nickname: 'Help you I can, yes!') 
## For an introduction to spatstat, type 'beginner'
## 
## Note: R version 3.6.1 (2019-07-05) is more than a year old; we strongly recommend upgrading to the latest version

Sebaran Spasial Dua Tipe Titik

Loads Ants Data Set

data(ants)
ants
## Marked planar point pattern: 97 points
## Multitype, with levels = Cataglyphis, Messor 
## window: polygonal boundary
## enclosing rectangle: [-25, 803] x [-49, 717] units (one unit = 0.5 feet)

Pada data ini dapat terlihat bahwa data ini berupa “planar point pattern” dengan 2 levels yaitu Cattaglyphis dan Messor.

plot antara Cataglysis dan Messor

plot(ants)

plot terpisah (plot Cataglyphis dan plot Messor)

plot(split(ants))

Metode Kuadran

Ingin membagi Cataglyphis dan Messor dalam kuadran dimana tidak ditentukan jumlah kuadrannya (berdasar default saja)

kuadran <- quadratcount(split(ants))
plot(kuadran)

Hipotesisnya adalah :

H0 : Titik-titik pada Himpunan PA dan PB adalah menyebar secara independen vs

H1 : Titik-titik pada Himpunan PA dan PB adalah menyebar secara tidak independen (Mereke berkorelasi secara spasial antara sesamanya)

Jika Khi-Kuadrat hitung > Khi-Kuadrat Tabel maka keputusan Tolak Ho. Artinya telah cukup bukti untuk mengatakan bahwa pada alpha 5%, sebaran spasial dari kedua spesies semut (Cattaglysis dan Messor) tidak saling bebas (mereka berkorelasi secara spasial antara sesamanya)

catag <- kuadran[[1]]
mess <- kuadran[[2]]
obs <- matrix(NA, 2, 2)
rownames(obs) <- c("A", "O")
colnames(obs) <- c("B", "0")
obs[1,1] <- sum(((catag > 0) + (mess > 0)) == 2) #ab 
obs[2,1] <- sum(((catag > 0) - (mess > 0)) == 1) #a0
obs[1,2] <- sum(((catag > 0) - (mess > 0)) == -1) #0b
obs[2,2] <- sum(((catag > 0) + (mess > 0)) == 0) #00
tes <- chisq.test(obs) 
## Warning in chisq.test(obs): Chi-squared approximation may be incorrect
chi_hitung <- sum((obs-tes$expected)^2/tes$expected)
chi_tabel <- qchisq(0.05, df=1, lower.tail = F)
p_valuechi <- pchisq(chi_hitung,1,lower.tail=F)
chisq.test(obs, correct = F)
## Warning in chisq.test(obs, correct = F): Chi-squared approximation may be
## incorrect
## 
##  Pearson's Chi-squared test
## 
## data:  obs
## X-squared = 11.2, df = 1, p-value = 0.000818

Metode K-function pada data “ants”

Kest{spatstat}

unique(ants$marks)
## [1] Messor      Cataglyphis
## Levels: Cataglyphis Messor
Kh <- Kdot(ants, "Messor")
plot(Kh)

Plot diatas menggambarkan independensi antara Messor dan spesies lainnya.

f1 <- function (X) {marks(X) == "Messor"}
f2 <- function (X) {marks(X) == "Cataglyphis"}
K <- Kmulti(ants, f1, f2)
plot(K)

Dengan Kcross, melihat antara Messor dan Cataglyphis saja

K01 <- Kcross(ants, "Messor", "Cataglyphis")
plot(K01)

Plot ants dengan menggunakan Kcross

plot(envelope(ants, Kcross,i="Messor",j="Cataglyphis",nsim=99,fix.n=T,fix.marks=T))
## Generating 99 simulations of CSR with fixed number of points of each type  ...
## 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, 98,  99.
## 
## Done.

Plot ants dengan menggunakan Kmulti

plot(envelope(ants, Kmulti,
I=f1, J=f2, nsim=99,
fix.n=T, fix.marks=T))
## Generating 99 simulations of CSR with fixed number of points of each type  ...
## 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, 98,  99.
## 
## Done.

Mad Test

mad.test(ants, Kcross, i="Messor", j="Cataglyphis", nsim=99, fix.n=T, fix.marks=T)
## Generating 99 simulations of CSR with fixed number of points of each type  ...
## 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, 98,  99.
## 
## Done.
## 
##  Maximum absolute deviation test of CSR
##  Monte Carlo test based on 99 simulations with fixed number of points
##  of each type
##  Summary function: Kcross["Messor", "Cataglyphis"](r)
##  Reference function: theoretical
##  Alternative: two.sided
##  Interval of distance values: [0, 191.5] units (one unit = 0.5 feet)
##  Test statistic: Maximum absolute deviation
##  Deviation = observed minus theoretical
## 
## data:  ants
## mad = 5161, rank = 71, p-value = 0.71

Lansing Wood Data

Here is the famous Lansing Woods dataset recording the positions of 2251 trees of 6 different species (hickories, maples, red oaks, white oaks, black oaks and miscellaneous trees).

Load the data

data(lansing)
lansing
## Marked planar point pattern: 2251 points
## Multitype, with levels = blackoak, hickory, maple, misc, redoak, whiteoak 
## window: rectangle = [0, 1] x [0, 1] units (one unit = 924 feet)
summary(lansing)
## Marked planar point pattern:  2251 points
## Average intensity 2251 points per square unit (one unit = 924 feet)
## 
## *Pattern contains duplicated points*
## 
## Coordinates are given to 3 decimal places
## i.e. rounded to the nearest multiple of 0.001 units (one unit = 924 feet)
## 
## Multitype:
##          frequency proportion intensity
## blackoak       135 0.05997335       135
## hickory        703 0.31230560       703
## maple          514 0.22834300       514
## misc           105 0.04664594       105
## redoak         346 0.15370950       346
## whiteoak       448 0.19902270       448
## 
## Window: rectangle = [0, 1] x [0, 1] units
## Window area = 1 square unit
## Unit of length: 924 feet
plot(lansing)

Ingin memisahkan plot masing-masing

plot(split(lansing))

plot(density(split(lansing)), ribbon = F)

hick <- split(lansing)$hickory
plot(hick)

Metode Kuadran

plot(quadratcount(split(lansing)))

Metode K-function

Kh_lansing <- Kdot(lansing, "hickory")
plot(Kh_lansing)