|
3 | 3 | #' |
4 | 4 | #' simulate.kppm |
5 | 5 | #' |
6 | | -#' $Revision: 1.12 $ $Date: 2023/10/20 11:04:52 $ |
| 6 | +#' $Revision: 1.13 $ $Date: 2025/04/19 05:23:43 $ |
7 | 7 |
|
8 | 8 | simulate.kppm <- function(object, nsim=1, seed=NULL, ..., |
9 | 9 | window=NULL, covariates=NULL, |
@@ -41,7 +41,7 @@ simulate.kppm <- function(object, nsim=1, seed=NULL, ..., |
41 | 41 | if(!is.null(n.cond)) { |
42 | 42 | ## fixed number of points |
43 | 43 | out <- condSimCox(object, nsim=nsim, seed=NULL, ..., |
44 | | - window=win, covariates=covariates, |
| 44 | + win=win, covariates=covariates, |
45 | 45 | n.cond=n.cond, w.cond=w.cond, |
46 | 46 | verbose=verbose, retry=retry, drop=drop) |
47 | 47 | out <- timed(out, starttime=starttime) |
@@ -144,98 +144,3 @@ simulate.kppm <- function(object, nsim=1, seed=NULL, ..., |
144 | 144 | return(out) |
145 | 145 | } |
146 | 146 |
|
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 | | -} |
0 commit comments