dm.na.interpolation: Detection and interpolation of missing values in dendrometer...

View source: R/dm.na.interpolation.R

dm.na.interpolationR Documentation

Detection and interpolation of missing values in dendrometer data

Description

Detects missing timestamps (based on provided resolution), inserts rows with NA, and optionally interpolates missing values using either cubic spline interpolation (na.spline) or seasonal decomposition interpolation (na.interp).

Optionally, the function can test the reliability of interpolation for increasing gap lengths (e.g., 6 hours to 3 days) using real data. This allows the user to assess how well a given interpolation method recovers missing values over different durations.

Usage

dm.na.interpolation(
  df,
  resolution,
  fill = FALSE,
  method = "spline",
  assess = FALSE,
  assess_lengths_hours = seq(6, 72, by = 6),
  assess_samples_per_length = 10,
  assess_buffer_hours = 6,
  assess_seed = NULL,
  verbose = FALSE
)

Arguments

df

A data frame with the first column as datetime ("yyyy-mm-dd HH:MM:SS", or POSIXct/Date), followed by dendrometer values.

resolution

Integer. Temporal resolution in minutes (e.g., 60 for hourly data).

fill

Logical. If TRUE, missing values will be interpolated.

method

Character. Interpolation method (only used if fill = TRUE):

  • "spline" — cubic spline (zoo::na.spline).

  • "seasonal" — seasonal decomposition (forecast::na.interp).

assess

Logical. If TRUE, run a simulation-based test to evaluate how well interpolation performs at different gap lengths (on existing data).

assess_lengths_hours

Integer vector. The gap durations (in hours) to test during assessment. Default: seq(6, 72, by = 6).

assess_samples_per_length

Integer. Number of artificial gaps per gap length per series. Default: 10.

assess_buffer_hours

Integer. Minimum number of hours of valid data required before and after the artificial gap to allow valid assessment. Default: 6.

assess_seed

Integer or NULL. Random seed to ensure reproducibility of sampling windows. Default: NULL.

verbose

Logical. If TRUE, prints messages during filling and testing.

Details

The assessment simulates interpolation over artificial gaps of different durations. For each dendrometer series and each gap length:

  1. Random windows (with clean data) are selected.

  2. Data in the window is temporarily set to NA.

  3. The gap is interpolated using the selected method.

  4. The predicted values are compared to the original (true) values using:

    • MAPE — Mean Absolute Percentage Error.

    • MdAPE — Median Absolute Percentage Error.

    • RMSE_pct — Root Mean Square Error (%).

    • Bias_pct — Mean Percentage Error (%).

    • Max_diff_abs — Mean absolute difference in daily maximum.

    • Min_diff_abs — Mean absolute difference in daily minimum.

    • Time_max_diff_h — Mean absolute difference in time of daily maximum (hours).

    • Time_min_diff_h — Mean absolute difference in time of daily minimum (hours).

Value

A list of class "dm_na_interpolation" with components:

$data

Data frame containing the original or gap-filled series.

$gap_info

Table describing timestamp gaps and internal NA runs.

$assessment

A tibble summarizing interpolation accuracy for each series and gap length, or NULL when assess = FALSE.

$filled

Logical indicating whether interpolation was performed.

$assessed

Logical indicating whether interpolation assessment was performed.

$method

Interpolation method used.

$resolution

Temporal resolution in minutes.

Examples


library(dendRoAnalyst)
data(nepa17)

#res0 <- dm.na.interpolation(nepa17[1:1000, ], resolution = 60)
#plot(res0)
#plot(res0, type = "gaps")

#res1 <- dm.na.interpolation(
#  nepa17[1:1000, ],
#  resolution = 60,
#  fill = TRUE,
#  method = "spline"
#)
#plot(res1)
#plot(res1, type = "gaps")
#plot(res1, type = "interpolation", original = nepa17[1:1000, ])

#res2 <- dm.na.interpolation(
#  nepa17[1:1000, ],
#  resolution = 60,
#  fill = TRUE,
#  method = "seasonal",
#  assess = TRUE
# )
#plot(res2)
#plot(res2, type = "assessment", metric = "MdAPE")



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