Skip to content

Commit c851934

Browse files
committed
Tweaked conditional simulation for Neyman-Scott
1 parent f9dfd5f commit c851934

File tree

8 files changed

+77
-139
lines changed

8 files changed

+77
-139
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: spatstat.model
22
Version: 3.3-5.002
3-
Date: 2025-04-18
3+
Date: 2025-04-19
44
Title: Parametric Statistical Modelling and Inference for the 'spatstat' Family
55
Authors@R: c(person("Adrian", "Baddeley",
66
role = c("aut", "cre", "cph"),
@@ -55,7 +55,7 @@ Authors@R: c(person("Adrian", "Baddeley",
5555
person("Hangsheng", "Wang",
5656
role = "ctb"))
5757
Maintainer: Adrian Baddeley <[email protected]>
58-
Depends: R (>= 3.5.0), spatstat.data (>= 3.1-4), spatstat.univar (>= 3.1-1), spatstat.geom (>= 3.3-4), spatstat.random (>= 3.3-2), spatstat.explore (>= 3.3-0), stats, graphics, grDevices, utils, methods, nlme, rpart
58+
Depends: R (>= 3.5.0), spatstat.data (>= 3.1-4), spatstat.univar (>= 3.1-1), spatstat.geom (>= 3.3-4), spatstat.random (>= 3.3-3.005), spatstat.explore (>= 3.3-0), stats, graphics, grDevices, utils, methods, nlme, rpart
5959
Imports: spatstat.utils (>= 3.1-2), spatstat.sparse (>= 3.1-0), mgcv, Matrix, abind, tensor, goftest (>= 1.2-2)
6060
Suggests: sm, gsl, locfit, spatial, fftwtools (>= 0.9-8), nleqslv, glmnet, spatstat.linnet (>= 3.2-2), spatstat (>= 3.3)
6161
Description: Functionality for parametric statistical modelling and inference for spatial data,

NAMESPACE

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ export("coef.summary.slrm")
110110
export("coef.vblogit")
111111
export("compareFit")
112112
export("Concom")
113-
export("condSimCox")
114113
export("contour.leverage.ppm")
115114
export("contour.objsurf")
116115
export("crosspaircounts")
@@ -305,6 +304,7 @@ export("is.multitype.ppm")
305304
export("is.multitype.rppm")
306305
export("is.multitype.slrm")
307306
export("is.poissonclusterprocess")
307+
export("is.poissonclusterprocess.clusterprocess")
308308
export("is.poissonclusterprocess.default")
309309
export("is.poissonclusterprocess.kppm")
310310
export("is.poissonclusterprocess.zclustermodel")
@@ -326,6 +326,7 @@ export("is.stationary.zgibbsmodel")
326326
export("Kcom")
327327
export("killinteraction")
328328
export("Kmodel")
329+
export("Kmodel.clusterprocess")
329330
export("Kmodel.detpointprocfamily")
330331
export("Kmodel.dppm")
331332
export("Kmodel.kppm")
@@ -457,6 +458,7 @@ export("parameters.slrm")
457458
export("parres")
458459
export("partialModelMatrix")
459460
export("pcfmodel")
461+
export("pcfmodel.clusterprocess")
460462
export("pcfmodel.detpointprocfamily")
461463
export("pcfmodel.dppm")
462464
export("pcfmodel.kppm")
@@ -903,6 +905,7 @@ S3method("is.multitype", "msr")
903905
S3method("is.multitype", "ppm")
904906
S3method("is.multitype", "rppm")
905907
S3method("is.multitype", "slrm")
908+
S3method("is.poissonclusterprocess", "clusterprocess")
906909
S3method("is.poissonclusterprocess", "default")
907910
S3method("is.poissonclusterprocess", "kppm")
908911
S3method("is.poissonclusterprocess", "zclustermodel")
@@ -919,6 +922,7 @@ S3method("is.stationary", "kppm")
919922
S3method("is.stationary", "ppm")
920923
S3method("is.stationary", "slrm")
921924
S3method("is.stationary", "zgibbsmodel")
925+
S3method("Kmodel", "clusterprocess")
922926
S3method("Kmodel", "detpointprocfamily")
923927
S3method("Kmodel", "dppm")
924928
S3method("Kmodel", "kppm")
@@ -978,6 +982,7 @@ S3method("parameters", "kppm")
978982
S3method("parameters", "ppm")
979983
S3method("parameters", "profilepl")
980984
S3method("parameters", "slrm")
985+
S3method("pcfmodel", "clusterprocess")
981986
S3method("pcfmodel", "detpointprocfamily")
982987
S3method("pcfmodel", "dppm")
983988
S3method("pcfmodel", "kppm")

R/clusterprocess.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#'
2+
#' clusterprocess.R
3+
#'
4+
#' $Revision: 1.1 $ $Date: 2025/04/19 05:17:27 $
5+
#'
6+
7+
#' clusterprocess() is defined in spatstat.random
8+
9+
#' The following methods are for generics defined in spatstat.model
10+
11+
is.poissonclusterprocess.clusterprocess <- function(model) { TRUE }
12+
13+
pcfmodel.clusterprocess <- function(model, ...) {
14+
p <- model$rules$pcf
15+
mpar <- model$par.idio
16+
other <- model$other
17+
f <- function(r) {
18+
as.numeric(do.call(p, c(list(par=mpar, rvals=r), other)))
19+
}
20+
return(f)
21+
}
22+
23+
Kmodel.clusterprocess <- function(model, ...) {
24+
K <- model$rules$K
25+
mpar <- model$par.idio
26+
other <- model$other
27+
f <- function(r) {
28+
as.numeric(do.call(K, c(list(par=mpar, rvals=r), other)))
29+
}
30+
return(f)
31+
}

R/simulatekppm.R

Lines changed: 2 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#'
44
#' simulate.kppm
55
#'
6-
#' $Revision: 1.12 $ $Date: 2023/10/20 11:04:52 $
6+
#' $Revision: 1.13 $ $Date: 2025/04/19 05:23:43 $
77

88
simulate.kppm <- function(object, nsim=1, seed=NULL, ...,
99
window=NULL, covariates=NULL,
@@ -41,7 +41,7 @@ simulate.kppm <- function(object, nsim=1, seed=NULL, ...,
4141
if(!is.null(n.cond)) {
4242
## fixed number of points
4343
out <- condSimCox(object, nsim=nsim, seed=NULL, ...,
44-
window=win, covariates=covariates,
44+
win=win, covariates=covariates,
4545
n.cond=n.cond, w.cond=w.cond,
4646
verbose=verbose, retry=retry, drop=drop)
4747
out <- timed(out, starttime=starttime)
@@ -144,98 +144,3 @@ simulate.kppm <- function(object, nsim=1, seed=NULL, ...,
144144
return(out)
145145
}
146146

147-
condSimCox <- function(object, nsim=1,
148-
..., window=NULL,
149-
n.cond=NULL, w.cond=NULL,
150-
giveup=1000, maxchunk=100,
151-
saveLambda=FALSE,
152-
verbose=TRUE, drop=FALSE) {
153-
if(!inherits(object, c("kppm", "zclustermodel")))
154-
stop("object should belong to class 'kppm' or 'zclustermodel'", call=FALSE)
155-
shortcut <- isFALSE(object$isPCP)
156-
157-
w.sim <- as.owin(window)
158-
fullwindow <- is.null(w.cond)
159-
if(fullwindow) {
160-
w.cond <- w.sim
161-
w.free <- NULL
162-
} else {
163-
stopifnot(is.owin(w.cond))
164-
w.free <- setminus.owin(w.sim, w.cond)
165-
}
166-
167-
nremaining <- nsim
168-
ntried <- 0
169-
accept <- FALSE
170-
nchunk <- 1
171-
phistory <- mhistory <- numeric(0)
172-
results <- list()
173-
while(nremaining > 0) {
174-
## increase chunk length
175-
nchunk <- min(maxchunk, giveup - ntried, 2 * nchunk)
176-
## bite off next chunk of simulations
177-
if(shortcut) {
178-
lamlist <- simulate(object, nsim=nchunk,
179-
Lambdaonly=TRUE,
180-
..., drop=FALSE, verbose=FALSE)
181-
} else {
182-
Xlist <- simulate(object, nsim=nchunk,
183-
saveLambda=TRUE,
184-
..., drop=FALSE, verbose=FALSE)
185-
lamlist <- lapply(unname(Xlist), attr, which="Lambda", exact=TRUE)
186-
}
187-
## compute acceptance probabilities
188-
lamlist <- lapply(lamlist, "[", i=w.sim, drop=FALSE, tight=TRUE)
189-
if(fullwindow) {
190-
mu <- sapply(lamlist, integral)
191-
} else {
192-
mu <- sapply(lamlist, integral, domain=w.cond)
193-
}
194-
p <- exp(n.cond * log(mu/n.cond) + n.cond - mu)
195-
phistory <- c(phistory, p)
196-
mhistory <- c(mhistory, mu)
197-
## accept/reject
198-
accept <- (runif(length(p)) < p)
199-
if(any(accept)) {
200-
jaccept <- which(accept)
201-
if(length(jaccept) > nremaining)
202-
jaccept <- jaccept[seq_len(nremaining)]
203-
naccepted <- length(jaccept)
204-
if(verbose)
205-
splat("Accepted the",
206-
commasep(ordinal(ntried + jaccept)),
207-
ngettext(naccepted, "proposal", "proposals"))
208-
nremaining <- nremaining - naccepted
209-
for(j in jaccept) {
210-
lamj <- lamlist[[j]]
211-
if(min(lamj) < 0)
212-
lamj <- eval.im(pmax(lamj, 0))
213-
if(fullwindow) {
214-
Y <- rpoint(n.cond, lamj, win=w.sim, forcewin=TRUE)
215-
} else {
216-
lamj.cond <- lamj[w.cond, drop=FALSE, tight=TRUE]
217-
lamj.free <- lamj[w.free, drop=FALSE, tight=TRUE]
218-
Ycond <- rpoint(n.cond, lamj.cond, win=w.cond)
219-
Yfree <- rpoispp(lamj.free)
220-
Y <- superimpose(Ycond, Yfree, W=w.sim)
221-
}
222-
if(saveLambda) attr(Y, "Lambda") <- lamj
223-
results <- append(results, list(Y))
224-
}
225-
}
226-
ntried <- ntried + nchunk
227-
if(ntried >= giveup && nremaining > 0) {
228-
message(paste("Gave up after", ntried,
229-
"proposals with", nsim - nremaining, "accepted"))
230-
message(paste("Mean acceptance probability =",
231-
signif(mean(phistory), 3)))
232-
break
233-
}
234-
}
235-
nresults <- length(results)
236-
results <- simulationresult(results, nresults, drop)
237-
attr(results, "history") <- data.frame(mu=mhistory, p=phistory)
238-
if(verbose && nresults == nsim)
239-
splat("Mean acceptance probability", signif(mean(phistory), 3))
240-
return(results)
241-
}

R/zclustermodel.R

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ zclustermodel <- function(name="Thomas", ..., mu, kappa, scale) {
1111
if(missing(mu)) stop("The mean cluster size mu must be given")
1212
if(missing(scale)) stop("The cluster scale must be given")
1313
rules <- spatstatClusterModelInfo(name)
14+
if(!(rules$isPCP)) stop("zclustermodel only supports Neyman-Scott processes")
1415
par.std <- c(kappa=kappa, scale=scale)
1516
par.std <- rules$checkpar(par.std, native=FALSE)
1617
par.idio <- rules$checkpar(par.std, native=TRUE)
@@ -62,26 +63,6 @@ print.zclustermodel <- local({
6263

6364

6465

65-
pcfmodel.zclustermodel <- function(model, ...) {
66-
p <- model$rules$pcf
67-
mpar <- model$par.idio
68-
other <- model$other
69-
f <- function(r) {
70-
as.numeric(do.call(p, c(list(par=mpar, rvals=r), other)))
71-
}
72-
return(f)
73-
}
74-
75-
Kmodel.zclustermodel <- function(model, ...) {
76-
K <- model$rules$K
77-
mpar <- model$par.idio
78-
other <- model$other
79-
f <- function(r) {
80-
as.numeric(do.call(K, c(list(par=mpar, rvals=r), other)))
81-
}
82-
return(f)
83-
}
84-
8566
intensity.zclustermodel <- function(X, ...) {
8667
X$par.std[["kappa"]] * X$mu
8768
}
@@ -121,8 +102,6 @@ reach.zclustermodel <- function(x, ..., epsilon) {
121102
2 * clusterradius(x, ..., thresh=thresh)
122103
}
123104

124-
is.poissonclusterprocess.zclustermodel <- function(model) { TRUE }
125-
126105
simulate.zclustermodel <- function(object, nsim=1, ..., win=unit.square()) {
127106
with(object, {
128107
switch(name,
@@ -161,18 +140,33 @@ simulate.zclustermodel <- function(object, nsim=1, ..., win=unit.square()) {
161140
...),
162141
clustargs))
163142
},
164-
LGCP = {
165-
do.call(rLGCP,
166-
resolve.defaults(
167-
list(kappa=par.std[["kappa"]],
168-
scale=par.std[["scale"]],
169-
mu=mu,
170-
win=win,
171-
nsim=nsim,
172-
...),
173-
clustargs))
174-
},
175-
stop(paste("Unrecognised model name", sQuote(object$name)),
143+
stop(paste("Unrecognised cluster process model name",
144+
sQuote(object$name)),
176145
call.=FALSE)
177146
)})
178147
}
148+
149+
#' The following methods are for generics defined in spatstat.model
150+
151+
is.poissonclusterprocess.zclustermodel <- function(model) { TRUE }
152+
153+
pcfmodel.zclustermodel <- function(model, ...) {
154+
p <- model$rules$pcf
155+
mpar <- model$par.idio
156+
other <- model$other
157+
f <- function(r) {
158+
as.numeric(do.call(p, c(list(par=mpar, rvals=r), other)))
159+
}
160+
return(f)
161+
}
162+
163+
Kmodel.zclustermodel <- function(model, ...) {
164+
K <- model$rules$K
165+
mpar <- model$par.idio
166+
other <- model$other
167+
f <- function(r) {
168+
as.numeric(do.call(K, c(list(par=mpar, rvals=r), other)))
169+
}
170+
return(f)
171+
}
172+

inst/doc/packagesizes.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ date version nhelpfiles nobjects ndatasets Rlines srclines
1919
"2024-11-19" "3.3-3" 269 726 0 39066 1155
2020
"2025-01-20" "3.3-4" 269 726 0 39070 1155
2121
"2025-03-22" "3.3-5" 269 726 0 39076 1155
22-
"2025-04-18" "3.3-5.002" 269 727 0 39131 1155
22+
"2025-04-19" "3.3-5.002" 269 729 0 39061 1155

inst/info/packagesizes.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ date version nhelpfiles nobjects ndatasets Rlines srclines
1919
"2024-11-19" "3.3-3" 269 726 0 39066 1155
2020
"2025-01-20" "3.3-4" 269 726 0 39070 1155
2121
"2025-03-22" "3.3-5" 269 726 0 39076 1155
22-
"2025-04-18" "3.3-5.002" 269 727 0 39131 1155
22+
"2025-04-19" "3.3-5.002" 269 729 0 39061 1155

man/spatstat.model-internal.Rd

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
\alias{coef.summary.ppm}
1818
\alias{coef.summary.slrm}
1919
\alias{coef.vblogit}
20-
\alias{condSimCox}
2120
\alias{damaged.ppm}
2221
\alias{data.mppm}
2322
\alias{deltasuffstat}
@@ -94,7 +93,9 @@
9493
\alias{is.multitype.slrm}
9594
\alias{is.poisson.mppm}
9695
\alias{is.poisson.rppm}
96+
\alias{is.poissonclusterprocess.clusterprocess}
9797
\alias{Kpcf.kppm}
98+
\alias{Kmodel.clusterprocess}
9899
\alias{Kmodel.slrm}
99100
\alias{killinteraction}
100101
\alias{kppmComLik}
@@ -129,6 +130,7 @@
129130
\alias{parameters.detpointprocfamily}
130131
\alias{PairPotentialType}
131132
\alias{partialModelMatrix}
133+
\alias{pcfmodel.clusterprocess}
132134
\alias{pcfmodel.slrm}
133135
\alias{ploterodewin}
134136
\alias{ploterodeimage}
@@ -260,8 +262,6 @@ check.separable(dmat, covname, isconstant, fatal)
260262
\method{coef}{summary.ppm}(object, \dots)
261263
\method{coef}{summary.slrm}(object, \dots)
262264
\method{coef}{vblogit}(object, \dots)
263-
condSimCox(object, nsim, \dots, window, n.cond, w.cond,
264-
giveup, maxchunk, saveLambda, verbose, drop)
265265
damaged.ppm(object)
266266
data.mppm(x)
267267
deltasuffstat(model, \dots,
@@ -346,7 +346,9 @@ is.mppm(x)
346346
\method{is.multitype}{slrm}(X, \dots)
347347
\method{is.poisson}{mppm}(x)
348348
\method{is.poisson}{rppm}(x)
349+
\method{is.poissonclusterprocess}{clusterprocess}(model)
349350
Kpcf.kppm(model, what)
351+
\method{Kmodel}{clusterprocess}(model, \dots)
350352
\method{Kmodel}{slrm}(model, \dots)
351353
killinteraction(model)
352354
kppmComLik(X, Xname, po, clusters, control, stabilize, weightfun, rmax,
@@ -419,6 +421,7 @@ oversize.quad(Q, \dots, nU, nX, p)
419421
PairPotentialType(pairpot)
420422
\method{parameters}{detpointprocfamily}(model, \dots)
421423
partialModelMatrix(X,D,model,callstring,\dots)
424+
\method{pcfmodel}{clusterprocess}(model, \dots)
422425
\method{pcfmodel}{slrm}(model, \dots)
423426
ploterodewin(W1, W2, col.edge, col.inside, do.plot, \dots)
424427
ploterodeimage(W, Z, \dots, Wcol, rangeZ, colsZ, do.plot)

0 commit comments

Comments
 (0)