Welcome to the PSYC3361 coding W3 self test. The test assesses your ability to use the coding skills covered in the Week 3 online coding modules.

In particular, it assesses your ability to…

It is IMPORTANT to document the code that you write so that someone who is looking at your code can understand what it is doing. Above each chunk, write a few sentences outlining which packages/functions you have chosen to use and what the function is doing to your data. Where relevant, also write a sentence that interprets the output of your code.

Your notes should also document the troubleshooting process you went through to arrive at the code that worked.

For each of the challenges below, the documentation is JUST AS IMPORTANT as the code.

Good luck!!

Jenny

PS- if you get stuck have a look in the /images folder for inspiration

load the packages you will need

library(tidyverse)
library(janitor)

#as a general rule, best practice to download following pckgs: 
  #tidyverse,
  #janitor 
  #here 

read the Alone data

alone <-read.csv(file="data/alone.csv")

#press tab to reveal file options after typing "alone"
#if object not found, highlight text and click source 
#object should appear in environment pane 

1. make a smaller dataset

We are mostly interested in gender, age, the days they lasted and whether contestants were medically evacuted. Use select() to make a smaller dataframe containing just the relevant variables. Rename the variable called medically_evacuated to make it shorter and easier to type

new_alone <- alone %>% 
  select(gender, age, days_lasted, medically_evacuated) %>% 
  rename(medivac = medically_evacuated)

#once again, dont forget to source
#new_alone should appear on the environment pane 

2. write code to determine how old the oldest male and female contestant are

age_sum <- new_alone %>% 
  group_by(gender) %>% 
  summarise(oldest = max(age)) %>% 
  ungroup()

print(age_sum)
## # A tibble: 2 × 2
##   gender oldest
##   <chr>   <int>
## 1 Female     57
## 2 Male       61
#alternatively, can use the slice function using the following code: 

# alone %>%
#   arrange(desc(age)) %>%
#   slice(1:2)

#the slice function selects only the first and second observations 

3. has the average length of time that alone contestants lasted changed over seasons?

seasonal<- alone %>% 
  group_by(season) %>% 
  summarise(mean_length = mean(days_lasted),
            sd_length = sd(days_lasted), 
            n = n(), 
            stderr = sd_length/sqrt(n)
  ) %>% 
  ungroup()


print(seasonal)
## # A tibble: 9 × 5
##   season mean_length sd_length     n stderr
##    <int>       <dbl>     <dbl> <int>  <dbl>
## 1      1        21.6      23.6    10   7.45
## 2      2        34.4      25.0    10   7.89
## 3      3        54.3      30.9    10   9.76
## 4      4        31.4      32.4    14   8.65
## 5      5        30.1      19.4    10   6.14
## 6      6        45.4      28.0    10   8.86
## 7      7        49.9      31.6    10   9.99
## 8      8        41.2      26.6    10   8.40
## 9      9        46.1      21.6    10   6.84
#try to keep names short and succint (time might be a better name here)
#named average length as avg_length 
#prepare sd, n and sderror separately for each season to make error bars
#note formula for standard error 
#dont forget the brackets after n 

HINT: can you make a line graph that has error bars around the mean for each season?

seasonal$season <- as.factor(seasonal$season)

seasonal %>% 
ggplot(aes(
  x = season, 
  y = mean_length, 
  group = "season"
  )
) + 
  geom_point() + 
  geom_line() + 
  geom_errorbar(aes(
    ymin = mean_length - stderr, 
    ymax = mean_length + stderr
  ), width = 0.2
  ) + 
  labs(title = "Mean Number of Days Spent Alone across Seasons", 
       y = "Mean number of days", 
       x = "Season") + 
  theme_minimal()

4. do women on average last longer in the game than men? Are men more likely to leave early?

alone_gen <- alone %>% 
  group_by(gender) %>% 
  summarise(mean = mean(days_lasted))

print(alone_gen)
## # A tibble: 2 × 2
##   gender  mean
##   <chr>  <dbl>
## 1 Female  49.4
## 2 Male    36.2

HINT: can you make a plot that captures the median and distribution of days survived, by gender?

alone %>% 
  ggplot(aes(x = gender, y = days_lasted, fill = gender)) + 
  geom_boxplot()+
  geom_jitter(width = 0.1, alpha = 0.5, size = 2) + 
  theme_minimal() + 
  labs(title = "The distribution of days lasted alone as a function of gender", 
       y = "Number of days lasted",
       x = "Gender")

5. do older contestants last longer?

HINT: Use case_when to create a new variable that groups participants by age in decades

alone_dec<- alone %>% 
  mutate(age_group = case_when(age <20 ~ "teen", 
                                age>=20 & age<30 ~ "twenties", 
                                age>=30 & age<40 ~ "thirties",
                                age>=40 & age<50 ~ "fourties",
                                age>=50 & age<60 ~ "fifties",
                                age>=60 & age<70 ~ "sixties"))
alone_dec %>% 
  tabyl(age_group)
##  age_group  n    percent
##    fifties  6 0.06382979
##   fourties 37 0.39361702
##    sixties  1 0.01063830
##       teen  2 0.02127660
##   thirties 36 0.38297872
##   twenties 12 0.12765957
#need to load janitor package in order to use tabyl function 
#having issues figuring out how to do the tabyl thing!!!!!????

HINT: what is the mean length of time in the game for each age group? How many participants fall into each group?

alone_dec %>% 
  group_by(age_group) %>% 
  summarise(Mtime = mean(days_lasted), 
            n = n())
## # A tibble: 6 × 3
##   age_group Mtime     n
##   <chr>     <dbl> <int>
## 1 fifties    42.2     6
## 2 fourties   37.1    37
## 3 sixties    74       1
## 4 teen        1.5     2
## 5 thirties   41.4    36
## 6 twenties   39.7    12

6. Are contestants who are medically evacuted, on average older than those who pull out themselves? does that differ by gender?

HINT: filter the dataset to keep only those contestants who didn’t win, then calculate the mean age, separately for those who were medically evacuated vs. not.

med_gender <-alone %>% 
  filter(result >1) %>% 
  group_by(medically_evacuated, gender) %>% 
  summarise(mean_age = mean(age))

med_gender$medically_evacuated <- as.factor(med_gender$medically_evacuated )

med_gender$medically_evacuated <- fct_relevel(med_gender$medically_evacuated, 
                                              c("TRUE" , "FALSE") )

HINT: make a column graph of the data you just summarised

med_gender %>% 
  ggplot(aes(
    x = gender, 
    y = mean_age, 
    fill = medically_evacuated
  )) + 
  geom_col(position = "dodge") + 
  theme_minimal() + 
  labs(title = "Mean Age of Contestants Medically Evacuated or Not, as a Function of Gender", x = "Gender", y = "Mean Age")

7. knit your document to pdf