1 of 39

Overview of Generative AI for R coding

A/Prof Chris Brown

University of Tasmania

c.j.brown@utas.edu.au

2 of 39

Claude Sonnet 3.7 ….

3 of 39

Overview

  • Seminar 9-10am
    • Background on LLMs
    • Jargon
    • Prompting better
    • What scientists should do
  • Interactive workshop till about 3:30/4:00pm

4 of 39

genAI what is it?

  • Artificial intelligence that can generate new content, such as text, images, or code.
  • We’ll focus on Large Language Models
  • They now excel at logic and generating code
  • They can also interpret images, use your web browser, run and debug code, use tools, access the internet, …
  • Predicted to be fully capable software developers in the next 1-3 years

5 of 39

How LLMs work

  • Complex neural network with multiple different types of layers
  • Trained on a large corpus of text data
  • Can generate new content by predicting the next token (effectively word)
  • Example 1 watch 1:25-2:50

6 of 39

LLM abilities

  • LLMs started as as text generators
  • Companies quickly realized they are good at coding
  • They are now very good at coding

7 of 39

Example

  • Show an example of ‘vibe coding’ with Roo code.
  • Set to code mode. Prompt:

Help me complete this analysis @benthic-readme.md

8 of 39

What do LLMs mean for science?

  • LLMs are rapidly improving
  • Jan 2025 LLMs could complete a full written analysis if it was simple
  • For complex workflows you needed to break it down into steps and track context across steps
  • As of Apr 2025: LLM can delegate sub-tasks and manage context across complex projects

9 of 39

What do LLMs mean for science?

  • Coding can be automated
  • Writing can be automated
  • Complete papers from researching idea to submission are already being automated
  • This will become more common
  • Other implications are hard to guess…

10 of 39

LLM evaluation

11 of 39

LLM evaluation – R example

  • Jansen et al. 2025 “Leveraging large language models for data analysis automation”
  • Looking at bioinformatics tasks with R

Level of complexity, number of tasks from:

1. Read data from file(s)

2. Data wrangling (filtering, transposing, etc.)

3. Visualization

4. Machine learning or statistics applications

5. Handling more than one dataset

12 of 39

LLM evaluation example

Jansen et al. 2025 “Leveraging large language models for data analysis automation”

  • Fraction of scripts that ran without error
  • More errors for more complex tasks
  • But prompt strategy matters

13 of 39

LLM evaluation

Jansen et al. 2025 “Leveraging large language models for data analysis automation”

  • LLM was better at making scripts that ran
  • Worse at writing scripts that were logically correct

14 of 39

Common responses to LLMs

  • LLMs scare me, I’m not going to use them
  • I don’t trust LLMs, I’m not going to use them
  • Is it safe to use LLMs to help with research?
  • Will LLMs replace quantitative scientists?
  • Should we still teach coding?

15 of 39

What should you learn right now?

  • Try web platforms (e.g. ChatGPT) for a range of tasks
  • Learn what they make you better at or save you time for
  • Examples: re-writing emails politely, summarizing papers, getting advice to improve your writing
  • Prompt better (more on this later)

16 of 39

What tools should I use for coding and stats?

  • Get github copilot
  • Free tier available
  • Also free license for academics and students
  • Use it for your coding
  • I recommend use in VScode, its better
  • Learn more programming tools
  • Start using Git and Github
  • Alternatives: Claude code, ChatGPT subscription

17 of 39

Github copilot in VScode

18 of 39

LLM jargon essentials

Token a part of a word that is the unit of prediction. e.g. GPT3.0 had ~50,000 tokens

Prompt a piece of text that is used to generate content from an LLM.

Prompt engineering the process of designing and refining prompts to get the desired output from an LLM.

Context window the number of tokens that the LLM uses to predict the next token. The LLM’s ‘memory’ size.

Temperature Low temperature means the LLM favours the highest probability token, high temperature means its more random

19 of 39

LLM jargon essentials

API Application Programming Interface

System message

User message

Assistant message

20 of 39

LLM software

AI assistant Software that manages your interactions with an LLM

Tools Give LLM abilities to do things like access data, run code

Agents Can run autonomously by writing code, returning results to themselves as a prompt, then responding and so on

21 of 39

Prompt engineering

A conversation looks like this:

System message

User message

Assistant response

User message

Assistant response

etc. until the context window is full

22 of 39

Prompting strategies

  • Treat the LLM like a very capable human (or alien?) assistant
  • It’s capable but don’t trust it!
  • Give it a role
  • Clear instructions
  • Write longer and more detailed prompts
  • Any document or image can be a prompt
  • Draw on a diverse knowledge base - humanities graduates can be the best at prompting

23 of 39

Be clear about your workflow

1. Plan statistical approach (science part)

2. Organize project and plan workflow

3. Implement plan – write the code

Best to treat each step separately

See our pre-print linked in the notes: Brown and Spillias (2025)

24 of 39

1. Prompting strategies for statistical advice

What the LLM won’t do that a real statistical consultant would do:

Ask you for more information before giving an answer

25 of 39

1. Prompting strategies for statistical advice

  • Give rich context
  • Attach domain knowledge
  • Use chain of thought reasoning
  • Attach data or data summaries
  • Don’t conflate implementation with statistical approach

26 of 39

1. Prompting strategies for statistical advice

You are an expert in ecological statistics with the R program.

I want to statistically test the dependence of fish abundance on coral cover. I have observations of coral cover (continuous percentage) and fish abundance (count of number of fish). Observations were made at 49 different locations. Observations were made with standardized surveys, so the area surveyed at each site was the same.

Sites are spatially clustered into different regions. Provide me with several options for statistical approaches would be appropriate for answering my research question. Use chain of thought to reason about each approach before providing a final summary.

I've attached the data [data] and a reference on analysis of count data with ecology [reference].

27 of 39

2. Prompting strategies for planning

  • Organize your project folder and data
  • Document everything carefully, including meta-data, stats decisions and more
  • Ask AI assistant to plan first
  • Use chain of thought reasoning

28 of 39

2. Prompting strategies for planning

Help me plan the steps to complete this analysis. This should include a series of scripts that we will need to make for each step of the analysis. It should also include a plan for how to structure the project directory. Create modular scripts for this analysis with separate files for data preparation, model fitting, diagnostics, and visualization. Save data files for intermediate steps. Use chain of thought reasoning to think carefully about each step.

29 of 39

2. Prompting strategies for planning

30 of 39

3. Prompting strategies for implementation

  • If everything is organized correctly this is the easy part
  • Provide tech context e.g. package preference and style
  • Manage context window
  • Better to say what you want to do, rather than what you don’t want to do
  • Ask for tests, check for mistakes
  • Iterate

31 of 39

What does this all mean for science?

  • Illusion of understanding
  • Ethics
  • Environmental impact
  • Data privacy
  • Security

32 of 39

Illusion of understanding

Messeri and Crockett Nature “Artificial intelligence and illusions of understanding in scientific research” https://www.nature.com/articles/s41586-024-07146-0

33 of 39

Illusion of understanding

Messeri and Crockett Nature “Artificial intelligence and illusions of understanding in scientific research” https://www.nature.com/articles/s41586-024-07146-0

34 of 39

Illusion of understanding

Messeri and Crockett Nature “Artificial intelligence and illusions of understanding in scientific research” https://www.nature.com/articles/s41586-024-07146-0

35 of 39

Ethics and environmental issues

  • Plagiarism - litigation ongoing
  • Water use and carbon emissions. My ‘prompts’ equate to about 1/10 of a 1.5 hour flight, or a 15 minute drive
  • But global cumulative impacts bigger

36 of 39

What this means for science?

  • Models are rapidly getting better
  • Assume this will be the worst model you use
  • Find out what it can do now, but check in a few months again
  • Decide what ethically you will do yourself

37 of 39

What you should do

  • Experiment with LLMs
  • Keep library of prompts examples
  • Still put in the work to learn things yourself
  • Use the LLM to aid learning, not as a replacement
  • Discuss with your lab appropriate uses, define boundaries
  • Never trust the LLM responses
  • Be sure to properly attribute works generated

38 of 39

What our discipline should do

  • Define standards for use
  • Define best practices for quality science

39 of 39

Interactive workshop

You’ll need:

  • R�
  • R IDE
  • ellmer package
  • VScode with github copilot
  • API key e.g. for Anthropic