temporal_mean: Smooth value across bands using moving mean window

temporal_meanR Documentation

Smooth value across bands using moving mean window

Description

temporal_mean is applicable for multiband raster image, where bands are regular timestamps or period. For each cell (local operation of map algebra), the values are averaged using moving window.

Usage

temporal_mean(obj, win = 7, cover = 0, verbose = FALSE)

Arguments

obj

Object of class ursaRaster or matrix, where spatial locations are by rows and temporal observations are by columns.

win

Positive integer. Size of moving window. Required odd value; otherwise is coerced to the closest odd integer.

cover

Numeric in the interval 0 <= cover <= 1 or positive numeric >1. The required amount of non-NA elements in window to do a filtering. Otherwise, NA value is in output cell. If cover<=1 then amount is relative to window size. Default is 0: NA values are produced only if all elements in window have NA value.

verbose

Logical. TRUE provides some additional information on console. Default is FALSE.

Details

temporal_mean is similar to function runmean(x=obj, k=win, endrule="mean") from package caTools.

Value

ursaRaster object, if obj is object of class ursaRaster.

matrix object, if obj is a matrix.

Advanced

temporal_mean is only smoothing of time-series. For time-series analysis and processing it is suggested to apply lower-level approach.

as.matrix (for ursaRaster object with argument coords=FALSE) or ursa_value return matrix with spatial component by rows and temporal component by columns. It is possible to use apply with argument MARGIN=1 to this matrix. If apply returns matrix Y, then this matrix can be coerced to ursaRaster object by calling as.ursa with argument t(Y).

      X <- as.matrix(obj)
      Y <- apply(X, 1, function(x) {y <- do_something_return_matrix(x); y})
      res <- as.ursa(t(Y))
   

For example, package caTools provides some functions for manipulation with moving window.

Author(s)

Nikita Platonov platonov@sevin.ru

References

Package caTools https://CRAN.R-project.org/package=caTools

See Also

caTools::runmean (click if package caTools is installed)

Examples

session_grid(NULL)
set.seed(352)
n <- 45 # bands
m <- 3 # sample size
k <- median(seq(n))+seq(m)-(m %/% 2)-1 ## sample subset
s <- 5 # window size
a <- round(ursa_dummy(n,min=-60,max=60,elements=15,mul=1/8))

 ## namespace of package 'caTools' is required
if (requireNamespace("caTools")) {
   b1 <- as.ursa(t(apply(as.matrix(a),1,caTools::runmean,k=s,endrule="mean")))
   b2 <- temporal_mean(a,s)
   print(b1[k])
   print(b2[k])
   print(c('identical?'=all.equal(ursa_value(b1),ursa_value(b2))))
}

a[a<(-40)] <- NA
va <- as.matrix(a) # or 'ursa_value(a)'
b3 <- temporal_mean(a,s,cover=3/4,verbose=TRUE)
b4 <- as.ursa(temporal_mean(as.matrix(va),s,cover=3/4,verbose=TRUE))
p <- list('Before moving window'=a[k]
         ,'After moving window'=b3[k]
         ,'\'temporal_mean\' to matrix'=b4[k])
print(p)
print(c('identical?'=all.equal(ursa_value(b3),ursa_value(b4))))
display(p[1:2],legend=list(list(1,"right"),list(2,"right")),decor=FALSE)

ursa documentation built on Oct. 17, 2023, 5:11 p.m.