Total: 10 points (Part A = 2 points, Part B = 2.5 points, Part C = 2.5 points, Part D = 3 points)

Part A: Getting Started — 2 points

1) The Working Directory (1 pt)

a. Run getwd() to see which folder R is currently looking in. (0.25 pts)

getting the working directory:

getwd()
## [1] "/Users/owner/Desktop/Lab 4"

b. Run list.files() to list the files in that folder. Is “ExamAnxiety.csv” one of them? (0.25 pts)

running the list to see if “ExamAnxiety.csv” is one of the files:

list.files()
## [1] "ExamAnxiety.csv"      "Lab 4 Assignment.Rmd" "Lab-4-Assignment.Rmd"

Answer: ## Yes, “ExamAnxiety.csv” is one of the files.

c. Use setwd() to set the working directory to the folder where you saved “ExamAnxiety.csv”, then run getwd() and list.files() again to confirm that R can now see the file. (0.5 pts)

setting the working directory:

setwd("/Users/owner/Desktop/Lab 4")

2) Loading Packages (0.5 pts)

In the code chunk below, use library() to load every package you need for this lab session. (0.5 pts)

loading the package needed for plotting:

library(ggplot2)

3) Reading in the Data (0.5 pts)

Save “ExamAnxiety.csv” into an object, then use head() to display the first 6 rows. (0.5 pts)

saving the .csv into an object and displaying the first 6 rows:

data <- read.csv("ExamAnxiety.csv")
head(data, n=6)
##   X Code Revise Exam Anxiety Gender
## 1 1    1    -99   40  86.298   Male
## 2 2    2     11   65  88.716 Female
## 3 3    3     27   80  70.178   Male
## 4 4    4     53   80  61.312   Male
## 5 5    5      4   40  89.522   Male
## 6 6    6     22   70  60.506 Female

Part B: Z-Scores — 2.5 points

4) Z-Scoring Anxiety (1.5 pts)

a. Using mean() and sd(), create a new column called Anxiety_z that holds the z-scored Anxiety values. Show the code, then use head() to display the first 6 rows. (0.5 pts)

finding values of central tendency and displaying the first 6 rows:

first, calculating the mean:

ExamAnxiety <- read.csv("ExamAnxiety.csv")
ExamAnxiety$Anxiety_z <- (ExamAnxiety$Anxiety - mean(ExamAnxiety$Anxiety)) / sd(ExamAnxiety$Anxiety)

head(ExamAnxiety)
##   X Code Revise Exam Anxiety Gender  Anxiety_z
## 1 1    1    -99   40  86.298   Male  0.7052090
## 2 2    2     11   65  88.716 Female  0.8459139
## 3 3    3     27   80  70.178   Male -0.2328242
## 4 4    4     53   80  61.312   Male -0.7487424
## 5 5    5      4   40  89.522   Male  0.8928156
## 6 6    6     22   70  60.506 Female -0.7956441

b. Check your work: what are the mean and the standard deviation of Anxiety_z? Why should they be 0 and 1? (0.5 pts)

mean(ExamAnxiety$Anxiety_z)
## [1] -2.536098e-16
sd(ExamAnxiety$Anxiety_z)
## [1] 1

Answer: ## The mean is approximately 0 and the standard deviation is 1. I got -2.536098e-16 as the mean, but this was likely due to a minimal rounding error and essentially equates to 0. These values should be 0 and 1 since z-scoring centers a data set around a mean of 0 and scales it in a way that results in a standard deviation of 1.

c. Participant 1 has an Anxiety score of 86.298. Report their z score and explain, in one sentence, what it tells you about where they fall in the distribution. (0.5 pts)

ExamAnxiety$Anxiety_z[1]
## [1] 0.705209

Answer: ## Participant 1 has a z-score of 0.705209, or about 0.0705, meaning that their Anxiety score is about 0.71 standard deviations above the mean.

5) Comparing Across Variables (1 pt)

a. Create Exam_z, the z-scored Exam column. For participant 1, which score is further from the mean: their Anxiety or their Exam score? (0.5 pts)

ExamAnxiety$Exam_z <- (ExamAnxiety$Exam - mean(ExamAnxiety$Exam)) / sd(ExamAnxiety$Exam)

ExamAnxiety$Exam_z[1]
## [1] -0.6384114

comparing the absolute values of the two z-scores:

abs(ExamAnxiety$Anxiety_z[1])
## [1] 0.705209
abs(ExamAnxiety$Exam_z[1])
## [1] 0.6384114

Answer: ## Since z = 0.705209 which is greater than 0.638411 (Exam z-score), meaning it is further from the mean.

b. Why can raw Anxiety and Exam scores not be compared directly, while their z scores can? (0.5 pts)

Answer: ## They cannot be compared directly since they might have different scales or ranges. Z-scores are supposed to standarise both variables to the same scale. The mean of that scale is supposed to be 0 with a standard deviation of 1, so they can be compared based on how far each score is from its own mean.

Part C: The Normal Distribution — 2.5 points

6) Generating Normal Data (1.25 pts)

a. Use rnorm() to generate 1000 scores from a normal distribution with the same mean and standard deviation as the real Anxiety variable. Save them in an object. (0.5 pts)

generating normal data:

Normal_Anxiety <- rnorm(1000, mean(ExamAnxiety$Anxiety), sd(ExamAnxiety$Anxiety))

b. Use mean() and sd() on your simulated scores. Are they exactly the values you asked for? Why or why not? (0.5 pts)

mean(Normal_Anxiety)
## [1] 74.59951
sd(Normal_Anxiety)
## [1] 17.4981

Answer: ## No, because the mean and standard deviation of the simulated scores won’t match those of the real Anxiety variable. This is due to how rnorm() randomly generates the scores, so the simulated sample will be close to the requested values, however, the random sampling will cause variation.

c. Plot the simulated scores with hist(). Describe the shape in one sentence. (0.25 pts)

hist(Normal_Anxiety)

Answer: ## This histogram can be described as bell-shaped and fairly symmetric. This indicates a normal distribution data set.

7) Probabilities of Normal Values (1.25 pts)

a. Use pnorm() with participant 1’s Anxiety z score. What proportion of a standard normal distribution falls below it? (0.5 pts)

pnorm(ExamAnxiety$Anxiety_z[1])
## [1] 0.7596599

Answer: ## About 76% of a standard normal distribution falls below Participant 1’s Anxiety z-score.

b. What proportion falls above that z score? (0.25 pts)

1 - pnorm(ExamAnxiety$Anxiety_z[1])
## [1] 0.2403401

Answer: ## About 24% falls above that z-score.

c. Use qnorm() to find the z score that marks the top 10% of the distribution (the 90th percentile). Then convert it back to an anxiety score with mean + z × sd. (0.5 pts)

z_90 <- qnorm(0.90)
z_90
## [1] 1.281552

converting back to an Anxiety score:

mean(ExamAnxiety$Anxiety) + z_90 * z_90 * sd(ExamAnxiety$Anxiety)
## [1] 102.4031

Answer: ## A z-score of 1.2816 marks the top 10% of the distribution which corresponds to the Anxiety score of 102.4031.

Part D: Visualizing with ggplot2 — 3 points

8) Scatterplot (1.5 pts)

a. Use ggplot() with aes() and geom_point() to plot Anxiety on the x-axis and Exam on the y-axis. (0.5 pts)

##plotting Anxiety & Exam on corresponding axes

ggplot(ExamAnxiety, aes(x = Anxiety, y = Exam)) + geom_point()

b. Add color = Gender inside aes(), and add a trend line with geom_smooth(method = “lm”). (0.5 pts)

##adding color and a trend line

ggplot(ExamAnxiety, aes(x = Anxiety, y = Exam, color = Gender)) +
         geom_point() +
         geom_smooth(method = "lm")
## `geom_smooth()` using formula = 'y ~ x'

c. What does the plot suggest about the relationship between exam anxiety and exam performance? (0.5 pts)

Answer: ## The plot suggests that there is a negative relationship between exam anxiety and exam performance, meaning that there is an association between higher exam anxiety and lower exam scores.

9) Boxplots, Bar Plots, and Facets (1.5 pts)

a. Use geom_boxplot() to show Exam scores for each Gender (x = Gender, y = Exam, fill = Gender). Label both axes with labs() and apply a theme such as theme_classic(). (0.75 pts)

Answer: ## creating box plots:

ggplot(ExamAnxiety, aes(x = Gender, y = Exam, fill = Gender)) + 
  geom_boxplot() +
  labs(x = "Gender", y = "Exam Score") +
  theme_classic()

b. Use geom_bar() to show how many participants are in each Gender group. (0.25 pts)

##using bar graph: Answer:

ggplot(ExamAnxiety, aes(x = Gender)) + 
  geom_bar()

c. Take your scatterplot from 8b and add facet_wrap(~ Gender). What does splitting the plot into panels let you see? (0.5 pts)

ggplot(ExamAnxiety, aes(x = Anxiety, y = Exam, color = Gender)) + 
  geom_point() +
  geom_smooth(method = "lm") +
  facet_wrap(~ Gender)
## `geom_smooth()` using formula = 'y ~ x'

Answer: ## Splitting the plot into panels let’s me see the relationship between Anxiety and Exam scores separately amongst the gender groups. This makes it easier to draw a comparison (i.e. the slope of males’ being steeper).