Title: | Trend Estimating Tools |
---|---|
Description: | The traditional linear regression trend, Modified Mann-Kendall (MK) non-parameter trend and bootstrap trend are included in this package. Linear regression trend is rewritten by '.lm.fit'. MK trend is rewritten by 'Rcpp'. Finally, those functions are about 10 times faster than previous version in R. Reference: Hamed, K. H., & Rao, A. R. (1998). A modified Mann-Kendall trend test for autocorrelated data. Journal of hydrology, 204(1-4), 182-196. <doi:10.1016/S0022-1694(97)00125-X>. |
Authors: | Dongdong Kong [aut, cre] , Heyang Song [aut] |
Maintainer: | Dongdong Kong <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.5 |
Built: | 2024-11-07 06:34:07 UTC |
Source: | CRAN |
If valid observations <= 5, NA will be returned.
mkTrend_r(y, ci = 0.95, IsPlot = FALSE) mkTrend(y, x = seq_along(y), ci = 0.95, IsPlot = FALSE)
mkTrend_r(y, ci = 0.95, IsPlot = FALSE) mkTrend(y, x = seq_along(y), ci = 0.95, IsPlot = FALSE)
y |
numeric vector |
ci |
critical value of autocorrelation |
IsPlot |
boolean |
x |
(optional) numeric vector |
mkTrend is 4-fold faster with .lm.fit
.
Z0
: The original (non corrected) Mann-Kendall test Z statistic.
pval0
: The original (non corrected) Mann-Kendall test p-value
Z
: The new Z statistic after applying the correction
pval
: Corrected p-value after accounting for serial autocorrelation
N/n*s
Value of the correction factor, representing the quotient of the number
of samples N divided by the effective sample size n*s
slp
: Sen slope, The slope of the (linear) trend according to Sen test
slp is significant, if pval < alpha.
Dongdong Kong
Hipel, K.W. and McLeod, A.I. (1994), Time Series Modelling of Water Resources and Environmental Systems. New York: Elsevier Science.
Libiseller, C. and Grimvall, A., (2002), Performance of partial Mann-Kendall tests for trend detection in the presence of covariates. Environmetrics 13, 71–84, doi:10.1002/env.507.
fume::mktrend
and trend::mk.test
x <- c(4.81, 4.17, 4.41, 3.59, 5.87, 3.83, 6.03, 4.89, 4.32, 4.69) r <- mkTrend(x) r_cpp <- mkTrend(x, IsPlot = TRUE)
x <- c(4.81, 4.17, 4.41, 3.59, 5.87, 3.83, 6.03, 4.89, 4.32, 4.69) r <- mkTrend(x) r_cpp <- mkTrend(x, IsPlot = TRUE)
NA and Inf values in the y will be ignored automatically.
movmean(y, halfwin = 1L, SG_style = FALSE, w = NULL) movmean2(y, win_left = 1L, win_right = 0L, w = NULL) movmean_2d(mat, win_left = 3L, win_right = 0L)
movmean(y, halfwin = 1L, SG_style = FALSE, w = NULL) movmean2(y, win_left = 1L, win_right = 0L, w = NULL) movmean_2d(mat, win_left = 3L, win_right = 0L)
y |
A numeric vector. |
halfwin |
Integer, half of moving window size |
SG_style |
If true, head and tail values will be in the style of SG (more weights on the center point), else traditional moving mean style. |
w |
Corresponding weights of y, with the same length. |
win_left , win_right
|
windows size in the left and right |
mat |
numeric matrix |
x <- 1:100 x[50] <- NA; x[80] <- Inf s1 <- movmean(x, 2, SG_style = TRUE) s2 <- movmean(x, 2, SG_style = FALSE) movmean2(c(4, 8, 6, -1, -2, -3, -1), 2, 0) movmean2(c(4, 8, NA, -1, -2, Inf, -1), 2, 0)
x <- 1:100 x[50] <- NA; x[80] <- Inf s1 <- movmean(x, 2, SG_style = TRUE) s2 <- movmean(x, 2, SG_style = FALSE) movmean2(c(4, 8, 6, -1, -2, -3, -1), 2, 0) movmean2(c(4, 8, NA, -1, -2, Inf, -1), 2, 0)
Set dimensions of an Object
set_dim(x, dim) set_dimnames(x, value)
set_dim(x, dim) set_dimnames(x, value)
x |
an R object, for example a matrix, array or data frame. |
dim |
integer vector, see also |
value |
For the default method, either |
x <- 1:12 set_dim(x, c(3, 4))
x <- 1:12 set_dim(x, c(3, 4))
calculate slope of rast object
slope_rast( r, period = c(2001, 2020), outfile = NULL, fun = rtrend::slope_mk, ..., overwrite = FALSE, .progress = "text" ) rast_filter_time(r, period = c(2001, 2020))
slope_rast( r, period = c(2001, 2020), outfile = NULL, fun = rtrend::slope_mk, ..., overwrite = FALSE, .progress = "text" ) rast_filter_time(r, period = c(2001, 2020))
r |
A yearly rast object, which should have time attribute |
period |
|
outfile |
The path of outputed tiff file. If specified, |
fun |
the function used to calculate slope, see |
... |
other parameters ignored |
overwrite |
logical. If |
.progress |
name of the progress bar to use, see
|
A terra rast object, with bands of slope
and pvalue
.
library(rtrend) library(terra) f <- system.file("rast/MOD15A2_LAI_China_G050_2001-2020.tif", package = "rtrend") r <- rast(f) r time(r) slp <- slope_rast(r, period = c(2001, 2020), outfile = "LAI_trend.tif", overwrite = TRUE, fun = rtrend::slope_mk, .progress = "none" ) # if you want to show progress, set `.progress = "text"` slp plot(slp) file.remove("LAI_trend.tif")
library(rtrend) library(terra) f <- system.file("rast/MOD15A2_LAI_China_G050_2001-2020.tif", package = "rtrend") r <- rast(f) r time(r) slp <- slope_rast(r, period = c(2001, 2020), outfile = "LAI_trend.tif", overwrite = TRUE, fun = rtrend::slope_mk, .progress = "none" ) # if you want to show progress, set `.progress = "text"` slp plot(slp) file.remove("LAI_trend.tif")
slope
: linear regression slope
slope_p
: linear regression slope and p-value
slope_mk
: mann kendall Sen's slope and p-value
slope_sen
: same as slope_mk
, but with no p-value
slope_boot
: bootstrap slope and p-value
slope_sen(y, x = NULL) slope(y, x, ...) slope_p(y, x, fast = TRUE) slope_sen_r(y, x = seq_along(y), ...) slope_mk(y, x = NULL, ...) slope_boot(y, x = NULL, slope_FUN = slope, times = 100, alpha = 0.1, seed, ...)
slope_sen(y, x = NULL) slope(y, x, ...) slope_p(y, x, fast = TRUE) slope_sen_r(y, x = seq_along(y), ...) slope_mk(y, x = NULL, ...) slope_boot(y, x = NULL, slope_FUN = slope, times = 100, alpha = 0.1, seed, ...)
y |
vector of observations of length n, or a matrix with n rows. |
x |
vector of predictor of length n, or a matrix with n rows. |
... |
ignored. |
fast |
Boolean. If true, |
slope_FUN |
one of |
times |
The number of bootstrap replicates. |
alpha |
significant level, defalt 0.1 |
seed |
a single value, interpreted as an integer, or |
slope
: linear regression coefficient
pvalue
: p-value <= 0.05`` means that corresponding
slope' is significant.
sd
: Std. Error
For slope_boot
, slope is estimated in many times. The lower, mean, upper
and standard deviation (sd) are returned.
y <- c(4.81, 4.17, 4.41, 3.59, 5.87, 3.83, 6.03, 4.89, 4.32, 4.69) r <- slope(y) r_p <- slope_p(y) r_mk <- slope_mk(y) r_boot <- slope_boot(y)
y <- c(4.81, 4.17, 4.41, 3.59, 5.87, 3.83, 6.03, 4.89, 4.32, 4.69) r <- slope(y) r_p <- slope_p(y) r_mk <- slope_mk(y) r_boot <- slope_boot(y)
NA and Inf values in the y has been ignored automatically.
smooth_wSG(y, halfwin = 1L, d = 1L, w = NULL) smooth_SG(y, halfwin = 1L, d = 1L)
smooth_wSG(y, halfwin = 1L, d = 1L, w = NULL) smooth_SG(y, halfwin = 1L, d = 1L)
y |
colvec |
halfwin |
halfwin of Savitzky-Golay |
d |
polynomial of degree. When d = 1, it becomes moving average. |
w |
colvec of weight |
y <- c(1, 3, 2, 5, 6, 8, 10, 1) w <- seq_along(y)/length(y) halfwin = 2 d = 2 s1 <- smooth_wSG(y, halfwin, d, w) s2 <- smooth_SG(y, halfwin, d)
y <- c(1, 3, 2, 5, 6, 8, 10, 1) w <- seq_along(y)/length(y) halfwin = 2 d = 2 s1 <- smooth_wSG(y, halfwin, d, w) s2 <- smooth_SG(y, halfwin, d)
summary method for class ".lm.fit".. It's 200 times faster than traditional lm
.
summary_lm(obj, ...)
summary_lm(obj, ...)
obj |
Object returned by |
... |
ignored |
a p x 4 matrix with columns for the estimated coefficient, its standard error, t-statistic and corresponding (two-sided) p-value. Aliased coefficients are omitted.
set.seed(129) n <- 100 p <- 2 X <- matrix(rnorm(n * p), n, p) # no intercept! y <- rnorm(n) obj <- .lm.fit (x = cbind(1, X), y = y) info <- summary_lm(obj)
set.seed(129) n <- 100 p <- 2 X <- matrix(rnorm(n * p), n, p) # no intercept! y <- rnorm(n) obj <- .lm.fit (x = cbind(1, X), y = y) info <- summary_lm(obj)