On 16 Jun 2021, at 10:00, 'Michael Smith' via R-inla discussion group <r-inla-disc...@googlegroups.com> wrote:
--
You received this message because you are subscribed to the Google Groups "R-inla discussion group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to r-inla-discussion...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/r-inla-discussion-group/9d4d14a3-13b9-439b-a61c-c428786f3c34n%40googlegroups.com.
```
require(rgdal)
require(rgeos)
library(inlabru)
library(sp)
library(raster)
library(spdep)
# read in shapefile with data points and coordinates as Spatial Points
# this .shp had to be altered in QGIS using 'Multipart to singleparts' function otherwise it caused an error
grid2 <- readOGR(paste(getwd(), "/31K_envs_clipped_tobuffer_UTM_singlepoint.shp", sep = ""))
# read in boundary of New Guinea as Spatial Polygons
boundary2 <- readOGR(paste(getwd(), "/NG_boundary_UTM_10km_buffer_exBGV.shp", sep = ""))
# transform both objects to use km instead of m scale
grid2 <- spTransform(grid2, "+proj=utm +zone=54 +south +ellps=WGS72 +towgs84=0,0,1.9,0,0,0.814,-0.38 +units=km +no_defs")
boundary2 <- spTransform(boundary2, "+proj=utm +zone=54 +south +ellps=WGS72 +towgs84=0,0,1.9,0,0,0.814,-0.38 +units=km +no_defs")
# read in all environmental rasters in folder and add to list
filesshp <- list.files("C:/Users/smith/Documents/NG_rasters/inlabru", pattern=".tif")
covlist <- list()
for(i in 1:length(filesshp)) {
r1 <- raster(paste("C:/Users/smith/Documents/NG_rasters/inlabru/", filesshp[i], sep = ""))
# convert raster to Spatial Pixels
s1 <- as(r1,"SpatialPixelsDataFrame")
# check there are no NA values in SpatialPixelsDataFrame
print(table(is.na(s1@data)))
# again change projection to km
s1 <- spTransform(s1, "+proj=utm +zone=54 +south +ellps=WGS72 +towgs84=0,0,1.9,0,0,0.814,-0.38 +units=km +no_defs")
# append to list
covlist <- c(covlist, s1)
}
# set up mesh and SPDE
Mesh6 <- inla.mesh.2d(loc=grid2, boundary=boundary2, max.edge = c(25, 250), cutoff = 3,
crs="+proj=utm +zone=54 +south +ellps=WGS72 +units=km +no_defs")
matern3 <- inla.spde2.pcmatern(Mesh6,
prior.sigma = c(1, 0.01),
prior.range = c(25, 0.01))
# set up formula with 2 covariates, a spatial term and an intercept
comp2 <- coordinates ~
cost(covlist[[5]], model = "linear") +
dem(covlist[[6]], model = "linear") +
mySmooth(coordinates, model = matern3) + Intercept(1)
# fit model (throws error)
fit2 <- lgcp(comp2, data = grid2, samplers = boundary2, domain = list(coordinates = Mesh6))
```
This gives the error:
Error in .M.kindC(clx) : not yet implemented for SpatialPointsDataFrame
In addition: Warning message:
In is.na(input) : is.na() applied to non-(list or vector) of type 'S4'
> traceback()
22: stop(gettextf(" not yet implemented for %s", clx@className), domain = NA)
21: .M.kindC(clx)
20: .M.kind(x)
19: Matrix::sparseMatrix(i = which(ok), j = rep(1, sum(ok)), x = input[ok], dims = c(NROW(input), ibm_n(mapper)))
18: ibm_amatrix.bru_mapper_linear(mapper[["mappers"]][[x]], input = input[[x]], multi = multi - 1)
17: ibm_amatrix(mapper[["mappers"]][[x]], input = input[[x]], multi = multi - 1)
16: FUN(X[[i]], ...)
15: lapply(indexing, function(x) {
ibm_amatrix(mapper[["mappers"]][[x]], input = input[[x]],
multi = multi - 1)
})
14: ibm_amatrix.bru_mapper_multi(component$mapper, input = val)
13: ibm_amatrix(component$mapper, input = val)
12: amatrix_eval.component(x, data = data, ...)
11: amatrix_eval(x, data = data, ...)
10: FUN(X[[i]], ...)
9: lapply(components, function(x) amatrix_eval(x, data = data, ...))
8: amatrix_eval.component_list(model$effects[included], data = lh[["data"]])
7: amatrix_eval(model$effects[included], data = lh[["data"]])
6: FUN(X[[i]], ...)
5: lapply(lhoods, function(lh) {
included <- parse_inclusion(names(model[["effects"]]), lh[["include_components"]],
lh[["exclude_components"]])
amatrix_eval(model$effects[included], data = lh[["data"]])
})
4: evaluate_A(model, lhoods)
3: iinla(model = info[["model"]], lhoods = info[["lhoods"]], options = info[["options"]])
2: bru(components, lik, options = options)
1: lgcp(comp2, data = grid2, samplers = boundary2, domain = list(coordinates = Mesh6))
```
To view this discussion on the web, visit https://groups.google.com/d/msgid/r-inla-discussion-group/8cb27f9d-1824-4a83-9b82-d94514ecd893n%40googlegroups.com.
On 17 Jun 2021, at 13:47, 'Michael Smith' via R-inla discussion group <r-inla-disc...@googlegroups.com> wrote:
To view this discussion on the web, visit https://groups.google.com/d/msgid/r-inla-discussion-group/055b1871-ac54-429b-9346-0d743a05e13fn%40googlegroups.com.
On 17 Jun 2021, at 14:00, Finn Lindgren <finn.l...@gmail.com> wrote:
Ah, yes, if Pixels need to be on a regular grid, that happens...
for(i in 1:length(filesshp)) {
r1 <- raster(paste("C:/Users/smith/Documents/NG_rasters/inlabru/", filesshp[i], sep = ""))
s1 <- as(r1,"SpatialPixelsDataFrame")
s2 <- spTransform(s1, "+proj=utm +zone=54 +south +ellps=WGS72 +towgs84=0,0,1.9,0,0,0.814,-0.38 +units=km +no_defs")
s1 <- as(s2,"SpatialPixelsDataFrame")
covlist <- c(covlist, s1)
}
...lgcp also fills in NA values somehow, which is useful (in this case where there are very few).
On 17 Jun 2021, at 19:12, 'Michael Smith' via R-inla discussion group <r-inla-disc...@googlegroups.com> wrote:
To view this discussion on the web, visit https://groups.google.com/d/msgid/r-inla-discussion-group/2c7cab87-2378-405a-a07c-563916c59979n%40googlegroups.com.