make_obs_matrices: Build observation matrices for the CNN1D model

View source: R/data_prep.R

make_obs_matricesR Documentation

Build observation matrices for the CNN1D model

Description

Converts a prepared data frame into the arrays needed for training or evaluation: a 3-D sequence array Xseq (shape ⁠[N, K, p+1]⁠) and companion vectors pf, y, and sample weights w.

Usage

make_obs_matrices(
  df,
  x_inputs,
  scaler,
  K = 64L,
  knot_grid = seq(0, 1, length.out = K),
  pf_left = -2,
  pf_right = 7.6,
  wet_split_cm = NULL,
  w_wet = 1,
  w_dry = 1
)

Arguments

df

A prepared data frame (output of prepare_swrc_data() or compatible structure) containing covariate columns, matric_head, theta_n, and theta_max_n.

x_inputs

Character vector of covariate column names.

scaler

A fitted scaler from fit_minmax().

K

Number of knot points (default 64L).

knot_grid

Numeric vector of knot positions (default seq(0, 1, length.out = K)).

pf_left

Left boundary of the pF domain (default -2).

pf_right

Right boundary of the pF domain (default 7.6).

wet_split_cm

Matric head threshold, in centimetres, separating wet from dry observations, or NULL (default) for a single data term with every observation weighted equally. Before 0.2.0 the default was 4.2, read as 4.2 cm (pF 0.62), which put every measured point on the dry side; a split at pF 4.2 is 10^4.2 cm.

w_wet

Sample weight for wet-end observations (default 1).

w_dry

Sample weight for dry-end observations (default 1).

Value

A named list:

Xseq

3-D numeric array [N, K, p+1].

pf

Numeric matrix [N, 1] of normalised pF values.

y

Numeric matrix [N, 2]: columns are theta_n and theta_max_n.

w

Numeric vector of sample weights.

Rows of df with a missing covariate, matric_head or theta_n are dropped, with a message, because a non-finite target makes the whole batch loss NaN and so destroys every weight in one step. N is therefore the number of usable rows, not nrow(df); the attributes rows_kept (indices into df) and n_dropped record which.

Examples

df <- prepare_swrc_data(swrc_example, depth_col = "depth")
x  <- c("clay", "silt", "bd_gcm3", "soc", "Depth_num")
sc <- fit_minmax(df, x)
m  <- make_obs_matrices(df, x, sc, K = 16L)
dim(m$Xseq)   # [N, K, p + 1]
range(m$pf)   # normalised to [0, 1]

# swrc_example carries missing water contents by design, so some rows
# are dropped and N is smaller than nrow(df):
c(rows_in = nrow(df), rows_used = dim(m$Xseq)[1],
  dropped = attr(m, "n_dropped"))


soilFlux documentation built on Dec. 4, 2026, 5:10 p.m.