detrendfield <- function(array3d,smooth=F,...){ # Description: # # Removes time trend at each grid point of a three-dimensional array of data # with first two space dimensions (e.g. longitude and latitude) and # third time dimension # # Usage: # # detrendfield(array3d,smooth=FALSE,...) # # Arguments: # # array3d: a three-dimensional array, with first two space dimensions (e.g. # longitude and latitude) and third time dimension # # smooth: Logical. If FALSE (the default) removes linear time trend by # fitting ordinary least squares regression at each grid. If TRUE, # then time trend is removed by fitting a local weighted regression # (lowess) at each grid point # # ...: Additional arguments passed to lowess # # Value: # # Three-dimensional array with the detrended data. # # Author: # # Caio Coelho 9 Jan 2006 # # Examples: # # x <- seq(-20, 20, 5) # y <- seq(30, 60, 5) # dim <- c(length(x), length(y), 100) # data <- array(rnorm(prod(dim),25,2), dim) # detrendfield(data) # detrendfield(data,smooth=TRUE) # detrendfield(data,smooth=TRUE,f=1/3) aperm(apply(array3d, c(1,2), detrend,smooth,...),c(2,3,1)) }