6  R and Risk Forecasting

The programming language used throughout this notebook is R. While alternatives like Julia, Matlab, or Python are certainly capable, we argue in Chapter 3 that R is particularly well-suited for the kind of statistical modeling, time series analysis, and data visualization that risk forecasting requires. R has a rich ecosystem of packages, excellent community support, and domain-specific tools that make it ideal for both academic and applied work in finance and risk management.

We do not assume any prior programming experience. If this is your first time coding, that’s perfectly fine. What we do expect is curiosity and a willingness to experiment and learn. From years of teaching this material, we’ve found that students with no programming background often become confident R users after a single semester of working through applied, hands-on forecasting tasks.

6.1 Installing R and RStudio

To begin working with R, you need two components:

  1. R, the language and statistical computing engine.
  2. RStudio, a powerful integrated development environment (IDE) that makes working with R much easier.

Follow these steps:

  1. Visit the Posit website;
  2. Download and install RStudio from the RStudio Desktop download page;
  3. If prompted, install R first — you will be directed to the correct version for your operating system.
    • Note: If you’re using a Windows ARM-based machine, you may need to search separately for the compatible R version, as it’s not always the default option.

Once both are installed, launch RStudio. You should see a console window where you can start entering R code immediately.

6.2 Getting Help and Learning Resources

R has extensive built-in help and documentation. To learn more about a function, use the ? symbol followed by the function name:

?plot

There are many resources for learning R, and we do not attempt to duplicate all of them here. The Big Book of R is an extremely comprehensive collection — though it can be overwhelming. We recommend browsing by topic or using the search function to find what’s most relevant.

Below we highlight some of the most accessible and effective resources for learning R — ranging from beginner-friendly tutorials to advanced programming books and statistical modeling references.

6.2.1 Getting started with R


6.2.2 Video tutorials


6.2.3 Official documentation and references


6.2.4 Books and in-depth guides

Beginner to intermediate:

Intermediate to advanced:

  • Advanced R by Hadley Wickham — Covers environments, functions, and R internals;
  • The Art of R Programming by Norman Matloff — Emphasizes programming techniques and computer science concepts.

6.2.5 Risk forecasting and time series


6.2.6 Data visualisation

  • ggplot2 Tutorial (r-statistics.co) — Complete guide to ggplot2;
  • R Graph Gallery — Examples of visualisations with reproducible R code;
  • ggplot2: Elegant Graphics for Data Analysis by Hadley Wickham — In-depth guide to the grammar of graphics in R.

6.2.7 Cheat sheets and quick references


6.2.8 Quarto and reporting


6.2.9 Communities, blogs, and forums


6.2.10 Online courses and platforms


6.2.11 Cloud-based R environments

  • Posit Cloud — Run R and RStudio in your browser; useful if you cannot install R locally.

6.2.12 AI help for R

If you’re stuck on an R coding problem, you can also consult AI coding assistants such as ChatGPT, GitHub Copilot, or Claude; these tools can suggest syntax, troubleshoot bugs, and explain functions — but always validate the code they generate.

6.3 User interfaces

R offers multiple user interfaces, each with distinct advantages depending on your workflow requirements. Some prioritise ease of learning, others emphasise performance or integration with other tools. The following sections outline the main options to help you make an informed choice for your R work.

6.3.1 RStudio

It is usually best to use the free RStudio software for programming in R. RStudio provides an integrated development environment that makes learning R significantly easier than using R’s basic interface. It combines code editing, console interaction, help documentation, and graphics in a single organised workspace. The interface is designed specifically for data analysis workflows, with features like syntax highlighting, code completion, and integrated help that reduce common beginner mistakes. RStudio also handles file management and project organisation automatically, allowing you to focus on learning R rather than managing technical details.

6.3.2 Posit Cloud

Posit Cloud provides access to RStudio through your web browser without requiring any local installation. This cloud-based service offers the same RStudio interface you would use on your desktop, but running on remote servers. Posit Cloud is particularly useful when you cannot install software on your computer due to restrictions, when working on different machines, or when you want to ensure everyone in a class or team uses identical R environments.

The service includes a free tier that allows limited monthly usage, making it suitable for learning and small projects. Posit Cloud handles all the technical setup automatically, including R installation, package management and file storage. You can share projects easily with others and access your work from any device with internet connection.

However, the free tier has usage limits and requires internet connectivity for all work. For extensive analysis or when working with sensitive data, local RStudio installation may be more appropriate.

6.3.3 Quarto

The RStudio vendor has developed an interactive development environment called Quarto, designed for R as well as Julia, Python and other languages. Quarto allows you to create dynamic documents that combine code, output and formatted text in a single file. This makes it particularly useful for creating reports, presentations and notebooks where you want to show both your analysis and your results together. Unlike traditional interfaces that separate code writing from document creation, Quarto integrates both processes. We use Quarto for these notes, allowing us to combine explanatory text with executable R code examples. Chapter 14 has more information.

6.3.4 Jupyter

Jupyter notebooks are an interactive development environment for notebooks, code, and data produced by Project Jupyter. They offer both computer code (e.g., R, Python, Julia) and rich text elements (paragraphs, equations, images, links, etc.). They are a great way to make reports and documents. They can be edited on a web server and exported as HTML, PDF via LaTeX, or other file formats. They are interactive, and you can independently run pieces of code.

For R users, Jupyter differs from RStudio in its cell-based approach where code is organised into individual cells that can be run independently. This requires installing the R kernel separately from the main Jupyter installation. Jupyter excels when sharing analysis with colleagues who use different programming languages, as the same interface works for R, Python and Julia. It is particularly useful for teaching or presenting analysis step-by-step.

Some users find the Jupyter way of coding really good, appreciating the ability to mix formatted explanations with code and see results immediately below each cell. Others find it very confining, preferring traditional script files where they can see all their code at once and use more sophisticated editing features. The cell structure can make it harder to refactor code or work with longer scripts compared to traditional text editors.

6.3.5 VSCode

A capable text editor is visual studio code or VScode, and it provides a very useful extension for R and Quarto. VScode is our go-to editor for writing and is how we made these notes.

VSCode works well as a general-purpose editor with useful features like intelligent code completion, integrated version control, and extensive customisation options. The R extension adds syntax highlighting, code execution, and debugging capabilities that are comparable to RStudio’s functionality. Unlike RStudio’s fixed layout, VSCode allows complete interface customisation and supports dozens of programming languages in a single environment.

VSCode also provides good support for agentic coding through AI-powered extensions like GitHub Copilot, Cursor, and similar tools. These extensions can suggest R code completions, generate entire functions based on comments, and help debug errors. This AI assistance can help with learning and development, though users should always understand and verify the generated code.

VSCode is particularly suitable when working across multiple programming languages or when you need advanced text editing features for longer scripts. The extension system allows integration with external tools and services that may not be available in RStudio. However, setup requires more configuration than RStudio, and some R-specific features like the integrated help browser are less polished than RStudio’s native implementation.

For R beginners, RStudio typically provides a more straightforward experience, while VSCode appeals to users who value customisation and work with multiple programming languages.

6.3.6 Zed

Zed is a newer text editor that may be faster than VSCode and offers a more streamlined experience. It also provides a useful extension for R. Zed is our go-to editor for coding.

As a relatively new editor, Zed focuses on performance and simplicity while still offering modern features like collaborative editing and AI integration. The R extension provides syntax highlighting and basic language support, though it may not have all the features available in more established editors like VSCode or RStudio. Notably, Zed does not currently have support for Quarto documents.

However, Zed works well for agentic coding, with AI assistance that can generate code, suggest completions, and help with debugging. This AI integration may be useful for development work.

Zed appeals to users who prefer a lighter, faster editing experience and do not require extensive customisation or complex integrations. However, being newer means it has a smaller ecosystem of extensions and fewer community resources compared to VSCode. The R support, while functional, may be less comprehensive than what is available in RStudio or VSCode.

For users who value speed and simplicity over extensive features, Zed might be worth considering, though those new to R might find RStudio’s integrated environment more helpful for learning.

6.3.7 Base R editor

R comes with a basic editor and environment for executing code. It has the advantage of being very lightweight and efficient but is rather basic. We use it by default because it is so lightweight and simple. For all its benefits, RStudio is neither.

The base R editor provides a simple text editing window alongside the R console, offering basic syntax highlighting and the ability to run code directly. The interface is minimal, with limited customisation options and no integrated help browser or project management features. However, this simplicity means it starts quickly and uses minimal system resources.

The base editor may be suitable when working on older computers with limited memory, when you need a quick R session without the overhead of larger interfaces, or when working in environments where installing additional software is not possible. It can also be helpful for users who prefer minimal interfaces and do not require advanced features.

However, compared to RStudio or other modern interfaces, the base editor lacks code completion, integrated graphics viewing, package management tools, and debugging features. The editing capabilities are quite limited, making it less suitable for longer scripts or complex analysis work.

The base editor differs from command line R in that it provides a basic graphical interface with separate windows for editing and console interaction, while command line R operates entirely within a terminal.

6.3.8 Command line

We can also access R on the command line, which is very useful for long parallel applications or when using remote computers.

Command line R is accessed by typing R in a terminal or command prompt, which starts the R interpreter directly. It operates entirely within the terminal, providing direct access to the R interpreter without any graphical interface. You type R commands and see results as text output, with no separate windows for editing, graphics, or help.

Command line R is often the preferred choice when working on remote servers, high-performance computing clusters, or when running R through SSH sessions. It uses minimal system resources and can run effectively over slow network connections. Many production environments rely on command line R for computational work that needs to run without graphical overhead.

Command line R can display graphics, typically opening plot windows separately from the terminal. This makes it suitable for interactive analysis work, though without the integrated interface that RStudio provides. The text-based interaction can be efficient for users comfortable with terminal environments.

However, command line R requires more familiarity with text-based computing and may be intimidating for users accustomed to graphical interfaces. Error debugging can be more challenging without the visual aids available in RStudio or other graphical environments. The lack of integrated help browsing and code editing features also makes it less convenient for learning or complex development work.

6.3.9 Non-interactive R

For automated workflows and batch processing, R can be run non-interactively using Rscript. This approach executes R scripts without opening an interactive session, making it suitable for scheduled tasks, data pipelines, and production environments.

Non-interactive R is invoked by typing Rscript filename.R in a terminal, where filename.R contains the R code to be executed. The script runs from start to finish without user input, with all output directed to the console or saved to files as specified in the script. Graphics are typically saved directly to files rather than displayed in windows.

This approach works well for automated reporting, regular data processing tasks, and integration with other systems or scheduling tools like cron jobs. It allows R analysis to be incorporated into larger workflows without requiring interactive sessions. Many organisations use non-interactive R for daily reports, data updates, and systematic analysis tasks.

Non-interactive R requires scripts to be fully self-contained, with all necessary data loading, processing, and output handling specified in advance. Error handling becomes more important since there is no opportunity for user intervention if problems arise during execution. The approach is less suitable for exploratory analysis or when immediate feedback is needed.