Artificial Intelligence and coding in R

An attempt at a tutorial to install a language model (LLM) directly on your own computer and use it, for instance, to help you write R code, for free, with no account, and without sending your data to a remote server.

Why run an AI model locally?

Running a model "locally" means executing it on your own machine rather than on a company's servers (like ChatGPT or Claude). The upsides: it's free, it works offline, your data stays private, and it's a great way to understand concretely how these tools work. The trade-off: quality depends on the size of the model your hardware can handle.

Hardware requirements

An LLM must be loaded into memory to produce text, so the main constraint is available memory. To generate decent R scripts, aim for a model of at least 3 to 7 billion parameters (written 3B to 7B).

Rule of thumb: a smaller model (0.5B–1.5B) will run on almost any machine but gives more limited results. It's up to you to find the best compromise for your computer.

The tutorial in 3 steps

  1. Install a local LLM server

    These tools handle downloading and running models, with a ChatGPT-style chat interface. The simplest place to start is Ollama. User-friendly alternatives: LM Studio and Open WebUI. Download, install, and you're ready.

  2. Download a model

    Once Ollama is installed, open a terminal and pull a model specialised in code. For example:

    ollama pull qwen2.5-coder:3b

    Replace 3b with the size you want (0.5b, 1.5b, 3b, 7b…) depending on your hardware.

  3. Ask your questions

    Launch the model and chat with it like any regular chatbot:

    ollama run qwen2.5-coder:3b

    You can then ask, for example: "Write me an R script that loads a CSV file, computes the mean per group and draws a boxplot." Always review and test the generated code, a small model can make mistakes.

Which models to try?

For help with R code, two good families to start with:

Tip: start small, judge the quality of the answers, then scale up if your machine can keep up.

Going further

A critical look: the ecological cost of AI

Using these tools, whether locally or in the cloud, is not neutral: training and running large models consume enormous amounts of energy and water and emit CO₂. A few readings to keep a critical mind (in French):

Put together by Antoine Géré.