Open RStudio. Open a new R script (File – New File – R Script), and save it as wpaXLastFirst.R (where X is the WPA number, and 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 wpaXLastFirst.R file could look
# Assignment: WPA X
# Name: LAST, FIRST
# Date: DAY MONTH YEAR
# TASK 1
1 + 1
# TASK 2
2 + 2
# ...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
Create a vector of the participant data called
participantusing thec()function.Now, create the
participantvector again, but this time use thea:bfunction.Now create the
participantvector again! But this time use theseq()function.Create a vector of the before drink data called
beforeusingc().Create a vector of the after drink data called
afterusingc().Create a vector of the age data called
ageusingc().Create a vector of the sex data called
sexbut don’t usec(). Instead, use therep()function by looking for a pattern in the data.Create a vector of the eye color data called
eye.colorbut don’t usec(). Instead, use therep()function by looking for the pattern in the data.
Combining and changing vectors
Create a new vector called
age.monthsthat shows the participants’ age in months instead of years. (Hint: use basic vector arithmetic)Create a new vector called
changethat shows the change in participants’ scores from before to after (Hint: use basic vector arithmetic)Create a new vector called
averagethat shows the participants’ average score across both tests. That is, the first element ofaverageshould 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 usemean()! Use basic vector arithmetic)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! Make sure you also update the
changevector!
Applying functions to vectors
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!What was the standard deviation of ages? Assign the result to a scaler object called
age.sd.What is the median age? Assign the result to a scaler object called
age.median.How many people were there of each sex? (Hint: use
table())What percent of people had each sex? (Hint: use
table()then divide by its sum withsum())Calculate the mean of the
sexcolumn. What happens and why?What was the mean
beforetime? Assign the result to a scaler object calledbefore.mean.What was the mean
aftertime? Assign the result to a scaler object calledafter.mean.What was the difference in the mean
beforetimes and the meanaftertimes? Calculate this in two ways: once using thechangevector, and once using thebefore.meanandafter.meanobjects. You should get the same answer for both! (If you don’t get the same answer for both, it’s probably because you didn’t update thechangevector in Question 12!)
CHECKPOINT!
If you got this far you are doing just fine! Keep it up!
Standardizing (z-scores) vectors
Create a vector called
before.zshowing a standardized version ofbefore.Create a vector called
after.zshowing a standardized version ofafter.What was the largest
beforescore? What was its corresponding z-score?What was the smallest
afterscore? What was its corresponding z-score?What should the mean and standard deviation of
before.zandafter.zbe? Test your predictions by making the appropriate calculations.
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)Create a vector called
samp.10that contains 10 samples from a Normal distribution with a mean of 100 and a standard deviation of 10.Create a vector called
samp.100000that contains 100,000 samples from the same Normal distribution as above (that is, also with a mean of 100 and standard deviation of 10).Before making any calculations, what would you guess the mean and standard deviations of
samp.10andsamp.100000are? Which prediction are you more confident in?Calculate the mean and standard deviations of
samp.10andsamp.100000separately. Was your prediction correct?
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 numberNow 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 wpaXLastFirst.R file to me at nathaniel.phillips@unibas.ch. Then, go to https://goo.gl/forms/rheaASgVwAGXesze2 to complete the WPA submission form.