Skip to contents

Given a vector containing numeric data, this function returns a vector of smoothed data by performing the computation specified by the 'statistic' argument over the input vector moving through centered sliding window.

Usage

slidWindow(
  x,
  Tstep,
  statistic = c("sum", "mean", "median", "var", "sd", "circular.var"),
  na.rm = TRUE
)

Arguments

x

A vector containing data to smooth.

Tstep

A numeric value corresponding to the length of the sliding window (i.e., the number of values).

statistic

A character string indicating the computation to preform over the sliding window. The following arguments are currently supported:

  • 'sum' = sum the values over each sliding window.

  • 'mean' = average the values over each sliding window.

  • 'median' = compute the median over each sliding window.

  • 'var' = compute the variance over each sliding window.

  • 'sd' = compute the standard deviation over each sliding window.

  • 'circular.var' = compute the variance of circular data (angular values expressed in radians) over each sliding window.

na.rm

A logical value (i.e., TRUE or FALSE) indicating whether NA values should be stripped before the computation proceeds (default = TRUE). In case na.rm = TRUE and the selected statistic argument is 'median', the function use the '+Big_alternate' method to compute the median while ignoring NA values (see the help page of the runmed function for more information).

Value

A vector of the same length than the input data (x) which contains the smoothed data.

See also

Author

Quentin PETITJEAN

Examples


set.seed(2023) 
# compute smoothed mean over a sliding window of 15 time unit in length
slidWindow(x = rnorm(150), Tstep = 15, statistic = 'mean',  na.rm = TRUE)
#>   [1]           NA           NA           NA           NA           NA
#>   [6]           NA           NA -0.194226194 -0.142082057 -0.036829378
#>  [11]  0.118314565  0.190507140  0.270887288  0.170723922  0.212017264
#>  [16]  0.226479547  0.269371416  0.270902822  0.125918441  0.111513213
#>  [21]  0.016638736  0.073409539  0.295951953  0.231068303  0.276455477
#>  [26]  0.192250666  0.129472883  0.048697624  0.046917425  0.100352715
#>  [31]  0.094395731  0.143164146  0.164534024  0.316276705  0.386113483
#>  [36]  0.472549962  0.357937802  0.195745439  0.260361097  0.333455203
#>  [41]  0.459251912  0.484605749  0.577461789  0.650977941  0.544438950
#>  [46]  0.440972947  0.454823906  0.568785945  0.692287198  0.526674971
#>  [51]  0.470436407  0.504121517  0.530405490  0.549993895  0.372851968
#>  [56]  0.172969451  0.072362480  0.091752780  0.038598488  0.167649135
#>  [61]  0.171061022  0.162006476  0.118045735 -0.132584979 -0.056396838
#>  [66] -0.034849536 -0.112684510 -0.114122557 -0.185381809 -0.174817101
#>  [71] -0.072554675  0.006815138 -0.137890818 -0.114472157 -0.054594835
#>  [76] -0.037793901 -0.075220828 -0.127490638 -0.008385773  0.119395312
#>  [81]  0.090534690  0.094492933 -0.078721822 -0.165950729 -0.128999886
#>  [86] -0.122696575  0.028181517  0.025634573  0.093802293 -0.122072421
#>  [91] -0.129542941 -0.216946054 -0.314648831 -0.329912429 -0.403467290
#>  [96] -0.308578177 -0.189744110  0.001340017  0.092817725  0.016133218
#> [101]  0.055754739 -0.156452988  0.013694026 -0.024371410  0.075710339
#> [106]  0.108830519  0.026415251  0.164891940  0.160252572  0.100545874
#> [111]  0.212127398  0.193856519  0.256266871  0.225727618  0.298372787
#> [116]  0.285239402  0.368536655  0.224799361  0.253416217  0.206707360
#> [121]  0.175433357  0.290854974  0.324037668  0.315362921  0.542227002
#> [126]  0.393813955  0.410500190  0.264166571  0.383539378  0.236414186
#> [131]  0.271838950  0.236717045  0.313310149  0.224691425  0.260823958
#> [136]  0.320118724  0.226212228  0.142834041  0.188207260  0.052744830
#> [141]  0.039321250 -0.031673798 -0.044879985           NA           NA
#> [146]           NA           NA           NA           NA           NA

# compute smoothed variance over a sliding window of 15 time unit in length
slidWindow(x = rnorm(150), Tstep = 15, statistic = 'var',  na.rm = TRUE)
#>   [1]        NA        NA        NA        NA        NA        NA        NA
#>   [8] 1.0311998 1.0336001 0.6963756 0.5367159 0.5194341 0.4556176 0.4373905
#>  [15] 0.4381824 0.5352017 0.8295470 0.7657615 0.5436327 0.8428741 0.9182289
#>  [22] 0.8518782 0.9439371 0.9300469 0.9314237 1.0104034 1.0087049 1.0688646
#>  [29] 1.1245997 1.1162607 1.1255670 0.8774615 0.8249182 0.7973200 0.5530188
#>  [36] 0.4929009 0.5104074 0.4291850 0.5837271 0.7951492 0.7543580 0.8979868
#>  [43] 0.7895734 1.0676118 1.1864629 1.1649968 1.4842808 1.3970380 1.4032790
#>  [50] 1.3585842 1.2174279 1.0984288 1.1086285 1.3846816 1.2461455 1.2894626
#>  [57] 1.2471931 1.2402450 0.9716234 0.8929746 1.0428331 0.5662800 0.7925613
#>  [64] 0.9095909 0.8492879 0.8458184 0.8510832 0.9387065 0.9119217 0.8746710
#>  [71] 0.8916285 0.8836690 0.8460196 0.8623367 0.9106981 0.8511991 0.9107934
#>  [78] 0.9533728 0.8685234 1.2042247 1.2533572 1.2001388 1.2122810 1.1596694
#>  [85] 1.1535457 1.2904178 1.2983435 1.2751031 1.2185360 1.2791389 1.3144553
#>  [92] 1.3863777 0.8272859 0.8286052 0.6949333 0.6970022 0.7020305 0.6396342
#>  [99] 0.7930941 0.7893645 0.6383795 0.6535180 0.6229440 0.6664506 0.6859611
#> [106] 0.8663319 0.8108366 0.7928647 0.7481595 0.6752311 0.6501400 0.6692449
#> [113] 0.7776988 0.6887659 0.7549126 0.7638126 0.7299134 0.7116201 0.7027583
#> [120] 0.7354203 0.3756680 0.4077747 0.7743371 0.9442099 0.9074310 0.8802945
#> [127] 0.8816763 0.8481104 0.9269095 0.9989280 1.0054827 0.9097580 0.9142402
#> [134] 1.0756216 1.1076018 1.1845236 1.1801116 0.8838022 0.6355241 0.6575411
#> [141] 0.6792715 0.6737378 0.6816449        NA        NA        NA        NA
#> [148]        NA        NA        NA