smooth_dm: Smoothing of Dendrometer Time Series

View source: R/dm_smooth.R

smooth_dmR Documentation

Smoothing of Dendrometer Time Series

Description

Applies various smoothing techniques to dendrometer (dm) time series data using a user-defined or automatically detected temporal resolution. The function supports several smoothing methods: robust median+mean, penalized spline, Savitzky-Golay filter, exponential moving average (EMA), and LOESS.

Usage

smooth_dm(
  time,
  dm,
  resolution_min = NULL,
  method = c("median_mean", "pspline", "sg", "ema", "loess"),
  window_hours = 3,
  sg_order = 2,
  ema_alpha = NULL
)

Arguments

time

A POSIXct vector representing the time column.

dm

A numeric vector of dendrometer values corresponding to time.

resolution_min

Integer. The resolution of the time series in minutes. If NULL, the resolution is auto-detected based on median time difference.

method

Smoothing method. One of: "median_mean", "pspline", "sg" (Savitzky-Golay), "ema" (exponential moving average), or "loess".

window_hours

Numeric. Smoothing window length in hours. Converted to points using resolution.

sg_order

Integer. Polynomial order for Savitzky-Golay smoothing. Ignored unless method = "sg".

ema_alpha

Numeric. Smoothing factor (0–1) for exponential moving average. If NULL, it is automatically calculated from the window size.

Details

The function is designed to smooth dendrometer time series (e.g., 10–60 min resolution) while preserving key features such as diurnal fluctuations or long-term growth, depending on the window size. It auto-detects the resolution (in minutes) if not provided.

Value

A numeric vector of the same length as dm containing the smoothed values.

Examples

## Not run: 
# Example: Create synthetic dendrometer time series (30-min resolution)
time_seq <- seq.POSIXt(from = as.POSIXct("2023-06-01 00:00:00"),
                       to   = as.POSIXct("2023-06-03 00:00:00"),
                       by   = "30 mins")
set.seed(123)
dm_raw <- cumsum(rnorm(length(time_seq), mean = 0.005, sd = 0.01)) +
          0.1 * sin(2 * pi * as.numeric(difftime(time_seq, min(time_seq), units = "hours")) / 24)

# Median plus moving mean smoothing (default robust filter)
dm_medmean <- smooth_dm(time = time_seq, dm = dm_raw,
                        method = "median_mean", window_hours = 3)

# Penalized spline smoothing
dm_pspline <- smooth_dm(time = time_seq, dm = dm_raw,
                        method = "pspline", window_hours = 6)

# Savitzky-Golay filter (requires 'signal' package)
if (requireNamespace("signal", quietly = TRUE)) {
  dm_sg <- smooth_dm(time = time_seq, dm = dm_raw,
                     method = "sg", window_hours = 2, sg_order = 2)
}

# Exponential moving average smoothing
dm_ema <- smooth_dm(time = time_seq, dm = dm_raw,
                    method = "ema", window_hours = 4)

# LOESS smoothing
dm_loess <- smooth_dm(time = time_seq, dm = dm_raw,
                      method = "loess", window_hours = 3)

# Plot raw and smoothed series
plot(time_seq, dm_raw, type = "l", col = "gray", lwd = 1, main = "Smoothed Dendrometer Series",
     ylab = "DM", xlab = "Time")
lines(time_seq, dm_medmean, col = "blue", lwd = 2)
lines(time_seq, dm_pspline, col = "green", lwd = 2)
lines(time_seq, dm_ema, col = "orange", lwd = 2)
lines(time_seq, dm_loess, col = "purple", lwd = 2)
legend("topright", legend = c("Raw", "Median+Mean", "P-spline", "EMA", "LOESS"),
       col = c("gray", "blue", "green", "orange", "purple"), lwd = 2)

## End(Not run)


dendRoAnalyst documentation built on May 20, 2026, 5:07 p.m.