## Example 1: small design with one factor
set.seed(1)
df <- data.frame(
y = rnorm(20),
X1 = rnorm(20),
X2 = rnorm(20),
G = factor(sample(c("A", "B"), 20, replace = TRUE))
)
## Two-way interactions and up to cubic terms in X1 and X2
spec <- bigexp_terms(
y ~ X1 + X2 + G,
data = df,
factorial_order = 2,
polynomial_order = 3
)
print(spec)
## Example 2: pure main effects (no interactions, no polynomial terms)
spec_main <- bigexp_terms(
y ~ X1 + X2 + G,
data = df,
factorial_order = 1, # main effects only
polynomial_order = 1 # no I(X^2) or higher
)
## Example 3: blocking factors (categorical and continuous)
set.seed(2)
df_block <- data.frame(
y = rnorm(30),
X1 = rnorm(30),
X2 = rnorm(30),
G = factor(sample(c("A", "B"), 30, replace = TRUE)),
Operator = factor(sample(paste0("Op", 1:3), 30, replace = TRUE)),
AmbientTemp = rnorm(30, mean = 22, sd = 2) # continuous blocking covariate
)
## Here Operator (categorical) and AmbientTemp (continuous) are blocking factors:
## they enter additively, but do not appear in interactions or polynomials.
spec_block <- bigexp_terms(
y ~ X1 + X2 + G,
data = df_block,
factorial_order = 2,
polynomial_order = 3,
blocking = c("Operator", "AmbientTemp")
)
print(spec_block)
spec_block$rhs
## Example 4: discrete numeric predictors (finite numeric support)
## A common case is a numeric process setting that only takes a small set
## of allowed values (e.g., 0.5, 1, 2, 4). Use `discrete_numeric` in
## bigexp_terms() so downstream sampling respects those levels automatically.
# \donttest{
set.seed(3)
D_allowed <- c(0.5, 1, 2, 4)
df_disc <- data.frame(
y = rnorm(60),
D = sample(D_allowed, 60, replace = TRUE), # numeric with discrete support
X1 = rnorm(60),
G = factor(sample(c("A", "B"), 60, replace = TRUE))
)
# Record that D should be treated as "discrete numeric" for downstream sampling.
# Levels are inferred automatically from the training data.
spec_disc <- bigexp_terms(
y ~ D + X1 + G,
data = df_disc,
factorial_order = 2,
polynomial_order = 2,
discrete_numeric = "D"
)
# Fit. The discrete support is expected to propagate into fit$sampling_schema
# (assuming the updated SVEMnet implementation that stores sampling_schema).
fit_disc <- SVEMnet(spec_disc, df_disc, nBoot = 20)
# Score random candidates; sampled D values stay in D_allowed
scored <- svem_score_random(
objects = list(y = fit_disc),
goals = list(y = list(goal = "max", weight = 1)),
n = 2000,
numeric_sampler = "random",
verbose = FALSE
)
table(scored$score_table$D)
stopifnot(all(scored$score_table$D %in% D_allowed))
# }
Run the code above in your browser using DataLab