# Load your librarieslibrary(car)library(pander)library(tidyverse)library(dplyr)library(mosaic)library(ggplot2)library(plotly)library(DT)# Load your data after saving a csv file in your Data folder.# You can use either # someName <- read.csv("../Data/YourDataFileName.csv", header=TRUE)# or# library(readr)# someName <- read_csv("../Data/YourDataFileName.csv")# Don't forget to run "Session -> Set Working Directory -> To Source file location"June_Nov_2024_Sleep_Data <-read_csv("../../data/June-Nov 2024 Sleep Data.csv")
I have been collecting data on my sleep for a good while. I use my apple watch and the app AutoSleep to track my sleep. I have been tracking on a higher frequency since June of this year so I collected data from June to November on my nightly sleep. I wanted to look at a category that my sleep tracker calls quality. Quality sleep includes the hours of light sleep, uninterrupted sleep, REM sleep, and deep sleep. I am interested in seeing if more hours of deep sleep correlates with more hours of quality sleep.
This analysis attempts to model my nightly quality sleep according to the reported quality sleep given by my sleep tracker using a simple linear regression.
The hypotheses for my study consider the slope of the regression model \beta_1. If the slope is zero then there is not a meaningful relationship between deep and quality sleep.
H_0: \beta_1 = 0 \\
H_a: \beta_1 \neq 0
\alpha = 0.05
Below is the scatter plot demonstrating the relationship of sleep quality and deep sleep. There is a fairly weak correlation of 0.2146, very few dots are on the line.
Show the code
plot(quality ~ deep, data=SleepD, pch=20, col="steelblue4", cex=1.2, las=1,xlab="Hours of Deep Sleep", ylab="Quality Sleep Hours",main="Does Deep Sleep Predict Sleep Quality?")Sleep.lm <-lm(quality ~ deep, data=SleepD)abline(Sleep.lm, lwd=2, col=rgb(.689, .133, .133, .3))grid(lty =1, col =rgb(0.5, 0.5, 0.5, 0.5), lwd =1.5)
This is a lovely plot but we are seeing most values above or below the mean regression line. We need to run the numbers and see the equation of this estimated regression.
There is a weak correlation as shown in the scatter plot and the linear regression summary results. With R^2 only being 0.2147 deep sleep hours do not appear to be a strong predictor of my hours of quality sleep.
We do know that the slope is not 0, finding significance evidence of that 1.955e-08 < \alpha. The slope is 0.6299 which shows that, on average, quality sleep hours are lower when deep sleep hours are lower. It shows that the actual hours of quality sleep is 62.99% of the deep sleep plus a baseline of 4.921 quality sleep hours. This shows that deep sleep did not predict quality sleep hours by about 37% on average.
Show the code
Sleep.lm <-lm(quality ~ deep, data=SleepD)par(mfrow=c(1,3))plot(Sleep.lm, which=1)qqPlot(Sleep.lm$residuals, main="Q-Q Plot", col="steelblue3", col.lines="steelblue4", pch=19, id=FALSE)plot(Sleep.lm$residuals, main ="Residuals vs. Order")
There are 5 associated assumptions to the linear regression. All but #4 can be checked with these above plots.
Assumption 1: Linear Relationship Between X and Y
Overall the appropriateness of the linear regression can be put into question. The linearity as shown in the residuals/fitted plot is attempting to show a quadratic relationship, however no signifigant linearity is shown there.
Assumption 2: Normal Distribution of Error Terms
We can see a skew in the normality of the data as shown in the Q-Q plot. This lack of normality may put the test into question but with majority of values falling within normal range we will not remove the validity of the test with this parameter.
Assumption 3: Constant Variance (X values)
As there does not seem to be a pattern in the data, showing constant variance.
Assumption 4: Fixed X Values
It can be assumed that this assumption has been met, as The AutoSleep app used to measure is expected to take accurate and consistent measurements of my sleep that does not change over time.
Assumption 5: Independent Error Terms
The Residuals vs Order plot shows the chaos in the data. Error terms do not appear to be correlated. There is not a clear pattern in the plot.
There is not sufficient evidence shown in the above plots to discredit the whole linear regression analysis.
: )
Credits
I wanted to give a lot of credit to the Body Weight Simple Linear Regression example I pulled a lot of the formatting from that analysis. I appreciated how clean it was and how clear the reporting was. I tried to replicate in my own style that formatting.