AE07 - Working with AI generated Code
Be the boss of your AI helper
Purpose
An LLM can hand you code that runs and is still bloated, built on guesses about your data, or quietly wrong. You are responsible for what you submit. Today you will read real ChatGPT output critically, see how much the code depends on what the model knows about your data, and practice cutting code down to the part that does the job.
By the end you should be able to:
- Spot the assumptions an LLM makes when it can’t see your data
- Cut bloated code down to the minimal tidyverse version and justify each deletion
- Decide what is safe to delegate to an LLM and what you have to understand yourself
Instructions
- Work in groups of 2-3. Talk through each question together, then individually write answers on the front page of your worksheet. Detach the front page from the back page, and turn it in by the end of class. The ChatGPT answers are on the back pages.
- The time estimates next to the suggestions are guidelines
The data (parts 1-3)
All of the code today is about the same two data sets from the Exam 1 take-home.
votes: California county presidential returns, 2008 to 2020. One row per county, year and candidate.
| county_name | year | county_fips | candidate | party | candidatevotes | totalvotes |
|---|---|---|---|---|---|---|
| ALAMEDA | 2008 | 6001 | BARACK OBAMA | DEMOCRAT | 489106 | 621029 |
| ALAMEDA | 2008 | 6001 | JOHN MCCAIN | REPUBLICAN | 119555 | 621029 |
| ALAMEDA | 2008 | 6001 | OTHER | OTHER | 12368 | 621029 |
(There are other columns too: state_po, office and version.)
census: one row per US county (usdata::county_complete). The columns that matter today:
| column | meaning |
|---|---|
fips |
County code. Stored as text, so it has to be converted to a number to match county_fips. |
name |
County name, such as “Alameda County” |
bachelors_2019 |
Percent of adults with a bachelor’s degree |
Part 1: A vague prompt (~10-15 min)
This is how most people actually write a prompt: short, with the details left out. This is what was typed into a new ChatGPT chat:
I have election results for California counties and a census data set. Find the five counties with the highest Democratic vote share in 2020 and their bachelor’s degree rate. Write R code.
Part 2: Trim the function (~10-15 min)
In the same chat, the next thing typed was:
Now turn that into a reusable R function that takes a year and a number n, and returns the top n counties.
The worksheet has ChatGPT’s function. We changed the names in it to match our data, and removed the example runs to fit it on one page.