Total: 10 points (Part A = 2 points, Part B = 2.5 points, Part C = 2.5 points, Part D = 3 points)
getwd()
## [1] "/Users/owner/Desktop/Lab 4"
list.files()
## [1] "ExamAnxiety.csv" "Lab 4 Assignment.Rmd" "Lab-4-Assignment.Rmd"
Answer: ## Yes, “ExamAnxiety.csv” is one of the files.
setwd("/Users/owner/Desktop/Lab 4")
In the code chunk below, use library() to load every package you need for this lab session. (0.5 pts)
library(ggplot2)
Save “ExamAnxiety.csv” into an object, then use head() to display the first 6 rows. (0.5 pts)
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
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
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.
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.
ExamAnxiety$Exam_z <- (ExamAnxiety$Exam - mean(ExamAnxiety$Exam)) / sd(ExamAnxiety$Exam)
ExamAnxiety$Exam_z[1]
## [1] -0.6384114
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.
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.
Normal_Anxiety <- rnorm(1000, mean(ExamAnxiety$Anxiety), sd(ExamAnxiety$Anxiety))
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.
hist(Normal_Anxiety)
Answer: ## This histogram can be described as
bell-shaped and fairly symmetric. This indicates a normal distribution
data set.
pnorm(ExamAnxiety$Anxiety_z[1])
## [1] 0.7596599
Answer: ## About 76% of a standard normal distribution falls below Participant 1’s Anxiety z-score.
1 - pnorm(ExamAnxiety$Anxiety_z[1])
## [1] 0.2403401
Answer: ## About 24% falls above that z-score.
z_90 <- qnorm(0.90)
z_90
## [1] 1.281552
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.
##plotting Anxiety & Exam on corresponding axes
ggplot(ExamAnxiety, aes(x = Anxiety, y = Exam)) + geom_point()
##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'
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.
Answer: ## creating box plots:
ggplot(ExamAnxiety, aes(x = Gender, y = Exam, fill = Gender)) +
geom_boxplot() +
labs(x = "Gender", y = "Exam Score") +
theme_classic()
##using bar graph: Answer:
ggplot(ExamAnxiety, aes(x = Gender)) +
geom_bar()
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).