install.packages(c("readr","dplyr","car","heplots","psych","ggplot2"))
## Installing packages into 'C:/Users/HP/AppData/Local/R/win-library/4.5'
## (as 'lib' is unspecified)
## 
##   There is a binary version available but the source version is later:
##       binary source needs_compilation
## dplyr  1.2.0  1.2.1              TRUE
## 
##   Binaries will be installed
## package 'readr' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'readr'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\HP\AppData\Local\R\win-library\4.5\00LOCK\readr\libs\x64\readr.dll to
## C:\Users\HP\AppData\Local\R\win-library\4.5\readr\libs\x64\readr.dll:
## Permission denied
## Warning: restored 'readr'
## package 'dplyr' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'dplyr'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\HP\AppData\Local\R\win-library\4.5\00LOCK\dplyr\libs\x64\dplyr.dll to
## C:\Users\HP\AppData\Local\R\win-library\4.5\dplyr\libs\x64\dplyr.dll:
## Permission denied
## Warning: restored 'dplyr'
## package 'car' successfully unpacked and MD5 sums checked
## package 'heplots' successfully unpacked and MD5 sums checked
## package 'psych' successfully unpacked and MD5 sums checked
## package 'ggplot2' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\HP\AppData\Local\Temp\RtmpKcQWGq\downloaded_packages
library(readr)
## Warning: package 'readr' was built under R version 4.5.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.5.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(car)
## Warning: package 'car' was built under R version 4.5.3
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
library(heplots)
## Warning: package 'heplots' was built under R version 4.5.3
## Loading required package: broom
library(psych)
## Warning: package 'psych' was built under R version 4.5.3
## 
## Attaching package: 'psych'
## The following object is masked from 'package:car':
## 
##     logit
df <- read_csv("Sleep_Health_and_Lifestyle_Dataset.csv")
## Rows: 374 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): Gender, Occupation, BMI Category, Blood Pressure, Sleep Disorder
## dbl (8): Person ID, Age, Sleep Duration, Quality of Sleep, Physical Activity...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(df)
## # 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>
str(df)
## spc_tbl_ [374 × 13] (S3: spec_tbl_df/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" "Normal" "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         : chr [1:374] "None" "None" "None" "Sleep Apnea" ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   `Person ID` = col_double(),
##   ..   Gender = col_character(),
##   ..   Age = col_double(),
##   ..   Occupation = col_character(),
##   ..   `Sleep Duration` = col_double(),
##   ..   `Quality of Sleep` = col_double(),
##   ..   `Physical Activity Level` = col_double(),
##   ..   `Stress Level` = col_double(),
##   ..   `BMI Category` = col_character(),
##   ..   `Blood Pressure` = col_character(),
##   ..   `Heart Rate` = col_double(),
##   ..   `Daily Steps` = col_double(),
##   ..   `Sleep Disorder` = col_character()
##   .. )
##  - attr(*, "problems")=<externalptr>
summary(df)
##    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  
##                    
##                    
## 
colnames(df)
##  [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"
df$Gender <- as.factor(df$Gender)
df$`BMI Category` <- as.factor(df$`BMI Category`)
df$`Sleep Disorder` <- as.factor(df$`Sleep Disorder`)
df$Occupation <- as.factor(df$Occupation)
summary(df)
##    Person ID         Gender         Age             Occupation Sleep Duration 
##  Min.   :  1.00   Female:185   Min.   :27.00   Nurse     :73   Min.   :5.800  
##  1st Qu.: 94.25   Male  :189   1st Qu.:35.25   Doctor    :71   1st Qu.:6.400  
##  Median :187.50                Median :43.00   Engineer  :63   Median :7.200  
##  Mean   :187.50                Mean   :42.18   Lawyer    :47   Mean   :7.132  
##  3rd Qu.:280.75                3rd Qu.:50.00   Teacher   :40   3rd Qu.:7.800  
##  Max.   :374.00                Max.   :59.00   Accountant:37   Max.   :8.500  
##                                                (Other)   :43                  
##  Quality of Sleep Physical Activity Level  Stress Level          BMI Category
##  Min.   :4.000    Min.   :30.00           Min.   :3.000   Normal       :195  
##  1st Qu.:6.000    1st Qu.:45.00           1st Qu.:4.000   Normal Weight: 21  
##  Median :7.000    Median :60.00           Median :5.000   Obese        : 10  
##  Mean   :7.313    Mean   :59.17           Mean   :5.385   Overweight   :148  
##  3rd Qu.:8.000    3rd Qu.:75.00           3rd Qu.:7.000                      
##  Max.   :9.000    Max.   :90.00           Max.   :8.000                      
##                                                                              
##  Blood Pressure       Heart Rate     Daily Steps        Sleep Disorder
##  Length:374         Min.   :65.00   Min.   : 3000   Insomnia   : 77   
##  Class :character   1st Qu.:68.00   1st Qu.: 5600   None       :219   
##  Mode  :character   Median :70.00   Median : 7000   Sleep Apnea: 78   
##                     Mean   :70.17   Mean   : 6817                     
##                     3rd Qu.:72.00   3rd Qu.: 8000                     
##                     Max.   :86.00   Max.   :10000                     
## 
cor(df[,c("Quality of Sleep","Stress Level")])
##                  Quality of Sleep Stress Level
## Quality of Sleep         1.000000    -0.898752
## Stress Level            -0.898752     1.000000
manova_model <- manova(
  cbind(`Quality of Sleep`, `Stress Level`) ~ 
    Gender + `BMI Category`,
  data = df
)

summary(manova_model, test="Pillai")
##                 Df  Pillai approx F num Df den Df    Pr(>F)    
## Gender           1 0.18918   42.931      2    368 < 2.2e-16 ***
## `BMI Category`   3 0.28471   20.416      6    738 < 2.2e-16 ***
## Residuals      369                                             
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
mancova_model <- manova(
  cbind(`Quality of Sleep`, `Stress Level`) ~ 
    Gender + `BMI Category` +
    Age +
    `Sleep Duration` +
    `Physical Activity Level` +
    `Heart Rate` +
    `Daily Steps`,
  data = df
)

summary(mancova_model, test="Pillai")
##                            Df  Pillai approx F num Df den Df    Pr(>F)    
## Gender                      1 0.61490   289.81      2    363 < 2.2e-16 ***
## `BMI Category`              3 0.78901    79.05      6    728 < 2.2e-16 ***
## Age                         1 0.78687   670.11      2    363 < 2.2e-16 ***
## `Sleep Duration`            1 0.77322   618.83      2    363 < 2.2e-16 ***
## `Physical Activity Level`   1 0.27275    68.07      2    363 < 2.2e-16 ***
## `Heart Rate`                1 0.51901   195.85      2    363 < 2.2e-16 ***
## `Daily Steps`               1 0.16374    35.54      2    363  8.04e-15 ***
## Residuals                 364                                             
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary.aov(manova_model)
##  Response Quality of Sleep :
##                 Df Sum Sq Mean Sq F value    Pr(>F)    
## Gender           1  45.37  45.367  43.751 1.312e-10 ***
## `BMI Category`   3 106.40  35.468  34.205 < 2.2e-16 ***
## Residuals      369 382.63   1.037                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
##  Response Stress Level :
##                 Df Sum Sq Mean Sq F value    Pr(>F)    
## Gender           1 184.21 184.206  79.009 < 2.2e-16 ***
## `BMI Category`   3 130.04  43.346  18.592 2.978e-11 ***
## Residuals      369 860.31   2.331                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1