0

I'm struggling with a function from the inlabru R package, which could help me fill in the NAs in my data.

According to the helpfile, I need:

bru_fill_missing(
  data,
  where,
  values)

data is a SpatialPointsDataFrame, which I have - it contains a data.frame of 17 climate variables, some of these include NAs.

where should be "A matrix, data.frame, or SpatialPoints or SpatialPointsDataFrame, containing the locations of the evaluated values" - I'm guessing this would be the coordinates of the SpatialPointsDataFrame?

values is "A vector of values to be filled in where is.na(values) is TRUE" - so this should be one of the climate variables from the SpatialPointsDataFrame.

cec2 <- inlabru:::bru_fill_missing(grid2, grid2@coords, grid2$cec)

...gives me:

Error in inlabru:::bru_fill_missing(grid2, grid2@coords, grid2$cec) : 
  inherits(data, c("SpatialPixelsDataFrame", "SpatialGridDataFrame")) is not TRUE

Why is this not working? I seem to be using the right sort of objects:

> class(grid2@coords)
[1] "matrix" "array" 
> class(grid2)
[1] "SpatialPointsDataFrame"
attr(,"package")
[1] "sp"
> class(grid2$cec)
[1] "numeric"
> grid2
class       : SpatialPointsDataFrame 
features    : 12179 
extent      : 499974.9, 1928094, 8696615, 9781413  (xmin, xmax, ymin, ymax)
crs         : NA 
variables   : 17
names       :     gbifID,             Species,    ID, Latitude, Longitude,    costpath,         cec,      Temp10,  thermicity,         tri,        fpar,        lai,        Elev,   soc_0.5cm,         sq3, ... 
min values  :         49, Abelmoschus manihot,     1,  -11.521,       141, 0.158908026, 4.099550837,           0, 12.01840072, 0.160710489, 5.959515748, 1.23322692,           0, 43.46748724,           0, ... 
max values  : 3039118788, Zygogynum argenteum, 13690,  -1.9667,   153.985, 422.5197024, 426.9292313, 101.4610783, 726.4499222, 3756.437675,         254,        254, 4204.876009,  1324.01969, 5.413501295, ... 

2 Answers 2

1

The documentation looks wrong. The code is:

function (data, where, values, layer = NULL, selector = NULL, 
    batch_size = 500) 
{
    stopifnot(inherits(data, c("SpatialPixelsDataFrame", "SpatialGridDataFrame")))

so the first thing it does is stop if the data isn't a SpatialPixels- or SpatialGrid- data frame.

You might be able to coerce your points to a pixels data frame:

as(grid2,"SpatialPixelsDataFrame")

but non-regular points will fail:

suggested tolerance minimum: 0.0146641 
Error in points2grid(points, tolerance, round) : 
  dimension 1 : coordinate intervals are not constant

The example code uses a spatial pixels data frame. Suggest you report this on their bug tracker: https://github.com/inlabru-org/inlabru/issues - ok I've done that now: https://github.com/inlabru-org/inlabru/issues/116

1
  • Thank you! - I suppose what they are doing makes sense, as you'd want to interpolate data from a grid covering the entire region (which would therefore have constant cordinate intervals). I think I'd better do that in QGIS though. Commented May 7, 2021 at 11:10
1

The documentation was as intended, stating support for SpatialPointsDataFrame input, but the check in the code was wrong, and that has now been fixed in the development version on github. (The support for SpatialGridDataFrame was also broken, but has also been fixed.)

The bru_fill_missing() function was added mainly to handle very local infilling on domain boundaries. For properly missing data, one should consider doing a proper model of the spatial field instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.