Probability Distributions Exercises in R: 25 Practice Problems

Twenty-five practice problems on probability distributions in R: normal, binomial, Poisson, t, chi-square, F, with d/p/q/r prefixes. Hidden solutions.

RRun this once before any exercise
library(ggplot2)

  

Exercise 1: Normal density at x=0

Difficulty: Beginner.

Show solution
RInteractive R
dnorm(0)

  

Exercise 2: Normal CDF P(X<=1.96)

Difficulty: Beginner.

Show solution
RInteractive R
pnorm(1.96)

  

Exercise 3: Normal quantile for 0.975

Difficulty: Beginner.

Show solution
RInteractive R
qnorm(0.975)

  

Exercise 4: Sample from normal(10,2)

Difficulty: Beginner.

Show solution
RInteractive R
set.seed(1); rnorm(10, mean = 10, sd = 2)

  

Exercise 5: P(80

Difficulty: Intermediate.

Show solution
RInteractive R
pnorm(120, 100, 15) - pnorm(80, 100, 15)

  

Exercise 6: Binomial P(X=5) for n=10, p=0.5

Difficulty: Beginner.

Show solution
RInteractive R
dbinom(5, size = 10, prob = 0.5)

  

Exercise 7: Binomial P(X<=3) for n=10, p=0.3

Difficulty: Intermediate.

Show solution
RInteractive R
pbinom(3, size = 10, prob = 0.3)

  

Exercise 8: Simulate 1000 coin flips

Difficulty: Beginner.

Show solution
RInteractive R
set.seed(1); rbinom(1000, size = 1, prob = 0.5) |> mean()

  

Exercise 9: Poisson P(X=4) lambda=3

Difficulty: Beginner.

Show solution
RInteractive R
dpois(4, lambda = 3)

  

Exercise 10: Poisson CDF

Difficulty: Intermediate.

Show solution
RInteractive R
ppois(5, lambda = 3)

  

Exercise 11: t distribution critical value (df=20, two-sided 5%)

Difficulty: Intermediate.

Show solution
RInteractive R
qt(0.975, df = 20)

  

Exercise 12: t-distribution density

Difficulty: Beginner.

Show solution
RInteractive R
dt(0, df = 20)

  

Exercise 13: Chi-square critical (df=10, 95%)

Difficulty: Intermediate.

Show solution
RInteractive R
qchisq(0.95, df = 10)

  

Exercise 14: F critical (df1=3, df2=20)

Difficulty: Intermediate.

Show solution
RInteractive R
qf(0.95, df1 = 3, df2 = 20)

  

Exercise 15: Uniform random

Difficulty: Beginner.

Show solution
RInteractive R
set.seed(1); runif(5, 0, 1)

  

Exercise 16: Exponential mean = 2

Difficulty: Intermediate.

Show solution
RInteractive R
set.seed(1); rexp(10, rate = 1/2)

  

Exercise 17: Plot normal density

Difficulty: Intermediate.

Show solution
RInteractive R
ggplot(data.frame(x = seq(-4, 4, length = 200)), aes(x)) + stat_function(fun = dnorm)

  

Exercise 18: Two normal densities overlaid

Difficulty: Intermediate.

Show solution
RInteractive R
ggplot(data.frame(x = seq(-4, 8, length = 200)), aes(x)) + stat_function(fun = dnorm, color = "blue") + stat_function(fun = function(x) dnorm(x, mean = 3), color = "red")

  

Exercise 19: Sample mean distribution (CLT demo)

Difficulty: Advanced.

Show solution
RInteractive R
set.seed(1) means <- replicate(5000, mean(rexp(50, rate = 1))) hist(means, breaks = 40)

  

Exercise 20: Simulate dice rolls

Difficulty: Beginner.

Show solution
RInteractive R
set.seed(1); sample(1:6, 100, replace = TRUE)

  

Exercise 21: Sample without replacement

Difficulty: Intermediate.

Show solution
RInteractive R
set.seed(1); sample(1:10, 5, replace = FALSE)

  

Exercise 22: Probability X > 1.96 in N(0,1)

Difficulty: Beginner.

Show solution
RInteractive R
1 - pnorm(1.96)

  

Exercise 23: Confidence-interval critical (df=29, 95%)

Difficulty: Intermediate.

Show solution
RInteractive R
qt(0.975, df = 29)

  

Exercise 24: Compare empirical to theoretical

Difficulty: Advanced.

Show solution
RInteractive R
set.seed(1); s <- rnorm(10000) list(empirical = mean(s < 1.96), theoretical = pnorm(1.96))

  

Exercise 25: Lognormal sample and inverse

Difficulty: Advanced.

Show solution
RInteractive R
set.seed(1); s <- rlnorm(1000) log(s) |> hist()

  

What to do next

  • Hypothesis-Testing-Exercises (shipped), apply these distributions.
  • Sampling-Methods-Exercises (coming), bootstrap and resampling.