---
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 2021 Paper 2 Question 1}
s_values = c(2, 4, 6, 8, 10)
s_probabilities = s_values / sum(s_values)
# mean of y
sum(s_values * s_probabilities)
# variance of y
sum(s_values^2 * s_probabilities) - (sum(s_values * s_probabilities))^2
```
```{r 2021 Paper 2 Question 3}
mites = c(6, 8, 11, 13, 6, 14, 11, 9, 6, 7, 11, 8, 6, 14)
differences = mites - 7
# remove 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
alternative = "greater")
# note that the value of V stated in the Wilcoxon test output is *not* the minimum rank sum
```
```{r 2021 Paper 2 Question 5}
brand_A_n = 11
brand_A_mean = 54
brand_A_st_dev = 5
brand_B_n = 15
brand_B_mean = 47
brand_B_st_dev = 11
# create simulated data sets with correct statistics
simulated_brand_A_times = scale(1:brand_A_n) * brand_A_st_dev + brand_A_mean
simulated_brand_B_times = scale(1:brand_B_n) * brand_B_st_dev + brand_B_mean
t.test(simulated_brand_A_times,
simulated_brand_B_times,
paired = FALSE,
var.equal = TRUE, #this will pool the samples
alternative = "two.sided")
```
```{r 2021 Paper 2 Question 6(c)}
# the package called 'BSDA' is needed for the z.test function
if(!require(BSDA)){install.packages("BSDA"); library(BSDA)}
time_n = 25
time_mean = 409
time_st_dev = 130
# create simulated data set with correct statistics
simulated_times = scale(1:time_n) * time_st_dev + time_mean
# the output from a z.test gives the confidence interval
z.test(x = simulated_times,
sigma.x = time_st_dev ,
alternative = "two.sided",
conf.level = 0.95)
```
```{r 2021 Paper 2 Question 8}
# the package called 'BSDA' is needed for the z.test function
if(!require(BSDA)){install.packages("BSDA"); library(BSDA)}
time_n = 50
time_mean = 16.1
time_st_dev = 2
# create simulated data set with correct statistics
simulated_times = scale(1:time_n) * time_st_dev + time_mean
# the output from a z.test gives the confidence interval
z.test(x = simulated_times,
sigma.x = time_st_dev,
mu = 15,
alternative = "greater",
conf.level = 0.95)
```
```{r 2021 Paper 2 Question 10(a)}
selling_beyond = 13
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(selling_beyond, 0),
c(sample_size, sample_size),
alternative = "two.sided",
conf.level = 0.95,
correct = FALSE) # to prevent Yate's Continuity Correction
```
```{r 2021 Paper 2 Question 11(b)}
# the package called 'BSDA' is needed for the z.test function
if(!require(BSDA)){install.packages("BSDA"); library(BSDA)}
group_B_n = 70
group_B_mean = 55.4
group_B_st_dev = 10.08
group_C_n = 60
group_C_mean = 51.8
group_C_st_dev = 10.49
# create simulated data sets with correct statistics
simulated_group_B_scores = scale(1:group_B_n) * group_B_st_dev + group_B_mean
simulated_group_C_scores = scale(1:group_C_n) * group_C_st_dev + group_C_mean
z.test(x = simulated_group_B_scores,
y = simulated_group_C_scores,
sigma.x = group_B_st_dev,
sigma.y = group_C_st_dev,
alternative = "two.sided")
# for interest, by comparison, here is what the t-test would have been
t.test(simulated_group_B_scores,
simulated_group_C_scores,
paired = FALSE,
var.equal = TRUE, #this will pool the samples
alternative = "two.sided")
```