Title: | Methods for the Office of Evaluation Sciences |
---|---|
Description: | Methods for statistical analysis and reporting preferred by the US Office of Evaluation Sciences (OES). This package prepares data from standard model output objects (such as from \code{lm()} and \code{estimatr::lm_robust()}) and creates visualizations of treatment effects from the prepared quantities, according to the standards of the US Office of Evaluation Sciences. |
Authors: | Ryan T. Moore [aut, cre] , Miles Williams [aut] |
Maintainer: | Ryan T. Moore <[email protected]> |
License: | GPL-3 |
Version: | 0.1.0 |
Built: | 2024-10-28 06:47:53 UTC |
Source: | CRAN |
A simulated dataset containing outcomes, intervention variables, and covariates.
df_oes
df_oes
A data frame with 10000 rows and 7 variables:
A binary 0/1 treatment assignment.
A four-level factor treatment assignment with levels "0"
, ..., "3"
.
A binary 0/1 outcome.
A binary 0/1 outcome.
A continuous normally-distributed covariate.
A binary 0/1 covariate.
A six-level factor covariate with levels "0"
, ..., "5"
.
Plot experimental results using OES style
oes_plot( prep, font = "sans", device = "pdf", treatment_fill = "#F2C446", control_fill = "#2E9AC4", digits = 3, report_stars = TRUE, xlab, ylab, title = "Outcomes under Treatment", save = FALSE, name_save = "figure1.png", base_size = 12, width = 3.1, height = 4, dpi_forplot = 300 )
oes_plot( prep, font = "sans", device = "pdf", treatment_fill = "#F2C446", control_fill = "#2E9AC4", digits = 3, report_stars = TRUE, xlab, ylab, title = "Outcomes under Treatment", save = FALSE, name_save = "figure1.png", base_size = 12, width = 3.1, height = 4, dpi_forplot = 300 )
prep |
A tidy tibble of estimates to plot, such as the output from
|
font |
Optional string giving font; defaults to |
device |
Set device for loading fonts. Default is |
treatment_fill |
Bar color for treatment conditions. |
control_fill |
Bar color for control condition. |
digits |
Integer representing number of digits after decimal point to report. Defaults to 3. |
report_stars |
Logical indicating whether to display asterisks for statistical
significance. Defaults to |
xlab |
String providing the |
ylab |
String providing the |
title |
String providing the plot title. |
save |
Logical indicating whether to save the plot. Defaults to
|
name_save |
File name for saved plot. |
base_size |
Base font size for plot. |
width |
Width of saved plot (in inches). Use 6.8 for multiple columns. |
height |
Height of saved plot (in inches). |
dpi_forplot |
Resolution of saved plot. |
oes_plot plots the observed response mean of a control group and the predicted response means of one or more treatment groups based on OES guidance on data reporting and visualization best-practice. Read more about this OES guidance at https://oes.gsa.gov/assets/files/reporting-statistical-results.pdf.
A plot; if save = TRUE
, a file containing a plot.
Miles Williams
data(df_oes) # Single binary treatment: fit <- lm(y1 ~ x1, df_oes) # Multiple treatment conditions: fit2 <- lm(y2 ~ x2, df_oes) # Using HC2 SE's from lm_robust(): fit_robust <- estimatr::lm_robust(y1 ~ x1, df_oes) fit_robust2 <- estimatr::lm_robust(y2 ~ x2, df_oes) # Using covariates and lm(): fit_covars <- lm(y2 ~ x2 + z1 + z2 + z3, df_oes) # Using covariates and lm_robust(): fit_covars_robust <- estimatr::lm_robust(y2 ~ x2 + z1 + z2 + z3, df_oes) # Specify treatment_arms: oes_prep(fit, treatment_arms = 1) |> oes_plot() # Specify treatment_vars: fit |> oes_prep(treatment_vars = "x1") |> oes_plot() # Specify treatment_arms: oes_prep(fit2, treatment_arms = 3) |> oes_plot() # Specify treatment_vars: fit2 |> oes_prep(treatment_vars = c("x21", "x22", "x23")) |> oes_plot() # Specify custom treatment_labels: prep_w_labels <- oes_prep(fit2, treatment_arms = 3, treatment_labels = c( "Email", "Email +\nReward", "Email +\nRisk"), control_label = "Status Quo") oes_plot(prep_w_labels) # Using objects from estimatr::lm_robust(): oes_prep(fit_robust, treatment_arms = 1) |> oes_plot() oes_prep(fit_robust2, treatment_arms = 3) |> oes_plot() # Specify covariates with lm: oes_prep(fit_covars, treatment_arms = 3) |> oes_plot() # Specify covariates with lm_robust(): oes_prep(fit_covars_robust, treatment_arms = 3) |> oes_plot() # For the Lin estimator, a manual version of lm_lin(): m.mat <- cbind(y2 = df_oes$y2, model.matrix(y2 ~ x2 + z1 + z2 + z3, df_oes)[, -1]) m.mat <- dplyr::mutate_at( data.frame(m.mat), .vars = c('z1', 'z2', 'z31', 'z32', 'z33', 'z34', 'z35'), function(x) x - mean(x) ) fit_lin <- estimatr::lm_robust(y2 ~ (x21 + x22 + x23) * (z1 + z2 + z31 + z32 + z33 + z34 + z35), m.mat) oes_prep(fit_lin, treatment_arms = 3) |> oes_plot()
data(df_oes) # Single binary treatment: fit <- lm(y1 ~ x1, df_oes) # Multiple treatment conditions: fit2 <- lm(y2 ~ x2, df_oes) # Using HC2 SE's from lm_robust(): fit_robust <- estimatr::lm_robust(y1 ~ x1, df_oes) fit_robust2 <- estimatr::lm_robust(y2 ~ x2, df_oes) # Using covariates and lm(): fit_covars <- lm(y2 ~ x2 + z1 + z2 + z3, df_oes) # Using covariates and lm_robust(): fit_covars_robust <- estimatr::lm_robust(y2 ~ x2 + z1 + z2 + z3, df_oes) # Specify treatment_arms: oes_prep(fit, treatment_arms = 1) |> oes_plot() # Specify treatment_vars: fit |> oes_prep(treatment_vars = "x1") |> oes_plot() # Specify treatment_arms: oes_prep(fit2, treatment_arms = 3) |> oes_plot() # Specify treatment_vars: fit2 |> oes_prep(treatment_vars = c("x21", "x22", "x23")) |> oes_plot() # Specify custom treatment_labels: prep_w_labels <- oes_prep(fit2, treatment_arms = 3, treatment_labels = c( "Email", "Email +\nReward", "Email +\nRisk"), control_label = "Status Quo") oes_plot(prep_w_labels) # Using objects from estimatr::lm_robust(): oes_prep(fit_robust, treatment_arms = 1) |> oes_plot() oes_prep(fit_robust2, treatment_arms = 3) |> oes_plot() # Specify covariates with lm: oes_prep(fit_covars, treatment_arms = 3) |> oes_plot() # Specify covariates with lm_robust(): oes_prep(fit_covars_robust, treatment_arms = 3) |> oes_plot() # For the Lin estimator, a manual version of lm_lin(): m.mat <- cbind(y2 = df_oes$y2, model.matrix(y2 ~ x2 + z1 + z2 + z3, df_oes)[, -1]) m.mat <- dplyr::mutate_at( data.frame(m.mat), .vars = c('z1', 'z2', 'z31', 'z32', 'z33', 'z34', 'z35'), function(x) x - mean(x) ) fit_lin <- estimatr::lm_robust(y2 ~ (x21 + x22 + x23) * (z1 + z2 + z31 + z32 + z33 + z34 + z35), m.mat) oes_prep(fit_lin, treatment_arms = 3) |> oes_plot()
Prepare output of linear modeling object into a tidy data table to feed into OES plotting function
oes_prep( model, treatment_vars = NULL, treatment_arms = NULL, scale = c("response", "percentage"), treatment_labels, control_label, alpha_level = 0.05 )
oes_prep( model, treatment_vars = NULL, treatment_arms = NULL, scale = c("response", "percentage"), treatment_labels, control_label, alpha_level = 0.05 )
model |
An object of class |
treatment_vars |
An optional character vector of treatment arm names. One
of |
treatment_arms |
An optional numeric value indicating the number of treatment
arms. (Not required if treatment_vars is given explicitly.) One
of |
scale |
String indicating the |
treatment_labels |
Optional vector of string labels providing treatment condition(s) |
control_label |
Optional string providing control condition label |
alpha_level |
The level at which to reject the null hypothesis for adding
asterisks to plots. Set to 0.05 by default. This value also determines the size
of the confidence intervals ( |
oes_prep()
takes a linear modeling output object (from lm()
or lm_robust()
) and returns a tidy tibble of estimates,
confidence bounds, and related quantities ready for oes_plot to plot.
Functionality for lm_lin()
objects is in development.
A tibble of rows and 8 columns, where
is the number
of treatment conditions specified via
treatment_vars
or
treatment_arms
.
Miles Williams
data(df_oes) # Single binary treatment: fit <- lm(y1 ~ x1, df_oes) # Multiple treatment conditions: fit2 <- lm(y2 ~ x2, df_oes) # Using HC2 SE's from lm_robust(): fit_robust <- estimatr::lm_robust(y1 ~ x1, df_oes) fit_robust2 <- estimatr::lm_robust(y2 ~ x2, df_oes) # Using covariates and lm(): fit_covars <- lm(y2 ~ x2 + z1 + z2 + z3, df_oes) # Using covariates and lm_robust(): fit_covars_robust <- estimatr::lm_robust(y2 ~ x2 + z1 + z2 + z3, df_oes) # Example specifying number of treatment arms: oes_prep(fit, treatment_arms = 1) # Example specifying name of treatment variable: oes_prep(fit, treatment_vars = "x1") # Example reporting outcomes as percentages: oes_prep(fit, treatment_vars = "x1", scale = "percentage") # Example specifying several treatment arms, labels, etc.: oes_prep(fit2, treatment_arms = 3, treatment_labels = c( "Email", "Email +\nReward", "Email +\nRisk"), control_label = "Status Quo", scale = "percentage") # Examples with lm_robust(): oes_prep(fit_robust, treatment_arms = 1) oes_prep(fit_robust2, treatment_arms = 3) # Examples with covariates: oes_prep(fit_covars, treatment_arms = 3) oes_prep(fit_covars_robust, treatment_arms = 3)
data(df_oes) # Single binary treatment: fit <- lm(y1 ~ x1, df_oes) # Multiple treatment conditions: fit2 <- lm(y2 ~ x2, df_oes) # Using HC2 SE's from lm_robust(): fit_robust <- estimatr::lm_robust(y1 ~ x1, df_oes) fit_robust2 <- estimatr::lm_robust(y2 ~ x2, df_oes) # Using covariates and lm(): fit_covars <- lm(y2 ~ x2 + z1 + z2 + z3, df_oes) # Using covariates and lm_robust(): fit_covars_robust <- estimatr::lm_robust(y2 ~ x2 + z1 + z2 + z3, df_oes) # Example specifying number of treatment arms: oes_prep(fit, treatment_arms = 1) # Example specifying name of treatment variable: oes_prep(fit, treatment_vars = "x1") # Example reporting outcomes as percentages: oes_prep(fit, treatment_vars = "x1", scale = "percentage") # Example specifying several treatment arms, labels, etc.: oes_prep(fit2, treatment_arms = 3, treatment_labels = c( "Email", "Email +\nReward", "Email +\nRisk"), control_label = "Status Quo", scale = "percentage") # Examples with lm_robust(): oes_prep(fit_robust, treatment_arms = 1) oes_prep(fit_robust2, treatment_arms = 3) # Examples with covariates: oes_prep(fit_covars, treatment_arms = 3) oes_prep(fit_covars_robust, treatment_arms = 3)
Defines the OES plotting theme
theme_oes(base_size = 12, base_family = "Lato", device = "pdf")
theme_oes(base_size = 12, base_family = "Lato", device = "pdf")
base_size |
An integer specifying a font size; defaults to |
base_family |
A character string identifying a font family; defaults to
the OES-preferred |
device |
A character string identifying a plotting device; defaults to
|
A list of length 93 of classes theme
and gg
that
defines elements of the display other than the data. theme_oes()
wraps
ggplot2::theme_bw()
and replaces several default values.
A list specifying a ggplot theme. See Details
for more.
Miles Williams