Welcome to Actuarial Statistics in R!
In Paper A, you learn the theory behind distributions and statistics. In Paper B, you put that theory into practice using R. This chapter focuses on the fundamental tools you will use in almost every CS1 exam: calculating summary statistics (like the mean and variance) and finding probabilities and quantiles for various distributions.
Think of R as a super-powered version of your scientific calculator. Instead of looking up values in the "Formulae and Tables" book (the Gold Book), you can ask R to give you the exact value in a fraction of a second. Let's dive in!
1. Exploratory Data Analysis: Summary Statistics
Before jumping into complex models, an actuary always looks at the data first. This is called Exploratory Data Analysis (EDA). We want to know where the "middle" of the data is (location) and how "spread out" it is (dispersion).
Measures of Location
To find the average or the middle value of a dataset x:
mean(x): Calculates the arithmetic mean \( \bar{x} = \frac{1}{n} \sum x_i \).
median(x): Finds the middle value when the data is ordered.
Measures of Spread
To see how much the data varies:
var(x): Calculates the sample variance \( s^2 = \frac{1}{n-1} \sum (x_i - \bar{x})^2 \).
sd(x): Calculates the sample standard deviation \( s = \sqrt{var(x)} \).
range(x): Gives the minimum and maximum values.
IQR(x): Calculates the Interquartile Range (the difference between the 75th and 25th percentiles).
The All-in-One Command
summary(x): This is a favorite for exam students! It provides the Minimum, 1st Quartile, Median, Mean, 3rd Quartile, and Maximum all at once. It’s the quickest way to get a "snapshot" of your data.
Quick Tip: If your data has missing values (labeled as NA in R), these functions might return NA. To fix this, add the argument na.rm = TRUE inside the brackets, like this: \( mean(x, na.rm = TRUE) \).
Key Takeaway: Use summary() for a quick overview and specific functions like sd() or var() when the question asks for a specific metric.
2. The "Big Four" Distribution Prefixes
In CS1, you deal with many distributions (Normal, Poisson, Binomial, etc.). R uses a very consistent naming system. Every distribution has a base name (e.g., norm for Normal, pois for Poisson) and four possible prefixes:
1. d (Density): Use this for the Probability Mass Function (PMF) for discrete variables \( P(X = x) \) or the Probability Density Function (PDF) value for continuous variables.
2. p (Probability): Use this for the Cumulative Distribution Function (CDF), \( P(X \le x) \). This is your "go-to" for finding probabilities.
3. q (Quantile): Use this for the inverse CDF. If you know the probability and want to find the value \( x \), use q.
4. r (Random): Use this to generate random samples from a distribution. (Note: We will cover this in detail in the next chapter on Simulation).
Visualizing the Difference:
If you want the area to the left of a point, use p.
If you want the point that has a specific area to its left, use q.
3. Common Distributions in R
Here are the distributions you need to know for the CS1 syllabus and their R names:
Discrete Distributions:
binom: Binomial \( (n, p) \)
pois: Poisson \( (\lambda) \)
geom: Geometric \( (p) \)
nbinom: Negative Binomial \( (k, p) \)
hyper: Hypergeometric
Continuous Distributions:
norm: Normal \( (\mu, \sigma) \)
exp: Exponential \( (\lambda) \)
gamma: Gamma \( (\alpha, \lambda) \)
chisq: Chi-square \( (\nu) \)
t: Student’s t-distribution \( (\nu) \)
f: F-distribution
beta: Beta distribution
unif: Uniform distribution
Example: To find the 95th percentile of a Standard Normal distribution \( Z \sim N(0,1) \):
\( qnorm(0.95, mean = 0, sd = 1) \)
Example: To find \( P(X \le 3) \) where \( X \sim Poisson(5) \):
\( ppois(3, lambda = 5) \)
4. Crucial Exam Traps (Don't lose marks here!)
The "Lower Tail" Argument
By default, R’s p-functions calculate \( P(X \le x) \) (the lower tail). If the exam asks for \( P(X > x) \), you have two choices:
1. Calculate \( 1 - p... \) (e.g., \( 1 - pnorm(x, ...) \))
2. Use the argument lower.tail = FALSE inside the function (e.g., \( pnorm(x, ..., lower.tail = FALSE) \)).
Discrete vs. Continuous
Don't forget! For continuous distributions (like Normal), \( P(X < x) \) is the same as \( P(X \le x) \).
But for discrete distributions (like Binomial or Poisson), it matters! R's p-functions always include the value: \( pbinom(k, ...) \) is \( P(X \le k) \). If you need \( P(X < k) \), you must calculate \( P(X \le k-1) \) in R.
The Gamma Distribution Parameters
In Paper A, we often use the rate parameter \( \lambda \). In R, the gamma functions can accept either a rate or a scale. Remember that \( scale = 1 / rate \). Always check your function arguments by typing ?rgamma into the R console if you are unsure.
Standard Deviation vs. Variance
In R's norm functions, the parameter is sd (\( \sigma \)), not variance (\( \sigma^2 \)). If the question says \( X \sim N(10, 25) \), you must enter sd = 5 in R.
5. Quantiles and Percentiles
A common task in Paper B is finding the Value at Risk (VaR) or specific quantiles. The q-functions are designed for this.
Scenario: An insurance claim distribution follows a Lognormal distribution with \( meanlog = 5 \) and \( sdlog = 2 \). Find the 99th percentile of claims.
R Command: \( qlnorm(0.99, meanlog = 5, sdlog = 2) \)
Key Takeaway: If the question gives you a probability (e.g., "top 5%" or "0.90 confidence"), you are likely looking for a q-function. If the question gives you a value (e.g., "What is the probability that claims exceed 1000?"), you are looking for a p-function.
Quick Review
Summary Statistics: Use mean(), var(), sd(), and summary().
Probabilities: Use p-name() for \( P(X \le x) \).
Quantiles: Use q-name() to find the value associated with a probability.
Upper Tails: Use lower.tail = FALSE for \( P(X > x) \).
Normal Dist: Always use standard deviation (\( \sigma \)), not variance (\( \sigma^2 \)).
Don't worry if you forget the exact order of arguments for a function! You can always type a question mark followed by the function name (e.g., ?pbinom) in R to see the help file and check what inputs it needs.