Instructions

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

In particular, it assesses your ability to…

  • choose packages/functions
  • read in data
  • group_by and summarise
  • make notes using RMarkdown
  • insert pictures in an Rmd document
  • write data to csv

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

Questions and Answers

1. Customise your Rmd document by adding your name as the author, a table of contents and choosing a theme that you like.

For this part of the self-test I just input the relevant changes into the YAML ~ I decided to use darkly because my eyes hurt when looking at bright things…

2. Load the packages you will need

  1. The only package I need will be the tidyverse package as it is necessary for data analysis
  • I accidentally cleared the console at this point, so I have no clue what I’ve done or did!
    • How exciting!
  • I’ve run the chunk, fingers crossed it works so that the tidyverse package is downloaded
# load packages 
library(tidyverse)

3. Read the birthweight data

  1. I originally tried to run read_csv(“birthweight_data.csv”) but soon afterwards I realised that I needed to specify that it has to be retrieved from the ‘data’ folder so I revised it to: frames <- read_csv(“data/birthweight_data.csv”)
#read data 
frames <- read_csv("data/birthweight_data.csv")

4. Calculate the mean birthweight separately for twins and singletons

  • I ran into a bit of trouble here because I couldn’t visualise the data so I had to input ‘frames’ into the console
# Calculate mean birthweight of twins and singletons 
mean_birthweight_of_twins_and_singletons <- frames %>%
  group_by(plurality) %>% 
  summarise(mean_birthweight = mean(birthweight)) %>% 
  ungroup()

print(mean_birthweight_of_twins_and_singletons)
## # A tibble: 2 × 2
##   plurality mean_birthweight
##   <chr>                <dbl>
## 1 singleton            3248.
## 2 twin                 2311.
  • I still had trouble understanding if I had completed the code correctly, so I used the print function to have a visual representation

  • According to the output, the mean birthweight of twins is 2311 grams, whereas singletons have a mean of 3248 grams.

5. Identify the earliest (i.e. the minimum value) gestational age for each ethicity group

  • Based on the same concept as the previous question, I attempted to find the minimum value by inputting min() function in the summarise condition
#Identifying earliest gestational age for each ethnicity group

earliest_gestational_age_per_ethnicity <- frames %>%
  group_by(child_ethn) %>% 
  summarise( Minimum_Gestataional_Age = min(gestation_age_w)) %>% 
  ungroup()

print(earliest_gestational_age_per_ethnicity)
## # A tibble: 10 × 2
##    child_ethn                        Minimum_Gestataional_Age
##    <chr>                             <chr>                   
##  1 Aboriginal/Torres Strait Islander 33                      
##  2 African/African-American          26                      
##  3 Caucasian                         26                      
##  4 East Asian                        33                      
##  5 Hispanic/Latino                   37                      
##  6 Middle-Eastern                    28                      
##  7 Missing                           36                      
##  8 Polynesian/Melanesian             28                      
##  9 South Asian                       28                      
## 10 South-East Asian                  29
  • Similar to Q4, I used the print command to help visualise the minimum gestational age for each ethnicity group
  • Thus, according to the output: Aboriginal/Toress Strait Islanders had minimum gestational age of 33 weeks, African/ African-American had 26 weeks, Caucasians also had 26 week, East Asians had 33 weeks, Hispanics/Latinos had 37 weeks , Middle-Eastern had 28 weeks, Polynesian/ Melanesian had 28 weeks, South Asians had 28 weeks, South-East Asians had 29 weeks and children with Missing ethnicity had a minimum gestational age of 36 weeks old.

6. Write some notes about how group_by and summarise work with the pipe below, including a link to documentation or a blog post that you think is useful

1.From my understanding, the function of the pipe operator is to act as a ‘connector’ that passes on the results/output of the previous operation into subsequent operations 2.As such, the group_by operation gathers the relevant variables and ‘feeds’ them into the pipe thereby allowing for further analysis of the now isolated dataset. 3.Thus, the **summarise* operation is used to identify the statistical qualities of the dataset provided by the pipe, including finding the mean, median, spread, etc…

A helpful link that helped me to understand the relationship between the pipe, group_by and summarise operators can be found here

7. Download a picture of a baby from the internet and insert it into your document below

8. Write the summary of mean birthweight by twins/singletons that you made in step 3 above to a new csv file

  • The following code I wrote is in reference to question 4
    • Interestingly it seems that you can use “path =” and “file =” interchangeably
write_csv(mean_birthweight_of_twins_and_singletons, file =  "mean_birthweight_of_twins_and_singletons.csv")

9. Knit your document and publish the output to RPubs

  • Initially I failed to knit as I did not indent html_document (line 6), which just goes to show how finicky YAML formatting can be