Friday 26 April 2013

Code for spatstat Lab

Here is some basic code to get started with spatstat using the Beilschmiedia pendula dataset.

install.packages("spatstat")
library(spatstat)
data(bei)

plot(bei)
plot(bei.extra$elev)
plot(bei.extra$grad)

fit.1 = ppm(bei, ~poly(elev, grad, degree = 2), covariates = bei.extra)
plot(predict(fit.1))
plot(bei, add = TRUE, cex = 0.4)
diagnose.ppm(fit.1)
AIC(fit.1)

fit.2 = ppm(bei, ~poly(elev, grad, degree = 2) + poly(x, y, degree = 2), covariates = bei.extra)

plot(predict(fit.2))
plot(bei, add = TRUE, cex = 0.4)
diagnose.ppm(fit.2)
AIC(fit.2)

Can you find a model which removes the main trend in residual plots?


10 comments:

  1. To view the tree locations atop of a map of gradient, type:

    plot(bei.extra$grad)
    plot(bei, add = TRUE, cex = 0.4)

    ReplyDelete
  2. To view the tree locations atop of a map of elevation, type:

    plot(bei.extra$elev)
    plot(bei, add = TRUE, cex = 0.4)

    ReplyDelete
  3. To determine significance of model parameters:

    summary(fit.1)

    ReplyDelete
  4. fit.2 still had some problems for me but it is a lot better than fit.1.
    You can compare them using
    anova(fit.1,fit.2,test="Chisq")
    if you believe the model is plausible. Big if though.

    I reckon a key thing we are missing here is aspect - eyeballing the raw data maps this species appears to be found on southerly aspects as well as at high elevation?
    The other thing is point interactions (dispersal etc...)

    ReplyDelete
  5. It looks like aspect might be a useful covariate. Loving the residual plot functionality spatstat!

    ReplyDelete
  6. To test for dependence among point locations:

    env.1 = envelope(fit.1)
    env.2 = envelope(fit.2)

    ReplyDelete
  7. I'm going to play devil's advocate and say why didn't I just generate some random "absence" points (since we have censused all the presences, and any point that isn't a presence is by definition an absence) and do a t-test against elevation???

    ReplyDelete
  8. Okay a bit slow on getting the comment up... but here's the AICs for both fits:

    fit.1 = fit.1 = ppm(bei, ~poly(elev, grad, degree = 2), covariates = bei.extra)

    AIC = 41563.16

    fit.2 = ppm(bei, ~poly(elev, grad, degree = 2) + poly(x, y, degree = 2), covariates = bei.extra)

    AIC = 40739.85

    ReplyDelete
  9. A t-test might be useful for determining whether elevation is important, but how can it be used to make predictions of species intensity based on elevation? A point process model takes into account the spatial nature of the data such that maps of predicted intensities, etc., can be made in addition to testing the significance of covariates.

    ReplyDelete
  10. Additionally, analyzing the data as a point pattern does not require a subjective subset of absences, and further allows users to access tools for assessing goodness of fit.

    ReplyDelete