library(readxl)
library(ggpubr)
## Loading required package: ggplot2
A5Q2 <- read_excel("C:/Users/nehab/OneDrive/A5221/Assignment 5/A5Q2.xlsx")

ggscatter(
  A5Q2,
  x = "phone",
  y = "sleep",
  add = "reg.line",
  xlab = "Daily Phone Use",
  ylab = "Nightly Sleep Duration"
)

# The relationship appears negative.
# As phone use increases, sleep duration tends to decrease.
# The pattern is generally linear, but there are some possible outliers.

mean(A5Q2$sleep)
## [1] 7.559076
sd(A5Q2$sleep)
## [1] 1.208797
median(A5Q2$sleep)
## [1] 7.524099
mean(A5Q2$phone)
## [1] 3.804609
sd(A5Q2$phone)
## [1] 2.661866
median(A5Q2$phone)
## [1] 3.270839
hist(
  A5Q2$sleep,
  main = "Distribution of Sleep Duration",
  xlab = "Nightly Sleep Duration"
)

hist(
  A5Q2$phone,
  main = "Distribution of Phone Use",
  xlab = "Daily Phone Use"
)

# Sleep has possible outliers and may not be normally distributed.
# Phone use appears right-skewed and is not normally distributed.

shapiro.test(A5Q2$sleep)
## 
##  Shapiro-Wilk normality test
## 
## data:  A5Q2$sleep
## W = 0.91407, p-value = 8.964e-08
shapiro.test(A5Q2$phone)
## 
##  Shapiro-Wilk normality test
## 
## data:  A5Q2$phone
## W = 0.89755, p-value = 9.641e-09
# Sleep is not normally distributed because p < .001.
# Phone use is not normally distributed because p < .001.
# Both p-values are less than .05.
# Therefore, a Spearman correlation will be used.

cor.test(
  A5Q2$phone,
  A5Q2$sleep,
  method = "spearman",
  exact = FALSE
)
## 
##  Spearman's rank correlation rho
## 
## data:  A5Q2$phone and A5Q2$sleep
## S = 908390, p-value < 2.2e-16
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
##        rho 
## -0.6149873
# A Spearman correlation was conducted to examine the relationship
# between daily phone use and nightly sleep duration.
# There was a statistically significant strong negative relationship,
# rho = -.61, p < .001.
# As daily phone use increased, nightly sleep duration tended to decrease.
# Therefore, the null hypothesis was rejected.