---
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 2016 Question 1}

heights = c(26, 15, 28, 23, 25, 15, 16, 20, 22, 27, 17, 30, 48)

# boxplot display
boxplot(x = heights,
        horizontal = TRUE,
        las = TRUE)

# stem-and-leaf display
stem(heights)

```

```{r 2016 Question 3}

n = 12
mean = 147.8
st_dev = 2.379

# create a simulated data set with correct statistics
simulated_weights = scale(1:n) * st_dev + mean

t.test(simulated_weights,
       mu = 150,
       alternative = "less")

```

```{r 2016 Question 4b}

sample_size = 20
sample_mean = 142 / sample_size
sample_st_dev = sqrt( (1120.16 - (142^2) / sample_size) / (sample_size - 1 ) )

# calculate P(X-bar > 8.1)
pnorm(q = 8.1,
      mean = sample_mean,
      sd = sample_st_dev / sqrt(sample_size),
      lower.tail = FALSE)
```

```{r 2016 Question 5(a)(i)}

yields = c(4.00, 4.50, 4.75, 4.88, 4.99, 5.10, 5.00, 5.25, 5.63)

# the t.test command delivers both hypothesis test result and confidence interval
t.test(yields,
       mu = 4.75,
       alternative = "two.sided",
       conf.level = 0.90)

```

```{r 2016 Question 8(a)}

recoveries = c(75, 65)
patients = c(100, 100)

prop.test(recoveries,
          patients,
          alternative = "greater",
          correct = FALSE) # to prevent Yate's Continuity Correction

```

```{r 2016 Question 9(b)(c)}

mu = 4

# calculate P(X > 4 + 2 * sqrt(4) ) where X ~ Po(4)
ppois(q = mu + 2 * sqrt(mu),
      lambda = mu,
      lower.tail = FALSE)

# calculate approximate P(T < 140) where T ~ Po(152)
pnorm(q = 139.5,
      mean = 152,
      sd = sqrt(152),
      lower.tail = TRUE)
```

```{r 2016 Question 10(b)}

y_values = c(1, 2)
y_probabilities = c(2/5, 3/5)

# mean of y
sum(y_values * y_probabilities)

# variance of y
sum(y_values^2 * y_probabilities) - (sum(y_values * y_probabilities))^2

```

```{r 2016 Question 11(a)(b)}

cross = c(23.5, 12.0, 21.0, 20.1, 22.0, 21.5, 22.1, 20.4, 18.3, 21.1, 21.0, 12.0)
self = c(17.4, 20.4, 20.0, 20.1, 20.0, 18.6, 18.6, 15.3, 16.5, 18.0, 18.0, 18.0)
differences = cross - self

t.test(differences,
       mu = 0,
       alternative = "greater")

# 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
```






