R package spatstat:当像素图像值为数字时,如何使用点过程模型协变量作为因子

时间:2014-10-02 14:22:36

标签: r spatial ppm spatstat

我正在尝试使用R中spatstat包中的ppm()函数为图像协变量建模点过程。我将我的栅格转换为im对象 与spatstat一起使用,我遇到了一个问题,使用im作为模型中的协变量。像素值是数字,但实际上只是代码 对于不同的景观区域,问题的关键在于使模型将像素值读取为因子而不是数值。我尝试了以下内容 两种方法(R代码和数据如下)。第一个包括在转换之前将栅格值从数字转换为因子 栅格对象到im对象。使用as.factor()函数,这似乎具有将值转换为因子所需的效果。但是,当我 使用此协变量运行ppm模型,ppm()函数不包括模型中每个因子水平的参数(与参考水平相比)。宁 它将协变量视为数字,只有一个协变量的一个参数。第二种方法是使用因子(协变量)运行ppm模型 用于指定公式参数中的协变量,而不仅仅是协变量本身。这实际上适用于拟合模型,给我一个参数 对于与参考相比的每个因子水平。但是,当我运行predict.ppm()来获取我的预测时,它会失败,因为我在公式中使用了factor() 论点。 qustion是,如何运行ppm模型,以便将协变量图像的值识别为因子,从而使模型与 每个因子水平的参数估计(减去参考)并允许使用predict.ppm()进行预测。

点过程数据采用csv格式:https://www.dropbox.com/s/tp1opzsmc14e2hb/EbolaData_AnalyticSet_8.8.14.csv?dl=0

协变量的tiff文件位于:https://www.dropbox.com/s/0fyt0jflokrpp5z/anthrome2000_global_5min.tif?dl=0

R代码如下:

library(raster)
library(spatstat)
library(geostatsp)

# First set the geographic extent we'll be using
e <- extent(-20, 60, -40, 35)

# Then read in the point process data in .csv file:
outbreaks <- read.csv("EbolaData_AnalyticSet_8.8.14.csv")
coordinates(outbreaks) <- ~Longitude+Latitude
proj4string(outbreaks) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs") 

# Then read in the anthropogenic biome tiff and crop it
anthro2000 <- raster("anthrome2000_global_5min.tif")
anthro2000.africa <- crop(anthro2000, e)

# Then we define oour point process and window for spatstat:
SP <- as(outbreaks, "SpatialPoints")
outbreaks.ppp <- as(SP, "ppp")

# Now let's create a window
SP.win <- as(e, "SpatialPolygons")
W <- as(SP.win, "owin")

# Before creating the im object, let's convert the pixel values in raster to factor:
is.factor(anthro2000.africa)
f <- as.factor(anthro2000.africa)
is.factor(f)
rat <- levels(f)[[1]]
rat$anthro <- c("Urban", "Mixed Settle", "Rice Villages", "Irrigated villages", "Rainfed villages", "Pastoral vilalges",
    "Resid. irrig. cropland", "Resid. rainfed cropland", "Pop. cropland", "Remote cropland", 
    "Resid. rangeland", "Pop. rangeland", "Remote rangeland", "Resid. forests", "Pop. forests",
    "Remote forests", "Inhabited treeless and barren", "Wild forests", "Wild treeless and Barren")
rat$code <- c(11,12,21,22,23,24,31,32,33,34,41,42,43,51,52,53,54,61,62)
levels(f) <- rat

# Now let's convert that raster to an im object for use in spatstat:
anthro2000.africa.img <- asImRaster(f)

# Everything is now set up for the ppm models

# Aprroach 1
spatial.m.1 <- ppm(outbreaks.ppp, ~ Cov1, covariates = list(Cov1 = anthro2000.africa.img))
spatial.m.1 # Notice the model is fitted, however the pixel values of the covariate are not interepreted as factor


# Approach 2:
spatial.m.2 <- ppm(outbreaks.ppp, ~ factor(Cov1), covariates = list(Cov1 = anthro2000.africa.img)) # Noticce the use of factor() here to force the covariate as factor
spatial.m.2 # Here the model is fitted correctly with covariate as factor and thus each factor level has a parameter estimate in the model (relative to the ref)

# However, the approach does not allow me to run the predictions:
p <- predict.ppm(spatial.m.2, covariates = list(Cov1 = anthro2000.africa.img))

1 个答案:

答案 0 :(得分:1)

问题在于R没有因子值矩阵,因此将因子放入im总是有点古怪,但一旦完成,一切都按预期工作。我的解决方案只是将整数值栅格转换为im格式并从那里处理所有内容(我不是光栅包的常规用户)。

我必须为命令SP <- as(outbreaks, "SpatialPoints")加载maptools库才能工作。另外,由于第一列中的一些奇怪的字符(我们还没有使用),R无法读取给定的csv文件,所以我不得不删除这些以使一切工作。

ppm下面的语法要求您运行spatstat 1.37-0或更新版本。此外,我正在使用1.38-0的新通用函数Window,对于旧版本,你需要稍微改变一下(我强烈推荐最新版本1.38-1):

library(raster)
library(spatstat)
library(geostatsp)
library(maptools)

# First set the geographic extent we'll be using
e <- extent(-20, 60, -40, 35)

# Then read in the point process data in .csv file:
outbreaks <- read.csv("EbolaData_AnalyticSet_8.8.14.csv")
coordinates(outbreaks) <- ~Longitude+Latitude
proj4string(outbreaks) <-
    CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs") 

# Then we define our point process (with the bounding box as temporary window)
# for spatstat:
SP <- as(outbreaks, "SpatialPoints")
outbreaks.ppp <- as(SP, "ppp")

# Then read in the anthropogenic biome tiff and crop it
anthro <- raster("anthrome2000_global_5min.tif")
anthro <- crop(anthro, e)

# Now let's convert that raster to an im object for use in spatstat
# (and make it into a factor):
anthro <- asImRaster(anthro)
anthro <- eval.im(as.factor(anthro))
levels(anthro) <-
    c("Urban", "Mixed Settle", "Rice Villages", "Irrigated villages",
      "Rainfed villages", "Pastoral vilalges", "Resid. irrig. cropland",
      "Resid. rainfed cropland", "Pop. cropland", "Remote cropland",
      "Resid. rangeland", "Pop. rangeland", "Remote rangeland",
      "Resid. forests", "Pop. forests", "Remote forests",
      "Inhabited treeless and barren", "Wild forests",
      "Wild treeless and Barren")

# Make Africa into the observation window (of type mask):
Window(outbreaks.ppp) <- Window(anthro)

# See the data we have read in:
plot(anthro)
plot(outbreaks.ppp, add = TRUE)

# Fit model and predict:
spatial.m.1 <- ppm(outbreaks.ppp ~ anthro)
p <- predict.ppm(spatial.m.1)