Bootstrapping

Learning Objectives

At the end of this lab, you will:

  1. Understand the principles of bootstrapping
  2. Understand how to apply the bootstrap confidence interval to inference in linear models

What You Need

  1. Be up to date with lectures
  2. Have completed previous lab exercises from Week 2 and Week 9

Required R Packages

Remember to load all packages within a code chunk at the start of your RMarkdown file using library(). If you do not have a package and need to install, do so within the console using install.packages(" "). For further guidance on installing/updating packages, see Section C here.

For this lab, you will need to load the following package(s):

  • tidyverse
  • psych
  • patchwork
  • sjPlot
  • kableExtra
  • car

Presenting Results

All results should be presented following APA guidelines.If you need a reminder on how to hide code, format tables/plots, etc., make sure to review the rmd bootcamp.

The example write-up sections included as part of the solutions are not perfect - they instead should give you a good example of what information you should include and how to structure this. Note that you must not copy any of the write-ups included below for future reports - if you do, you will be committing plagiarism, and this type of academic misconduct is taken very seriously by the University. You can find out more here.

Lab Data

You can download the data required for this lab here or read it in via this link https://uoepsy.github.io/data/science-faith-attitude.csv

Study Overview

Research Question

Is there an association between peoples’ attitudes towards science and faith and their scientific knowledge after accounting for their age?

Attitudes Codebook

Setup

Setup
  1. Create a new RMarkdown file
  2. Load the required package(s)
  3. Read in the science-faith-attitude dataset into R, assigning it to an object named ebsurvey

Solution

Exercises

Study Overview & Data Management

Question 1

Examine the dataset, and perform any necessary and appropriate data management steps.

Note, to address the research question, we only need to refer to the kstot, age, and toomuchscience variables. Subset the data to only have those 3 columns.

  • To subset the data to only include the 3 variables of interest, we can use the select() function
  • Check that the dataset is complete (i.e., are there any NA values?). We can check this using is.na()
    • There are numerous ways to deal with this. Two common commands are na.omit() and drop_na(). The former will remove all rows from a dataset that contain NA values in any column. In the latter, we can specify which columns we want to identify NA values in, and remove only rows containing NA values for those specific columns. In other words, the latter can help to preserve more data
  • If needed, provide better variable names

Solution


Question 2

Provide a brief overview of the study design and data, before detailing your analysis plan to address the research question.

  • Give the reader some background on the context of the study
  • State what type of analysis you will conduct in order to address the research question
  • Specify the model to be fitted to address the research question (note that you will need to specify the reference level of any categorical variable(s))
  • Specify your chosen significance (\(\alpha\)) level
  • State your hypotheses

Much of the information required can be found in the Study Overview codebook. The statistical models flashcards may also be useful to refer to.

Solution

Descriptive Statistics & Visualisations

Question 3

Alongside descriptive statistics, visualise the marginal distributions of the attitude, science_knowledge, and age variables.

Review the many ways to numerically and visually explore your data by reading over the data exploration flashcards.

For examples, see flashcards on descriptives statistics tables and data visualisation - marginal.

Solution


Question 4

Produce plots of the associations between the outcome variable and each of the explanatory variables.

Review how to visually explore bivariate associations via the data visualisation flashcards. For specifically visualising associations between variables, see the bivariate examples flashcards.

Note that using geom_point() here might not be the best idea - all we would see from the plot is all combinations of Attitude towards Science and Faith and a) Age and b) Science Knowledge that were observed in the data, and this is not very informative:

Figure 4: Scatterplots displaying the associations between Attitude towards Science and Faith and a) Age, and b) Science Knowledge

Instead, you may want to consider using geom_jitter() to add a little bit of noise (or jitter) to the plot. Within the geom_jitter() argument, take some time to experiment with the size = and alpha = arguments to find optimal values to aid interpretation.

Solution


Question 5

Produce a correlation matrix of the variables which are to be used in the analysis, and write a short paragraph describing the associations.

Review the correlation flashcards, and remember to interpret in the context of the research question.

Solution

Model Fitting & Interpretation

Question 6

Fit the specified model, and assign it the name “att_mdl”.

\[ \text{Attitude} = \beta_0 + \beta_1 \cdot \text{Science Knowledge} + \beta_2 \cdot \text{Age} + \epsilon \]

Solution


Question 7

Check the assumptions of your model. Note any violations of the model assumptions.

Review the assumptions flashcards and consider the most efficient way to do this (might be a good idea to review the useful assumption plots).

Solution


Question 8

Bootstrap your model, computing 1000 bootstrap samples.

Provide key model results in a formatted table, and interpret your coefficients in the context of the research question.

Review the bootstrap flashcards.

You can’t use tab_model() here, but instead will need to use kable() and kable_styling(). Check over the tables flashcard, and in particular review the RMD bootcamp lesson which is signposted to.

Solution


Question 9

Obtain 95% confidence intervals for your bootstrapped model estimates.

Review the bootstrap flashcards.

Solution

Writing Up & Presenting Results

Question 10

Interpret the results from your bootstrapped model in the context of the research question.

Make reference to your key result table(s) and plot(s).

Make sure to include a decision in relation to your null hypothesis - based on the evidence, should you reject or fail to reject the null?

Solution

Compile Report

Compile Report

Knit your report to PDF, and check over your work. To do so, you should make sure:

  • Only the output you want your reader to see is visible (e.g., do you want to hide your code?)
  • Check that the tinytex package is installed
  • Ensure that the ‘yaml’ (bit at the very top of your document) looks something like this:
---
title: "this is my report title"
author: "B1234506"
date: "07/09/2024"
output: bookdown::pdf_document2
---

If you are having issues knitting directly to PDF, try the following:

  • Knit to HTML file
  • Open your HTML in a web-browser (e.g. Chrome, Firefox)
  • Print to PDF (Ctrl+P, then choose to save to PDF)
  • Open file to check formatting

Review Lesson 5 of the rmd bootcamp for a detailed description/worked examples.

To not show the code of an R code chunk, and only show the output, write:

```{r, echo=FALSE}
# code goes here
```

To show the code of an R code chunk, but hide the output, write:

```{r, results='hide'}
# code goes here
```

To hide both code and output of an R code chunk, write:

```{r, include=FALSE}
# code goes here
```

You must make sure you have tinytex installed in R so that you can “Knit” your Rmd document to a PDF file:

install.packages("tinytex")
tinytex::install_tinytex()

Solution