--- title: "Which selector to trust: planted truth at several effect sizes" output: rmarkdown::html_vignette: toc: true toc_depth: 2 vignette: > %\VignetteIndexEntry{Which selector to trust: planted truth at several effect sizes} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include = FALSE} run <- requireNamespace("agridat", quietly = TRUE) knitr::opts_chunk$set(collapse = TRUE, comment = "#>", eval = run) library(flexySelect) ``` Two selectors run on the same data often keep different terms, and the data alone cannot say which of them is right, because the true model is unknown. This page plants a known truth into the design of a real field trial, at several effect sizes, and scores how often each selector recovers it. The selector that recovers planted effects on this design is the one to trust on it. # Data The Las Rosas trial is a corn field in Córdoba, Argentina. It has six nitrogen rates laid out in strips, three replicates, and 3,443 yield-monitor records from the 1999 and 2001 harvests (Anselin, Bongiovanni and Lowenberg-DeBoer, 2004). It ships with the 'agridat' package. The data were collected to ask whether the yield response to nitrogen changes with landscape position and with soil brightness, which is higher on soils low in organic matter. ```{r data} d <- agridat::lasrosas.corn dim(d) table(d$year, d$topo) ``` The landscape positions are the west slope (W), the hilltop (HT), the east slope (E) and the low east (LO). The candidate terms are: * the nitrogen rate and its square * soil brightness (`bv`) * indicators for HT, LO and W, with the east slope as the reference * an indicator for the 2001 season * nitrogen by LO, nitrogen by HT, and nitrogen by brightness Nitrogen is centred and expressed in hundreds of kg/ha. Brightness is centred and divided by ten, and the nitrogen-by-brightness term is the product of the two centred variables. All candidates are fixed effects, so the example runs in seconds. ```{r candidates} n_c <- (d$nitro - mean(d$nitro)) / 100 bv_c <- (d$bv - mean(d$bv)) / 10 x <- cbind( nitro = n_c, nitro_sq = n_c^2, bv = bv_c, topo_HT = as.numeric(d$topo == "HT"), topo_LO = as.numeric(d$topo == "LO"), topo_W = as.numeric(d$topo == "W"), year_2001 = as.numeric(d$year == 2001L), nitro_x_LO = n_c * (d$topo == "LO"), nitro_x_HT = n_c * (d$topo == "HT"), nitro_x_bv = n_c * bv_c ) round(cor(x)[c("nitro", "nitro_x_LO", "nitro_x_HT"), ], 2) ``` The candidates are far from independent: each interaction is correlated with nitrogen, and the landscape indicators with the interactions built on them. Correlated candidates are where selectors part company. # Two selectors disagree `marginal_bh` fits each candidate alone against yield, takes the Wald p-value of its slope, and keeps the candidates that pass a Benjamini-Hochberg step-up at a false discovery rate `q`. `forward_stepwise` starts from the intercept and adds, one at a time, the candidate that lowers the AIC most, stopping when none does. ```{r select} problem <- fs_problem(d$yield, x) res_bh <- fs_select(problem, "marginal_bh", q = 0.05, seed = 1L) res_step <- fs_select(problem, "forward_stepwise", seed = 1L) res_bh$terms[res_bh$selected] res_step$terms[res_step$selected] ``` Marginal screening keeps both nitrogen-by-landscape interactions, and stepwise selection drops all three interactions. On these data the two answers to the trial's own question, whether the nitrogen response changes with landscape position, are opposite. # Planting a known truth `fs_implant()` keeps the real candidates and draws a new outcome from a stated truth. With `mode = "simulate"`, it picks `n_true` of the columns as true predictors, gives them effects, and adds noise. Given the real yield as `y`, the new outcome has the mean and variance of the real one. The effect size is `r2`, the share of the outcome's variance that the planted effects explain. ```{r implant} sim <- fs_implant(x, y = d$yield, n_true = 3L, r2 = 0.02, seed = 1L) sim$truth$support ``` Every selector can now be scored against `sim$truth`, because the true support is known. # Effect size as a factor of the study A single draw says little. A study repeats the draw and crosses the effect size with the selectors. `fs_grid()` crosses every argument of the generator given more than one value, so three values of `r2` give three data-generating mechanisms. The data arguments `x` and `y` are each one value. `fs_study()` writes the plan, with the replicate count and the seed, and `fs_run()` runs it. ```{r study} grid <- fs_grid(fs_implant, x = x, y = d$yield, n_true = 3L, r2 = c(0.005, 0.02, 0.08)) grid study <- fs_study( aims = "Which selector recovers planted effects on the Las Rosas design?", data = grid, methods = c("marginal_bh", "forward_stepwise"), target = "support", n_sim = 20L, seed = 20261203L ) run <- fs_run(study) ``` The true positive rate is the share of the planted predictors a selector keeps, and the false discovery rate is the share of its kept predictors that were not planted. Each comes with its Monte Carlo standard error. ```{r performance} perf <- run$performance perf <- perf[perf$truth_def %in% "exact" & perf$measure %in% c("tpr", "fdr"), c("method", "mechanism", "measure", "estimate", "mcse")] perf$estimate <- round(perf$estimate, 2) perf$mcse <- round(perf$mcse, 2) perf ``` Both selectors find more of the planted predictors as the effects grow. The difference is in what else they keep. The false discovery rate of stepwise selection falls as the effects grow, while that of marginal screening climbs, to about one false term for each true one. Once a true predictor has a strong effect, the candidates correlated with it are significant on their own, and a test of one candidate at a time cannot tell them apart. On this design, the stepwise answer is the one to trust, and the interactions that marginal screening kept on the real yield are the kind of term it keeps falsely. Twenty replicates keep this page fast. A study to report runs more, and `fs_study(mcse = )` chooses the count that reaches a stated precision. # Other ways to set the effect size | Generator | Effect size | Pattern across true predictors | |---|---|---| | `fs_design()` | `r2`, the share of latent variance | `effects`: equal, Gaussian or decreasing | | `fs_implant()` | `r2`, and `r2_pair`, `r2_group`, `r2_slope` | `effects`, as in `fs_design()` | | `fs_plant()` | `effect`, in outcome standard deviations | one planted treatment | | `fs_grid()` on a regime | an edge coefficient, as `"X->Y" = c(0, 0.2)` | one coefficient per edge | Any of them can be crossed in `fs_grid()` in the same way as `r2` above. # References Anselin, L., Bongiovanni, R., & Lowenberg-DeBoer, J. (2004). A spatial econometric approach to the economics of site-specific nitrogen management in corn production. *American Journal of Agricultural Economics, 86*(3), 675--687. https://doi.org/10.1111/j.0002-9092.2004.00610.x