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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
|
#
# Kmulti.inhom.S
#
# $Revision: 1.53 $ $Date: 2020/08/05 02:50:32 $
#
#
# ------------------------------------------------------------------------
Lcross.inhom <- function(X, i, j, ..., correction) {
if(!is.multitype(X, dfok=FALSE))
stop("Point pattern must be multitype")
if(missing(i)) i <- levels(marks(X))[1]
if(missing(j)) j <- levels(marks(X))[2]
if(missing(correction)) correction <- NULL
K <- Kcross.inhom(X, i, j, ..., correction=correction)
L <- eval.fv(sqrt(pmax.int(K,0)/pi))
iname <- make.parseable(paste(i))
jname <- make.parseable(paste(j))
# relabel the fv object
L <- rebadge.fv(L,
substitute(L[inhom,i,j](r),
list(i=iname,j=jname)),
c("L", paste0("list", paren(paste("inhom", i, j, sep=",")))),
new.yexp=substitute(L[list(inhom,i,j)](r),
list(i=iname,j=jname)))
attr(L, "labl") <- attr(K, "labl")
attr(L, "dangerous") <- attr(K, "dangerous")
return(L)
}
Ldot.inhom <- function(X, i, ..., correction) {
if(!is.multitype(X, dfok=FALSE))
stop("Point pattern must be multitype")
if(missing(i)) i <- levels(marks(X))[1]
if(missing(correction)) correction <- NULL
K <- Kdot.inhom(X, i, ..., correction=correction)
L <- eval.fv(sqrt(pmax.int(K,0)/pi))
# relabel the fv object
iname <- make.parseable(paste(i))
L <- rebadge.fv(L,
substitute(L[inhom, i ~ dot](r), list(i=iname)),
c("L", paste0("list(inhom,", iname, "~symbol(\"\\267\"))")),
new.yexp=substitute(L[list(inhom, i ~ symbol("\267"))](r),
list(i=iname)))
attr(L, "labl") <- attr(K, "labl")
attr(L, "dangerous") <- attr(K, "dangerous")
return(L)
}
"Kcross.inhom" <-
function(X, i, j, lambdaI=NULL, lambdaJ=NULL, ...,
r=NULL, breaks=NULL,
correction = c("border", "isotropic", "Ripley", "translate"),
sigma=NULL, varcov=NULL,
lambdaIJ=NULL,
lambdaX=NULL, update=TRUE, leaveoneout=TRUE)
{
verifyclass(X, "ppp")
if(!is.multitype(X, dfok=FALSE))
stop("Point pattern must be multitype")
if(missing(correction))
correction <- NULL
miss.update <- missing(update)
miss.leave <- missing(leaveoneout)
marx <- marks(X)
if(missing(i))
i <- levels(marx)[1]
if(missing(j))
j <- levels(marx)[2]
I <- (marx == i)
J <- (marx == j)
Iname <- paste("points with mark i =", i)
Jname <- paste("points with mark j =", j)
K <- Kmulti.inhom(X, I, J, lambdaI, lambdaJ, ...,
r=r,breaks=breaks,correction=correction,
sigma=sigma, varcov=varcov,
lambdaIJ=lambdaIJ, Iname=Iname, Jname=Jname,
lambdaX=lambdaX, update=update, leaveoneout=leaveoneout,
miss.update=miss.update, miss.leave=miss.leave)
iname <- make.parseable(paste(i))
jname <- make.parseable(paste(j))
result <-
rebadge.fv(K,
substitute(K[inhom,i,j](r),
list(i=iname,j=jname)),
c("K", paste0("list", paren(paste("inhom", i, j, sep=",")))),
new.yexp=substitute(K[list(inhom,i,j)](r),
list(i=iname,j=jname)))
attr(result, "dangerous") <- attr(K, "dangerous")
return(result)
}
"Kdot.inhom" <-
function(X, i, lambdaI=NULL, lambdadot=NULL, ...,
r=NULL, breaks=NULL,
correction = c("border", "isotropic", "Ripley", "translate"),
sigma=NULL, varcov=NULL,
lambdaIdot=NULL,
lambdaX=NULL, update=TRUE, leaveoneout=TRUE)
{
verifyclass(X, "ppp")
if(!is.multitype(X, dfok=FALSE))
stop("Point pattern must be multitype")
if(missing(correction))
correction <- NULL
miss.update <- missing(update)
miss.leave <- missing(leaveoneout)
marx <- marks(X)
if(missing(i))
i <- levels(marx)[1]
I <- (marx == i)
J <- rep.int(TRUE, X$n) # i.e. all points
Iname <- paste("points with mark i =", i)
Jname <- paste("points")
K <- Kmulti.inhom(X, I, J, lambdaI, lambdadot, ...,
r=r,breaks=breaks,correction=correction,
sigma=sigma, varcov=varcov,
lambdaIJ=lambdaIdot,
Iname=Iname, Jname=Jname,
lambdaX=lambdaX, update=update, leaveoneout=leaveoneout,
miss.update=miss.update, miss.leave=miss.leave)
iname <- make.parseable(paste(i))
result <-
rebadge.fv(K,
substitute(K[inhom, i ~ dot](r), list(i=iname)),
c("K", paste0("list(inhom,", iname, "~symbol(\"\\267\"))")),
new.yexp=substitute(K[list(inhom, i ~ symbol("\267"))](r),
list(i=iname)))
if(!is.null(dang <- attr(K, "dangerous"))) {
dang[dang == "lambdaJ"] <- "lambdadot"
dang[dang == "lambdaIJ"] <- "lambdaIdot"
attr(result, "dangerous") <- dang
}
return(result)
}
"Kmulti.inhom"<-
function(X, I, J, lambdaI=NULL, lambdaJ=NULL,
...,
r=NULL, breaks=NULL,
correction = c("border", "isotropic", "Ripley", "translate"),
lambdaIJ=NULL,
sigma=NULL, varcov=NULL,
lambdaX=NULL, update=TRUE, leaveoneout=TRUE)
{
verifyclass(X, "ppp")
dflt <- list(Iname="points satisfying condition I",
Jname="points satisfying condition J",
miss.update=missing(update),
miss.leave=missing(leaveoneout))
extrargs <- resolve.defaults(list(...), dflt)
if(length(extrargs) > length(dflt))
warning("Additional arguments unrecognised")
Iname <- extrargs$Iname
Jname <- extrargs$Jname
miss.update <- extrargs$miss.update
miss.leave <- extrargs$miss.leave
npts <- npoints(X)
W <- as.owin(X)
areaW <- area(W)
# validate edge correction
correction.given <- !missing(correction) && !is.null(correction)
if(is.null(correction))
correction <- c("border", "isotropic", "Ripley", "translate")
correction <- pickoption("correction", correction,
c(none="none",
border="border",
"bord.modif"="bord.modif",
isotropic="isotropic",
Ripley="isotropic",
trans="translate",
translate="translate",
translation="translate",
best="best"),
multi=TRUE)
correction <- implemented.for.K(correction, W$type, correction.given)
# validate I, J
I <- ppsubset(X, I)
J <- ppsubset(X, J)
if(is.null(I) || is.null(J))
stop("I and J must be valid subset indices")
XI <- X[I]
XJ <- X[J]
nI <- sum(I)
nJ <- sum(J)
if(nI == 0) stop(paste("There are no", Iname))
if(nJ == 0) stop(paste("There are no", Jname))
# r values
rmaxdefault <- rmax.rule("K", W, nJ/areaW)
breaks <- handle.r.b.args(r, breaks, W, rmaxdefault=rmaxdefault)
r <- breaks$r
rmax <- breaks$max
dangerous <- c("lambdaI", "lambdaJ")
dangerI <- dangerJ <- TRUE
## intensity data
if(!is.null(lambdaX)) {
## Intensity values for all points of X
if(!is.null(lambdaI))
warning("lambdaI was ignored, because lambdaX was given", call.=FALSE)
if(!is.null(lambdaJ))
warning("lambdaJ was ignored, because lambdaX was given", call.=FALSE)
if(is.im(lambdaX)) {
## Look up intensity values
lambdaI <- safelookup(lambdaX, X[I])
lambdaJ <- safelookup(lambdaX, X[J])
} else if(is.function(lambdaX)) {
## evaluate function at locations
lambdaI <- lambdaX(XI$x, XI$y)
lambdaJ <- lambdaX(XJ$x, XJ$y)
} else if(is.numeric(lambdaX) && is.vector(as.numeric(lambdaX))) {
## vector of intensity values
if(length(lambdaX) != npts)
stop(paste("The length of", sQuote("lambdaX"),
"should equal the number of points of X"))
lambdaI <- lambdaX[I]
lambdaJ <- lambdaX[J]
} else if(is.ppm(lambdaX) || is.kppm(lambdaX) || is.dppm(lambdaX)) {
## point process model provides intensity
model <- lambdaX
if(!update) {
## just use intensity of fitted model
lambdaI <- predict(model, locations=XI, type="trend")
lambdaJ <- predict(model, locations=XJ, type="trend")
} else {
## re-fit model to data X
if(is.ppm(model)) {
model <- update(model, Q=X)
lambdaX <- fitted(model, dataonly=TRUE, leaveoneout=leaveoneout)
} else {
model <- update(model, X=X)
lambdaX <- fitted(model, dataonly=TRUE, leaveoneout=leaveoneout)
}
lambdaI <- lambdaX[I]
lambdaJ <- lambdaX[J]
dangerI <- dangerJ <- FALSE
dangerous <- "lambdaIJ"
if(miss.update)
warn.once(key="Kmulti.inhom.update",
"The behaviour of Kmulti.inhom when lambda is a ppm object",
"has changed (in spatstat 1.45-3 and later).",
"See help(Kmulti.inhom)")
}
} else stop(paste("Argument lambdaX is not understood:",
"it should be a numeric vector,",
"an image, a function(x,y)",
"or a fitted point process model (ppm, kppm or dppm)"))
} else {
## lambdaI, lambdaJ expected
if(is.null(lambdaI)) {
## estimate intensity
dangerI <- FALSE
dangerous <- setdiff(dangerous, "lambdaI")
lambdaI <- density(X[I], ..., sigma=sigma, varcov=varcov,
at="points", leaveoneout=leaveoneout)
} else if(is.im(lambdaI)) {
## look up intensity values
lambdaI <- safelookup(lambdaI, X[I])
} else if(is.function(lambdaI)) {
## evaluate function at locations
lambdaI <- lambdaI(XI$x, XI$y)
} else if(is.numeric(lambdaI) && is.vector(as.numeric(lambdaI))) {
## validate intensity vector
if(length(lambdaI) != nI)
stop(paste("The length of", sQuote("lambdaI"),
"should equal the number of", Iname))
} else if(is.ppm(lambdaI) || is.kppm(lambdaI) || is.dppm(lambdaI)) {
## point process model provides intensity
model <- lambdaI
if(!update) {
## just use intensity of fitted model
lambdaI <- predict(model, locations=XI, type="trend")
} else {
## re-fit model to data X
if(is.ppm(model)) {
model <- update(model, Q=X)
lambdaX <- fitted(model, dataonly=TRUE, leaveoneout=leaveoneout)
} else {
#' kppm or dppm
model <- update(model, X=X)
lambdaX <- fitted(model, dataonly=TRUE, leaveoneout=leaveoneout)
}
lambdaI <- lambdaX[I]
dangerI <- FALSE
dangerous <- setdiff(dangerous, "lambdaI")
if(miss.update)
warn.once(key="Kmulti.inhom.update",
"The behaviour of Kmulti.inhom when lambda is a ppm object",
"has changed (in spatstat 1.45-3 and later).",
"See help(Kmulti.inhom)")
}
} else stop(paste(sQuote("lambdaI"), "should be a vector or an image"))
if(is.null(lambdaJ)) {
## estimate intensity
dangerJ <- FALSE
dangerous <- setdiff(dangerous, "lambdaJ")
lambdaJ <- density(X[J], ..., sigma=sigma, varcov=varcov,
at="points", leaveoneout=leaveoneout)
} else if(is.im(lambdaJ)) {
## look up intensity values
lambdaJ <- safelookup(lambdaJ, X[J])
} else if(is.function(lambdaJ)) {
## evaluate function at locations
XJ <- X[J]
lambdaJ <- lambdaJ(XJ$x, XJ$y)
} else if(is.numeric(lambdaJ) && is.vector(as.numeric(lambdaJ))) {
## validate intensity vector
if(length(lambdaJ) != nJ)
stop(paste("The length of", sQuote("lambdaJ"),
"should equal the number of", Jname))
} else if(is.ppm(lambdaJ) || is.kppm(lambdaJ) || is.dppm(lambdaJ)) {
## point process model provides intensity
model <- lambdaJ
if(!update) {
## just use intensity of fitted model
lambdaJ <- predict(model, locations=XJ, type="trend")
} else {
## re-fit model to data X
if(is.ppm(model)) {
model <- update(model, Q=X)
if(leaveoneout && !miss.leave)
lambdaX <- fitted(model, dataonly=TRUE, leaveoneout=leaveoneout)
} else {
model <- update(model, X=X)
lambdaX <- fitted(model, dataonly=TRUE, leaveoneout=leaveoneout)
}
lambdaJ <- lambdaX[J]
dangerJ <- FALSE
dangerous <- setdiff(dangerous, "lambdaJ")
if(miss.update)
warn.once(key="Kmulti.inhom.update",
"The behaviour of Kmulti.inhom when lambda is a ppm object",
"has changed (in spatstat 1.45-3 and later).",
"See help(Kmulti.inhom)")
}
} else
stop(paste(sQuote("lambdaJ"), "should be a vector or an image"))
}
## Weight for each pair
if(!is.null(lambdaIJ)) {
dangerIJ <- TRUE
dangerous <- union(dangerous, "lambdaIJ")
if(!is.matrix(lambdaIJ))
stop("lambdaIJ should be a matrix")
if(nrow(lambdaIJ) != nI)
stop(paste("nrow(lambdaIJ) should equal the number of", Iname))
if(ncol(lambdaIJ) != nJ)
stop(paste("ncol(lambdaIJ) should equal the number of", Jname))
} else {
dangerIJ <- FALSE
}
danger <- dangerI || dangerJ || dangerIJ
# Recommended range of r values
alim <- c(0, min(rmax, rmaxdefault))
# this will be the output data frame
# It will be given more columns later
K <- data.frame(r=r, theo= pi * r^2)
desc <- c("distance argument r", "theoretical Poisson %s")
fname <- c("K", "list(inhom,I,J)")
K <- fv(K, "r", quote(K[inhom, I, J](r)),
"theo", , alim,
c("r", makefvlabel(NULL, NULL, fname, "pois")),
desc,
fname=fname,
yexp=quote(K[list(inhom,I,J)](r)))
# identify close pairs of points
close <- crosspairs(XI, XJ, max(r), what="ijd")
# map (i,j) to original serial numbers in X
orig <- seq_len(npts)
imap <- orig[I]
jmap <- orig[J]
iX <- imap[close$i]
jX <- jmap[close$j]
# eliminate any identical pairs
if(any(I & J)) {
ok <- (iX != jX)
if(!all(ok)) {
close$i <- close$i[ok]
close$j <- close$j[ok]
close$d <- close$d[ok]
}
}
# extract information for these pairs (relative to orderings of XI, XJ)
dclose <- close$d
icloseI <- close$i
jcloseJ <- close$j
# Form weight for each pair
if(is.null(lambdaIJ))
weight <- 1/(lambdaI[icloseI] * lambdaJ[jcloseJ])
else
weight <- 1/lambdaIJ[cbind(icloseI, jcloseJ)]
# Compute estimates by each of the selected edge corrections.
if(any(correction == "none")) {
## uncorrected
wh <- whist(dclose, breaks$val, weight)
Kun <- cumsum(wh)/areaW
rmax <- diameter(W)/2
Kun[r >= rmax] <- NA
K <- bind.fv(K, data.frame(un=Kun),
makefvlabel(NULL, "hat", fname, "un"),
"uncorrected estimate of %s",
"un")
}
if(any(correction == "border" | correction == "bord.modif")) {
# border method
# Compute distances to boundary
b <- bdist.points(XI)
bI <- b[icloseI]
# apply reduced sample algorithm
RS <- Kwtsum(dclose, bI, weight, b, 1/lambdaI, breaks)
if(any(correction == "border")) {
Kb <- RS$ratio
K <- bind.fv(K, data.frame(border=Kb),
makefvlabel(NULL, "hat", fname, "bord"),
"border-corrected estimate of %s",
"border")
}
if(any(correction == "bord.modif")) {
Kbm <- RS$numerator/eroded.areas(W, r)
K <- bind.fv(K, data.frame(bord.modif=Kbm),
makefvlabel(NULL, "hat", fname, "bordm"),
"modified border-corrected estimate of %s",
"bord.modif")
}
}
if(any(correction == "translate")) {
## translation correction
edgewt <- edge.Trans(XI[icloseI], XJ[jcloseJ], paired=TRUE)
allweight <- edgewt * weight
wh <- whist(dclose, breaks$val, allweight)
Ktrans <- cumsum(wh)/areaW
rmax <- diameter(W)/2
Ktrans[r >= rmax] <- NA
K <- bind.fv(K, data.frame(trans=Ktrans),
makefvlabel(NULL, "hat", fname, "trans"),
"translation-corrected estimate of %s",
"trans")
}
if(any(correction == "isotropic")) {
## Ripley isotropic correction
edgewt <- edge.Ripley(XI[icloseI], matrix(dclose, ncol=1))
allweight <- edgewt * weight
wh <- whist(dclose, breaks$val, allweight)
Kiso <- cumsum(wh)/areaW
rmax <- diameter(W)/2
Kiso[r >= rmax] <- NA
K <- bind.fv(K, data.frame(iso=Kiso),
makefvlabel(NULL, "hat", fname, "iso"),
"Ripley isotropic correction estimate of %s",
"iso")
}
## default is to display them all
formula(K) <- . ~ r
unitname(K) <- unitname(X)
if(danger)
attr(K, "dangerous") <- dangerous
return(K)
}
|