File: qqplotppm.R

package info (click to toggle)
r-cran-spatstat.core 2.4-4-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,440 kB
  • sloc: ansic: 4,402; sh: 13; makefile: 5
file content (346 lines) | stat: -rw-r--r-- 12,111 bytes parent folder | download
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
#
#    QQ plot of smoothed residual field against model
#
#  qqplot.ppm()       QQ plot (including simulation)
#
#  $Revision: 1.32 $   $Date: 2022/01/18 09:51:29 $
#

qqplot.ppm <- local({

  ## How to refit the model
  refit <- function(fit, pattern) {
    update.ppm(fit, Q=pattern, use.internal=(fit$method != "mppm"))
  }
  
  ## how to compute the residual field
  residualfield <- function(fit, ..., addtype=FALSE) {
    d <- diagnose.ppm(fit, which="smooth",
                      plot.it=FALSE, compute.cts=FALSE, compute.sd=FALSE,
                      check=FALSE, ...)
    result <- d$smooth$Z$v
    if(addtype) {
      attr(result, "type") <- d$type
      attr(result, "typename") <- d$typename
    }
    return(result)
  }

  qqplot.ppm <-
    function(fit, nsim=100, expr=NULL, ..., type="raw", style="mean",
             fast=TRUE, verbose=TRUE, plot.it=TRUE,
             dimyx=NULL, nrep=if(fast) 5e4 else 1e5,
             control=update(default.rmhcontrol(fit), nrep=nrep),
             saveall=FALSE,
             monochrome=FALSE,
             limcol=if(monochrome) "black" else "red",
             maxerr=max(100, ceiling(nsim/10)),
             check=TRUE, repair=TRUE, envir.expr) {
    verifyclass(fit, "ppm")

    if(check && damaged.ppm(fit)) {
      if(!repair)
        stop("object format corrupted; try update(fit, use.internal=TRUE)")
      message("object format corrupted; repairing it.")
      fit <- update(fit, use.internal=TRUE)
    }
  
    if(fast) {
      oldnpixel <- spatstat.options("npixel")
      if(is.null(dimyx)) 
        dimyx <- pmin(40, rev(oldnpixel))
      spatstat.options(npixel=rev(dimyx))
    } 
    
    ################   How to evaluate residuals ##########################
  
    ## Quantiles of the residual field will be computed.

    ## Data values
    dat <- residualfield(fit, type=type, ..., dimyx=dimyx, addtype=TRUE)

    ## type of residuals (partially matched and validated by diagnose.ppm)
    type <- attr(dat, "type")
    typename <- attr(dat, "typename") 

    ##################  How to perform simulations?  #######################

    ## envir.call <- sys.parent()
    envir.here <- sys.frame(sys.nframe())

    ## extract.from.list <- FALSE
    inext <- 0 # to placate package checker
    dont.complain.about(inext)
    
    if(is.null(expr)) {
      ## We will simulate from the fitted model 'nsim' times
      ## and refit the model to these simulations
      simsource <- "fit"
      how.simulating <- "simulating from fitted model" 

      ## prepare rmh arguments
      rcontrol <- rmhcontrol(control)
      rmodel   <- rmhmodel(fit,
                           control=rcontrol, project=FALSE, verbose=verbose)
      rstart   <- rmhstart(n.start=data.ppm(fit)$n)
      ## pre-digest arguments
      rmhinfolist <- rmh(rmodel, rstart, rcontrol, preponly=TRUE, verbose=FALSE)
    
      ## expression to be evaluated each time
      expr <- expression(
        refit(fit, 
              rmhEngine(rmhinfolist, verbose=FALSE)))
      envir.expr <- envir.here

      ## pacify code checkers
      dont.complain.about(rmhinfolist)
    } else if(is.expression(expr)) {
      simsource <- "expr"
      how.simulating <- paste("evaluating", sQuote("expr"))  
      if(missing(envir.expr) || is.null(envir.expr))
        envir.expr <- parent.frame()
    } else if(inherits(expr, "envelope")) {
      simpat <- attr(expr, "simpatterns")
      if(!is.null(simpat) && all(sapply(simpat, is.ppp))) {
        expr <- expression(simpat[[inext]])
        envir.expr <- envir.here
        dont.complain.about(simpat)
        simsource <- "list"
        how.simulating <- "extracting point pattern from list"
      } else stop(paste("Argument", sQuote("expr"),
                        "is an envelope object,",
                        "but does not contain point patterns"),
                  call.=FALSE)
    } else if(is.list(expr) && all(sapply(expr, is.ppp))) {
      simpat <- expr
      expr <- expression(simpat[[inext]])
      envir.expr <- envir.here
      dont.complain.about(simpat)
      simsource <- "list"
      how.simulating <- "extracting point pattern from list"
    } else stop(paste(sQuote("expr"),
                      "should be an expression, or an envelope object,",
                      "or a list of point patterns"),
                call.=FALSE)

    exprstring <- if(simsource == "expr") deparse(expr) else NULL

    ######  Perform simulations
    if(verbose) {
      cat(paste("Simulating", nsim, "realisations... "))
      pstate <- list()
    }
    simul.sizes <- numeric(nsim)
    isim <- 0
    ierr <- 0
    repeat {
      inext <- isim + 1
      ## protect from randomly-generated crashes in gam
      ##      ei <- try(eval(expr, envir=envir.expr), silent=!verbose)
      ei <- eval(expr, envir=envir.expr)
      if(inherits(ei, "try-error")) {
        ## error encountered in evaluating 'expr'
        ierr <- ierr + 1
        if(ierr > maxerr) 
          stop(paste("Exceeded maximum of", maxerr,
                     "failures in", how.simulating,
                     "after generating only", isim, "realisations"))
        else break
      } else {
        ## simulation successful
        isim <- isim + 1
        fiti <- 
          if(simsource == "fit")
            ei
          else if(is.ppm(ei))
            ei
          else if(is.ppp(ei))
            refit(fit, ei)
          else
            stop("result of eval(expr) is not a ppm or ppp object")
        ## diagnostic info
        simul.sizes[isim] <- data.ppm(fiti)$n
        ## compute residual field
        resi <- residualfield(fiti, type=type, ..., dimyx=dimyx)
        if(isim == 1)
          sim <- array(, dim=c(dim(resi), nsim))
        sim[,,isim] <- resi
        if(verbose) 
          pstate <- progressreport(isim, nsim, state=pstate)
        if(isim >= nsim)
          break
      }
    }

    ###### Report diagnostics
    if(ierr > 0)
      cat(paste("\n\n**Alert:",
                ierr, "failures occurred in", how.simulating, "\n\n"))
    nempty <- sum(simul.sizes == 0)
    if(nempty > 0)
      cat(paste("\n\n**Alert:",
                nempty, "out of", nsim,
                "simulated patterns were empty.\n\n"))
    else
      cat(paste("\nDiagnostic info:\n",
                "simulated patterns contained an average of",
                mean(simul.sizes), "points.\n"))
    if(nempty == nsim)
      warning("All simulated patterns were empty")
    ############ Plot them
    switch(style,
           classical = {
             rr <- range(c(dat,sim))
             result <- qqplot(sim, dat, xlim=rr, ylim=rr, asp=1.0,
                              xlab="Quantiles of simulation",
                              ylab="Quantiles of data",plot.it=plot.it)
             title(sub=typename)
             abline(0,1, lty=2)
             result <- append(result,
                              list(data=dat,
                                   sim=sim,
                                   xlim=rr,
                                   ylim=rr,
                                   xlab="Quantiles of simulation",
                                   ylab="Quantiles of data",
                                   rtype=type,
                                   typename=typename,
                                   nsim=nsim,
                                   fit=fit,
                                   expr=exprstring,
                                   simsource = simsource
                                   )
                              )
           },
           mean = {
             ## compute quantiles corresponding to probabilities p[i]
             ## separately in each realisation.
             if(verbose) cat("Calculating quantiles...")
             if(fast) {
               p <- ppoints(min(100,length(dat)), 3/8)
               qsim <- apply(sim, 3, quantile, probs=p, na.rm=TRUE)
             } else {
               qsim <- apply(sim, 3, sort, na.last=TRUE)
             }
             if(verbose) cat("averaging...")
             ## sample mean of each quantile
             meanq <- apply(qsim, 1, mean, na.rm=TRUE)
             ## et cetera
             varq <- apply(qsim, 1, var, na.rm=TRUE)
             sdq <- sqrt(varq)
             q.025 <- apply(qsim, 1, quantile, probs=0.025, na.rm=TRUE)
             q.975 <- apply(qsim, 1, quantile, probs=0.975, na.rm=TRUE)
  
             rr <- range(c(meanq,dat), na.rm=TRUE)

             dats <- if(fast) quantile(dat, probs=p, na.rm=TRUE) else
                              sort(dat, na.last=TRUE)

             if(verbose) cat("..Done.\n")
             if(plot.it) {
               plot(meanq, dats,
                    xlab="Mean quantile of simulations", ylab="data quantile",
                    xlim=rr, ylim=rr, asp=1.0)
               abline(0,1)
               lines(meanq, q.025, lty=2, col=limcol)
               lines(meanq, q.975, lty=2, col=limcol)
               title(sub=typename)
             }
             result <- list(x=meanq, y=dats, sdq=sdq,
                            q.025=q.025, q.975=q.975,
                            data=dat, sim=sim,
                            xlim=rr, ylim=rr,
                            xlab="Mean quantile of simulations",
                            ylab="data quantile",
                            rtype=type,
                            typename=typename,
                            nsim=nsim,
                            fit=fit,
                            expr=exprstring,
                            simsource=simsource)
           },
           stop(paste("Unrecognised option for", sQuote("style")))
           )

    ## Throw out baggage if not wanted         
    if(!saveall) {
      result$fit <- summary(fit, quick=TRUE)
      result$sim <- NULL
    }
         
    ## reset npixel
    if(fast)
      spatstat.options(npixel=oldnpixel)
    ##
    class(result) <- c("qqppm", class(result))
    return(invisible(result))
  }

  qqplot.ppm

})


plot.qqppm <- local({

  plot.qqppm <- function(x, ..., limits=TRUE,
                         monochrome=spatstat.options('monochrome'),
                         limcol=if(monochrome) "black" else "red") {
    stopifnot(inherits(x, "qqppm"))
    default.type <- if(length(x$x) > 150) "l" else "p"
    do.call(myplot,
            resolve.defaults(list(quote(x), ..., type=default.type,
                                  limits=limits, limcol=limcol)))
    return(invisible(x))
  }

  myplot <- function(object,
                     xlab = object$xlab, ylab = object$ylab,
                     xlim = object$xlim, ylim = object$ylim,
                     asp = 1,
                     type = default.type,
                     ..., limits=TRUE, limcol="red") {
    plot(object$x, object$y, xlab = xlab, ylab = ylab,
         xlim = xlim, ylim = ylim, asp = asp, type = type, ...)
    abline(0, 1)
    
    if(limits) {
      if(!is.null(object$q.025))
        lines(object$x, object$q.025, lty = 2, col=limcol)
      if(!is.null(object$q.975))
        lines(object$x, object$q.975, lty = 2, col=limcol)
    }
    typename <- object$typename %orifnull% paste(object$rtype, "residuals")
    title(sub=typename)
  }

  plot.qqppm
})


print.qqppm <- function(x, ...) {
  stopifnot(inherits(x, "qqppm"))
  splat("Q-Q plot of point process residuals",
        "of type", sQuote(x$rtype), "\n",
        "based on", x$nsim, "simulations")
  simsource <- x$simsource
  if(is.null(simsource)) # old version
    simsource <- if(x$simulate.from.fit) "fit" else "expr"
  switch(simsource,
         fit = {
           fit  <- x$fit
           sumfit <- if(is.ppm(fit)) summary(fit, quick=TRUE)
                     else if(inherits(fit, "summary.ppm")) fit
                     else list(name="(unrecognised format)")
           splat("\nSimulations from fitted model:", sumfit$name)
         },
         expr = {
           splat("Simulations obtained by evaluating the following expression:")
           print(x$expr)
         },
         list = {
           splat("Simulated point patterns were provided in a list")
         })
  invisible(NULL)
}