Skip to contents

Generates a self-contained script that loads the data, reads the specifications, fits them with run_specs() and prints the results table. By default the specifications are embedded in the script as YAML, so the one file reproduces the results; pass spec_file to refer to a separate YAML file instead (see write_specs_yaml()).

Usage

write_spec_script(
  specs,
  path,
  data_code,
  title = "sdidlab analysis",
  spec_file = NULL
)

Arguments

specs

A list of sdid_spec()s (or a single one).

path

File to write, or NULL to return the script as text.

data_code

R code, as a string, that creates an sdid_panel() called panel. For example 'panel <- sdid_panel(read.csv("my_data.csv"), unit = "state", time = "year", outcome = "y")'.

title

A title for the script's header comment.

spec_file

Path the script should read the specifications from, instead of embedding them.

Value

The path, invisibly, or the script text when path = NULL.

Details

Each specification is fitted with its own seed (seed_for()), so the script reproduces every standard error regardless of order.

Examples

code <- write_spec_script(
  list(sdid_spec(label = "Prop 99", inference = list(replications = 50))),
  path = NULL,
  data_code = "panel <- prop99_panel()",
  title = "Proposition 99"
)
cat(code)
#> # Proposition 99
#> # Generated by sdidlab 0.1.0 on 2026-09-29.
#> # Each specification is fitted with its own seed (seed_for()), so every
#> # standard error reproduces exactly, whatever order the specs are run in.
#> 
#> library(sdidlab)
#> 
#> # -- Data ---------------------------------------------------------------
#> panel <- prop99_panel()
#> 
#> # -- Specifications -----------------------------------------------------
#> spec_yaml <- r"---(
#> # Proposition 99
#> # Placebo standard errors depend on the RNG stream: fit these in file
#> # order under one seed, or with run_specs(seed_each = TRUE).
#> defaults:
#>   inference:
#>     replications: 50
#> specs:
#> - label: Prop 99
#> )---"
#> spec_path <- tempfile(fileext = ".yml")
#> writeLines(spec_yaml, spec_path)
#> specs <- read_specs(spec_path)
#> 
#> # -- Fit ------------------------------------------------------------------
#> runs <- run_specs(specs, panel, seed_each = TRUE)
#> results <- sdid_results_table(runs)
#> print(results, row.names = FALSE)