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 will load the tidyverse package as it contains ggplot and dplyr packages. I will also load the here package as it can tell R where the data is when reading it in.
library(tidyverse)
library(here)
To read the dino data in, use read_csv as the file is in .csv format. The “here” function tells R to find the data in the data folder. R will also make a new object labelled dino.
dino <- read_csv(here("data", "dino.csv"))
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 will use ggplot to produce the plot.
I wll plot x on the x-axis, and y on the y-axis. Geom_point() will show
the data as dots. To make different plots for each data set, I will use
facet_wrap(). To produce a regression line for the data, i will use
geom_smooth(). The regression lines were not stright but using “lm”
resulted in a stright line. To have the limits of the y-axis be between
0 and 100, i used scale_y_continuous.
dino %>%
ggplot(aes(x = x, y = y)) +
geom_point() +
geom_smooth(method = "lm") +
facet_wrap(~ dataset) +
scale_y_continuous(limits = c(0,100))
## `geom_smooth()` using formula = 'y ~ x'
HINT: add some colour, play with palettes, try a different theme, add a title, subtitle, caption
To add colour into the plot i will ass colour across x-values. To have a rainbow pattern, i will use scale_colour_gradient. To get rid of the grey background and have the theme look a bit cleaner i used “theme_minimal”. I also added a title and caption.
dino %>%
ggplot(aes(x = x, y = y, colour = x)) +
geom_point() +
geom_smooth(method = "lm") +
scale_colour_gradientn(colours = rainbow(10)) +
facet_wrap(~ dataset) +
scale_y_continuous(limits = c(0,100)) +
theme_minimal() +
labs(title = "Plots with same mean, variance of x and y and correlation", caption = "however they are plotted differently!")
## `geom_smooth()` using formula = 'y ~ x'
## Warning: The following aesthetics were dropped during statistical transformation:
## colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?
## The following aesthetics were dropped during statistical transformation:
## colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?
## The following aesthetics were dropped during statistical transformation:
## colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?
## The following aesthetics were dropped during statistical transformation:
## colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?
## The following aesthetics were dropped during statistical transformation:
## colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?
## The following aesthetics were dropped during statistical transformation:
## colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?
## The following aesthetics were dropped during statistical transformation:
## colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?
## The following aesthetics were dropped during statistical transformation:
## colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?
## The following aesthetics were dropped during statistical transformation:
## colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?
## The following aesthetics were dropped during statistical transformation:
## colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?
## The following aesthetics were dropped during statistical transformation:
## colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?
## The following aesthetics were dropped during statistical transformation:
## colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?
## The following aesthetics were dropped during statistical transformation:
## colour.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?
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
dino %>%
group_by(dataset) %>%
summarise(meanx = mean(x), meany = mean(y),
varx = var(x), vary = var(y), cor_xy = cor(x, y))
## # A tibble: 13 × 6
## dataset meanx meany varx vary cor_xy
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 away 54.3 47.8 281. 726. -0.0641
## 2 bullseye 54.3 47.8 281. 726. -0.0686
## 3 circle 54.3 47.8 281. 725. -0.0683
## 4 dino 54.3 47.8 281. 726. -0.0645
## 5 dots 54.3 47.8 281. 725. -0.0603
## 6 h_lines 54.3 47.8 281. 726. -0.0617
## 7 high_lines 54.3 47.8 281. 726. -0.0685
## 8 slant_down 54.3 47.8 281. 726. -0.0690
## 9 slant_up 54.3 47.8 281. 726. -0.0686
## 10 star 54.3 47.8 281. 725. -0.0630
## 11 v_lines 54.3 47.8 281. 726. -0.0694
## 12 wide_lines 54.3 47.8 281. 726. -0.0666
## 13 x_shape 54.3 47.8 281. 725. -0.0656