Skip to contents

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 = 4.2,
  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 (cm) separating wet / dry end (default 4.2, corresponding to pF approximately 0.62).

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)
#> make_obs_matrices(): dropped 48 of 4800 rows with a missing covariate, head or theta_n.
dim(m$Xseq)   # [N, K, p + 1]
#> [1] 4752   16    6
range(m$pf)   # normalised to [0, 1]
#> [1] 0.2083333 0.9375000

# 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"))
#>   rows_in rows_used   dropped 
#>      4800      4752        48