Package 'mkac'

Title: Mann-Kendall correlation and Theil-Sen slope for possibly autocorrelated time series
Description: A re-do of part of the fume package to be (hopefully) feasible for larger data sets.
Authors: Ken Butler [aut, cre]
Maintainer: Ken Butler <[email protected]>
License: MIT + file LICENSE
Version: 1.1
Built: 2025-02-19 03:13:29 UTC
Source: https://github.com/nxskok/mkac

Help Index


Detrend a time series by subtracting Theil-Sen slope

Description

Detrend a time series by subtracting Theil-Sen slope

Usage

detrend(x)

Arguments

x

numeric, a time series return time series detrended

Author(s)

Ken Butler, [email protected]

Examples

y=c(2,3,5,6)
detrend(y)

Calculate S statistic for Kendall correlation with time (+1 for concordance, -1 for discordance)

Description

Calculate S statistic for Kendall correlation with time (+1 for concordance, -1 for discordance)

Usage

kendall_S(x)

Arguments

x

numeric, a time series

Value

number of concordances minus number of discordances

Author(s)

Ken Butler, [email protected]

Examples

kendall_S(c(2,3,5,4))

Kendall test statistic for testing time trend (no autocorrelation)

Description

Kendall test statistic for testing time trend (no autocorrelation)

Usage

kendall_Z(x)

Arguments

x

numeric, a time series

Value

test statistic

Author(s)

Ken Butler, [email protected]

Examples

kendall_Z(c(2,3,5,4))

Mann-Kendall correlation test, unadjusted and adjusted for autocorrelation, with P-values

Description

Mann-Kendall correlation test, unadjusted and adjusted for autocorrelation, with P-values

Usage

kendall_Z_adjusted(x, ...)

Arguments

x

numeric, a time series

...

optional arguments to "acf", eg. lag.max (see sample_size_ratio)

Value

list: unadjusted and adjusted Z statistics, effective sample size ratio, unadjusted and adjusted P-values

Author(s)

Ken Butler, [email protected]

Examples

set.seed(457299)
x=rnorm(100) # uncorrelated (no adjustment needed)
x=x+0.02*(1:100) # with trend
kendall_Z_adjusted(x)

# ar(1) data
set.seed(457298)
xx=stats::arima.sim(list(ar=0.8),100) # autocorrelated, needs adjusting
kendall_Z_adjusted(xx)
# P-value adjusted is much less significant

ratio of actual to effective sample sizes for possibly autocorrelated time series

Description

ratio of actual to effective sample sizes for possibly autocorrelated time series

Usage

sample_size_ratio(x, ...)

Arguments

x

numeric, a time series

...

optional parameters for acf()

Value

sample size ratio (a number)

Author(s)

Ken Butler, [email protected]

Examples

set.seed(457298)
xx=stats::arima.sim(list(ar=0.8),100) # posoitively autocorrelated, sample size ratio > 1
sample_size_ratio(xx)

display calculations for sum in adjustment for autocorrelation (mainly for debugging)

Description

display calculations for sum in adjustment for autocorrelation (mainly for debugging)

Usage

sum_df(x, ...)

Arguments

x

numeric, a time series

...

optional arguments for acf

Value

data frame showing terms in sum

Author(s)

Ken Butler, [email protected]

Examples

set.seed(457298)
xx=stats::arima.sim(list(ar=0.8),100) # posoitively autocorrelated, sample size ratio > 1
sum_df(xx)

Theil-Sen slope with time

Description

Theil-Sen slope with time

Usage

theil_sen_slope(y, x = 1:length(y))

Arguments

y

numeric, a time series

x

numeric, the time points of the time series (optional, defaults to 1, 2, ...)

Value

the Theil-Sen slope, a number

Author(s)

Ken Butler, [email protected]

Examples

theil_sen_slope(c(2,3,5,10))
theil_sen_slope(y = c(2,3,5,10), x = 1:4)
theil_sen_slope(y = c(2,3,5,10), x = c(1, 2, 3, 5))

plot time series with smooth trend

Description

plot time series with smooth trend

Usage

ts_plot(x, time = 1:length(x))

Arguments

x

numeric, a time series

time

numeric, the time points of the time series (default '1:n' where 'n' is length of series)

Value

a ggplot plot

Author(s)

Ken Butler, [email protected]

Examples

set.seed(457299)
x=rnorm(100) # uncorrelated
x=x+0.02*(1:100) # with trend
ts_plot(x)

Variance of Kendall correlation S statistic under null hypothesis of no trend and assuming independence

Description

Variance of Kendall correlation S statistic under null hypothesis of no trend and assuming independence

Usage

var_kendall_S(x)

Arguments

x

numeric, a time series (only its length is used)

Value

the variance (a number)

Author(s)

Ken Butler, [email protected]

Examples

var_kendall_S(rnorm(100))