---
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 2019 Question 2(c)}

table = t(data.frame(
  colour_blind_no = c(450, 506),
  colour_blind_yes = c(40, 4)
  ))
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 2019 Question 6}

energies = c(2725, 2650, 2421, 2793, 2239, 3225, 2156, 2692, 2369, 2725)

# the t.test command delivers both hypothesis test result and confidence interval
t.test(energies,
       alternative = "two.sided",
       conf.level = 0.95)

t.test(energies,
       alternative = "two.sided",
       conf.level = 0.99)

```

```{r 2019 Question 8(b)(c)}

Sgg = 531.5676
Sgc = 555.0811
Scc = 1731.2973

coeff_determination = Sgc^2 / (Sgg * Scc)
coeff_determination

r = sqrt(coeff_determination)
t = r * sqrt(37 - 2) / sqrt(1 - r^2)

p_value = 2 * pt(q = t,
                 df = 37 - 2,
                 lower.tail = FALSE)
p_value

```

```{r 2019 Question 9}

marks = c(86, 80, 78, 73, 69, 65, 62, 61, 59, 58, 54, 51, 49, 47, 43, 40, 38, 37, 35, 32, 29, 29)
differences = marks - 65

# 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 = "less")

```

```{r 2019 Question 10(a)}

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

wingspan_n = 25
wingspan_mean = 48.3
wingspan_st_dev = 4

# create simulated data set with correct statistics
simulated_wingspans = scale(1:wingspan_n) * wingspan_st_dev + wingspan_mean

z.test(x = simulated_wingspans,
       sigma.x = wingspan_st_dev,
       mu = 50,
       alternative = "less")

```





