Download the source material (RMarkdown, css, js files) to generate this lab as zip or tgz archive.
Over the course of the year our prehonours students all complete a series of labs that are designed to help them develop and expand their understanding of research methods, research practices, and research analysis. There are 6 of these labs in Year 1 (3 per Semester) and 18 of these labs in Year 2 (9 per semester). Within each lab a certain length of time (e.g. 1hr) is given over to elements related to analysis including: reproducible coding; data-wrangling; generating figures; probability; power and effect sizes; distributions; descriptives; inferentials; General Linear Model.
All the labs have the format of:
For this Lab demonstartion we are using an updated version of the 4th inclass activity from our Year 2 course, Semester 1 - as such the wording assumes you have completed the preclass activity for this lab which focussed on explaining probability via the binomial distribution and coin tosses. By this stage in their development students have already been introduced to RMarkdown, installing and loading libraries, data-wrangling and the Wickham six verbs, and generating figures through ggplot.
In nearly all the labs this year we will need the tidyverse
to complete our activities. Start a new script or Rmd file and copy and run the below line to bring the tidyverse into our working environment.
library("tidyverse")
Before doing any coding we will start off by briefly looking at a couple of examples of probability, based on the preclass activity, to make sure you have understood it. Just select the correct answer from the dropdown menus - they will turn blue if you are correct. Also, keep in mind that R can be used as a calculator (e.g. calculation <- 4 + 3
) if you want to run some quick maths. See the hints below the questions if you are unsure.
One possible outcome from six
Three possible outcomes from six
Combining probabilities should make things more unlikely but still possible
The probability of Question 1 by the probability of Question 1
Great! We will now look again at the binomial distribution and test your knowledge of probability and of coding. We covered this in the preclass activity so refer back to that or the supplied hints should you get stuck. S
You design a psychology experiment where you are interested in how quickly people spot faces. Using a standard task you compare images of faces, houses and noise patterns and you ask participants to respond face
or not
. You set the experiment so that across the whole experiment the number of images per stimuli type are evenly split (1/3 per type) but not necessarily evenly split across the three stimuli in any one block. Each block contains 60 trials. To test your experiment you run one block and get concerned that you only saw 10 faces and thought something might be wrong. You decide to create a probabiliy distribution for one block to test the likelihood of seeing a face (coded as 1) versus not seeing a face (coded as 0).
sample()
function but it is incomplete. Replace both the NULL
to establish an estimate of how many faces you would likely see in 1 block of 60 trials. Note that the probability (prob) is set to weight “not seeing a face” (2/3) more than “seeing a face” (1/3).sample(0:1, NULL, replace = NULL, prob = c(2/3, 1/3)) %>% sum()
2. So we now have a rough number of how many times a face will likely have been seen in a block. But we want to know how that compares to our actual count of 10 faces when we ran the experiment. To do this we will need to create a distribution. Replacing the NULL and saving your result as blocks_5k
, use the replicate
function to repeat the test in Step 1 for 5000 blocks of 60 trials.
blocks_5k <- NULL
3. If your code has worked so far then running the below chunk should create a tibble with two columns: faces
showing the number of faces seen in individual runs, and n
the count if how many times that number of faces was seen. Run these lines in your code and see. If not, something has gone wrong; debug.
data5K <- tibble(faces = blocks_5k) %>%
count(faces)
n
to a probability and places these probabilties in a column called p
.data5K <- NULL
5. Now that we have our distribution we should look at it. Plot the distribution of probabilities stored within data5K
. Add a theme of your chosing and remember to label the axes appropriately.
6. From your data5k
tibble, what was the probability of seeing only 10 faces in a block. Do this through a line of code, replacing the NULL, and storing the answer as a single value in prob_10faces
. You will need the new function pluck()
.
prob_10faces <- NULL
An alternative to the above process would have be to use the the appropriate binom()
function to determine the probability of 10 faces in 60 trials. Do this now, replacing the NULL in the below chunk with one line of code.
act_p_10faces <- NULL
And we can look at it from a different viewpoint using the pbinom()
function. What would be the probability of seeing more than 25 faces in a block of 60 trials.
act_p <- NULL
pbinom
to find the probability of values less than or equal to some X value, the argument lower.tail
should be set to Congratulations. You have now completed the Lab demonstration.
For homework, students would now be given a link to a stub Rmd file with similar questions as to the above where they replace the NULL in the code chunk with either the apporpriate value, function, or code. Students then complete the code chunks and submit the Rmd file to Moodle for assessment within one week as well as completing any preparation for the following lab. Further information as regards the computer-assisted marking of these assessments will be shown in the next talk session.
sample(0:1, 60, replace = TRUE, prob = c(2/3, 1/3)) %>% sum()
blocks_5k <- replicate(5000, sum(sample(0:1, 60, TRUE, c(2/3, 1/3))))
data5K <- data5K %>% mutate(p = n / 5000)
ggplot(data5K, aes(faces, p)) +
geom_col() +
labs(x = "Number of Faces", y = "Probability") +
theme_bw()
prob_10faces <- filter(data5K, faces == 10) %>% pluck("p")
act_p_10faces <- dbinom(10, 60, 1/3)
act_p <- pbinom(26, 60, 1/3, lower.tail = FALSE)
This page was created using a Web Exercise template created by the psychology teaching team at the University of Glasgow, inspired by Software Carpentry webpages. This template enables instructors to easily create web documents that students can use in self-assessment.
The webexercises package provides a number of functions that you use in inline R code to create HTML widgets (text boxes, pull down menus, buttons that reveal hidden content). Examples are provided in this document. Knit this file to HTML to see how it works.
NOTE: If the widgets don’t work for you in the built-in RStudio browser, open the HTML file in a different browser.