1

I have the following traceback and I would like some help to understand/solve it. Can anyone explain to me what to look here?

6: stop("cannot use this formula, probably because it is not vectorized")
5: .overlayList(x, fun = fun, filename = filename, forcefun = forcefun, 
       ...)
4: .local(x, y, ...)
3: raster::overlay(x, fun = tslp, ...)
2: raster::overlay(x, fun = tslp, ...)
1: raster.kendall(raster_stack, tau = TRUE, intercept = TRUE, p.value = TRUE, 
       z.value = TRUE, confidence = TRUE)

example data:

dates<-as.data.frame(
  as.Date(c('2010-10-1','2009-5-26','2008-5-10','2007-3-13','2006-2-2')))

rst = raster(matrix(1:15,3,4))
raster_stack = stack(rst,rst,rst,rst,rst)
raster_stack <- setZ(raster_stack, dates[,1], "my_dates")
  • 2
    @Robert Hijmans, I would really appreciate your support on this. I believe that this error relates to the raster package (?). – foo Sep 24 at 8:32
  • What are you doing to generate this error? How can we reproduce this? – Spacedman Sep 24 at 9:51
  • @Spacedman, I am trying to calculate a trend for my raster stack, as it appears on number 1. of the traceback. The raster.Kendall function is from the spatialEco library. It can be reproduced with any raster stack, I will edit my question. – foo Sep 24 at 9:58
  • Works for me if I run your setup code and then run raster.kendall(raster_stack, tau = TRUE, intercept = TRUE, p.value = TRUE,z.value = TRUE, confidence = TRUE) from the last line of your traceback. spatialEco: 1.1.1 and raster: 2.9.5 versions – Spacedman Sep 24 at 14:44
  • @Spacedman thank you for your time. I have the raster version 3.0-2 and spatialEco 1.2-0. Do you know if I can use the versions that you suggest to see if the issue persist? Is it possible to install older versions separately? – foo Sep 24 at 14:58
1

This seems to be a bug that happens when there's less than 8 layers in your stack. Here's a test function that generates and runs on an ixjxk rows x columns x layers stack:

testkendall <-
function(i,j,k){
  raster_stack = do.call(stack, replicate(k, raster(matrix(runif(i*j),i,j))))
  raster.kendall(raster_stack)
}

And it works on 23x17x8:

> testkendall(23, 17, 8)
Outputting: slope tau intercept p.value z.value LCI UCI 

class      : RasterLayer 
dimensions : 23, 17, 391  (nrow, ncol, ncell)
resolution : 0.05882353, 0.04347826  (x, y)
extent     : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
crs        : NA 
source     : memory
names      : layer 
values     : -0.1246867, 0.1165944  (min, max)

But fails on 23x17x7:

> testkendall(23, 17, 7)
Outputting: slope tau intercept p.value z.value LCI UCI 

Error in .overlayList(x, fun = fun, filename = filename, forcefun = forcefun,  : 
  cannot use this formula, probably because it is not vectorized

This is because the kendall function in spatialEco requires 8 or more layers:

     kendall(y, tau = TRUE, p.value = TRUE, z.value = TRUE,
       confidence = TRUE, intercept = TRUE, prewhiten = FALSE, na.rm, ...)

Arguments:

       y: A vector representing a timeseries with >= 8 obs

Which I suspect is a change since the docs for raster.kendall was written, which says 5 layers:

     raster.kendall(x, tau = FALSE, intercept = FALSE, p.value = FALSE,
       z.value = FALSE, confidence = FALSE, autocorrelation = FALSE, ...)

Arguments:

       x: A rasterStack object with at least 5 layers
  • 2
    Yes, I do have an error check for < 8 layers that is not reporting. That said, I have had enough issues with erroneous vectorization errors in raster:overlay and raster::calc that I am splitting out functions like this so that they can be used independently. In the development release of the package, I have the kendall function so that it operates on a vector and can then be passed to raster::overlay or used as an apply function on an SpatialPixelsDataFrame object. The raster.kendall function is being changed accordingly. – Jeffrey Evans Sep 24 at 19:28
  • 1
    BTW, I changed from 5 to 8 minimum observations to facilitate an autocorrelation via prewhitening option. I guess I did not change the documentation or error message, just the function in `spatialEco::kendall'. I am weary of a trend with fewer than 8 obs so, I think that I will leave it there regardless of applying prewhitening. Changed now, thanks. – Jeffrey Evans Sep 24 at 19:56
  • Dear both, thank you so much. I do not think that the issue is the number of layers for me, because the actual raster stack that I use has 30 objects. I used the older versions that Spacedman suggested and is working great again. BTW, great package Jeffrey Evans, thank you! – foo Sep 25 at 7:04

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