Data Analysis⏱ Self-paced, one calculation at a time

Write and Debug Research Code with AI

Write the answer to a five-row temperature file on paper, then use it to test an AI-written script. A missing day and a real zero reveal different bugs.

AudienceResearchers who use Python or R to turn raw observations into analysis tables
Tools coveredClaude, GitHub Copilot

You’re about to ask Claude or GitHub Copilot for a daily-temperature script. Beat it to the first answer. On one day the thermometer reads 8 °C, misses a reading, then records 12 °C. On the back of an envelope, write 10 °C from two readings. Now you have something more useful than confidence in the code. You know what one row has to say.

1. Give the assistant a small input you understand

Keep the raw file read-only. Make a working copy with just the columns you need, and use only data you’re allowed to share in that environment. For the station example, a few invented rows will do. Supply the column names, units, missing-value codes and the rule for assigning a reading to a day. Show what one output row should look like.

Save these five records as readings.csv in an empty folder. Each row represents one scheduled reading. The station has already assigned the local calendar day. Creek missed every reading on its second day. Ridge recorded a genuine freezing temperature.

station,day,temp_c
Creek,2026-05-01,8
Creek,2026-05-01,
Creek,2026-05-01,12
Creek,2026-05-02,
Ridge,2026-05-01,0

How many output rows do the five lines deserve? Trace the station and day with your finger and you’ll find three distinct pairs. Write that on the envelope too. For real timestamps, decide whose midnight counts. Just after midnight UTC can still be yesterday evening at the field station.

2. Read the plan before running the script

I need a daily mean and a count of valid readings for each station. Check the column types and walk me through how you’ll group the rows and handle blanks. Flag anything you need me to decide. Then write the script and give me the dependencies and the command to run it.

Read the proposed transformations. If the assistant fills a blank with zero, you already know where the calculation will go wrong. Check which files the script reads and writes before you run it on the working copy.

3. Try to break your example

Try duplicating the two valid readings. The mean stubbornly stays at 10 °C, so a test that checks only the mean congratulates broken code. The count gives the bug away. Now try a whole day of missed readings. For this question the day still happened, its count is zero, and its mean is missing.

If a check fails, give the assistant the smallest input that reproduces the error and the row you expected. Review the correction before rerunning. If you go on to fit a statistical model, check its assumptions, dependence between observations, uncertainty and diagnostics separately.

Run a reference calculation

Here is a small independent check for the example file. Save it beside the CSV as check_daily.py and run python3 check_daily.py from that folder. It uses Python’s standard library. If your analysis is in R, you can compare its output with these same three rows. Python’s CSV reader and writer

import csv
import sys
from statistics import mean

groups = {}
with open("readings.csv", newline="", encoding="utf-8") as source:
    for row in csv.DictReader(source):
        key = (row["station"], row["day"])
        values = groups.setdefault(key, [])
        text = row["temp_c"].strip()
        if text:
            values.append(float(text))

output = csv.writer(sys.stdout)
output.writerow(["station", "day", "n_valid", "mean_temp_c"])
for (station, day), values in sorted(groups.items()):
    average = mean(values) if values else ""
    output.writerow([station, day, len(values), average])
Station and day Valid readings Mean temperature
Creek, May 1 2 10 °C
Creek, May 2 0 Empty field
Ridge, May 1 1 0 °C

Did Creek lose its second day? Follow a blank through the script. Filtering blanks before creating the groups makes an entire station-day vanish. Did Ridge lose its zero instead? Look at the test for a missing value. Numeric zero can fail a true-or-false check. The reference script tests for an empty string before converting the value, allowing the string 0 to make it through as a real measurement.

4. Leave a runnable analysis

Save the script with the input source, package versions, any relevant random seed and the command that generates the output. Start a clean session and reproduce the table.

Take the envelope with you when you graduate to the full file. The sum of the daily counts should match the included, nonmissing input readings. If the totals first diverge after you attach station metadata, look for a repeated station in the lookup table. “This join doubled Creek” is a much easier debugging request to answer than “my averages look odd.”

If the assistant chooses tools dynamically

Some assistants can look up a tool during a run. Google’s November 19 Tool Retrieval preview documents three steps in the Gemini Interactions API. The model searches for a tool, the API retrieves its parameter schema, and the model requests a function call. Google’s Tool Retrieval walkthrough

If your workflow uses that feature, save those steps with the exact tool definitions, arguments, returned data and code that actually ran. Include the model identifier and access date. Limit write access to the files the task requires. Someone checking your mean can then trace which input reached which calculation. What tool retrieval means

Original sources