Skip to contents

Applies the Wald–Wolfowitz runs test to a numeric vector in order to assess whether it behaves as a random sequence. The test statistic and p-value is computed using the number of runs (sequences of values above or below the median). Under the null hypothesis, the data is random. The runs test can be used to assess whether the residuals of a nonstationary model are random, indicating the suitability of the assumed nonstationary structure (e.g., linear).

Usage

eda_runs_test(values, years, alpha = 0.05)

Arguments

values

A numeric vector of values to check for randomness.

years

Numeric vector of observation years corresponding to data. Must be the same length as data and strictly increasing.

alpha

Numeric scalar in \([0.01, 0.1]\). The significance level for confidence intervals or hypothesis tests. Default is 0.05.

Value

A list containing the test results, including:

  • values: The values argument.

  • years: The years argument.

  • alpha: The significance level as specified in the alpha argument.

  • null_hypothesis: A string describing the null hypothesis.

  • alternative_hypothesis: A string describing the alternative hypothesis.

  • n: The length of the input vector after removing the median.

  • runs: The number of runs in the transformed sequence of residuals.

  • statistic: The runs test statistic, computed using runs and n.

  • p_value: The p-value derived from the normally distributed test statistic.

  • reject: If TRUE, the null hypothesis was rejected at significance alpha.

References

Wald, A. and Wolfowitz, J. (1940). On a test whether two samples are from the same population. Annals of Mathematical Statistics, 11(2), 147–162.

Examples

data <- rnorm(n = 100, mean = 100, sd = 10)
years <- seq(from = 1901, to = 2000)
sens_trend <- eda_sens_trend(data, years)
eda_runs_test(sens_trend$residuals, years)
#> $values
#>   [1]  25.46430969  -9.98727195  -9.06148722  -9.56791924  -1.53895896
#>   [6]  -1.23675557   8.02694389 -12.82381293   5.07085225   1.13658002
#>  [11]  -5.37587969   7.59712200   7.82502705  -5.56935476  17.92272579
#>  [16]  -7.24110088   6.58578273  15.19467829  25.45441327  19.97716279
#>  [21] -13.69280788  -2.58751312  21.74119769  -9.93908033   4.60602766
#>  [26]  -6.06063338  -5.04366585  -3.78884820   8.38539197   1.68850802
#>  [31]   3.70908938  14.83010696 -14.43397885   0.29741182 -16.27193945
#>  [36]   4.96679389   6.59292895  -7.58056363  -1.12924693  -5.57562782
#>  [41]   6.05892279 -19.62599883  -1.14112539   2.78266968   7.33566144
#>  [46]   4.21883084 -25.76302028  -3.26017958   4.61546335   2.98527913
#>  [51]   0.83373268 -15.91476334  -0.02367158  -7.83077000   5.94242574
#>  [56]  -6.05339522  -0.12849234 -14.60323678  19.19731253   4.10835630
#>  [61] -16.91821303  -3.20666786  -0.74185625   6.21678452  -2.01851044
#>  [66]   8.47323359  17.52362255  -4.41598954 -13.24737453 -19.27871022
#>  [71]   3.93977440  -8.86962334  13.19385284   9.33981765  -8.08620222
#>  [76]  -2.48886519  18.69185530  25.24296184  -3.29916828   1.15422156
#>  [81]  14.64029482  -8.65608495  -8.45772691   0.02367158  12.58593839
#>  [86] -12.94257747 -20.21289360   0.51516802  -3.44913389  -2.68716405
#>  [91] -14.25625068   1.10512458  -7.16781604   1.76227656   3.74176131
#>  [96]   6.47304189   7.74224258  -4.89913386  21.53207989  20.81765392
#> 
#> $years
#>   [1] 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915
#>  [16] 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930
#>  [31] 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
#>  [46] 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
#>  [61] 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975
#>  [76] 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990
#>  [91] 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
#> 
#> $alpha
#> [1] 0.05
#> 
#> $null_hypothesis
#> [1] "The input vector is random."
#> 
#> $alternative_hypothesis
#> [1] "The input vector is not random."
#> 
#> $n
#> [1] 100
#> 
#> $runs
#> [1] 53
#> 
#> $statistic
#> [1] 0.4020356
#> 
#> $p_value
#> [1] 0.6876578
#> 
#> $reject
#> [1] FALSE
#>