Workshop

The logic of hypothesis testing and confidence intervals

Introduction

A little monster flying a biplane wearing aviator glasses, pulling a banner that says 'Fully expecting to hate this class.' Below, a teacher wearing a cheerleading outfit labeled 'STATS' with a bullhorn labeled 'CODE' cheering desperately with pom-poms, trying to help students believe stats is actually going to be awesomely life-changing.

Artwork by Horst (2023): “love this class”

Session overview

In this session you will remind yourself how to import files, and calculate confidence intervals on large and small samples.

Philosophy

Workshops are not a test. It is expected that you often don’t know how to start, make a lot of mistakes and need help. It is expected that you are familiar with independent study content before the workshop. However, you need not remember or understand every detail as the workshop should build and consolidate your understanding. Tips

  • don’t worry about making mistakes
  • don’t let what you can not do interfere with what you can do
  • discussing code with your neighbours will help
  • look things up in the independent study material
  • look things up in your own code from earlier
  • there are no stupid questions
Key

These four symbols are used at the beginning of each instruction so you know where to carry out the instruction.

Something you need to do on your computer. It may be opening programs or documents or locating a file.

Something you should do in RStudio. It will often be typing a command or using the menus but might also be creating folders, locating or moving files.

Something you should do in your browser on the internet. It may be searching for information, going to the VLE or downloading a file.

A question for you to think about and answer. Record your answers in your script for future reference.

Getting started

Start RStudio from the Start menu.

Make an RStudio project for this workshop by clicking on the drop-down menu on top right where it says Project: (None) and choosing New Project, then New Directory, then New Project. Navigate to the data-analysis-in-r-1 folder and name the RStudio Project ‘week-5’.

Make a new script then save it with a name like analysis.R to carry out the rest of the work.

Add a comment to the script: # The logic of hypothesis testing and confidence intervals and load the tidyverse (Wickham et al. 2019) package

Make a new folder called data-raw.

Exercises

Remind yourself how to import files!

Importing data from files was covered in a previous workshop (Rand 2023) if you need to remind yourself.

Confidence intervals (large samples)

The data in beewing.txt are left wing widths of 100 honey bees (mm). The confidence interval for large samples is given by:

\(\bar{x} \pm 1.96 \times s.e.\)

Where 1.96 is the quantile for 95% confidence.

Save beewing.txt to your data-raw folder.

Read in the data and check the structure of the resulting dataframe.

Calculate and assign to variables: the mean, standard deviation and standard error:

# mean
m <- mean(bee$wing)

# standard deviation
sd <- sd(bee$wing)

# sample size (needed for the se)
n <- length(bee$wing)

# standard error
se <- sd / sqrt(n)

To calculate the 95% confidence interval we need to look up the quantile (multiplier) using qnorm()

q <- qnorm(0.975)

This should be about 1.96.

Now we can use it in our confidence interval calculation

lcl <- m - q * se
ucl <- m + q * se

Print the values

lcl
[1] 4.473176
ucl
[1] 4.626824

This means we are 95% confident the population mean lies between 4.47 mm and 4.63 mm. The usual way of expressing this is that the mean is 4.55 +/- 0.07 mm

Between what values would you be 99% confident of the population mean being?

Confidence intervals (small samples)

The confidence interval for small samples is given by:

\(\bar{x} \pm \sf t_{[d.f]} \times s.e.\)

The only difference between the calculation for small and large sample is the multiple. For large samples we use the “the standard normal distribution” accessed with qnorm(); for small samples we use the “t distribution” assessed with qt().The value returned by q(t) is larger than that returned by qnorm() which reflects the greater uncertainty we have on estimations of population means based on small samples.

The fatty acid Docosahexaenoic acid (DHA) is a major component of membrane phospholipids in nerve cells and deficiency leads to many behavioural and functional deficits. The cross sectional area of neurons in the CA 1 region of the hippocampus of normal rats is 155 \(\mu m^2\). A DHA deficient diet was fed to 8 animals and the cross sectional area (csa) of neurons is given in neuron.txt

Save neuron.txt to your data-raw folder

Read in the data and check the structure of the resulting dataframe

Assign the mean to m.

Calculate and assign the standard error to se.

To work out the confidence interval for our sample mean we need to use the t distribution because it is a small sample. This means we need to determine the degrees of freedom (the number in the sample minus one).

We can assign this to a variable, df, using:

df <- length(neur$csa) - 1

The t value is found by:

t <- qt(0.975, df = df)

Note that we are using qt() rather than qnorm() but that the probability, 0.975, used is the same. Finally, we need to put our mean, standard error and t value in the equation. \(\bar{x} \pm \sf t_{[d.f]} \times s.e.\).

The upper confidence limit is:

(m + t * se) |> round(2)
[1] 151.95

The first part of the command, (m + t * se) calculates the upper limit. This is ‘piped’ in to the round() function to round the result to two decimal places.

Calculate the lower confidence limit:

Given the upper and lower confidence values for the estimate of the population mean, what do you think about the effect of the DHA deficient diet?

You’re finished!

🥳 Well Done! 🎉

Header text 'R learners' above five friendly monsters holding up signs that together read 'we believe in you.'

Artwork by Horst (2023): “We belive in you!”

Independent study following the workshop

Consolidate

The Code file

This contains all the code needed in the workshop even where it is not visible on the webpage.

The workshop.qmd file is the file I use to compile the practical. Qmd stands for Quarto markdown. It allows code and ordinary text to be interweaved to produce well-formatted reports including webpages. View the Qmd in Browser. Coding and thinking answers are marked with #---CODING ANSWER--- and #---THINKING ANSWER---

Pages made with R (R Core Team 2023), Quarto (Allaire et al. 2022), knitr (Xie 2022), kableExtra (Zhu 2021)

References

Allaire, J. J., Charles Teague, Carlos Scheidegger, Yihui Xie, and Christophe Dervieux. 2022. Quarto. https://doi.org/10.5281/zenodo.5960048.
Horst, Allison. 2023. “Data Science Illustrations.” https://allisonhorst.com/allison-horst.
R Core Team. 2023. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.
Rand, Emma. 2023. Data Analysis in r for Becoming a Bioscientist. https://3mmarand.github.io/R4BABS/.
Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019. “Welcome to the Tidyverse 4: 1686. https://doi.org/10.21105/joss.01686.
Xie, Yihui. 2022. “Knitr: A General-Purpose Package for Dynamic Report Generation in r.” https://yihui.org/knitr/.
Zhu, Hao. 2021. “kableExtra: Construct Complex Table with ’Kable’ and Pipe Syntax.” https://CRAN.R-project.org/package=kableExtra.