library(readxl)
library(ggpubr)
## Loading required package: ggplot2
PhoneSleep <- read_excel("C:/Users/tawan/OneDrive - Saint Louis University/AA 5221/Assignment 5/A5Q2.xlsx")
View(PhoneSleep)

#creating the scatter plot
ggscatter(
  PhoneSleep,
  x = "sleep",
  y = "phone",
  add = "reg.line",
  xlab = "sleep",
  ylab = "phone"
)

# The relationship is linear.
# The relationship is negative.
# There are no outliers.

#calculation of descriptive statistics
mean(PhoneSleep$sleep)
## [1] 7.559076
sd(PhoneSleep$sleep)
## [1] 1.208797
median(PhoneSleep$sleep)
## [1] 7.524099
mean(PhoneSleep$phone)
## [1] 3.804609
sd(PhoneSleep$phone)
## [1] 2.661866
median(PhoneSleep$phone)
## [1] 3.270839
#checking normality visually Histogram
hist(PhoneSleep$sleep,
     main = "sleep",
     breaks = 20,
     col = "lightblue",
     border = "white",
     xlab = "Hours of Sleep",
     cex.main = 1,
     cex.axis = 1,
     cex.lab = 1)

# Variable 1: sleep hours
# The variable looks abnormally distributed.
# The data is positively skewed.
# The data does not have a proper bell curve.

hist(PhoneSleep$phone,
     main = "phone",
     breaks = 20,
     col = "lightcoral",
     border = "white",
     xlab = "Hours of Phone Usage",
     cex.main = 1,
     cex.axis = 1,
     cex.lab = 1)

# Variable 1: phone usage hours
# The variable looks abnormally distributed.
# The data is negatively skewed.
# The data does not have a proper bell curve.

shapiro.test(PhoneSleep$sleep)
## 
##  Shapiro-Wilk normality test
## 
## data:  PhoneSleep$sleep
## W = 0.91407, p-value = 8.964e-08
shapiro.test(PhoneSleep$phone)
## 
##  Shapiro-Wilk normality test
## 
## data:  PhoneSleep$phone
## W = 0.89755, p-value = 9.641e-09
# Variable 1: sleep
# The variable is abnormally distributed p < 0.05.

# Variable 2: phone
# The variable is abnormally distributed p < 0.05.

#conducting a spearman correlation
cor.test(
  PhoneSleep$sleep,
  PhoneSleep$phone,
  method = "spearman"
)
## 
##  Spearman's rank correlation rho
## 
## data:  PhoneSleep$sleep and PhoneSleep$phone
## S = 908390, p-value < 2.2e-16
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
##        rho 
## -0.6149873
# A Pearson correlation was conducted to test the relationship between sleep (M = 7.55, SD = 1.21) and phone usage hours (M = 3.80.11, SD = 2.66).

# There was a statistically significant relationship between the two variables, r(98) = .61, p < .001.

# The relationship was negative and strong.

# As hours of phone usage increased, sleep hours decreased.