A data cleaning function for optimal Box-Cox transformation, detrending, standarizing variance, de-meaning
A locations x time data matrix, or a time series vector (for 1 location)
The times of measurement, spacing 1
The level of cleaning to do, 1 through 5. See details.
A vector of lambdas to test for optimal Box-Cox transformation, if Box-Cox is
performed. Ignored for
NAs, Infs, etc. in
dat trigger an error. If
clev==1, time series are (individually)
clev==2, time series are (individually) linearly detrended and de-meaned. If
time series are (individually) linearly detrended and de-meaned, and variances are standardized to 1. If
clev==4, an optimal Box-Cox normalization procedure is applied jointly to all time series (so the same
Box-Cox transformation is applied to all time series after they are individually shifted depending on the value
mints). Transformed time series are then individually linearly detrended, de-meaned, and variances are
standardized to 1. If
clev==5, an optimal Box-Cox normalization procedure is applied to each time series
individually (again after individually shifting according to
mints), and transformed time series are then
individually linearly detrended, de-meaned, and variances are standardized to 1. Constant time series and perfect
linear trends trigger an error for
clev>=4 and the optimal
lambda for one or
more time series is a boundary case or if there is more than one optimal lambda, it triggers a warning. A wider
lambda should be considered in the former case.
cleandat returns a list containing the cleaned data,
clev, and the optimal
lambdas from the Box-Cox procedure (
clev<4, see details).
Box, GEP and Cox, DR (1964) An analysis of transformations (with discussion). Journal of the Royal Statistical Society B, 26, 211–252.
Venables, WN and Ripley, BD (2002) Modern Applied Statistics with S. Fourth edition. Springer.
Sheppard, LW, et al. (2016) Changes in large-scale climate alter spatial synchrony of aphid pests. Nature Climate Change. DOI: 10.1038/nclimate2881
1 2 3 4 5 6 7
times<-1:100 dat<-rnorm(100) res1<-cleandat(dat,times,1) #this removes the mean res2<-cleandat(dat,times,2) #detrends and removes the mean res3<-cleandat(dat,times,3) #variances also standardized res4<-cleandat(dat,times,4) #also joint Box-Cox applied res5<-cleandat(dat,times,5) #1-3, also indiv Box-Cox
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.