Welcome to the PSYC3361 coding W2 self test. The test assesses your ability to use the coding skills covered in the Week 2 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
I chose the ’‘’tidyverse’’’ package (reasoning in w1 self test) which is stored in and loaded using the ’‘’library()’’’ function. I also chose the ’‘’ggplot2’’’ package, to allow me to create plots with the data.
library(tidyverse)
library(ggplot2)
library(papaja)
library(tinytex)
I first used the ‘read_csv()’ function, which read created an object called ‘dino’.
dino <- read_csv("C:/Users/maxmo/OneDrive - UNSW/Year 4/Year 4 Term 2/PSYC3361 Research Internship/psyc3361Rfiles/psyc3361selftestsRMD/data/dino.csv")
From Jenny: The dino dataset comes from a paper illustrating the importance of plotting your data. In each of these datasets, the mean and variance of x and y are identical and the two variables are correlated in the same way (R = -0.06). When plotted, however, each reveals a very different pattern.
I first created an object called
‘dino_again’ and used the ‘<-’ assignment operator to assign the
following plot to that object. I used the function ‘ggplot’ to create a
plot and specified where the data would be sourced using the previously
created ‘dino’ object in ‘(data = dino)’.
I was having a lot of difficulty reproducing the above plot until I realised I needed to include ‘facet_wrap’, which allows me to break up the data into subplots, and ‘vars’, which breaks up the data depending on certain variables, which in this case I used the ‘dataset’ variable.
To create the correlational line I used ‘geom_smooth’, which produced a weird looking line. I then looked up how the function worked to and found ‘method’, and after experimenting a bit I found that ‘method = lm’ produced a very similar looking correlational line.
dino_again <- ggplot(data = dino, mapping = aes(x = x, y = y)) +
geom_point() +
facet_wrap(vars(dataset)) +
geom_smooth(method = "lm")
plot(dino_again)
## `geom_smooth()` using formula = 'y ~ x'
HINT: add some colour, play with palettes, try a different theme, add a title, subtitle, caption
I just made the colour of the points in ‘geom_point’ purple using ‘colour = “purple”’.
dino_again <- ggplot(data = dino, mapping = aes(x = x, y = y)) +
geom_point(colour = "purple") +
facet_wrap(vars(dataset)) +
geom_smooth(method = "lm")
plot(dino_again)
## `geom_smooth()` using formula = 'y ~ x'
Can you write code to show that the mean, variance, and correlation between x and y is the same for each of the datasets?? HINT: this is a group_by and summarise problem
Here I created a new variable ‘dino_data’, grouped by the ‘dataset’ variable, then used summarise to calculate the means and variances of ‘x’ and ‘y’, and also the ‘cor()’ function to calculate the correlation between ‘x’ and ‘y’.
dino_data <- dino %>%
group_by(dataset) %>%
summarise(mean_x = mean(x),
var_x = var(x),
mean_y = mean(y),
var_y = var(y),
cor_x_y = cor(x, y)) %>%
ungroup()
print(dino_data)
## # A tibble: 13 × 6
## dataset mean_x var_x mean_y var_y cor_x_y
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 away 54.3 281. 47.8 726. -0.0641
## 2 bullseye 54.3 281. 47.8 726. -0.0686
## 3 circle 54.3 281. 47.8 725. -0.0683
## 4 dino 54.3 281. 47.8 726. -0.0645
## 5 dots 54.3 281. 47.8 725. -0.0603
## 6 h_lines 54.3 281. 47.8 726. -0.0617
## 7 high_lines 54.3 281. 47.8 726. -0.0685
## 8 slant_down 54.3 281. 47.8 726. -0.0690
## 9 slant_up 54.3 281. 47.8 726. -0.0686
## 10 star 54.3 281. 47.8 725. -0.0630
## 11 v_lines 54.3 281. 47.8 726. -0.0694
## 12 wide_lines 54.3 281. 47.8 726. -0.0666
## 13 x_shape 54.3 281. 47.8 725. -0.0656