This is the chisquare function.
The inputs are "num" which is how many quadrats you want to create, "A"
is the area you want your quadrats to be,
"pts" which
is a point data set that has both classes(black and white) combined, "pts1",
which is one point data set (ie, just black) and "pts2" is the other
dataset. The input data sets must be only two columns of x and y
coordinate number.
This test is preliminary, and must be used with caution. The test disregards a lot of data. In addition, the results are highly dependent upon the number of randomly created quadrats, and the areas of those. The results of many different runs of this showed that, depending on what number you use, both dependence and independence were discovered.
chisquare _ function(num, A, pts,
pts1, pts2)
{
if(A == 0){
cat("Area must
be greater than 0")
return()}
if(num <= 20){
cat(" number
of quadrats should be greater than 20", "\n","in order to gain high enough
expected values for the", "\n","Chi_Square statistic to be reliable.")
return()}
else dist _ sqrt(A)/2
region _ sbox(pts)
corners _ list()
i _ 1
blk _ 0
wht _ 0
none _ 0
both _ 0
while(i <= num){
x _ runif(1,
min(region[,1]), max(region[,1]))
y _ runif(1,
min(region[,2]), max(region[,2]))
corners$x _
c(x - dist, x - dist, x + dist, x + dist)
corners$y _
c(y - dist, y + dist, y + dist, y - dist)
cnr.points _
as.points(corners)
quad _ sbox(cnr.points)
pips1 _ inpip(pts1,
quad)
pips2 _ inpip(pts2,
quad)
if(( length(pips1) > 0 ) && ( length(pips2) > 0 ))
both _ both + 1
if(( length(pips1) > 0 ) && ( length(pips2) == 0 ))
blk _ blk + 1
if(( length(pips1) == 0 ) && ( length(pips2) > 0 ))
wht _ wht + 1
if(( length(pips1) == 0 ) && ( length(pips2) == 0 ))
none _ none + 1
i _ i + 1
}
cat( "\n", "number
of quadrats where no crimes were observed =", none, "\n",
"number
of quadrats where both white crimes and black crimes were observed =",
both, "\n",
"number
of quadrats where only black crimes were observed =", blk, "\n",
"number
of quadrats where only white crimes were observed =", wht, "\n\n")
chi2mtx _ matrix(
, 2, 2)
{
chi2mtx[1,1] _ both
chi2mtx[1,2] _ wht
chi2mtx[2,1] _ blk
chi2mtx[2,2] _ none
}
expmtx _ matrix(
, 2, 2)
{
expmtx[1,1] _ (sum(chi2mtx[1,]) * sum(chi2mtx[,1])) / sum(chi2mtx)
expmtx[1,2] _ (sum(chi2mtx[1,]) * sum(chi2mtx[,2])) / sum(chi2mtx)
expmtx[2,1] _ (sum(chi2mtx[2,]) * sum(chi2mtx[,1])) / sum(chi2mtx)
expmtx[2,2] _ (sum(chi2mtx[2,]) * sum(chi2mtx[,2])) / sum(chi2mtx)
}
X2stat _ sum((chi2mtx - expmtx)^2 / expmtx)
cat("The Chi-Square
Test Statistic =", X2stat, "\n \n")
cat("Test this
number against the Chi-Square distribution number for .05 percent with
\n one degree of freedom")
return()
}