Coding is one of the areas where AI is most useful to researchers. Assistants can write a first draft of a script, explain an error message, translate between R and Python, and suggest how to approach an analysis. They also make mistakes that run without errors and produce plausible but wrong results. The aim is to use AI to code faster while staying fully responsible for what the code does.
Key points
- AI is most useful for debugging, boilerplate, learning and documentation.
- It can produce code that runs but silently does the wrong thing, or makes inappropriate statistical choices.
- Choose your methods yourself, share structure rather than real data, and read every line.
- Test on known answers, check the data at each step, and keep the final analysis reproducible without AI.
- Record AI use in comments and your log.
Where AI helps most
- Debugging. Paste the error message and the relevant code, and ask what is wrong. This is often the biggest time saver.
- Boilerplate. Importing data, reshaping tables, renaming variables and making standard plots.
- Learning. Explaining what a function does, line by line, or why one approach is preferred over another.
- Translation. Converting code between languages or packages.
- Documentation. Adding comments and writing a README for your analysis.
- Refactoring. Turning repeated code into functions and tidying scripts so others can follow them.
Where it goes wrong
- Silent errors. Code that runs but does the wrong thing: dropping missing values without saying so, joining tables incorrectly, or filtering the wrong rows.
- Wrong statistical choices. Applying a test whose assumptions your data do not meet, or using default settings that are inappropriate for your design.
- Outdated or invented functions. Suggesting arguments or packages that no longer exist, or never did.
- Overconfidence about interpretation. Describing results in stronger terms than the analysis supports.
- Data exposure. Pasting real data into a tool can breach data protection or confidentiality.
A safe workflow
- Decide the analysis yourself. Choose methods based on your research question, design and field conventions, with your advisor or a statistician. Use AI to help implement them, not to choose them.
- Share structure, not data. Describe your variables, or share column names and a few fake rows, rather than real data.
- Read every line. If you cannot explain what a line does, ask, and do not use it until you can.
- Test on known answers. Run the code on a small subset where you can check results by hand, or on simulated data where you know the true values.
- Check the data at each step. Count rows before and after joins and filters, look at summaries, and plot distributions.
- Compare with the documentation. Check function arguments and defaults against the official documentation.
- Use version control so you can see what changed and when.
- Record AI use in your code comments and your AI log.
Useful prompts
- “I have a data frame with these columns: [list]. Write R code using [package] to [task]. Explain each step.”
- “This code gives the error below. Explain the cause before suggesting a fix.”
- “What assumptions does this model make, and how can I check them with my data?”
- “Review this script for steps that could silently drop or change data.”
- “Write a test that checks this function returns the right result for a small example.”
Reproducibility
Your analysis should be reproducible from your code and data without the AI. Keep the final scripts, record package versions, and make sure someone else could run them and get the same results. Examiners and reviewers may ask about your analysis choices, so you need to understand them, regardless of who typed the first draft.