---
title: "SQA AH Statistics Exam Papers, using R"
output: html_document
date: "2026-06-026"
---

INSTRUCTIONS

Expand (or Collapse) the code chunks by clicking on the grey triangle to the right of the line number, or use the menu item: Edit > Folding > Expand All or Collapse All

Locate the code chunk you want from the Year and Question number

Run any code chunk by clicking on the green triangle 'play button' in the top right of each section of code

```{r 2026 Paper 1 Question 1b}

table = t(data.frame(
  england = c(7, 12, 8, 4, 1),
  ireland = c(10, 6, 6, 5, 6),
  scotland = c(14, 9, 3, 4, 2),
  wales = c(13, 4, 6, 5, 7)
))

# perform test
output = chisq.test(x = table,
                    correct = FALSE) # to prevent Yate's Continuity Correction

# display expected frequencies that were used
print("Expected frequencies:")
output$expected

```

```{r 2026 Paper 1 Question 2e}

# setup variable values
m = 18
n = 34
W = 404

# calculate mean and variance of normal approximation to W
E_W = m * (m + n + 1) / 2
V_W = m * n * (m + n + 1) / 12

# calculate p-value
pnorm(q = W + 0.5,
      mean = E_W,
      sd = sqrt(V_W))

```

```{r 2026 Paper 2 Question 1b}

# the package called 'BSDA' is needed for the z.test function
if(!require(BSDA)){install.packages("BSDA"); library(BSDA)}

white_mean = 20.2
white_st_dev = 1.38
white_n = 216

asian_mean = 19.8
asian_st_dev = 1.15
asian_n = 87

# create simulated data sets with correct statistics
simulated_white_values = scale(1:white_n) * white_st_dev + white_mean
simulated_asian_values = scale(1:asian_n) * asian_st_dev + asian_mean

# perform the z-test
z.test(x = simulated_white_values,
       y = simulated_asian_values,
       sigma.x = white_st_dev,
       sigma.y = asian_st_dev,
       alternative = "two.sided")

```

```{r 2026 Paper 2 Question 4}

accidental = c(5, 9, 7, 8, 4, 8, 9)
deliberate = c(38, 30, 32, 30, 36, 40, 44)
differences = deliberate - 6 * accidental

# remove any zeros from differences before performing the Wilxocon test
updated_differences = differences[differences != 0]

wilcox.test(x = updated_differences,
            mu = 0, # the command uses mu rather than median
            exact = FALSE, # do not calculate an exact p-value
            alternative = "greater")

```

```{r 2026 Paper 2 Question 6bii}

sample_n = 15
sample_mean = 67
sample_st_dev = 4

# create a simulated data set that has the correct mean and standard deviation
simulated_masses = scale(1:sample_n) * sample_st_dev + sample_mean

# perform t-test
t.test(x = simulated_masses,
       mu = 70,
       alternative = "less")

```

```{r 2026 Paper 2 Question 7}

# 7a) calculate P(X = 8) where X ~ Po(7.5)
dpois(x = 8,
      lambda = 7.5)

# 7c) calculate P(Y > 15) using normal approximation and continuity correction, where Y ~ Po(20)
pnorm(q = 15.5,
      mean = 20,
      sd = sqrt(20),
      lower.tail = FALSE)

```

```{r 2026 Paper 2 Question 8}

# record experimental results for 18-month and 14-month olds
successes = c(38, 6)
trials = c(55, 31)

# perform two-sample proportion test
prop.test(x = successes,
          n = trials,
          alternative = "greater",
          correct = FALSE) # to prevent continuity correction

# you will see from the above test output the mention of X-squared, which stems from a lot of parallels between two-sample proportion tests and a chi-squared test of association

# the next lines of code perform the same test, but manually.....

# calculate and display the pooled proportion
p = sum(successes) / sum(trials) 
p 

# calculate and display the test statistic
z = -diff(successes / trials) / sqrt (p * (1-p) * sum(1 / trials))
z 

# calculate the p-Value of the test
pnorm(q = z,
      mean = 0,
      sd = 1,
      lower.tail = FALSE)

```

```{r 2026 Paper 2 Question 11}

# 11a) calculate P(X >= 1) = P(X > 0), where X ~ B(100, 0.043)
pbinom(q = 0,
       size = 100,
       prob = 0.043,
       lower.tail = FALSE)

# 11c) calculating a 95% confidence interval

late_trains = 6
sample_size = 75

# this uses the 2-sample proportion test command syntax, but sets the second sample to have zero 'successes'. This ensures that the generated confidence interval agrees with hand-calculated answers.
prop.test(c(late_trains, 0),
          c(sample_size, sample_size),
          alternative = "two.sided",
          conf.level = 0.95,
          correct = FALSE) # to prevent Yate's Continuity Correction

```