Norwegian Spruces - Main Effects and Interaction: 1 Model

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Norwegian spruces - main effects and interaction

Ege
September 5, 2011

1 Model
In this document we use the following model:

X X X
λ(x, r; Hr ) = exp(κ(r)) exp[−θ1 kx−xi kα −θ2 (ri −r)β −θ3 kx−xi kα (ri −r)β ]I[r+ri < kxi −xk for
i:ri >r i:ri >r i:ri >r

where the mark intensity function κ is parametrized as piecewise constant


m
X
κ(r) = κj I(rj ≤ r < rj+1 ),
j=1

where r1 , . . . , rm+1 is a set of break points covering the mark range.


First we load spatstat and the modified functions that can handle processes
with real marks.
> library(markedCSA)
Here we will be using the Norwegian spruces dataset available in spatstat.

> data(spruces)
> Y <- spruces
> Y <- Y %mark% (marks(Y)/2)

> .mpl.maxmatrix <- 1e+07


> maxpoismean <- 1e+06
> nsim <- 39
> ND <- 150
> dtimes <- 50
> nbreaks <- 10
> HIST <- "greater"
> .mpl.maxmatrix <- 1e+06
> maxpoismean <- 1e+05
> nsim <- 9
> ND <- 50

1
> dtimes <- 10
> nbreaks <- 10
> HIST <- "greater"
> alphavec <- -c(2, 3, 4)
> betavec <- c(0, 0.001, 0.1, 0.5)
> phigen <- function(alpha, beta) {
+ phi <- function(dx, mx, mu) {
+ dt <- mx - mu
+ rslt <- c(-dx^alpha, -dt^beta, -dx^alpha * dt^beta)
+ rslt[dx < mx + mu] <- -Inf
+ rslt
+ }
+ return(phi)
+ }
> nphi <- length(phigen(1, 1)(1, 1, 1))

To define the model we need the breakpoints for the piecewise constant mark
intensity. Here we just use nbreaks intervals of the same size covering the mark
range.

> markrange <- range(marks(Y))


> markdiff <- diff(markrange)
> nbreaks

[1] 10

> markbreaks <- seq.int(markrange[1] - markdiff/1000, markrange[2] + markdiff/1000, length.o


+ 1)
> volume <- area.owin(Y$window) * diff(markbreaks)
> fn <- function(r) {
+ cut(r, breaks = markbreaks, labels = 1:nbreaks)
+ }

Next we make a data frame with different values of α and β and fit the model
for each pair of values. The fitted parameter values and the log-likelihood value
is recorded in the data frame as well and the results are saved for later model
checking.

> alphavec

[1] -2 -3 -4

> betavec

[1] 0.000 0.001 0.100 0.500

> dat <- expand.grid(alpha = alphavec, beta = betavec)


> N <- nrow(dat)

2
> kappa <- data.frame(matrix(-Inf, N, nbreaks))
> theta <- data.frame(matrix(0, N, nphi))
> for (i in 1:nbreaks) {
+ names(kappa)[i] <- paste("kappa.", i, sep = "")
+ }
> for (i in 1:nphi) {
+ names(theta)[i] <- paste("theta.", i, sep = "")
+ }
> dat <- cbind(dat, L = 0, theta, kappa, poismean = 0, converged = FALSE)
> vcovList <- vector("list", N)
> timestart <- proc.time()[1]
> Q <- quadscheme.spacetime(Y, default.dummy(Y, nd = ND), dummytimes = dtimes)
> Q
Quadrature scheme
134 data points, 76368 dummy points
Total weight 223.44
> for (i in 1:N) {
+ phi <- phigen(dat$alpha[i], dat$beta[i])
+ fit <- ppm(Q, ~fn(marks) - 1, GM3(phi, HIST), correction = "none")
+ dat$L[i] <- fit$maxlogpl
+ glmfit <- getglmfit(fit)
+ dat$converged <- glmfit$converged
+ vcovList[[i]] <- vcov(glmfit)
+ coeff <- fit$coef
+ coefname <- names(coeff)
+ theta <- coeff[substr(coefname, 1, 5) == "Inter"]
+ kappaindex <- substr(coefname, 1, 5) != "Inter"
+ kappaname <- coefname[kappaindex]
+ kappaname <- paste("kappa", substr(kappaname, 10, nchar(kappaname)), sep = ".")
+ kappa <- coeff[kappaindex]
+ datname <- substr(names(dat), 1, 5)
+ dat[i, kappaname] <- kappa
+ dat[i, datname == "theta"] <- theta
+ dat$poismean[i] <- sum(exp(dat[i, datname == "kappa"]) * volume)
+ rm(fit)
+ }
> runtime <- proc.time()[1] - timestart
> runtime
user.self
161.286
Now we make sure the fitted model does not involve too big a thinning.
> index <- which.max(dat$L)
> alpha <- dat$alpha[index]

3
> beta <- dat$beta[index]
> phi <- phigen(alpha, beta)
> kappa <- as.numeric(dat[index, datname == "kappa"])
> theta <- as.numeric(dat[index, datname == "theta"])
> simList <- replicate(nsim, ppp(x = numeric(0), y = numeric(0), window = Y$window), simplif
> timestart <- proc.time()[1]
> if (dat$poismean[index] <= maxpoismean) {
+ for (i in 1:nsim) {
+ simList[[i]] <- piecewisepois(markbreaks, kappa, Y$window)
+ cat(proc.time()[1] - timestart)
+ simList[[i]] <- thin(simList[[i]], phi, theta, output = 1)
+ if (i == nsim)
+ cat("\n")
+ }
+ }

00.7441.4442.1682.9243.744.465.1845.908

> save(dat, markbreaks, simList, vcovList, file = "all.Rdata")

You might also like