Open a new R script in R and save it as wpa_X_LastFirst.R (where Last and First is your last and first name). At the top of your script, write the assignment number, your name and date in comments. When you answer a task, indicate which task you are answering with appopriate comments.

Here is an example of how your wpa_X_LastFirst.R file could look

# Assignment: WPA X
# Name: LAST, FIRST
# Date: X MONTH YEAR


# TASK 1
1 + 1

# TASK 2
# Draw 100 samples from a standard normal
x <- rnorm(100)
# Conduct a t-test
t.test(x)

#... and so on...

Does drinking non-alcoholic beer affect cognitive performance?

A psychologist has a theory that some of the negative cognitive effects of alcohol are the result of psychological rather than physiological processes. To test this, she has 12 participants perform a cognitive test before and after drinking non-alcoholic beer which was labelled to contain 5% alcohol. Results from the study, including some demographic data, are presented in the following table. Note that higher scores on the test indicate better performance.

participant before after age sex eye.color
1 45 43 20 male blue
2 49 50 19 female blue
3 40 61 22 male brown
4 48 44 20 female brown
5 44 45 27 male blue
6 70 20 22 female blue
7 90 85 22 male brown
8 75 65 20 female brown
9 80 72 25 male blue
10 65 65 22 female blue
11 80 70 24 male brown
12 52 75 22 female brown

Creating vectors from scratch

  1. Create a vector of the participant data called participant using the c() function.
participant <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
  1. Now, create the participant vector again, but this time use the a:b function.
participant <- 1:12
  1. Now create the participant vector again! But this time use the seq() function.
participant <- seq(from = 1, to = 12, by = 1)
  1. Create a vector of the before drink data called before using c().
before <- c(45, 49, 40, 48, 44, 70, 90, 75, 80, 65, 80, 52)
  1. Create a vector of the after drink data called after using c().
after <- c(43, 50, 61, 44, 45, 20, 85, 65, 72, 65, 70, 75)
  1. Create a vector of the age data called age using c().
age <- c(20, 19, 22, 20, 27, 22, 22, 20, 25, 22, 24, 22)
  1. Create a vector of the sex data called sex but don’t use c(). Instead, use the rep() function by looking for a pattern in the data.
sex <- rep(c("male", "female"), length.out = 12)
# OR
sex <- rep(c("male", "female"), times = 6)
  1. Create a vector of the eye color data called eye.color but don’t use c(). Instead, use the rep() function by looking for the pattern in the data.
# Here are different methods:
eye.color <- rep(c("blue", "brown"), each = 2, times = 3)
eye.color <- rep(c("blue", "brown"), each = 2, length.out = 12)
eye.color <- rep(c("blue", "blue", "brown", "brown"), times = 3)

Combining and changing vectors

  1. Create a new vector called age.years that shows the participants’ age in months instead of years. (Hint: use basic vector arithmetic)
# Note: I probably should have called this age.months...
age.years <- age * 12
  1. Create a new vector called change that shows the change in participants’ scores from before to after (Hint: use basic vector arithmetic)
change <- after - before
  1. Create a new vector called average that shows the participants’ average score across both tests. That is, the first element of average should be the average of the first participant’s two scores, and the second element should be the average of the second participant’s two scores…(Hint: Don’t use mean()! Use basic vector arithmetic)
average <- (after + before) / 2
  1. Oops! It turns out that the watch used to measure time was off. All the before times are 1 second too fast, and all the after times are 1 second too slow. Correct them!
before <- before + 1
after <- after - 1

Applying functions to vectors

  1. How many elements are in each of the original data vectors? (Hint: use length()). If the number of elements in each is not the same, you typed something in wrong!
length(participant)
## [1] 12
length(before)
## [1] 12
length(after)
## [1] 12
length(age)
## [1] 12
length(sex)
## [1] 12
length(eye.color)
## [1] 12
  1. What was the standard deviation of ages? Assign the result to a scaler object called age.sd.
age.sd <- sd(age)
  1. What is the median age? Assign the result to a scaler object called age.median.
age.median <- median(age)
  1. How many people were there of each sex? (Hint: use table())
table(sex)
## sex
## female   male 
##      6      6
  1. What percent of people had each sex? (Hint: use table() then divide by its sum with sum())
table(sex) / sum(table(sex))
## sex
## female   male 
##    0.5    0.5
# OR

table(sex) / length(sex)
## sex
## female   male 
##    0.5    0.5
  1. Calculate the mean of the sex column. What happens and why?
# We get an NA because sex is not a numeric vector!
mean(sex)
## [1] NA
  1. What was the mean before time? Assign the result to a scaler object called before.mean.
before.mean <- mean(before)
  1. What was the mean after time? Assign the result to a scaler object called after.mean.
after.mean <- mean(after)
  1. What was the difference in the mean before times and the mean after times? Calculate this in two ways: once using the change vector, and once using the before.mean and after.mean objects. You should get the same answer for both!
after.mean - before.mean
## [1] -5.583333
# We have to create the change vector again because we updated after and before!
change <- after - before
mean(change)
## [1] -5.583333

CHECKPOINT!

If you got this far you are doing just fine! Keep it up!

Standardizing (z-scores) vectors

  1. Create a vector called before.z showing a standardized version of before.
before.z <- (before - mean(before)) / sd(before)
  1. Create a vector called after.z showing a standardized version of after.
after.z <- (after - mean(after)) / sd(after)
  1. What was the largest before score? What was its corresponding z-score?
max(before)
## [1] 91
max(before.z)
## [1] 1.662411
  1. What was the smallest after score? What was its corresponding z-score?
min(after)
## [1] 19
min(after.z)
## [1] -2.106802
  1. What should the mean and standard deviation of before.z and after.z be? Test your predictions by making the appropriate calculations.
# The mean should be 0 the sd should be 1!

mean(before.z)
## [1] 1.619979e-17
sd(before.z)
## [1] 1
mean(after.z)
## [1] 1.526557e-16
sd(after.z)
## [1] 1

Random samples from distributions

You can create random samples of values from a Normal distribution using the rnorm(n, mean, sd) function. For example, the following code chunk will save a vector of 50 values from a Normal distribution with mean of 20 and standard deviation of 10

# Random sample of 50 values from N(mean = 20, sd = 10)
x <- rnorm(n = 50, mean = 20, sd = 10)
  1. Create a vector called samp.10 that contains 10 samples from a Normal distribution with a mean of 100 and a standard deviation of 10.
samp.10 <- rnorm(n = 10, mean = 100, sd = 10)
  1. Create a vector called samp.100000 that contains 100,000 samples from the same Normal distribution as above (that is, also with a mean of 100 and standard deviation of 10).
samp.100000 <- rnorm(n = 100000, mean = 100, sd = 10)
  1. Before making any calculations, what would you guess the mean and standard deviations of samp.10 and samp.100000 are? Which prediction are you more confident in?
# My best guess for both means is 100 and for both sds is 10.
#  However I'm more confident in samp.100000 because the sample size
#   is larger!
  1. Calculate the mean and standard deviations of samp.10 and samp.100000 separately. Was your prediction correct?
mean(samp.10)
## [1] 103.2149
sd(samp.10)
## [1] 9.748821
mean(samp.100000)
## [1] 100.0389
sd(samp.100000)
## [1] 10.00989
# Yes my prediction was correct! (at least for this sample...)

The Room with 100 Boxes

Imagine the following. There is a room with 100 boxes. 99 of the 100 boxes each contain 100,000 EUR which you can keep. 1 of the 100 boxes contains a bomb which kills you if you open it.

Here’s the question…if you walked into the room with 100 boxes, how many would you want to open?

You can easily play the Room with 100 Boxes game using the sample function in R.

First, put the number of boxes you want to open as a new scaler object called i.will.open:

i.will.open <- 0  # How many do you want to open? Change the value from 0 to your number

Now run the following code to see what you get!

# Play the Room with 100 Boxes Game!

boxes.result <- sample(x = c(rep(100000, 99), -Inf),  # Sample from the box...
                 size = i.will.open)

# Print what you got!

boxes.result  # Show what's in each box (1 means 10,000 EUR)
sum(boxes.result)  # Your total winnings!

You can also represent the boxes game with a custom function in R. Run the following chunk to create the new function play.boxes.game:

# Run this chunk to create the function
play.boxes.game <- function(i.will.open) {

# Prevents exponent printing  
options("scipen" = 100, "digits" = 4)
  
if(i.will.open == 0) {
  
  print("You didn't open any boxes! You earned nothing but are still alive")}
  
if(i.will.open > 0) {
  
  boxes.result <- sample(x = c(rep(100000, 99), -Inf),
                   size = i.will.open)

if(-Inf %in% boxes.result) {
  
  print(paste("You're dead!!! You opened ", i.will.open, 
              " boxes and got the bomb!!!", sep = ""))}

if((-Inf %in% boxes.result) == FALSE) {
  
  print(paste("Congratulations!!! You opened ", i.will.open, 
              " boxes and earned ", sum(boxes.result), 
              " Euros! Don't you want to play again? :)", sep = ""))}
}
  
}

Now play the game just by running the function with the number of boxes you want to open as the main argument! For example…

play.boxes.game(0)  # Play with 0 boxes
## [1] "You didn't open any boxes! You earned nothing but are still alive"

That’s it! Now it’s time to submit your assignment! Save and email your .R file to me at nathaniel.phillips@unibas.ch. Then, go to https://goo.gl/forms/UblvQ6dvA76veEWu1 to complete the WPA submission form.