A plan is often written by someone who never sees the data: a composer, human or automated, sends the plan to the data holder, the holder runs it, and only a return comes back. This vignette follows one run from the table the holder checks before anything runs to the report the composer writes afterwards. Five steps, each a function of the package:
conduct())run_return())default_checker())suppress_return())trace_report())agridat::hernandez.nitrogen holds 136 maize plots from a
2006 trial at five Minnesota sites, four replicates per site, at
nitrogen rates from 0 to 268 kg/ha (Hernandez and Mulla 2008). Yield is
in t/ha. Site S5 has 24 plots, the other sites 28.
data("hernandez.nitrogen", package = "agridat")
d <- hernandez.nitrogen
table(d$site)
#>
#> S1 S2 S3 S4 S5
#> 28 28 28 28 24One tool fits the response of yield to nitrogen, across all plots and
at each site. The site estimates go in the manifest’s
params table, one row per site with its plot count.
fit_response <- function(inputs, data, node) {
fit <- lm(yield ~ nitro, data = data)
slope <- unname(coef(fit)["nitro"])
n <- nrow(data)
by_site <- do.call(rbind, lapply(split(data, data$site), function(s) {
data.frame(site = as.character(s$site[1]),
slope = unname(coef(lm(yield ~ nitro, data = s))["nitro"]),
mean_yield = mean(s$yield),
n_plots = nrow(s))
}))
m <- lens_to_manifest(
"effect", slope > 0, emitter = "maize", method = "maize:ols",
metrics = list(slope = structure(slope, n_units = n),
slope_per_100 = structure(100 * slope, n_units = n),
mean_yield = structure(mean(data$yield), n_units = n)))
m@params <- by_site
m
}The plan declares two things beyond its node. The input
data must have 136 rows, no negative rate, and one row per
site, replicate and rate. The node declares what it may return: the
overall values at dose 1, the site rows at dose 2, each with its unit
and its significant figures. A site row rests on the plots in its
n_plots column.
plots <- plan_input("data", key = c("site", "rep", "nitro"),
postconditions = c(P_rows = ".n_rows == 136",
P_rate = "nitro >= 0"))
per_kg <- "t/ha per kg N/ha"
plan <- oap_plan(
id = "maize_nitrogen",
meta = list(question = "How does maize yield respond to nitrogen?"),
inputs = list(plots),
nodes = list(node(
"fit", fit_response, emits = "treatment_effects",
returns = list(
list(field = "slope", dose = 1, unit = per_kg, digits = 3),
list(field = "slope_per_100", dose = 1, unit = "t/ha", digits = 3),
list(field = "mean_yield", dose = 1, unit = "t/ha", digits = 3),
list(field = "params.slope", dose = 2, unit = per_kg,
n_units_field = "n_plots", digits = 3),
list(field = "params.mean_yield", dose = 2, unit = "t/ha",
n_units_field = "n_plots", digits = 3)))))
plan_returns(plan)
#> node field dose unit n_units_field digits
#> 1 fit slope 1 t/ha per kg N/ha <NA> 3
#> 2 fit slope_per_100 1 t/ha <NA> 3
#> 3 fit mean_yield 1 t/ha <NA> 3
#> 4 fit params.slope 2 t/ha per kg N/ha n_plots 3
#> 5 fit params.mean_yield 2 t/ha n_plots 3A table that breaks a postcondition stops the run before the first node. The error names the condition and how many rows fail, never a value.
broken <- d
broken$nitro[3] <- -33.6
msg <- tryCatch(conduct(plan, data = broken, verbose = FALSE),
postcondition_failed = function(e) conditionMessage(e))
cat(msg)
#> postcondition_failed: 1 postcondition is not met:
#> - data.P_rate `nitro >= 0`: 1 of 136 rows failThe real table passes, and the run goes ahead.
At dose 0 the return is the record of the run and its typed message
alone. Every requested value is listed as refused, with the reason
above_dose.
requests <- plan_returns(plan)
ret0 <- run_return(run, dose = 0, requests = requests)
ret0$lines
#> [1] "stop"
ret0$refused
#> node field dose reason n_rows
#> 1 fit slope 1 above_dose NA
#> 2 fit slope_per_100 1 above_dose NA
#> 3 fit mean_yield 1 above_dose NA
#> 4 fit params.slope 2 above_dose NA
#> 5 fit params.mean_yield 2 above_dose NAAt dose 1 the overall values come back. The site rows still need dose 2.
ret1 <- run_return(run, dose = 1, requests = requests)
ret1$values
#> node field dose index value
#> 1 fit slope 1 1 0.0234
#> 2 fit slope_per_100 1 1 2.34
#> 3 fit mean_yield 1 1 11.3
ret1$refused
#> node field dose reason n_rows
#> 1 fit params.slope 2 above_dose NA
#> 2 fit params.mean_yield 2 above_dose NAAt dose 2 the site rows are requested too. The checker refuses any
value resting on fewer than k units, here
k = 25 plots. A params field is checked row by
row, so S5’s row, on 24 plots, is withheld and the other four are
returned. The refusal counts the withheld rows and does not name
them.
ret2 <- run_return(run, dose = 2, requests = requests,
checker = default_checker(k = 25))
ret2$values[ret2$values$field == "params.mean_yield", ]
#> node field dose index value
#> 8 fit params.mean_yield 2 1 9.67
#> 9 fit params.mean_yield 2 2 11.5
#> 10 fit params.mean_yield 2 3 11.7
#> 11 fit params.mean_yield 2 4 11.6
ret2$refused
#> node field dose reason n_rows
#> 1 fit params.slope 2 too_few_units 1
#> 2 fit params.mean_yield 2 too_few_units 1Withholding S5’s mean yield is not enough. The overall mean is the plot-weighted mean of the five site means, so S5’s mean can be solved from what was returned:
site <- aggregate(yield ~ site, data = d, FUN = mean)
site$site <- as.character(site$site)
n <- as.vector(table(d$site))
solved <- (sum(n) * mean(d$yield) - sum(site$yield[1:4] * n[1:4])) / n[5]
all.equal(solved, site$yield[5])
#> [1] TRUEsuppress_return() looks across the cells of a release
for such routes. The holder declares that the five sites partition the
whole. What the released sites leave of the whole is then one withheld
site, so one more site is withheld to break the route.
cells <- data.frame(node = "fit", field = "mean_yield",
unit = c("all", site$site),
value = c(mean(d$yield), site$yield),
status = c(rep("released", 5), "withheld"),
n_units = c(sum(n), n))
parts <- data.frame(parent = "all", child = site$site)
res <- suppress_return(cells, partitions = parts, k = 25)
res$changes[, c("unit", "status", "rule")]
#> unit status rule
#> 1 S1 withheld complement
res$cells[, c("unit", "status")]
#> unit status
#> 1 all released
#> 2 S1 withheld
#> 3 S2 released
#> 4 S3 released
#> 5 S4 released
#> 6 S5 withheldWith two sites withheld, the released values give their combined mean but neither one alone. The check finds only the routes it is told about, here the declared partition, and those it searches for by itself.
The composer writes the report from the return. A value is written as
a reference, {{<node>:<field>}}, and
trace_report() fills it in. Any number written out must
match a value of the return at the digits shown, or be declared in
allow as not a result, such as the plot count or a nitrogen
rate.
A number the composer worked out for itself does not trace, and the report is blocked. Here the draft gives the gain from a 150 kg/ha dressing, a number the run never computed:
draft <- c(
"Across 136 plots, yield rose by {{fit:slope}} t/ha for each kg/ha of",
"nitrogen, so a 150 kg/ha dressing added about 3.5 t/ha. Mean yield was",
"{{fit:mean_yield}} t/ha.")
blocked <- trace_report(draft, ret1, allow = c(136, 150), run = run)
blocked$released
#> [1] FALSE
blocked$trace[blocked$trace$status == "untraced",
c("line", "shown", "status", "reason")]
#> line shown status reason
#> 4 2 3.5 untraced no_matchA number the report needs in another form is computed in the run, so
that it too is a value of the return. The tool already returns the gain
per 100 kg/ha as slope_per_100, and the report refers to it
instead.
report <- c(
"Across 136 plots, yield rose by {{fit:slope}} t/ha for each kg/ha of",
"nitrogen, about {{fit:slope_per_100}} t/ha per 100 kg/ha. Mean yield",
"was {{fit:mean_yield}} t/ha.")
traced <- trace_report(report, ret1, allow = c(136, 100), run = run)
traced$released
#> [1] TRUE
cat(traced$text, sep = "\n")
#> Across 136 plots, yield rose by 0.0234 t/ha for each kg/ha of
#> nitrogen, about 2.34 t/ha per 100 kg/ha. Mean yield
#> was 11.3 t/ha.
traced$trace[, c("shown", "status", "node", "field", "value")]
#> shown status node field value
#> 1 {{fit:slope}} filled fit slope 0.0234
#> 2 136 declared <NA> <NA> <NA>
#> 3 {{fit:slope_per_100}} filled fit slope_per_100 2.34
#> 4 100 declared <NA> <NA> <NA>
#> 5 {{fit:mean_yield}} filled fit mean_yield 11.3The trace carries the plan and input hashes of the return, so every number in the released report points back to one run of one plan on one table.
Hernandez, J. A., & Mulla, D. J. (2008). Estimating uncertainty of economically optimum fertilizer rates. Agronomy Journal, 100(5), 1221–1229. https://doi.org/10.2134/agronj2007.0273