| kzFilter | R Documentation |
kzFilter() applies a Kolmogorov-Zurbenko filter to one or more pollutant
columns in a data frame at multiple window sizes and returns the decomposed
components. The KZ filter is a low-pass filter formed by iterating a simple
moving average k times with window size m. kzaFilter() applies an
adaptive Kolmogorov-Zurbenko filter to one or more pollutant columns in a
data frame at multiple window sizes and returns the decomposed components.
The KZA filter uses a standard KZ filter to detect structural breaks and
shrinks the window near those breaks to preserve sharp features.
kzFilter(
mydata,
pollutant = "o3",
m = c(3L, 13L, 107L, 721L, 8761L),
k = 5L,
data.thresh = 0.25,
type = "default",
components = TRUE,
comp.names = c("sub_day", "diurnal", "synoptic", "intermediate", "seasonal", "trend"),
to_narrow = FALSE,
...
)
kzaFilter(
mydata,
pollutant = "o3",
m = c(3L, 13L, 107L, 721L, 8761L),
k = 5L,
sensitivity = 1,
data.thresh = 0.5,
type = "default",
components = TRUE,
comp.names = c("sub_day", "diurnal", "synoptic", "intermediate", "seasonal", "trend"),
to_narrow = FALSE,
...
)
mydata |
A data frame containing a |
pollutant |
The name of a pollutant, e.g., |
m |
Integer vector of window sizes ( |
k |
Integer. The number of iterations applied at each window size
( |
data.thresh |
Numeric (0–1). Minimum fraction of valid (non- |
type |
Used for splitting the data further. Passed to |
components |
Logical. If |
comp.names |
Character vector of names for the component columns. Must
have length |
to_narrow |
Logical. If |
... |
Passed to |
sensitivity |
Numeric. Controls how aggressively the window shrinks at
structural breaks (higher = more aggressive). Used in |
When to_narrow = FALSE (default), a tibble with the original
columns plus intermediate filter columns (kz(a)_{m}) and component
columns. When to_narrow = TRUE, a tidy tibble with a component column
(factor ordered fast to slow) and a value column.
With the default window sizes of 3, 13, 107, 721 and 8761 (suited to hourly data), the function returns six intermediate filtered columns and six physical components derived by differencing:
sub_day — Short intra-day variations
(pollutant - kz(a)_3)
diurnal — daily cycle (kz(a)_3 - kz(a)_13)
synoptic — 1–10 day weather systems (kz(a)_13 - kz(a)_107)
intermediate — weekly to monthly variability
(kz(a)_107 - kz(a)_721)
seasonal — monthly to annual variability (kz(a)_721 - kz(a)_8761)
trend — multi-year trend (kz(a)_8761)
At the start and end of the series the filter window is silently truncated
rather than padded, so no NAs are introduced. However, values within the
affected boundary zone are averaged over fewer points than the interior and
should be interpreted with caution.
The affected length at each end of the series for a single filter pass is
floor(m / 2) observations. Because the filter is iterated k times (each
pass consuming the output of the previous one), the total affected zone at
each end is approximately k * floor(m / 2) observations. With the default
m = c(3, 13, 107, 721, 8761) and k = 5, the affected zones are roughly 5
h, 30 h (~1 day), 265 h (~11 days), 1,800 h (~75 days), and 21,900 h
(~2.5 years) at each end respectively. The trend component therefore
requires at least 5–6 years of data for the interior estimates to be
unaffected.
David Carslaw
## Not run:
# Default: 5 window sizes, 6 descriptively named components returned
mydata <- kzFilter(mydata, pollutant = "nox")
# Tidy long format
mydata <- kzFilter(mydata, pollutant = "nox", to_narrow = TRUE)
# Single window size (no component decomposition)
mydata <- kzFilter(mydata, pollutant = "nox", m = 24, k = 5)
## End(Not run)
## Not run:
# Default: 5 window sizes, 6 descriptively named components returned
mydata <- kzaFilter(mydata, pollutant = "nox")
# Tidy long format
mydata <- kzaFilter(mydata, pollutant = "nox", to_narrow = TRUE)
# Single window size (no component decomposition)
mydata <- kzaFilter(mydata, pollutant = "nox", m = 24, k = 5)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.