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
|
## support for class 'detpointprocfamily'
print.detpointprocfamily <- function(x, ...){
splat(x$name, "determinantal point process model",
ifelse(is.numeric(x$dim), paste("in dimension", x$dim), ""))
#' Not used:
#' parnames <- names(x$par)
anyfixed <- length(x$fixedpar)>0
if(anyfixed){
fixedlambda <- NULL
if(!is.null(x$intensity) && is.element(x$intensity, names(x$fixedpar))){
lambda <- signif(x$fixedpar[[x$intensity]], 4)
x$fixedpar <- x$fixedpar[names(x$fixedpar)!=x$intensity]
fixedlambda <- paste(x$intensity, ifelse(is.null(x$thin), paste("=", lambda), "= an image"))
}
if(length(x$fixedpar)>0){
fixedparstring <- paste(names(x$fixedpar), signif(unlist(x$fixed),4), sep = " = ", collapse = ", ")
fixedparstring <- paste(fixedlambda, fixedparstring, sep=", ")
} else{
fixedparstring <- fixedlambda
}
}
## Partially specified model:
if(length(x$freepar)>0){
splat("The model is only partially specified.")
splat("The following parameters are free (e.g. to be estimated by dppm):")
cat(x$freepar, sep = ", ")
cat("\n")
if(anyfixed){
cat("The fixed parameters are: ")
cat(fixedparstring, sep = ", ")
} else{
splat("There are no fixed parameters.")
}
} else{
cat("The parameters are: ")
cat(fixedparstring, sep = ", ")
}
cat("\n")
if(!is.null(x$intensity)){
splat("The parameter", x$intensity,
"specifies the intensity of the process.")
}
if(is.character(x$dim)){
splat("The parameter", x$dim,
"specifies the dimension of the state space.")
}
invisible(NULL)
}
reach.detpointprocfamily <- function(x, ...){
model <- x
fun <- model$range
nam <- names(formals(fun))
do.call(model$range, c(model$fixedpar[is.element(names(model$fixedpar),nam)], list(...)))
}
dppparbounds <- function(model, name, ...){
if(inherits(model, "dppm"))
model <- model$fitted
if(!inherits(model, "detpointprocfamily"))
stop("input model must be of class detpointprocfamily or dppm")
fun <- model$parbounds
nam <- names(formals(fun))
if(missing(name))
name <- nam[!is.element(nam, c("name", model$dim))]
rslt <- matrix(0,length(name), 2, dimnames = list(name, c("lower", "upper")))
for(nn in name){
tmp <- try(do.call(fun, c(model$fixedpar[is.element(names(model$fixedpar),nam)], list(...), list(name=nn))), silent=TRUE)
if(inherits(tmp, "try-error")) {
rslt[nn,] <- c(NA, NA)
}else{
rslt[nn,] <- tmp
}
}
rslt
}
valid.detpointprocfamily <- function(object, ...){
if(length(object$freepar)>0)
return(NA)
## If there is no function for checking validity we always return TRUE:
if(is.null(object$valid))
return(TRUE)
do.call(object$valid, object$fixedpar)
}
dppspecdenrange <- function(model){
## If there is no function for checking finite range of spectral density we always return Inf:
fun <- model$specdenrange
if(is.null(fun))
return(Inf)
xx <- try(fun(model), silent = TRUE)
if(inherits(xx, "try-error")) Inf else xx
}
dppspecden <- function(model){
fun <- model$specden
if(is.null(fun))
stop("Spectral density unknown for this model!")
if(length(model$freepar)>0)
stop("Cannot extract the spectral density of a partially specified model. Please supply all parameters.")
specden <- function(x, ...){
allargs <- c(list(x), model$fixedpar, list(...))
do.call(fun, allargs)
}
return(specden)
}
dppkernel <- function(model, ...){
if(inherits(model, "dppm"))
model <- model$fitted
fun <- model$kernel
if(is.null(fun))
return(dppapproxkernel(model, ...))
if(length(model$freepar)>0)
stop("Cannot extract the kernel of a partially specified model. Please supply all parameters.")
firstarg <- names(formals(fun))[1L]
kernel <- function(x){
allargs <- c(structure(list(x), .Names=firstarg), model$fixedpar)
do.call(fun, allargs)
}
return(kernel)
}
dppapproxkernel <- function(model, trunc = .99, W = NULL){
if(inherits(model, "dppm")){
W <- model$window
model <- model$fitted
}
####### BACKDOOR TO SPHERICAL CASE ########
if(!is.null(spherefun <- model$approxkernelfun)){
spherefun <- get(spherefun)
rslt <- spherefun(model, trunc)
return(rslt)
}
###########################################
d <- dim(model)
if(is.null(W))
W <- boxx(replicate(d, c(-.5,.5), simplify=FALSE))
W <- as.boxx(W)
if(d!=ncol(W$ranges))
stop(paste("The dimension of the window:", ncol(W$ranges), "is inconsistent with the dimension of the model:", d))
Wscale <- as.numeric(W$ranges[2L,]-W$ranges[1L,])
tmp <- dppeigen(model, trunc, Wscale, stationary=FALSE)
index <- tmp$index
eig <- tmp$eig
prec <- tmp$prec
trunc <- tmp$trunc
rm(tmp)
f <- function(r){
x <- matrix(0, nrow=length(r), ncol=d)
x[,1L] <- r
basis <- fourierbasis(x, index, win = W)
approx <- matrix(eig, nrow=length(eig), ncol=length(r)) * basis
return(Re(colSums(approx)))
}
attr(f, "dpp") <- list(prec = prec, trunc = trunc)
return(f)
}
dppDpcf <- function(model, ...){
if(inherits(model, "dppm"))
model <- model$fitted
fun <- model$Dpcf
if(is.null(fun))
stop("Gradient of the pcf is not available for this model.")
if(length(model$freepar)>0)
stop("Cannot extract the gradient of the pcf of a partially specified model. Please supply all parameters.")
firstarg <- names(formals(fun))[1L]
Dg <- function(x){
allargs <- c(structure(list(x), .Names=firstarg), model$fixedpar)
do.call(fun, allargs)
}
return(Dg)
}
pcfmodel.detpointprocfamily <- function(model, ...){
kernel <- dppkernel(model, ...)
f <- function(x){
1 - (kernel(x)/kernel(0))^2
}
return(f)
}
dppapproxpcf <- function(model, trunc = .99, W = NULL){
kernel <- dppapproxkernel(model, trunc = trunc, W = W)
f <- function(x){
1 - (kernel(x)/kernel(0))^2
}
attr(f, "dpp") <- attr(kernel, "dpp")
return(f)
}
Kmodel.detpointprocfamily <- function(model, ...){
if(length(model$freepar)>0)
stop("Cannot extract the K function of a partially specified model. Please supply all parameters.")
fun <- model$Kfun
if(!is.null(fun)){
firstarg <- names(formals(fun))[1L]
Kfun <- function(r){
allargs <- c(structure(list(r), .Names=firstarg), model$fixedpar)
do.call(fun, allargs)
}
} else{
pcf <- pcfmodel(model, ...)
intfun <- function(xx){
2*pi*xx*pcf(xx)
}
Kfun <- function(r){
r <- sort(r)
if(r[1L]<0)
stop("Negative values not allowed in K function!")
r <- c(0,r)
int <- unlist(lapply(2:length(r), function(i) integrate(intfun, r[i-1L], r[i], subdivisions=10)$value))
return(cumsum(int))
}
}
return(Kfun)
}
update.detpointprocfamily <- function(object, ...){
newpar <- list(...)
if(length(newpar)==1L && is.list(newpar[[1L]]) && !is.im(newpar[[1L]]))
newpar <- newpar[[1L]]
nam <- names(newpar)
if(length(newpar)>0&&is.null(nam))
stop(paste("Named arguments are required. Please supply parameter values in a", sQuote("tag=value"), "form"))
oldpar <- object$fixedpar[!is.element(names(object$fixedpar), nam)]
thin <- object$thin
object <- do.call(object$caller, c(newpar,oldpar))
if(is.null(object$thin))
object$thin <- thin
return(object)
}
is.stationary.detpointprocfamily <- function(x){
if(is.null(x$intensity))
return(FALSE)
lambda <- getElement(x$fixedpar, x$intensity)
if(!is.null(lambda)&&is.numeric(lambda)&&is.null(x$thin))
return(TRUE)
return(FALSE)
}
intensity.detpointprocfamily <- function(X, ...){
lambda <- NULL
if(!is.null(X$intensity))
lambda <- getElement(X$fixedpar, X$intensity)
if(!is.null(lambda)){
if(!is.null(X$thin))
lambda <- lambda*X$thin
return(lambda)
}
return(NA)
}
parameters.dppm <- parameters.detpointprocfamily <- function(model, ...){
if(inherits(model, "dppm"))
model <- model$fitted
c(model$fixed, structure(rep(NA,length(model$freepar)), .Names = model$freepar))
}
dim.detpointprocfamily <- function(x){
if(is.numeric(d <- x$dim)){
return(d)
} else{
return(getElement(x$fixedpar, d))
}
}
|