File: idw.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 (143 lines) | stat: -rw-r--r-- 5,335 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
#
#  idw.R
#
#  Inverse-distance weighted smoothing
#
#  $Revision: 1.13 $ $Date: 2021/01/07 03:08:41 $

idw <- function(X, power=2, at=c("pixels", "points"), ..., se=FALSE) {
  stopifnot(is.ppp(X) && is.marked(X))
  at <- match.arg(at)
  marx <- marks(X)
  if(is.data.frame(marx)) {
    if((nc <- ncol(marx)) > 1) {
      ## multiple columns of marks - process one-by-one
      each <- vector(mode="list", length=nc)
      for(j in 1:nc) 
        each[[j]] <- idw(X %mark% marx[,j], power=power, at=at, ..., se=se)
      names(each) <- colnames(marx)
      ##
      if(!se) {
        ## estimates only
        switch(at,
               pixels = { out <- as.solist(each) },
               points = { out <- as.data.frame(each) }
               )
      } else {
        ## estimates and standard errors
        est <- lapply(each, getElement, name="estimate")
        SE  <- lapply(each, getElement, name="SE")
        switch(at,
               pixels = {
                 out <- list(estimate = as.solist(est),
                             SE       = as.solist(SE))
               },
               points = {
                 out <- list(estimate = as.data.frame(est),
                              SE      = as.data.frame(SE))
               })
      }
      return(out)
    } else 
      marx <- marx[,1L]
  }
  if(!is.numeric(marx))
    stop("Marks must be numeric")
  check.1.real(power)
  switch(at,
         pixels = {
           ## create grid
           W <- as.mask(as.owin(X), ...)
           dim <- W$dim
           npixels <- prod(dim)
           ## call C
           if(!se) {
             z <- .C(SC_Cidw,
                     x = as.double(X$x),
                     y = as.double(X$y),
                     v = as.double(marx),
                     n = as.integer(npoints(X)),
                     xstart = as.double(W$xcol[1L]),
                     xstep  = as.double(W$xstep),
                     nx     = as.integer(dim[2L]),
                     ystart = as.double(W$yrow[1L]),
                     ystep  = as.double(W$ystep),
                     ny     = as.integer(dim[1L]),
                     power  = as.double(power),
                     num    = as.double(numeric(npixels)),
                     den    = as.double(numeric(npixels)),
                     rat    = as.double(numeric(npixels)),
                     PACKAGE="spatstat.core")
             out <- as.im(matrix(z$rat, dim[1L], dim[2L]), W=W)
             out <- out[W, drop=FALSE]
           } else {
             z <- .C(SC_Cidw2,
                     x = as.double(X$x),
                     y = as.double(X$y),
                     v = as.double(marx),
                     n = as.integer(npoints(X)),
                     xstart = as.double(W$xcol[1L]),
                     xstep  = as.double(W$xstep),
                     nx     = as.integer(dim[2L]),
                     ystart = as.double(W$yrow[1L]),
                     ystep  = as.double(W$ystep),
                     ny     = as.integer(dim[1L]),
                     power  = as.double(power),
                     num    = as.double(numeric(npixels)),
                     den    = as.double(numeric(npixels)),
                     rat    = as.double(numeric(npixels)),
                     mtwo   = as.double(numeric(npixels)),
                     wtwo   = as.double(numeric(npixels)),
                     PACKAGE="spatstat.core")
             est <- as.im(matrix(z$rat, dim[1L], dim[2L]), W=W)
             est <- est[W, drop=FALSE]
             sumw  <- z$den
             sumw2 <- z$wtwo
             m2    <- z$mtwo
             varden <- sumw - sumw2/sumw
             varden[varden <= 0] <- NA
             SE <- sqrt(m2/varden)
             SE <- as.im(matrix(SE, dim[1L], dim[2L]), W=W)
             SE <- SE[W, drop=FALSE]
             out <- solist(estimate=est, SE=SE)
           }
         },
         points={
           npts <- npoints(X)
           if(!se) {
             z <- .C(SC_idwloo,
                     x = as.double(X$x),
                     y = as.double(X$y),
                     v = as.double(marx),
                     n = as.integer(npts),
                     power  = as.double(power),
                     num    = as.double(numeric(npts)),
                     den    = as.double(numeric(npts)),
                     rat    = as.double(numeric(npts)),
                     PACKAGE="spatstat.core")
             out <- z$rat
           } else {
             z <- .C(SC_idwloo2,
                     x = as.double(X$x),
                     y = as.double(X$y),
                     v = as.double(marx),
                     n = as.integer(npts),
                     power  = as.double(power),
                     num    = as.double(numeric(npts)),
                     den    = as.double(numeric(npts)),
                     rat    = as.double(numeric(npts)),
                     mtwo   = as.double(numeric(npts)),
                     wtwo   = as.double(numeric(npts)),
                     PACKAGE="spatstat.core")
             est <- z$rat
             sumw  <- z$den
             sumw2 <- z$wtwo
             m2    <- z$mtwo
             varden <- sumw - sumw2/sumw
             varden[varden <= 0] <- NA
             SE <- sqrt(m2/varden)
             out <- list(estimate=est, SE=SE)
           }
         })
  return(out)
}