Use GitHub Copilot Chat in Visual Studio Code to explain inherited analysis code
For Economist (Private Sector / Consulting)s ·
What This Does
You inherit an R script with no comments, from a colleague who has moved on. Copilot Chat in Visual Studio Code can explain a block of code in plain language next to the code itself. You read the explanation against the code, add comments you agree with, and test the one line where an explanation can sound right and still be wrong. An explanation tells you what Copilot reads in the code. Only a test on synthetic data tells you what the code does.
Before You Start
- You use Visual Studio Code, signed in to GitHub, with Copilot access. GitHub's page says the features you get depend on your Copilot plan, the client you use and your organization's policies. It also says individuals can try Copilot for free, and individual paid plans start at Pro ($10/month). At work, access usually comes through your firm, so ask IT which plan is enabled. GitHub's page adds that if your organization owner has disabled chat, you cannot use it.
- You use code and synthetic data only. Check your firm's policy before you put client-specific code into Copilot, because variable names, file paths and comments can reveal a client or a matter. Client data, documents produced in litigation and anything under a protective order or NDA never go into the editor while Copilot is active, and code that runs on confidential data runs in the firm's own environment. Check what Copilot keeps and whether prompts are used for training under your plan's settings.
- You have R available to run the check at the end. The same steps apply to Stata or Python code.
Steps
1. Open the practice script
Create a folder with no real data in it and save this invented script as practice_panel.R:
library(dplyr)
panel <- read.csv("practice_panel.csv")
panel <- panel %>%
group_by(firm_id) %>%
arrange(year) %>%
mutate(growth = sales / lag(sales) - 1) %>%
ungroup()
summary(panel$growth)
The file practice_panel.csv is invented. It has the columns firm_id, year and sales.
2. Open Copilot Chat
GitHub's page says to click the chat icon in the title bar of Visual Studio Code. If the icon is not shown, right-click the title bar and make sure Command Center is selected. The chat view has a mode dropdown at the bottom. Choose Ask, which the page describes as the mode for answering questions about your code.
3. Ask for an explanation
Type a prompt in the prompt box. To point Copilot at the file, type # and pick it from the list of chat variables the page describes.
Explain this script step by step in plain language for an economist who reads R but did not write it.
For each line, say what it does to the data.
Do not change the code.
GitHub's page says chat can explain code, suggest fixes and generate unit tests. Read the reply, and then select the Used n references dropdown at the top of the response to see which files Copilot used.
4. Read the explanation against the code
Go line by line. Copilot should say that the pipe groups by firm_id, orders by year within firm and computes sales divided by the previous sales value, minus 1. Check that the explanation matches each function you can see. If a line is described vaguely, ask a follow-up about that line only.
5. Catch what the explanation can hide
Here is the catch. lag(sales) takes the previous row, not the previous year. If a firm has no row for one year, the row after the gap divides by a value from two years earlier, and the growth value is wrong without any warning. Ask Copilot directly:
What does lag(sales) return if a firm has no row for one year? Show a small invented example.
Whatever it answers, confirm it by running the script on a small synthetic file. Build it with these invented rows:
| firm_id | year | sales |
|---|---|---|
| 1 | 2021 | 100 |
| 1 | 2022 | 110 |
| 1 | 2024 | 121 |
Row 2 should give growth of 0.10, because 110 divided by 100 is 1.10. Row 3 is the trap. The script computes 121 divided by 110 minus 1, which is 0.10, but 2024 follows 2022, so that value is not a one-year growth rate. A correct script would give a missing value for 2024.
6. Add a check and comments
Write the fix yourself, or ask Copilot to suggest one and then read it. One approach is to keep the growth value only when the previous row is exactly one year earlier:
mutate(growth = if_else(year - lag(year) == 1, sales / lag(sales) - 1, NA_real_))
Run it on the synthetic file again. Row 3 should now show NA. Then add the comments you agree with to the script, and delete the ones that overstate what the code does.
Real Example
Scenario: You open an inherited panel script and need to know what growth means before you reuse it in a project.
What you type: The prompts from steps 3 and 5.
What you get: A line-by-line explanation that reads well. The explanation alone would not reveal the gap-year problem unless you asked. The synthetic run does. Row 3 shows 0.10 in the original script and NA in the corrected one. You add a comment that says growth is defined only for consecutive years, and you note the change in your methods log.
Tips
- Ask for explanations and tests, not for the model. Which specification to run is your choice and your project lead's.
- Generated tests can catch a broken calculation, but only the ones you read and run count.
- Keep prompts free of client names, matter codes and file paths that identify an engagement.
Tool interfaces change. If a button has moved, look for similar Copilot options in the same menu area.