Module 8: AI Safety in Health Research
AI-Powered Coding and Data Analysis
Exercise: Download ZIP (two folders, one per machine)
GitHub repository (code only, no data)
Agents work best with few restrictions. They read files, run code, and fix their own errors. Protected data needs the opposite. On a personal computer an agent can usually reach any file the user can.
Layers of protection, strongest first
- Separation: the agent runs where the data is absent, such as a secure enclave, a separate server, or a virtual machine.
- Git and
.gitignore: data never enters the repository. (.gitignoredoesn’t affect files git already tracks.) - Pre-commit hooks: a git hook that refuses commits containing private data. Framework installs one, and
--no-verifyskips it. - Tool permissions: rules the tool enforces, such as Claude Code’s deny rules and sandbox. They’re specific to each tool and can have gaps. This module’s project sets them for Claude Code only. A Codex user who allows everything is protected only by
AGENTS.mdand the skills. The project requires Claude Code’s sandbox, which needs WSL2 on Windows. - Context files and skills: instructions to stay away. They are a useful backstop and are not proof of safety.
Code and non-sensitive materials can move across the boundary. Private data stays inside the secure environment.
Secure enclave workflow
The ZIP holds two copies of the same project, which stand in for two machines:
yale-ai-stats-04-protected-data/
secure/ has the protected data; never run an assistant here
ai-enabled/ no protected data; run Claude Code, Codex, or Posit Assistant here
- Open each folder as its own project, in two windows. In RStudio, open
secure/secure.Rprojandai-enabled/ai-enabled.Rproj. - Both copies use the same paths: simulated data in
inputs/private/simulated/and reviewed metadata ininputs/metadata/. Onlysecure/hasinputs/private/raw/. - Data and metadata move by hand. You review each file in
secure/and copy it to the same path inai-enabled/in Finder or File Explorer. - Code moves through Git.
secure/is a clone ofai-enabled/, so you commit inai-enabled/and rungit pullinsecure/. - Git ignores all data and metadata in both copies.
1. SECURE MACHINE: Extract the metadata
In secure/, with no assistant running, run source("scripts/extract-data-info.R"). It writes metadata to outputs/private/metadata/.
2. SECURE MACHINE: Review the metadata
Review every file for identifiers, rare categories, small counts, exact dates, and free text. Merge categories with counts under 50 rather than deleting them, since other counts can reveal them by subtraction. Round or omit exact minimums and maximums that could identify people. Remove survey weights and design variables unless needed. Save only the reviewed metadata in inputs/metadata/ on the secure machine.
3. SECURE MACHINE → AI-ENABLED MACHINE: Copy the reviewed metadata
Copy only the approved files from secure/inputs/metadata/ to ai-enabled/inputs/metadata/ by hand. Check each file before you copy it. Never copy raw data or unreviewed output. Metadata stays out of Git.
4. AI-ENABLED MACHINE: Write the generator
Have the agent complete scripts/generate-sim-data.R using only the reviewed metadata. The script defaults to inputs/private/simulated/nhanes_2021_2023_analysis.csv. The agent tests it by passing an output path in a temporary folder, since it cannot write to inputs/private/.
The files in inputs/metadata/ describe a protected dataset you can't access. Complete scripts/generate-sim-data.R so it uses that metadata to generate a simulated dataset with the same file name, column names, types, category levels, and plausible ranges. The script takes an output path as an argument, with the default inputs/private/simulated/nhanes_2021_2023_analysis.csv. Create new IDs; don't reconstruct real ones. Draw categories from the proportions, with counts that vary. Don't reproduce exact minimums or maximums. Keep derived columns consistent (age_group with age_years, pregnancy only for women). Let systolic_mean rise with age so the model has something to fit. Test the script by writing to a temporary folder. Don't write anything to inputs/private/.5. AI-ENABLED MACHINE → SECURE MACHINE: Transfer the generator through version control
Review the generator. In a terminal in ai-enabled/, check what changed, then commit only the script:
git status
git add scripts/generate-sim-data.R
git commit -m "Add simulated data generator"In a terminal in secure/, with no assistant running, pull the commit:
git pullOn real machines, both copies push and pull through an approved shared repository instead. If Git asks for your name and email, run git config --global user.name "Your Name" and git config --global user.email "you@example.com".
6. SECURE MACHINE: Generate the simulated data
In secure/, with no assistant running, run source("scripts/generate-sim-data.R"). It reads the reviewed metadata and writes the simulated file to inputs/private/simulated/.
7. SECURE MACHINE: Review the simulated data
Review the generated file for small cells, exact minimums and maximums, recognizable combinations of values, and IDs that match real ones. Complete the required disclosure review and obtain approval before transferring it.
8. SECURE MACHINE → AI-ENABLED MACHINE: Copy the approved simulated data
Copy only nhanes_2021_2023_analysis.csv from secure/inputs/private/simulated/ to ai-enabled/inputs/private/simulated/ by hand. Check that it is the approved simulated file, not the raw file in inputs/private/raw/. Both copies stay out of Git.
9. AI-ENABLED MACHINE: Develop the analysis
Have the agent fill in notebooks/01_data-summary.qmd and notebooks/02_modeling.qmd. Leave data_dir <- "simulated" so they read the approved simulated copy. Check code with knitr::knit() into a temporary folder. Do not render here, because rendering writes to outputs/private/. Develop and check the code without interpreting simulated results.
Fill in notebooks/01_data-summary.qmd with code that checks structure, missingness, category levels, and basic summaries. Don't interpret the numbers; the data are simulated. Don't change data_dir.Fill in notebooks/02_modeling.qmd with a simple regression of systolic_mean on age_years and gender among adults, plus diagnostics. Don't write interpretations; the data are simulated. Don't change data_dir.10. AI-ENABLED MACHINE → SECURE MACHINE: Transfer the analysis through version control
Review the notebooks. In ai-enabled/, commit only the code:
git status
git add notebooks/01_data-summary.qmd notebooks/02_modeling.qmd
git commit -m "Add data summary and model"In secure/, with no assistant running, run git pull.
11. SECURE MACHINE: Run the analysis on real data
In secure/, with no assistant running, change data_dir to "raw" at the top of each notebook and render them yourself. They read inputs/private/raw/ and write HTML to outputs/private/notebooks/. Review all real-data output before sharing it, including small counts, exact minimums and maximums, and plots with one point per person. Remove text that describes the results as simulated.
On one laptop, the separation is on the honor system. An agent started in ai-enabled/ can reach ../secure/ with your file access. Claude Code’s sandbox blocks its shell commands from that folder, and its Read tool asks before opening files outside the project. Say no. AGENTS.md tells every agent to stay in its own folder. Only separate machines, VMs, or servers enforce the boundary, so use them for real protected data.
Simulated data generators
The exercise’s generate-sim-data.R draws each variable from the reviewed metadata. Its disclosure risk depends on what the metadata reveals and how the script uses it. Review is still required. It keeps only the relationships between variables that you build in, such as systolic_mean rising with age.
The synthpop R package fits models to the real data inside the secure environment and generates synthetic records from them (Nowok, Raab, and Dibben, 2016, Journal of Statistical Software 74(11)). Its default method is classification and regression trees (CART). It can preserve more relationships between variables, depending on the models and settings. Assess disclosure risk for the intended release. If you use synthpop:
- Run it only in the secure environment.
- Drop direct identifiers before you call
syn(). - Coarsen rare categories and top-code extreme values.
- Compare distributions with
compare()and validate the intended analyses against the original data inside the secure environment. Utility checks do not establish privacy. - Choose relevant quasi-identifiers as
keysfor disclosure checks.sdc(..., rm.replicated.uniques = TRUE)can remove replicated unique combinations of those keys; this alone does not establish privacy. - Label the output as synthetic. The
labelargument ofsdc()adds a column that marks every record. - Review the output as a release, like any other output.
- Check your data use agreement. Synthetic data derived from restricted data may still count as restricted.
The synthpop documentation describes its utility and disclosure tools. Keep generated data, fitted synthesis objects, and diagnostic reports inside the secure environment until approved for release. Use the same gitignored folders and manual transfer step as the exercise.