2

I am working with NetCDF file for analysis of trend. I am using the following codes for analysis of trend..

library(modifiedmk)
library(raster)

    I <- brick("I.nc")
    d <- stack(I)
    tsfun = function(x,na.rm){
      if(all(is.na(x))){return(NaN)}
      return(pwmk(x)[["Sen's Slope"]])
    }

    t <- calc(d, fun=tsfun, na.rm = TRUE)

The above codes are doing good but when I'm replacing [["Sen's Slope"]] with other names it is throwing an error saying "Error in pwmk(x)[["P-value"]] : subscript out of bounds"

The function is stable with only two arguments [["Sen's Slope"]] and [["old. Sen's Slope"]]. I need P-Value, Tau and Z-value. Is there a solution to this?

2

First, you are using a double bracket where you do not need to and second, it is often better to use a numeric index.

library(modifiedmk)
  x <- c(Nile)

Here, if you use a single bracket it returns the desired statistic.

pwmk(x)["P-value"]

Now, if you use a numeric index you can simplify things a bit. This returns the: "P-value", "Z-value", "Sen's Slope" and "Tau", in that order.

pwmk(x)[c(4,1,2,7)]
3
  • Again, the function is only working with Sen's Slope. The raster stack have cells with NA values is that causing a problem? and with the numeric index "Error in setValues(out, x) : values must be numeric, integer, logical or factor" this error is occuring. How can I handle the NA in the function?
    – S.H
    Jan 15 '20 at 0:12
  • tsfun = function(x,na.rm){ if(all(is.na(x))){return(NaN)} return(pwmk(x)["P-value"]) } > t <- calc(d, fun=tsfun, na.rm = F) > t class : RasterLayer dimensions : 129, 135, 17415 (nrow, ncol, ncell) resolution : 0.25, 0.25 (x, y) extent : 66.375, 100.125, 6.375, 38.625 (xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 data source : in memory names : layer values : NA, NA (min, max)
    – S.H
    Jan 15 '20 at 0:24
  • Since you are writing out to a raster stack, you need to have the same number of NA's as output values (raster layers). Something like: if(all(is.na(x))){ return(rep(NA,4)) } Jan 15 '20 at 2:44

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.