Introduction Loading Libraries into memory

library(readxl)
library(ggpubr)
## Loading required package: ggplot2

Loading Dataset from public directory

Replace Full Path with required path when you load it

D1 <- D1 <- read_excel("A4Q2.xlsx")

Loading Dataset from public directory

Plot the graph

ggscatter(
  D1,
  x = "phone",
  y = "sleep",
  color = "#00A",
  shape = "triangle",
  add = "reg.line",
  add.params = list(color = "red"), # Change ONLY the line color here
  xlab = "Independent Variable (phone) /hours",
  ylab = "Dependent Variable (sleep) /hours"
)

The relationship is [linear].

The relationship is [linear]. The relationship is [negative ]. The relationship is [strong]. There [are] outliers.

Descriptive Stastics

phone_mean <- mean(D1$phone)
phone_sd <- sd(D1$phone)
phone_median <- median(D1$phone)

cat( "\n",
     "phone mean = ",phone_mean, "hours",
     "\n", 
     "phone SD = ", phone_sd , "hours",
     "\n", 
     "phone median = ", phone_median, "hours",
     "\n" )
## 
##  phone mean =  3.804609 hours 
##  phone SD =  2.661866 hours 
##  phone median =  3.270839 hours
sleep_mean <- mean(D1$sleep)
sleep_sd  <- sd(D1$sleep)
sleep_median <- median(D1$sleep)

cat("\n", 
    "sleep mean = ",sleep_mean, "hours",
    "\n", 
    "sleep SD = ", sleep_sd , "hours",
    "\n", 
    "sleep median = ", sleep_median, "hours",
    "\n" )
## 
##  sleep mean =  7.559076 hours 
##  sleep SD =  1.208797 hours 
##  sleep median =  7.524099 hours

Plotting normal distribution curves

phone histogram

hist(D1$phone,
     main = "phone",
     breaks = 30,
     col = "lightblue",
     border = "white",
     freq = TRUE,
     cex.main = 1,
     cex.axis = 1,
     cex.lab = 1)

#frequency plot
hist(D1$phone,
     main = "phone",
     breaks = 30,
     col = "lightblue",
     border = "white",
     freq = FALSE,
     cex.main = 1,
     cex.axis = 1,
     cex.lab = 1)
curve(dnorm(x, mean = phone_mean, sd = phone_sd), 
      add=TRUE, 
      col = "red",
      lwd = 2)
abline(v = phone_mean, col = "blue", lwd = 2, lty = 2)
abline(v = phone_median, col = "darkgreen", lwd = 2, lty = 3)

Sleep histogram

#frequency plot
hist(D1$sleep,
     main = "sleep",
     breaks = 20,
     freq = TRUE,
     col = "lightcoral",
     border = "white",
     cex.main = 1,
     cex.axis = 1,
     cex.lab = 1)

#density plot
hist(D1$sleep,
     main = "sleep",
     breaks = 20,
     freq = FALSE,
     col = "lightcoral",
     border = "white",
     cex.main = 1,
     cex.axis = 1,
     cex.lab = 1)
curve(dnorm(x, mean = sleep_mean, sd = sleep_sd), 
      add=TRUE, 
      col = "red",
      lwd = 2)
abline(v = sleep_mean, col = "blue", lwd = 2, lty = 2)
abline(v = sleep_median, col = "darkgreen", lwd = 2, lty = 3)

Variable 1: phone

The phone looks normally distributed. The data is skewed left. The data has a proper bell curve.

Variable 2: sleep

The sleep looks normally distributed. The data is skewed right. The data has a proper bell curve.

normality stastistics

st1 <- shapiro.test(D1$phone)
st1
## 
##  Shapiro-Wilk normality test
## 
## data:  D1$phone
## W = 0.89755, p-value = 9.641e-09
if (st1$p.value >= 0.05) { print ("phone data is normal") } else { print ("phone data is abnormal") }
## [1] "phone data is abnormal"
st2 <- shapiro.test(D1$sleep)
st2
## 
##  Shapiro-Wilk normality test
## 
## data:  D1$sleep
## W = 0.91407, p-value = 8.964e-08
if (st2$p.value >= 0.05) { print ("sleep data is normal") } else { print ("sleep data is abnormal") }
## [1] "sleep data is abnormal"
if (st2$p.value != st1$p.value) {cat ( "\n" , "Spearman test needed")} else {"pearsons test needed"}
## 
##  Spearman test needed

Normality test Filed

Data looks abnormally distributed

Spearmans correlation test

cor.test(D1$phone, D1$sleep, method = "spearman")
## 
##  Spearman's rank correlation rho
## 
## data:  D1$phone and D1$sleep
## S = 908390, p-value < 2.2e-16
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
##        rho 
## -0.6149873

Report results

A spearman correlation was conducted to test the relationship between phone (Mdn = 3.271 hours) and sleep (Mdn = 7.524 hours).

There was a statistically significant relationship between the two variables, ρ = -0.615, p < 0.001.

The relationship was negative and strong.

As phone use increased, sleep decreased.