---
title: "SQA AH Statistics Exam Papers, using R"
output: html_document
date: "2023-07-19"
---
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 2022 Paper 2 Question 1}
table = t(data.frame(
infected_yes = c(76, 129),
infected_no = c(399, 332)
))
output = chisq.test(x = table,
correct = FALSE) # to prevent Yate's Continuity Correction
output # displays test statistic, degrees of freedom and p-value
print("Expected frequencies:")
output$expected # displays table of expected frequencies
```
```{r 2022 Paper 2 Question 2}
# (b) P(X = 0) where X ~ Po(2.3)
dpois(x = 0,
lambda = 2.3)
# (c) P(X = 2 and Y = 2) where X ~ Po(2.3) and Y ~ Po(1.7)
dpois(x = 2, lambda = 2.3) * dpois(x = 2, lambda = 1.7)
# (d) P(W > 5) where W ~ Po(4)
ppois(q = 5,
lambda = 4,
lower.tail = FALSE)
```
```{r 2022 Paper 2 Question 5(b)}
french = c(67, 83, 71, 59, 49, 89, 42, 55, 77)
german = c(64, 82, 71, 62, 42, 85, 39, 50, 75)
differences = french - german
t.test(differences,
mu = 0,
alternative = "two.sided")
```
```{r 2022 Paper 2 Question 7(b)}
successes = 14
sample_size = 50
# 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(successes, 0),
c(sample_size, sample_size),
alternative = "two.sided",
conf.level = 0.99,
correct = FALSE) # to prevent Yate's Continuity Correction
```
```{r 2022 Paper 2 Question 9(b)}
# the package called 'BSDA' is needed for the z.test function
if(!require(BSDA)){install.packages("BSDA"); library(BSDA)}
width_n = 45
width_mean = 52.6
width_st_dev = sqrt(103.25)
# create simulated data sets with correct statistics
simulated_widths = scale(1:width_n) * width_st_dev + width_mean
z.test(x = simulated_widths,
sigma.x = width_st_dev,
mu = 50,
alternative = "greater")
```
```{r 2022 Paper 2 Question 10(a)}
Sxx = 278.61
Syy = 10.95
Sxy = 46.29
n = 6
r = Sxy / sqrt(Sxx * Syy)
r
t = r * sqrt(n - 2) / sqrt(1 - r^2)
t
p_value = 2 * pt(q = t,
df = n - 2,
lower.tail = FALSE)
p_value
```
```{r 2022 Paper 2 Question 11(a)}
adults = c(1.3, 2.2, 1.5, 3.5, 0.3, 2.7, 3.5, 2.3, 2.9, 4.0)
juveniles = c(1.1, 4.1, 1.7, 1.3, 0.7, 1.9, 2.9, 1.1, 2.8, 0.9)
# back-to-back stem and leaf diagrams are not built into R by default
# the 'aplpack' package does support them, but technical issues may need to be overcome
stem(adults)
stem(juveniles)
# the process for a Mann-Whitney test is embedded within the wilcox.test command
wilcox.test(x = adults,
y = juveniles,
paired = FALSE,
alternative = "two.sided")
# note that the value of 'W' in the output is *not* the rank sum used in the AH Stats course
```