---
title: "Skills Lab 3"
author: "Jennifer Mankin"
date: "11/02/2022"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

# Essential packages
while(!require(remotes)) install.packages("remotes")
library(kableExtra)

## At home
library(tidyverse)

## On uni computers
library(magrittr)
library(dplyr)
library(tidyr)

# Helper packages
while(!require(ViewPipeSteps)) install.packages("ViewPipeSteps")
while(!require(fcuk)) install.packages("fcuk")
while(!require(introverse)) remotes::install_github("spielmanlab/introverse")
```

## As Usual...

- Load required packages in the `setup` code chunk
- Read in the data

URL: https://and.netlify.app/datasets/robot_data_messy.csv

```{r}
robot_data <- readr::read_csv("https://and.netlify.app/datasets/robot_data_messy.csv")
```

### Codebook

```{r}
readr::read_csv("https://and.netlify.app/datasets/robot_codebook.csv")
```

## Overview

```{r}
initial_n <- nrow(robot_data)

summary(robot_data)
```

What issues do you notice?

Compare the information about these variables to the Codebook!

## Working with Data

A few key pieces of advice!

1. **ALWAYS** write a script/document to keep track of each change you make!
  - If you make a mistake, you can recreate everything you did easily
1. Investigate, view, or get summaries of your data **before** you start making changes
1. Start with "easy" changes and build up to more complicated ones
1. Write yourself notes and name your objects sensibly!

## Remove Variables

```{r}
robot_data <- robot_data %>% 
  dplyr::select(-Country, - Ethnicity)

names(robot_data)
```

## Recoding Variables

```{r}
robot_data %>% 
  dplyr::pull(Gender) %>% 
  table()

# How do I know which numbers represent which values...??

robot_data <- robot_data %>% 
  dplyr::mutate(Gender = factor(Gender,
                                       labels = c("Male", "Female"))
  )
```

## Correcting Class Errors

```{r}
robot_data$Age %>% class()

robot_data %>% 
  dplyr::pull(Age) %>% 
  mean()
# uh oh :(

robot_data$Age %>% table()
# words? in MY numeric variable??? it's more likely than you think

# the as.*() family of functions "coerce" each element of the input into whatever the * is
# so as.numeric() changes each element to numeric
robot_data$Age %>% as.numeric()

# the is.* family of functions ask whether each element of the input is a certain class and returns a logical vector
# so is.na() returns TRUE for each element that is missing (NA) and otherwise returns FALSE
robot_data$Age %>% as.numeric() %>% is.na()

# Find out WHICH row(s) are the problem
robot_data %>% 
  dplyr::filter(is.na(as.numeric(Age)))

####### IMPORTANT #######
# Keep a record of the changes you make!!!
age_char <- robot_data %>% 
  dplyr::filter(is.na(as.numeric(Age))) %>% 
  nrow()

# Change the problem value
# Then change the class to numeric
robot_data <- robot_data %>% 
  dplyr::mutate(Age = dplyr::recode(Age,
                                    "18 years" = "18"),
                Age = as.numeric(Age))

# Check if that's solved it!
robot_data$Age %>% summary()
```

## Checking for Ethical and Sensible Values

Low-risk ethical approval requires that we *cannot* test anyone under the age of 18. If anyone under 18 did participate, we need to remove them.

We also should look out for "impossible" values and decide what to do about them!

```{r}
robot_data %>% 
  dplyr::filter(Age < 18 | Age > 100)

# We MUST remove the people under 18
# What about the people over 99?

# save the number you're removing for each reason
age_removed <- robot_data %>% 
  dplyr::filter(Age < 18 | Age > 100) %>% 
  nrow()

robot_data <- robot_data %>% 
  dplyr::filter(Age >= 18 & Age <= 100)
```

## Removing Non-Completions

```{r}
# Have a look at what is.na() spits out for a whole dataset
is.na(robot_data) %>% tibble::as_tibble()

# Add up the number of missing values for each row (participant)
# For logical data, TRUE = 1 and FALSE = 0
rowSums(is.na(robot_data))

# How many total variables did we have again?
ncol(robot_data)
names(robot_data)

# Return only the participants that have at least one missing value
robot_data %>% 
  dplyr::filter(rowSums(is.na(.)) > 0)

# Which of these are a problem?
# Which are NOT a problem?

# Save the number you removed!
missing_removed <- robot_data %>% 
  dplyr::filter(rowSums(is.na(.)) > 1) %>% 
  nrow()

# Make the change
robot_data <- robot_data %>% 
  dplyr::filter(rowSums(is.na(.)) <= 1)
```

## Save the Final Number

```{r}
final_n <- nrow(robot_data)
```


## Writing Up

To begin, `r initial_n` people were recruited to take part in the questionnaire. Of these, `r age_removed` people were excluded because they reported an age either too young (below 18) or improbably old (above 100). A further `r missing_removed` people were excluded who had not answered any of the questions. As ethical approval allowed for people not to answer particular questions at their discretion, no other cases were removed. The following analysis is based on the final `r final_n` cases.