1

I am working with an NDVI time series raster composite (1989 to 2019). I am running a time series function on each pixel of the raster stack using the R code provided by Pironkova et al. 2018. The code looks like this:

#install packages
install.packages("raster")
install.packages("rkt")
install.packages("trend")
install.packages("zoo")


#load packages 

require(raster)
require(rkt)
require(trend)
require(zoo)

rasterOptions(maxmemory=1e+06, chunksize=1e+07, progress = 'text')

# Set working directory (location of raster)
setwd("D:/Local /Data/NDVI_10yr")
results <-"D:/Local /Data/NDVI_10yr/Results"

# stack all files with .tif extension in working directory

stacked_files <-stack(list.files(pattern = ".tif"))

#brick the stacked files

bricked_files <-brick("ndvi30year_sbr.tif")

#remove unnecessary files from memory
rm(stacked_files)
gc()

plot(bricked_files)

# Set year range for analysis
years <-seq(1989, 2019)


# Analysis function
rktFun <-function(x) {
  if(all(is.na(x))){
    c(NA,NA,NA)
  } else {
    analysis <-rkt(years, x)
    a <-analysis$B # theil sen slope
    b <-analysis$sl # pvalue
    c <-analysis$tau # tau
    return(cbind(a, b, c))
  } }
rRaster <-calc(bricked_files, rktFun)


This function returns an error :

*Error: first mandatory vector should be positive: year or year+fraction
       second mandatory vector should be numerical: measured data (ties allowed)
       third optional vector should be positive integer: season, month, site or a unique code for season and site
       fourth optional vector should be numerical: covariable
       all with the same length, at least 4*

Where am I going wrong?

4
  • Can you help us understand what you are doing? Questions that are large blocks of code that we can't run without your data are unlikely to get good answers because we have to do a load of prep (installing packages, simulating data etc) before we can reproduce your error and then debug it for you. Its hard, even for experts, to look at someone's code and see "Oh, X and Y should be the other way round" or whatever the problem is. Can you make a minimal example that gives the same error, perhaps with simple data created in the code itself?
    – Spacedman
    Apr 19 at 8:22
  • I am working with a time series raster of NDVI, each band represents a year's NDVI for the study area. I want to run the time series analysis on each pixel using the aforesaid function.
    – Purnendu
    Apr 19 at 10:07
  • That doesn't help much. The error message seems to be saying something about data not being right - "first mandatory vector should be positive" etc. Maybe your data has negative numbers in it? How would we know? Without a way we can replicate your error its difficult for anyone to answer your question unless the error is really really obvious.
    – Spacedman
    Apr 19 at 10:10
  • My data is in Raster format. So how should I share my data for you to test.
    – Purnendu
    Apr 19 at 11:25

1 Answer 1

1

Mucking around with this for half an hour and I can generate that message if the years vector is not the same length as the number of layers in the brick.

Sample data

     r1 = raster(matrix(runif(12),3,4))
     r2 = raster(matrix(runif(12),3,4))
     r3 = raster(matrix(runif(12),3,4))
     r4 = raster(matrix(runif(12),3,4))
     s = brick(r1,r2,r3,r4)

Try with correct vector of years:

 years = 1:nlayers(s)
 cc = calc(s, rktFun)

So that works.

Try with wrong number of years:

years = 2:nlayers(s)
c = calc(s, rktFun)
Error: first mandatory vector should be positive: year or year+fraction
       second mandatory vector should be numerical: measured data (ties allowed)
       third optional vector should be positive integer: season, month, site or a unique code for season and site
       fourth optional vector should be numerical: covariable
       all with the same length, at least 4

Of course I can't know this is also your problem because you've not shown us the size of your brick data. But your years length is:

> years <-seq(1989, 2019)
> length(years)
[1] 31

and your data is called ndvi30year_sbr.tif so is that meant to be 30 layers? Yes, 2019 minus 1989 is exactly 30 but this is a sequence from start to finish values so there's 31 of them (lookup "fencepost error" if this is what you've done).

For future questions, please always either give sample data that shows the problem, or link to data files that you work with, or display as much information as possible about your data (size, coordinates, summaries etc) and make your code as minimal as possible without unneeded packages. I think this only needs rkt loaded (although that may depend on zoo and trend, the only function being used is from rkt).

1
  • Thank you. The code is working now
    – Purnendu
    Apr 19 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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