File: evalcovarslrm.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 (153 lines) | stat: -rw-r--r-- 4,913 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
#'
#'   evalcovarslrm.R
#'
#'   method for 'evalCovar' for class 'slrm'
#'
#'   $Revision: 1.4 $ $Date: 2021/06/28 05:56:35 $

evalCovar.slrm <- function(model, covariate, ...,
                           lambdatype=c("probabilities", "intensity"),
                           jitter=TRUE, jitterfactor=1,
                           modelname=NULL, covname=NULL,
                           dataname=NULL, subset=NULL) {
  lambdatype <- match.arg(lambdatype)
  #' trap misuse
  badargs <- intersect(c("eps", "dimyx"), names(list(...)))
  nbad <- length(badargs)
  if(nbad > 0)
    warning(paste(ngettext(nbad, "Argument", "Arguments"),
                  commasep(sQuote(badargs)),
                  ngettext(nbad, "is", "are"),
                  "ignored by rhohat.slrm"),
            call.=FALSE)
  #' evaluate covariate values at presence pixels and all pixels
  #' determine names
  if(is.null(modelname))
    modelname <- short.deparse(substitute(model))
  if(covNotNamed <- is.null(covname)) {
    covname <- singlestring(short.deparse(substitute(covariate)))
    if(is.character(covariate)) covname <- covariate
  }
  if(is.null(dataname))
    dataname <- model$CallInfo$responsename

  csr <- is.stationary(model)
  
  info <-  list(modelname=modelname, covname=covname,
                dataname=dataname, csr=csr, ispois=TRUE,
                spacename="two dimensions")

  FIT  <- model$Fit$FIT
  link <- model$CallInfo$link
  ## original point pattern
  X <- model$Data$response
  W <- Window(X)

  ## extract data from each pixel (or split pixel)
  df <- model$Data$df
    
  ## restrict to subset if required
  if(!is.null(subset)) {
    ok <- inside.owin(df$x, df$y, subset)
    df <- df[ok, drop=FALSE]
    X <- X[subset]
    W <- W[subset, drop=FALSE]
  }

  ## presence/absence values
  responsename <- model$CallInfo$responsename
  presence <- as.logical(df[[responsename]])
  ## areas of pixels or split pixels
  pixelareas <- exp(df$logpixelarea)

  ## pixel centres as a point pattern
  P <- ppp(df$x, df$y, window=W)

  #' parse covariate argument
  if(is.character(covariate)) {
    #' One of the characters 'x' or 'y'
    #' Turn it into a function.
    ns <- length(covariate)
    if(ns == 0) stop("covariate is empty")
    if(ns > 1) stop("more than one covariate specified")
    covname <- covariate
    covNotNamed <- FALSE
    covariate <- switch(covname,
                        x=function(x,y) { x },
                        y=function(x,y) { y },
                        stop(paste("Unrecognised covariate",
                                   dQuote(covariate))))
  } 

  if(is.im(covariate)) {
    type <- "im"
    ZP <- safelookup(covariate, P)
    Z <- covariate[W, drop=FALSE]
    W <- as.owin(Z)
  } else if(is.function(covariate)) {
    type <- "function"
    ZP <- covariate(P$x, P$y)
    if(!all(is.finite(ZP)))
      warning("covariate function returned NA or Inf values")
    #' window
    W <- as.mask(W)
    #' covariate in window
    Z <- as.im(covariate, W=W)
    #' collapse function body to single string
    if(covNotNamed) covname <- singlestring(covname)
  } else if(is.null(covariate)) {
    stop("The covariate is NULL", call.=FALSE)
  } else stop(paste("The covariate should be",
                    "an image, a function(x,y)",
                    "or one of the characters",
                    sQuote("x"), "or", sQuote("y")),
              call.=FALSE)

  #' values of covariate at pixels or split pixels
  Zvalues <- ZP
  #'values of covariate at 'presence' pixels
  ZX <- Zvalues[presence]

  #' fitted probability/intensity values at all pixels or split pixels
  switch(lambdatype,
         probabilities = {
           lambda <- predict(FIT, newdata=df, type="response")
         },
         intensity = {
           if(link == "cloglog") {
             linkvalues <- predict(FIT, newdata=df, type="link")
             lambda <- exp(linkvalues)/pixelareas
           } else {
             probs <- predict(FIT, newdata=df, type="response")
             lambda <- -log(1-probs)/pixelareas
           }
         })

  #' apply jittering to avoid ties
  if(jitter) {
    ZX <- jitter(ZX, factor=jitterfactor)
    Zvalues <- jitter(Zvalues, factor=jitterfactor)
  }

  lambdaname <- paste("the fitted", lambdatype)
  check.finite(lambda, xname=lambdaname, usergiven=FALSE)
  check.finite(Zvalues, xname="the covariate", usergiven=TRUE)
  
  #' lambda values at data points
  lambdaX <- lambda[presence]

  #' lambda image(s)
  lambdaimage <- predict(model, window=W, type=lambdatype)
    
  #' wrap up 
  values <- list(Zimage      = Z,
                 lambdaimage = lambdaimage,
                 Zvalues     = Zvalues,
                 lambda      = lambda,
                 lambdaX     = lambdaX,
                 weights     = pixelareas,
                 ZX          = ZX,
                 type        = type)
  return(list(values=values, info=info))
}