Sleep Patterns and Well-being: A Holistic Analysis

Naveed Ali Shah

26 January 2024

Introduction:

Sleep Patterns and Well-being” explores the complex relationships among age, gender, occupation, physical activity, stress levels, BMI, blood pressure, heart rate, daily steps, insomnia, and sleep apnea to unravel patterns influencing sleep quality. This project aims to offer data-driven insights and practical strategies for managing specific sleep disorders. Join us on a journey toward a deeper understanding of sleep and its impact on overall well-being

knitr::include_graphics("sleep.jpg")

Dataset Overview:

Sleep Health and Lifestyle Dataset

The Sleep Health and Lifestyle Dataset comprises 374 rows and 13 columns, covering a wide range of variables related to sleep and daily habits. It includes details such as gender, age, occupation, sleep duration, quality of sleep, physical activity level, stress levels, BMI category, blood pressure, heart rate, daily steps, and the presence or absence of sleep disorders.

Key Features of the Dataset:

1.Comprehensive Sleep Metrics: Explore sleep duration, quality, and factors influencing sleep patterns.
2.Lifestyle Factors: Analyze physical activity levels, stress levels, and BMI categories.
3.Cardiovascular Health: Examine blood pressure and heart rate measurements.
4.Sleep Disorder Analysis: Identify the occurrence of sleep disorders such as Insomnia and Sleep Apnea.

Dataset Columns:

1.Person ID: An identifier for each individual.
2.Gender: The gender of the person (Male/Female).
3.Age: The age of the person in years.
4.Occupation: The occupation or profession of the person.
5.Sleep Duration (hours): The number of hours the person sleeps per
6.Quality of Sleep (scale: 1-10): A subjective rating of the quality of sleep, ranging from 1 to 10.
7.Physical Activity Level (minutes/day): The number of minutes the person engages in physical activity daily.
8.Stress Level (scale: 1-10): A subjective rating of the stress level experienced by the person, ranging from 1 to 10.
9.BMI Category: The BMI category of the person (e.g., Underweight, Normal, Overweight).
10.Blood Pressure (systolic/diastolic): The blood pressure measurement of the person, indicated as systolic pressure over diastolic pressure.
11.Heart Rate (bpm): The resting heart rate of the person in beats per minute.
12.Daily Steps: The number of steps the person takes per day.
13.Sleep Disorder: The presence or absence of a sleep disorder in the person (None, Insomnia, Sleep Apnea).

Details about Sleep Disorder Column:

1.None: The individual does not exhibit any specific sleep disorder.
2.Insomnia: The individual experiences difficulty falling asleep or staying asleep, leading to inadequate or poor-quality sleep.
3.Sleep Apnea: The individual suffers from pauses in breathing during sleep, resulting in disrupted sleep patterns and potential health risks.

Loading Librarires

library(tidyverse)
library(ggplot2)
library(readr)
library(dplyr)
library(plotly)
library(DT)

Loading Dataset

sleep_lifestyle <- read_csv("D:/MS DS/Tools/data/Sleep_health_and_lifestyle_dataset.csv",show_col_types = FALSE)
sleep_lifestyle %>% datatable(options(list(pageLength = 12)))

Exploring DataSet

summary(sleep_lifestyle)
##    Person ID         Gender               Age         Occupation       
##  Min.   :  1.00   Length:374         Min.   :27.00   Length:374        
##  1st Qu.: 94.25   Class :character   1st Qu.:35.25   Class :character  
##  Median :187.50   Mode  :character   Median :43.00   Mode  :character  
##  Mean   :187.50                      Mean   :42.18                     
##  3rd Qu.:280.75                      3rd Qu.:50.00                     
##  Max.   :374.00                      Max.   :59.00                     
##  Sleep Duration  Quality of Sleep Physical Activity Level  Stress Level  
##  Min.   :5.800   Min.   :4.000    Min.   :30.00           Min.   :3.000  
##  1st Qu.:6.400   1st Qu.:6.000    1st Qu.:45.00           1st Qu.:4.000  
##  Median :7.200   Median :7.000    Median :60.00           Median :5.000  
##  Mean   :7.132   Mean   :7.313    Mean   :59.17           Mean   :5.385  
##  3rd Qu.:7.800   3rd Qu.:8.000    3rd Qu.:75.00           3rd Qu.:7.000  
##  Max.   :8.500   Max.   :9.000    Max.   :90.00           Max.   :8.000  
##  BMI Category       Blood Pressure       Heart Rate     Daily Steps   
##  Length:374         Length:374         Min.   :65.00   Min.   : 3000  
##  Class :character   Class :character   1st Qu.:68.00   1st Qu.: 5600  
##  Mode  :character   Mode  :character   Median :70.00   Median : 7000  
##                                        Mean   :70.17   Mean   : 6817  
##                                        3rd Qu.:72.00   3rd Qu.: 8000  
##                                        Max.   :86.00   Max.   :10000  
##  Sleep Disorder    
##  Length:374        
##  Class :character  
##  Mode  :character  
##                    
##                    
## 
glimpse(sleep_lifestyle)
## Rows: 374
## Columns: 13
## $ `Person ID`               <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1…
## $ Gender                    <chr> "Male", "Male", "Male", "Male", "Male", "Mal…
## $ Age                       <dbl> 27, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, …
## $ Occupation                <chr> "Software Engineer", "Doctor", "Doctor", "Sa…
## $ `Sleep Duration`          <dbl> 6.1, 6.2, 6.2, 5.9, 5.9, 5.9, 6.3, 7.8, 7.8,…
## $ `Quality of Sleep`        <dbl> 6, 6, 6, 4, 4, 4, 6, 7, 7, 7, 6, 7, 6, 6, 6,…
## $ `Physical Activity Level` <dbl> 42, 60, 60, 30, 30, 30, 40, 75, 75, 75, 30, …
## $ `Stress Level`            <dbl> 6, 8, 8, 8, 8, 8, 7, 6, 6, 6, 8, 6, 8, 8, 8,…
## $ `BMI Category`            <chr> "Overweight", "Normal", "Normal", "Obese", "…
## $ `Blood Pressure`          <chr> "126/83", "125/80", "125/80", "140/90", "140…
## $ `Heart Rate`              <dbl> 77, 75, 75, 85, 85, 85, 82, 70, 70, 70, 70, …
## $ `Daily Steps`             <dbl> 4200, 10000, 10000, 3000, 3000, 3000, 3500, …
## $ `Sleep Disorder`          <chr> "None", "None", "None", "Sleep Apnea", "Slee…
head(sleep_lifestyle)
## # A tibble: 6 × 13
##   `Person ID` Gender   Age Occupation        `Sleep Duration` `Quality of Sleep`
##         <dbl> <chr>  <dbl> <chr>                        <dbl>              <dbl>
## 1           1 Male      27 Software Engineer              6.1                  6
## 2           2 Male      28 Doctor                         6.2                  6
## 3           3 Male      28 Doctor                         6.2                  6
## 4           4 Male      28 Sales Representa…              5.9                  4
## 5           5 Male      28 Sales Representa…              5.9                  4
## 6           6 Male      28 Software Engineer              5.9                  4
## # ℹ 7 more variables: `Physical Activity Level` <dbl>, `Stress Level` <dbl>,
## #   `BMI Category` <chr>, `Blood Pressure` <chr>, `Heart Rate` <dbl>,
## #   `Daily Steps` <dbl>, `Sleep Disorder` <chr>

Checking for NA values

There is no missing Values, as it can be seen below.

sum(is.na(sleep_lifestyle))
## [1] 0
BMI_Category <- sleep_lifestyle %>% count(`BMI Category`)
BMI_Category
## # A tibble: 4 × 2
##   `BMI Category`     n
##   <chr>          <int>
## 1 Normal           195
## 2 Normal Weight     21
## 3 Obese             10
## 4 Overweight       148

Replacing the varibale “Normal” with “Normal Weight” and other variables

sleep_lifestyle <- sleep_lifestyle %>%
  mutate(`BMI Category` = if_else(`BMI Category` == "Normal", "Normal Weight", `BMI Category`))

BMI_Category <- sleep_lifestyle %>% count(`BMI Category`)
BMI_Category
## # A tibble: 3 × 2
##   `BMI Category`     n
##   <chr>          <int>
## 1 Normal Weight    216
## 2 Obese             10
## 3 Overweight       148

Renaming Column Names as it has spaces,capital letters replacing spaces with under score & lower letters

sleep_lifestyle <- sleep_lifestyle %>%
  rename(
    quality_of_sleep = "Quality of Sleep",
    stress_level = "Stress Level",
    bmi_category = "BMI Category",
    sleep_duration = "Sleep Duration",
    heart_rate = "Heart Rate",
    age = "Age",
    occupation= "Occupation",
    physical_activity_level= "Physical Activity Level",
    blood_pressure = "Blood Pressure",
    daily_steps = "Daily Steps",
    sleep_disorder= "Sleep Disorder",
    gender="Gender"
    
  )

Checking that Rename done or not

colnames(sleep_lifestyle)
##  [1] "Person ID"               "gender"                 
##  [3] "age"                     "occupation"             
##  [5] "sleep_duration"          "quality_of_sleep"       
##  [7] "physical_activity_level" "stress_level"           
##  [9] "bmi_category"            "blood_pressure"         
## [11] "heart_rate"              "daily_steps"            
## [13] "sleep_disorder"

Splitting the blood_pressure column into two separate columns

sleep_lifestyle$high_bp <- as.numeric(sapply(strsplit(as.character(sleep_lifestyle$blood_pressure), "/"), `[`, 1))
sleep_lifestyle$low_bp <- as.numeric(sapply(strsplit(as.character(sleep_lifestyle$blood_pressure), "/"), `[`, 2))

colnames(sleep_lifestyle)
##  [1] "Person ID"               "gender"                 
##  [3] "age"                     "occupation"             
##  [5] "sleep_duration"          "quality_of_sleep"       
##  [7] "physical_activity_level" "stress_level"           
##  [9] "bmi_category"            "blood_pressure"         
## [11] "heart_rate"              "daily_steps"            
## [13] "sleep_disorder"          "high_bp"                
## [15] "low_bp"

checking male/female distribution in data set

gender_dist <- ggplot(sleep_lifestyle, aes(x = factor(gender))) +
  geom_bar(fill = 'purple', stat = 'count') +
  geom_text(stat = 'count', aes(label = ..count..), vjust = -0.5) +
  labs(title = 'Gender Distribution',
       x = 'Gender',
       y = 'Count') +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = -40, hjust = 0))
ggplotly(gender_dist)

Analysis: In our dataset, we have 185 females and 189 males

Gender by Occupation in dataset

gender_by_occupation <- sleep_lifestyle %>%
  group_by(occupation, gender) %>%
  summarise(count = n(), .groups = 'drop')

gender_colors <- c('Male' = 'blue', 'Female' = 'red')

gender_occ <- ggplot(gender_by_occupation, aes(x = reorder(occupation, -count), y = count, fill = gender)) +
  geom_col(position = position_dodge(width = 0.8)) +  
  geom_text(aes(label = count), position = position_dodge(width = 0.8), vjust = -0.5) +
  scale_fill_manual(values = gender_colors) +
  labs(x = 'Occupation', y = 'Count', title = 'Gender by Occupation') +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = -45, hjust = 0)) + coord_cartesian(ylim = c(0, 80))
ggplotly(gender_occ)

Analysis: The dataset includes the highest count of nurses, with 73, followed by doctors, with a count of 69

BMI Category distribution in dataset

bmi_cat <- ggplot(sleep_lifestyle, aes(x = factor(bmi_category), fill = "red")) +
  geom_bar() +
  geom_text(stat = "count", aes(label = after_stat(count)), vjust = -0.5) + 
  labs(title = "BMI Category Counts", x = "BMI Category", y = "Count") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = -45, hjust = 0)) + coord_cartesian(ylim = c(0, 230))
ggplotly(bmi_cat)

Analysis: In the dataset, 216 individuals are classified as normal weight, 10 as obese, and 148 as overweight

BMI Category distribution in dataset

gender_bmi <- ggplot(sleep_lifestyle, aes(x = bmi_category, y = ..count.., fill = gender)) +
  geom_bar(stat = "count", position = "stack") +
  geom_text(aes(label = ..count..), stat = "count", position = position_stack(vjust = .4), color = "black", size = 6) +
  labs(x = "BMI", y = "Count", title = "Gender by BMI") +
  theme(axis.text.x = element_text(angle = -45, hjust = 0)) +
  scale_fill_manual(values = c("Male" = "blue", "Female" = "pink"))

ggplotly(gender_bmi)

Analysis: In the normal and obese categories, male contribution is high, while in the overweight category, female contribution is high

Sleep Disorder vs gender distribution

sleep_lifestyle$sleep_disorder <- as.factor(sleep_lifestyle$sleep_disorder)

disorder_gender <- ggplot(sleep_lifestyle, aes(x = gender, fill = sleep_disorder)) +
  geom_bar(position = "identity", alpha = 0.7) +
  geom_text(stat = "count", aes(label = ..count..), position = position_stack(vjust = 0.5)) + 
  facet_grid(. ~ sleep_disorder, scales = 'free_y') +
  labs(x = "Gender", y = "Count", title = "Histograms by Sleep Disorder") +
  theme_bw()
ggplotly(disorder_gender)

Analysis: Here, in the Insomnia category, the count of males is high, while in the Sleep Apnea category, the count of females is high.

Histograms by Sleep Disorder With Respect to age

sleep_lifestyle$sleep_disorder <- as.factor(sleep_lifestyle$sleep_disorder)
disorder_age <- ggplot(sleep_lifestyle, aes(x = age, fill = sleep_disorder)) +
  geom_bar(position = "identity", alpha = 0.7) +

  facet_grid(. ~ sleep_disorder, scales = 'free_y') +
  labs(x = "age", y = "Count", title = "Histograms by Sleep Disorder With Respect to age") +
  theme_bw()
ggplotly(disorder_age)

Analysis: The number of individuals experiencing insomnia is high in the age range of 43 to 45, whereas sleep apnea is more prevalent in individuals aged 49 and above

Histograms by Sleep Disorder With Respect to Occupation

sleep_lifestyle$sleep_disorder <- as.factor(sleep_lifestyle$sleep_disorder)
options(repr.plot.width = 100, repr.plot.height = 50)
disorder_occ <- ggplot(sleep_lifestyle, aes(x = occupation, fill = sleep_disorder)) +
  geom_bar(position = "identity", alpha = 0.7) +
  geom_text(
    stat = "count", 
    aes(label = stat(count)), 
    position = position_stack(vjust = 0.5),
    size = 3,
    color = "black"
  ) +
  facet_grid(. ~ sleep_disorder, scales = 'free_y') +
  labs(x = "Occupation", y = "Count", title = "Histograms by Sleep Disorder With Respect to Occupation") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggplotly(disorder_occ)

Analysis: Insomnia rates are high among salespersons and teachers, while sleep apnea is high among nurses

Histograms by Sleep Disorder With Respect to sleep_duration

sleep_lifestyle$sleep_disorder <- as.factor(sleep_lifestyle$sleep_disorder)
options(repr.plot.width = 30, repr.plot.height = 50)
disorder_sleep <- ggplot(sleep_lifestyle, aes(x = sleep_duration, fill = sleep_disorder)) +
  geom_bar(position = "identity", alpha = 0.7) +
  geom_text(
    stat = "count", 
    aes(label = stat(count)), 
    position = position_stack(vjust = 0.5),
    size = 3,
    color = "black"
  ) +
  facet_grid(. ~ sleep_disorder, scales = 'free_y') +
  labs(x = "sleep_duration", y = "Count", title = "Histograms by Sleep Disorder With Respect to sleep_duration") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggplotly(disorder_sleep)

Analysis: Insomnia is higher in individuals with a sleep duration of 6.3 - 6.6 hours, while the ratio of sleep apnea is elevated in those with a sleep duration of 6.0 - 6.1 and 8.0 - 8.2 hours

Histograms by Sleep Disorder With Respect to quality_of_sleep

library(ggplot2)
sleep_lifestyle$sleep_disorder <- as.factor(sleep_lifestyle$sleep_disorder)
options(repr.plot.width = 30, repr.plot.height = 50)
disorder_sleep_quality <- ggplot(sleep_lifestyle, aes(x = quality_of_sleep, fill = sleep_disorder)) +
  geom_bar(position = "identity", alpha = 0.7) +
  geom_text(
    stat = "count", 
    aes(label = stat(count)), 
    position = position_stack(vjust = 0.5),
    size = 3,
    color = "black"
  ) +
  facet_grid(. ~ sleep_disorder, scales = 'free_y') +
  labs(x = "quality_of_sleep", y = "Count", title = "Histograms by Sleep Disorder With Respect to quality_of_sleep") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggplotly(disorder_sleep_quality)

Analysis: The insomnia rate is higher with sleep quality 6 and 7, whereas sleep apnea is associated with sleep quality 6 and 9.

Histograms by Sleep Disorder With Respect to physical_activity_level

sleep_lifestyle$sleep_disorder <- as.factor(sleep_lifestyle$sleep_disorder)
options(repr.plot.width = 30, repr.plot.height = 50)
disorder_phy_act <- ggplot(sleep_lifestyle, aes(x = physical_activity_level, fill = sleep_disorder)) +
  geom_bar(position = "identity", alpha = 0.7) +
  geom_text(
    stat = "count", 
    aes(label = stat(count)), 
    position = position_stack(vjust = 0.5),
    size = 3,
    color = "black"
  ) +
  facet_grid(. ~ sleep_disorder, scales = 'free_y') +
  labs(x = "physical_activity_level", y = "Count", title = "Histograms by Sleep Disorder With Respect to physical_activity_level") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggplotly(disorder_phy_act)

Analysis: The insomnia ratio is high for a physical activity level of 45, while the sleep apnea ratio is high for physical activity levels of 75 and 90

Histograms by Sleep Disorder With Respect to stress_level

sleep_lifestyle$sleep_disorder <- as.factor(sleep_lifestyle$sleep_disorder)
options(repr.plot.width = 30, repr.plot.height = 50)
disorder_stress <- ggplot(sleep_lifestyle, aes(x = stress_level, fill = sleep_disorder)) +
  geom_bar(position = "identity", alpha = 0.7) +
  geom_text(
    stat = "count", 
    aes(label = stat(count)), 
    position = position_stack(vjust = 0.5),
    size = 3,
    color = "black"
  ) +
  facet_grid(. ~ sleep_disorder, scales = 'free_y') +
  labs(x = "stress_level", y = "Count", title = "Histograms by Sleep Disorder With Respect to stress_level") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggplotly(disorder_stress)

Analysis: Insomnia is high for Stress Level 4 and 7, while Sleep Apnea is high for Stress Level 3 and 8

Histograms by Sleep Disorder With Respect to BMI_category

sleep_lifestyle$sleep_disorder <- as.factor(sleep_lifestyle$sleep_disorder)
options(repr.plot.width = 30, repr.plot.height = 50)
disorder_bmi_category <- ggplot(sleep_lifestyle, aes(x = bmi_category, fill = sleep_disorder)) +
  geom_bar(position = "identity", alpha = 0.7) +
  geom_text(
    stat = "count", 
    aes(label = stat(count)), 
    position = position_stack(vjust = 0.5),
    size = 3,
    color = "black"
  ) +
  facet_grid(. ~ sleep_disorder, scales = 'free_y') +
  labs(x = "BMI_category", y = "Count", title = "Histograms by Sleep Disorder With Respect to BMI_category") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggplotly(disorder_bmi_category)

Analysis: Insomnia and sleep apnea are more prevalent in the overweight category.

Histograms by Sleep Disorder With Respect to heart_rate

sleep_lifestyle$sleep_disorder <- as.factor(sleep_lifestyle$sleep_disorder)
options(repr.plot.width = 30, repr.plot.height = 50)
disorder_heart <- ggplot(sleep_lifestyle, aes(x = heart_rate, fill = sleep_disorder)) +
  geom_bar(position = "identity", alpha = 0.7) +
  geom_text(
    stat = "count", 
    aes(label = stat(count)), 
    position = position_stack(vjust = 0.5),
    size = 3,
    color = "black"
  ) +
  facet_grid(. ~ sleep_disorder, scales = 'free_y') +
  labs(x = "heart_rate", y = "Count", title = "Histograms by Sleep Disorder With Respect to heart_rate") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggplotly(disorder_heart)

Analysis: Insomnia is high for heart rates 65 and 72, while Sleep Apnea is high for heart rates 68 and 75

Histograms by Sleep Disorder With Respect to daily_steps

sleep_lifestyle$sleep_disorder <- as.factor(sleep_lifestyle$sleep_disorder)
options(repr.plot.width = 30, repr.plot.height = 50)
disorder_daily_steps <- ggplot(sleep_lifestyle, aes(x = daily_steps, fill = sleep_disorder)) +
  geom_bar(position = "identity", alpha = 0.7) +
  facet_grid(. ~ sleep_disorder, scales = 'free_y') +
  labs(x = "daily_steps", y = "Count", title = "Histograms by Sleep Disorder With Respect to daily_steps") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggplotly(disorder_daily_steps)

Analysis: Insomnia is more likely for individuals with a daily step count of 6000, while Sleep Apnea is more prevalent among those with daily step counts of 7000 and 10000.

Histograms by Sleep Disorder With Respect to high_bp

sleep_lifestyle$sleep_disorder <- as.factor(sleep_lifestyle$sleep_disorder)
options(repr.plot.width = 30, repr.plot.height = 50)
disorder_high_bp <- ggplot(sleep_lifestyle, aes(x = high_bp, fill = sleep_disorder)) +
  geom_bar(position = "identity", alpha = 0.7) +
  geom_text(
    stat = "count", 
    aes(label = stat(count)), 
    position = position_stack(vjust = 0.5),
    size = 3,
    color = "black"
  ) +
  facet_grid(. ~ sleep_disorder, scales = 'free_y') +
  labs(x = "high_bp", y = "Count", title = "Histograms by Sleep Disorder With Respect to high_bp") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggplotly(disorder_high_bp)

Analysis: Insomnia is higher for high blood pressure levels of 130 and 135, while Sleep Apnea is more prevalent at a high blood pressure level of 140.

Histograms by Sleep Disorder With Respect to low_bp

sleep_lifestyle$sleep_disorder <- as.factor(sleep_lifestyle$sleep_disorder)
options(repr.plot.width = 30, repr.plot.height = 50)
disorder_low_bp <- ggplot(sleep_lifestyle, aes(x = low_bp, fill = sleep_disorder)) +
  geom_bar(position = "identity", alpha = 0.7) +
  geom_text(
    stat = "count", 
    aes(label = stat(count)), 
    position = position_stack(vjust = 0.5),
    size = 3,
    color = "black"
  ) +
  facet_grid(. ~ sleep_disorder, scales = 'free_y') +
  labs(x = "low_bp", y = "Count", title = "Histograms by Sleep Disorder With Respect to low_bp") +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggplotly(disorder_low_bp)

Analysis: Insomnia is higher for low blood pressure levels of 85 and 90, while Sleep Apnea is more prevalent for a low blood pressure level of 95

sleep_lifestyle_updated <- sleep_lifestyle
str(sleep_lifestyle_updated)
## tibble [374 × 15] (S3: tbl_df/tbl/data.frame)
##  $ Person ID              : num [1:374] 1 2 3 4 5 6 7 8 9 10 ...
##  $ gender                 : chr [1:374] "Male" "Male" "Male" "Male" ...
##  $ age                    : num [1:374] 27 28 28 28 28 28 29 29 29 29 ...
##  $ occupation             : chr [1:374] "Software Engineer" "Doctor" "Doctor" "Sales Representative" ...
##  $ sleep_duration         : num [1:374] 6.1 6.2 6.2 5.9 5.9 5.9 6.3 7.8 7.8 7.8 ...
##  $ quality_of_sleep       : num [1:374] 6 6 6 4 4 4 6 7 7 7 ...
##  $ physical_activity_level: num [1:374] 42 60 60 30 30 30 40 75 75 75 ...
##  $ stress_level           : num [1:374] 6 8 8 8 8 8 7 6 6 6 ...
##  $ bmi_category           : chr [1:374] "Overweight" "Normal Weight" "Normal Weight" "Obese" ...
##  $ blood_pressure         : chr [1:374] "126/83" "125/80" "125/80" "140/90" ...
##  $ heart_rate             : num [1:374] 77 75 75 85 85 85 82 70 70 70 ...
##  $ daily_steps            : num [1:374] 4200 10000 10000 3000 3000 3000 3500 8000 8000 8000 ...
##  $ sleep_disorder         : Factor w/ 3 levels "Insomnia","None",..: 2 2 2 3 3 1 1 2 2 2 ...
##  $ high_bp                : num [1:374] 126 125 125 140 140 140 140 120 120 120 ...
##  $ low_bp                 : num [1:374] 83 80 80 90 90 90 90 80 80 80 ...

Factoring columns gender, occupation, bmi_category

sleep_lifestyle_updated$gender <- as.factor(sleep_lifestyle_updated$gender)
sleep_lifestyle_updated$occupation <- as.factor(sleep_lifestyle_updated$occupation)
sleep_lifestyle_updated$bmi_category <- as.factor(sleep_lifestyle_updated$bmi_category)

Droping Blood Pressure Column and checking

sleep_lifestyle_updated <- sleep_lifestyle_updated %>% select(-blood_pressure)
sleep_lifestyle_updated <- sleep_lifestyle_updated %>% select(-(`Person ID`))

colnames(sleep_lifestyle_updated)
##  [1] "gender"                  "age"                    
##  [3] "occupation"              "sleep_duration"         
##  [5] "quality_of_sleep"        "physical_activity_level"
##  [7] "stress_level"            "bmi_category"           
##  [9] "heart_rate"              "daily_steps"            
## [11] "sleep_disorder"          "high_bp"                
## [13] "low_bp"
str(sleep_lifestyle_updated)
## tibble [374 × 13] (S3: tbl_df/tbl/data.frame)
##  $ gender                 : Factor w/ 2 levels "Female","Male": 2 2 2 2 2 2 2 2 2 2 ...
##  $ age                    : num [1:374] 27 28 28 28 28 28 29 29 29 29 ...
##  $ occupation             : Factor w/ 11 levels "Accountant","Doctor",..: 10 2 2 7 7 10 11 2 2 2 ...
##  $ sleep_duration         : num [1:374] 6.1 6.2 6.2 5.9 5.9 5.9 6.3 7.8 7.8 7.8 ...
##  $ quality_of_sleep       : num [1:374] 6 6 6 4 4 4 6 7 7 7 ...
##  $ physical_activity_level: num [1:374] 42 60 60 30 30 30 40 75 75 75 ...
##  $ stress_level           : num [1:374] 6 8 8 8 8 8 7 6 6 6 ...
##  $ bmi_category           : Factor w/ 3 levels "Normal Weight",..: 3 1 1 2 2 2 2 1 1 1 ...
##  $ heart_rate             : num [1:374] 77 75 75 85 85 85 82 70 70 70 ...
##  $ daily_steps            : num [1:374] 4200 10000 10000 3000 3000 3000 3500 8000 8000 8000 ...
##  $ sleep_disorder         : Factor w/ 3 levels "Insomnia","None",..: 2 2 2 3 3 1 1 2 2 2 ...
##  $ high_bp                : num [1:374] 126 125 125 140 140 140 140 120 120 120 ...
##  $ low_bp                 : num [1:374] 83 80 80 90 90 90 90 80 80 80 ...
sleep_lifestyle_updated$sleep_disorder <-  as.integer(factor(sleep_lifestyle_updated$sleep_disorder))
View(sleep_lifestyle_updated)

Hypothesis Testing

Hypothesis 1: Physical Activity and Sleep Quality

Null Hypothesis (H0): There is no correlation between physical activity levels and sleep quality.

Alternative Hypothesis (H1): There is a significant correlation between physical activity levels and sleep quality.

cor_test_result <- cor.test(sleep_lifestyle_updated$physical_activity_level, sleep_lifestyle_updated$quality_of_sleep)
print(cor_test_result)
## 
##  Pearson's product-moment correlation
## 
## data:  sleep_lifestyle_updated$physical_activity_level and sleep_lifestyle_updated$quality_of_sleep
## t = 3.7917, df = 372, p-value = 0.0001745
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.09331529 0.28865656
## sample estimates:
##       cor 
## 0.1928965

Conclusion: So We Reject Null hypothesis in favor of Alternate hypothesis, there high significance level Physical Activity and Sleep Quality

Hypothesis 2: occupation and sleep_disorder

Null Hypothesis (H0): There is no correlation between occupation and sleep quality.

Alternative Hypothesis (H1): There is a significant correlation between occupation and sleep quality.

contingency_table <- table(sleep_lifestyle_updated$occupation, sleep_lifestyle_updated$sleep_disorder)
fisher_test_result <- fisher.test(contingency_table, simulate.p.value = TRUE)
print(fisher_test_result)
## 
##  Fisher's Exact Test for Count Data with simulated p-value (based on
##  2000 replicates)
## 
## data:  contingency_table
## p-value = 0.0004998
## alternative hypothesis: two.sided

Conclusion: Based on the small p-value, you can conclude that there is a statistically significant association between ‘occupation’ and ’sleep_disorder.So we reject null hypothesis

Hypothesis 3: heart_rate and sleep_disorder

Null Hypothesis (H0): There is no correlation between heart_rate and sleep quality.

Alternative Hypothesis (H1): There is a significant correlation between heart_rate and sleep quality.

cor_test_result_heart <- cor.test(sleep_lifestyle_updated$heart_rate, sleep_lifestyle_updated$quality_of_sleep)
print(cor_test_result_heart)
## 
##  Pearson's product-moment correlation
## 
## data:  sleep_lifestyle_updated$heart_rate and sleep_lifestyle_updated$quality_of_sleep
## t = -16.938, df = 372, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.7135260 -0.5985071
## sample estimates:
##        cor 
## -0.6598647

Conclusion: So We Reject Null hypothesis based on small p-value in favor of Alternate hypothesis, there high significance heart_rate and Sleep Quality

Hypothesis 4: daily_steps and sleep_disorder

Null Hypothesis (H0): There is no correlation between daily_steps and sleep quality.

Alternative Hypothesis (H1): There is a significant correlation between daily_steps and sleep quality.

cor_test_result_daily_steps <- cor.test(sleep_lifestyle_updated$daily_steps, sleep_lifestyle_updated$quality_of_sleep)
print(cor_test_result_daily_steps)
## 
##  Pearson's product-moment correlation
## 
## data:  sleep_lifestyle_updated$daily_steps and sleep_lifestyle_updated$quality_of_sleep
## t = 0.32391, df = 372, p-value = 0.7462
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.08475941  0.11799700
## sample estimates:
##        cor 
## 0.01679141

Conclusion: The p-value of 0.7462 is greater than the commonly used significance level of 0.05. Therefore, we do not have sufficient evidence to reject the null hypothesis that the true correlation between daily_steps and quality_of_sleep is equal to 0.

Hypothesis 5: high_bp and sleep_disorder

Null Hypothesis (H0): There is no correlation between high_bp and sleep quality.

Alternative Hypothesis (H1): There is a significant correlation between high_bp and sleep quality.

cor_test_result_high_bp <- cor.test(sleep_lifestyle_updated$high_bp, sleep_lifestyle_updated$quality_of_sleep)
print(cor_test_result_high_bp)
## 
##  Pearson's product-moment correlation
## 
## data:  sleep_lifestyle_updated$high_bp and sleep_lifestyle_updated$quality_of_sleep
## t = -2.3635, df = 372, p-value = 0.01862
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.22032100 -0.02047808
## sample estimates:
##       cor 
## -0.121632

Conclusion: So We Reject Null hypothesis based on small p-value with respect to alpha=5% in favor of Alternate hypothesis, there high significance high_bp and Sleep Quality

Hypothesis 6: low_bp and sleep_disorder

Null Hypothesis (H0): There is no correlation between low_bp and sleep quality.

Alternative Hypothesis (H1): There is a significant correlation between low_bp and sleep quality.

cor_test_result_low_bp <- cor.test(sleep_lifestyle_updated$low_bp, sleep_lifestyle_updated$quality_of_sleep)
print(cor_test_result_low_bp)
## 
##  Pearson's product-moment correlation
## 
## data:  sleep_lifestyle_updated$low_bp and sleep_lifestyle_updated$quality_of_sleep
## t = -2.1375, df = 372, p-value = 0.03321
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.209220433 -0.008843204
## sample estimates:
##        cor 
## -0.1101509

Conclusion: So We Reject Null hypothesis based on small p-value with respect to alpha=5% in favor of Alternate hypothesis, there high significance low_bp and Sleep Quality

colnames(sleep_lifestyle_updated)
##  [1] "gender"                  "age"                    
##  [3] "occupation"              "sleep_duration"         
##  [5] "quality_of_sleep"        "physical_activity_level"
##  [7] "stress_level"            "bmi_category"           
##  [9] "heart_rate"              "daily_steps"            
## [11] "sleep_disorder"          "high_bp"                
## [13] "low_bp"

Linear Regression

sleep_lifestyle_updated.lm=lm(sleep_disorder ~ .,data = sleep_lifestyle_updated)
sleep_lifestyle_updated.lm
## 
## Call:
## lm(formula = sleep_disorder ~ ., data = sleep_lifestyle_updated)
## 
## Coefficients:
##                    (Intercept)                      genderMale  
##                     -1.624e+00                      -2.753e-03  
##                            age                occupationDoctor  
##                     -5.527e-04                       4.905e-01  
##             occupationEngineer                occupationLawyer  
##                      4.363e-01                       4.908e-01  
##              occupationManager                 occupationNurse  
##                      3.315e-01                       1.400e+00  
## occupationSales Representative           occupationSalesperson  
##                      2.178e+00                      -1.212e-01  
##            occupationScientist     occupationSoftware Engineer  
##                      1.267e+00                       2.254e-01  
##              occupationTeacher                  sleep_duration  
##                      3.618e-01                      -6.704e-02  
##               quality_of_sleep         physical_activity_level  
##                      3.814e-01                      -1.497e-03  
##                   stress_level               bmi_categoryObese  
##                      9.544e-02                       6.223e-02  
##         bmi_categoryOverweight                      heart_rate  
##                      1.186e-01                       5.246e-02  
##                    daily_steps                         high_bp  
##                      5.029e-05                      -3.216e-02  
##                         low_bp  
##                      4.944e-03
summary(sleep_lifestyle_updated.lm)
## 
## Call:
## lm(formula = sleep_disorder ~ ., data = sleep_lifestyle_updated)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.77974 -0.11880  0.00404  0.18599  1.88120 
## 
## Coefficients:
##                                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                    -1.624e+00  1.902e+00  -0.854  0.39362    
## genderMale                     -2.753e-03  1.377e-01  -0.020  0.98406    
## age                            -5.527e-04  1.064e-02  -0.052  0.95859    
## occupationDoctor                4.905e-01  1.528e-01   3.211  0.00145 ** 
## occupationEngineer              4.363e-01  1.524e-01   2.863  0.00444 ** 
## occupationLawyer                4.908e-01  1.675e-01   2.930  0.00361 ** 
## occupationManager               3.315e-01  4.186e-01   0.792  0.42902    
## occupationNurse                 1.400e+00  1.601e-01   8.742  < 2e-16 ***
## occupationSales Representative  2.178e+00  3.838e-01   5.676 2.88e-08 ***
## occupationSalesperson          -1.212e-01  1.839e-01  -0.659  0.51031    
## occupationScientist             1.267e+00  2.663e-01   4.759 2.85e-06 ***
## occupationSoftware Engineer     2.254e-01  2.437e-01   0.925  0.35555    
## occupationTeacher               3.618e-01  1.414e-01   2.559  0.01091 *  
## sleep_duration                 -6.704e-02  8.675e-02  -0.773  0.44022    
## quality_of_sleep                3.814e-01  9.162e-02   4.162 3.97e-05 ***
## physical_activity_level        -1.497e-03  2.647e-03  -0.566  0.57202    
## stress_level                    9.544e-02  5.710e-02   1.672  0.09550 .  
## bmi_categoryObese               6.223e-02  3.170e-01   0.196  0.84450    
## bmi_categoryOverweight          1.186e-01  1.617e-01   0.733  0.46377    
## heart_rate                      5.246e-02  1.632e-02   3.215  0.00143 ** 
## daily_steps                     5.029e-05  3.695e-05   1.361  0.17446    
## high_bp                        -3.216e-02  2.785e-02  -1.155  0.24896    
## low_bp                          4.944e-03  3.685e-02   0.134  0.89334    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3975 on 351 degrees of freedom
## Multiple R-squared:  0.6422, Adjusted R-squared:  0.6198 
## F-statistic: 28.64 on 22 and 351 DF,  p-value: < 2.2e-16
options(scipen=4) 
summary(sleep_lifestyle_updated.lm)
## 
## Call:
## lm(formula = sleep_disorder ~ ., data = sleep_lifestyle_updated)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.77974 -0.11880  0.00404  0.18599  1.88120 
## 
## Coefficients:
##                                   Estimate  Std. Error t value     Pr(>|t|)    
## (Intercept)                    -1.62427782  1.90169073  -0.854      0.39362    
## genderMale                     -0.00275286  0.13766131  -0.020      0.98406    
## age                            -0.00055275  0.01063786  -0.052      0.95859    
## occupationDoctor                0.49046693  0.15276318   3.211      0.00145 ** 
## occupationEngineer              0.43628939  0.15236572   2.863      0.00444 ** 
## occupationLawyer                0.49079160  0.16747752   2.930      0.00361 ** 
## occupationManager               0.33147556  0.41864309   0.792      0.42902    
## occupationNurse                 1.39960559  0.16010154   8.742      < 2e-16 ***
## occupationSales Representative  2.17838306  0.38375837   5.676 0.0000000288 ***
## occupationSalesperson          -0.12116677  0.18385582  -0.659      0.51031    
## occupationScientist             1.26701432  0.26625598   4.759 0.0000028529 ***
## occupationSoftware Engineer     0.22544609  0.24369827   0.925      0.35555    
## occupationTeacher               0.36181473  0.14137871   2.559      0.01091 *  
## sleep_duration                 -0.06703551  0.08675437  -0.773      0.44022    
## quality_of_sleep                0.38137363  0.09162297   4.162 0.0000396566 ***
## physical_activity_level        -0.00149714  0.00264697  -0.566      0.57202    
## stress_level                    0.09544017  0.05709616   1.672      0.09550 .  
## bmi_categoryObese               0.06222698  0.31702472   0.196      0.84450    
## bmi_categoryOverweight          0.11856766  0.16165513   0.733      0.46377    
## heart_rate                      0.05246434  0.01632018   3.215      0.00143 ** 
## daily_steps                     0.00005029  0.00003695   1.361      0.17446    
## high_bp                        -0.03215688  0.02784646  -1.155      0.24896    
## low_bp                          0.00494380  0.03684581   0.134      0.89334    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3975 on 351 degrees of freedom
## Multiple R-squared:  0.6422, Adjusted R-squared:  0.6198 
## F-statistic: 28.64 on 22 and 351 DF,  p-value: < 2.2e-16

Conclusion:

The estimated coefficients from the linear regression model provide insights into the relationship between the independent variables and the dependent variable (sleep disorder). Notable findings include:

Occupation Significance: Occupations such as Doctor, Engineer, Lawyer, Nurse, Sales Representative, Scientist, and Teacher exhibit significant associations with sleep disorder.

Quality of Sleep Impact: Quality of sleep has a positive and statistically significant impact on sleep disorders, with an increase in quality leading to an increase in sleep disorder likelihood.

Heart Rate Influence: Higher heart rates are associated with an increased likelihood of sleep disorders.

Age, Gender, and Physical Activity Level: Age, gender, and physical activity level do not show statistically significant associations with sleep disorders.

attributes(sleep_lifestyle_updated.lm)
## $names
##  [1] "coefficients"  "residuals"     "effects"       "rank"         
##  [5] "fitted.values" "assign"        "qr"            "df.residual"  
##  [9] "contrasts"     "xlevels"       "call"          "terms"        
## [13] "model"        
## 
## $class
## [1] "lm"
sleep_lifestyle_updated.lm$coef
##                    (Intercept)                     genderMale 
##                 -1.62427781744                 -0.00275285763 
##                            age               occupationDoctor 
##                 -0.00055274786                  0.49046693281 
##             occupationEngineer               occupationLawyer 
##                  0.43628938996                  0.49079160011 
##              occupationManager                occupationNurse 
##                  0.33147556482                  1.39960559045 
## occupationSales Representative          occupationSalesperson 
##                  2.17838305668                 -0.12116677179 
##            occupationScientist    occupationSoftware Engineer 
##                  1.26701431973                  0.22544608761 
##              occupationTeacher                 sleep_duration 
##                  0.36181472721                 -0.06703550627 
##               quality_of_sleep        physical_activity_level 
##                  0.38137362767                 -0.00149714150 
##                   stress_level              bmi_categoryObese 
##                  0.09544016603                  0.06222697620 
##         bmi_categoryOverweight                     heart_rate 
##                  0.11856766464                  0.05246433972 
##                    daily_steps                        high_bp 
##                  0.00005028596                 -0.03215688020 
##                         low_bp 
##                  0.00494379933
round(summary(sleep_lifestyle_updated.lm)$coef, 3) %>% 
  datatable(options(list(pageLength = 12)))

Pair graph to check Co-Relation of all variables

dependent_var.names <- c("gender", "age", "occupation", "sleep_duration", "quality_of_sleep", "physical_activity_level","stress_level","bmi_category","heart_rate","daily_steps","sleep_disorder","high_bp","low_bp")
pair_graph <-pairs(sleep_lifestyle_updated[,dependent_var.names])

ggplotly(pair_graph)

Co relation summary

`

sleep_lifestyle_updated[, dependent_var.names] <- lapply(sleep_lifestyle_updated[, dependent_var.names], as.numeric)
cor_matrix <- round(cor(sleep_lifestyle_updated[, dependent_var.names]), 3)
print(cor_matrix)
##                         gender    age occupation sleep_duration
## gender                   1.000 -0.596     -0.219         -0.122
## age                     -0.596  1.000      0.231          0.345
## occupation              -0.219  0.231      1.000         -0.326
## sleep_duration          -0.122  0.345     -0.326          1.000
## quality_of_sleep        -0.291  0.474     -0.278          0.883
## physical_activity_level  0.001  0.179     -0.104          0.212
## stress_level             0.396 -0.422      0.021         -0.811
## bmi_category            -0.340  0.524      0.694         -0.378
## heart_rate               0.217 -0.226      0.044         -0.516
## daily_steps             -0.015  0.058     -0.106         -0.040
## sleep_disorder          -0.253  0.232     -0.170          0.178
## high_bp                 -0.211  0.606      0.519         -0.180
## low_bp                  -0.268  0.594      0.522         -0.167
##                         quality_of_sleep physical_activity_level stress_level
## gender                            -0.291                   0.001        0.396
## age                                0.474                   0.179       -0.422
## occupation                        -0.278                  -0.104        0.021
## sleep_duration                     0.883                   0.212       -0.811
## quality_of_sleep                   1.000                   0.193       -0.899
## physical_activity_level            0.193                   1.000       -0.034
## stress_level                      -0.899                  -0.034        1.000
## bmi_category                      -0.304                   0.076        0.164
## heart_rate                        -0.660                   0.137        0.670
## daily_steps                        0.017                   0.773        0.187
## sleep_disorder                     0.180                   0.433       -0.036
## high_bp                           -0.122                   0.265        0.103
## low_bp                            -0.110                   0.383        0.092
##                         bmi_category heart_rate daily_steps sleep_disorder
## gender                        -0.340      0.217      -0.015         -0.253
## age                            0.524     -0.226       0.058          0.232
## occupation                     0.694      0.044      -0.106         -0.170
## sleep_duration                -0.378     -0.516      -0.040          0.178
## quality_of_sleep              -0.304     -0.660       0.017          0.180
## physical_activity_level        0.076      0.137       0.773          0.433
## stress_level                   0.164      0.670       0.187         -0.036
## bmi_category                   1.000      0.249       0.016          0.014
## heart_rate                     0.249      1.000      -0.030          0.206
## daily_steps                    0.016     -0.030       1.000          0.342
## sleep_disorder                 0.014      0.206       0.342          1.000
## high_bp                        0.724      0.294       0.103          0.242
## low_bp                         0.759      0.271       0.242          0.310
##                         high_bp low_bp
## gender                   -0.211 -0.268
## age                       0.606  0.594
## occupation                0.519  0.522
## sleep_duration           -0.180 -0.167
## quality_of_sleep         -0.122 -0.110
## physical_activity_level   0.265  0.383
## stress_level              0.103  0.092
## bmi_category              0.724  0.759
## heart_rate                0.294  0.271
## daily_steps               0.103  0.242
## sleep_disorder            0.242  0.310
## high_bp                   1.000  0.973
## low_bp                    0.973  1.000

After Droping few variables like physical_activity_level, bmi_category, occupation

sleep_lifestyle.lm2 <- lm(sleep_disorder ~ heart_rate + high_bp + quality_of_sleep + daily_steps + stress_level + sleep_duration + age + gender  , data = sleep_lifestyle_updated)
sleep_lifestyle.lm2
## 
## Call:
## lm(formula = sleep_disorder ~ heart_rate + high_bp + quality_of_sleep + 
##     daily_steps + stress_level + sleep_duration + age + gender, 
##     data = sleep_lifestyle_updated)
## 
## Coefficients:
##      (Intercept)        heart_rate           high_bp  quality_of_sleep  
##     -11.19285444        0.06240293        0.03057779        0.45825581  
##      daily_steps      stress_level    sleep_duration               age  
##       0.00008271        0.24751730        0.28707904       -0.03371132  
##           gender  
##      -0.65573199
summary(sleep_lifestyle.lm2)
## 
## Call:
## lm(formula = sleep_disorder ~ heart_rate + high_bp + quality_of_sleep + 
##     daily_steps + stress_level + sleep_duration + age + gender, 
##     data = sleep_lifestyle_updated)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.73487 -0.23262 -0.00353  0.30191  1.83997 
## 
## Coefficients:
##                      Estimate   Std. Error t value Pr(>|t|)    
## (Intercept)      -11.19285444   0.98024454 -11.418  < 2e-16 ***
## heart_rate         0.06240293   0.00949008   6.576 1.68e-10 ***
## high_bp            0.03057779   0.00591789   5.167 3.92e-07 ***
## quality_of_sleep   0.45825581   0.07278953   6.296 8.79e-10 ***
## daily_steps        0.00008271   0.00001862   4.441 1.19e-05 ***
## stress_level       0.24751730   0.04559018   5.429 1.03e-07 ***
## sleep_duration     0.28707904   0.07614666   3.770  0.00019 ***
## age               -0.03371132   0.00662077  -5.092 5.70e-07 ***
## gender            -0.65573199   0.07931410  -8.268 2.57e-15 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.48 on 365 degrees of freedom
## Multiple R-squared:  0.4573, Adjusted R-squared:  0.4454 
## F-statistic: 38.45 on 8 and 365 DF,  p-value: < 2.2e-16

Orignal results without droping outliers without log transformation

par(mfrow = c(2,2))
ggplotly(plot(sleep_lifestyle.lm2))

Analysis= Here we can see that data is fitted on some lines but scattering on some points..

Orignal results without droping outliers with log transformation

sleep_lifestyle.lm3 <- lm(log(sleep_disorder) ~ I(log(heart_rate + high_bp + quality_of_sleep + daily_steps + stress_level + sleep_duration + age + gender))  , data = sleep_lifestyle_updated)
par(mfrow = c(2,2))
ggplotly(plot(sleep_lifestyle.lm3))

Analysis= After log transformation.. heteroscedasticity is increased in graphs than without log results

Droping some outliers, to see its impact

sleep_lifestyle.lm3_outer_drop <- sleep_lifestyle_updated[-c(279,5,95,104,220,146,19,92,4,271,90,269), ]
sleep_lifestyle.lm3_outer_drop <- sleep_lifestyle.lm3_outer_drop[-c(88, 265, 264, 263, 262, 261, 260, 259, 258), ]
sleep_lifestyle.lm3_outer_drop <- sleep_lifestyle.lm3_outer_drop[-c(12,14, 26, 18,256,88, 258), ]
sleep_lifestyle.lm3_outer_drop %>% 
  datatable(options(list(pageLength = 12)))

Results after droping outliers with log transformation

sleep_lifestyle_outer_drop.lm <- lm(log(sleep_disorder) ~ I(log(heart_rate + high_bp + quality_of_sleep + daily_steps + stress_level + sleep_duration + age + gender))  , data = sleep_lifestyle.lm3_outer_drop)
par(mfrow = c(2,2))
ggplotly(plot(sleep_lifestyle_outer_drop.lm))

Analysis= after dropping outlier points with log transformation, heteroscedasticity is decreased but no such gains observed in Residual vs leverage graph

Results after droping outliers with out log transformation

sleep_lifestyle.lm5 <- lm(sleep_disorder ~ heart_rate + high_bp + quality_of_sleep + daily_steps + stress_level + sleep_duration + age + gender, data = sleep_lifestyle.lm3_outer_drop)

summary(sleep_lifestyle.lm5 )
## 
## Call:
## lm(formula = sleep_disorder ~ heart_rate + high_bp + quality_of_sleep + 
##     daily_steps + stress_level + sleep_duration + age + gender, 
##     data = sleep_lifestyle.lm3_outer_drop)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.42763 -0.19510 -0.02486  0.25210  1.33835 
## 
## Coefficients:
##                      Estimate   Std. Error t value Pr(>|t|)    
## (Intercept)      -11.71441250   0.98196231 -11.930  < 2e-16 ***
## heart_rate         0.06426864   0.01032041   6.227 1.41e-09 ***
## high_bp            0.03011372   0.00558586   5.391 1.32e-07 ***
## quality_of_sleep   0.52989174   0.06934880   7.641 2.26e-13 ***
## daily_steps        0.00009123   0.00001803   5.059 6.94e-07 ***
## stress_level       0.27835749   0.04356968   6.389 5.56e-10 ***
## sleep_duration     0.27123162   0.07318110   3.706 0.000246 ***
## age               -0.03553010   0.00630691  -5.634 3.73e-08 ***
## gender            -0.73472417   0.07747695  -9.483  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4387 on 337 degrees of freedom
## Multiple R-squared:  0.5184, Adjusted R-squared:  0.507 
## F-statistic: 45.35 on 8 and 337 DF,  p-value: < 2.2e-16
sleep_lifestyle.lm4 <- lm(sleep_disorder ~ heart_rate + high_bp + quality_of_sleep + daily_steps + stress_level + sleep_duration + age + gender, data = sleep_lifestyle.lm3_outer_drop)
par(mfrow = c(2,2))
ggplotly(plot(sleep_lifestyle.lm4))

Analysis= after dropping outlier points without log transformation, results are improved almost in all graphs

Conclusion The linear regression model indicates that heart rate, high blood pressure, quality of sleep, daily steps, stress level, sleep duration, age, and gender are significant predictors of sleep disorder. The model has a moderate fit (R-squared = 0.5184), explaining 51.84% of the variability. Higher heart rate, high blood pressure, better sleep quality, more daily steps, higher stress levels, longer sleep duration, and younger age are associated with increased sleep disorder, while being female is associated with a decrease. The overall model is statistically significant (p < 2.2e-16).

Image Source: https://www.verywellhealth.com/insomnia-vs-sleep-apnea-5213997

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.