options(repos = c(CRAN = "https://cloud.r-project.org"))
install.packages("psych")
## Installing package into 'C:/Users/User/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## 
##   There is a binary version available but the source version is later:
##       binary source needs_compilation
## psych 2.3.12  2.4.1             FALSE
## installing the source package 'psych'
install.packages("car")
## Installing package into 'C:/Users/User/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'car' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\User\AppData\Local\Temp\RtmpWqNBmY\downloaded_packages

Source of data: https://www.kaggle.com/datasets/uom190346a/sleep-health-and-lifestyle-dataset/data

This dataset will be used in both research questions.

mydata <- read.csv("./Sleep&health_dataset.csv", header=TRUE, sep=",", dec=".")

head(mydata, 10)
##    Person.ID Gender Age           Occupation Sleep.Duration Quality.of.Sleep
## 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 Representative            5.9                4
## 5          5   Male  28 Sales Representative            5.9                4
## 6          6   Male  28    Software Engineer            5.9                4
## 7          7   Male  29              Teacher            6.3                6
## 8          8   Male  29               Doctor            7.8                7
## 9          9   Male  29               Doctor            7.8                7
## 10        10   Male  29               Doctor            7.8                7
##    Physical.Activity.Level Stress.Level BMI.Category Blood.Pressure Heart.Rate
## 1                       42            6   Overweight         126/83         77
## 2                       60            8       Normal         125/80         75
## 3                       60            8       Normal         125/80         75
## 4                       30            8        Obese         140/90         85
## 5                       30            8        Obese         140/90         85
## 6                       30            8        Obese         140/90         85
## 7                       40            7        Obese         140/90         82
## 8                       75            6       Normal         120/80         70
## 9                       75            6       Normal         120/80         70
## 10                      75            6       Normal         120/80         70
##    Daily.Steps Sleep.Disorder
## 1         4200           None
## 2        10000           None
## 3        10000           None
## 4         3000    Sleep Apnea
## 5         3000    Sleep Apnea
## 6         3000       Insomnia
## 7         3500       Insomnia
## 8         8000           None
## 9         8000           None
## 10        8000           None

Description:

Person ID: An identifier for each individual.

Gender: The gender of the person (Male/Female).

Age: The age of the person in years.

Occupation: The occupation or profession of the person.

Sleep Duration (hours): The number of hours the person sleeps per day.

Quality of Sleep (scale: 1-10): A subjective rating of the quality of sleep, ranging from 1 to 10.

Physical Activity Level (minutes/day): The number of minutes the person engages in physical activity daily.

Stress Level (scale: 1-10): A subjective rating of the stress level experienced by the person, ranging from 1 to 10.

BMI Category: The BMI category of the person (e.g., Underweight, Normal, Overweight).

Blood Pressure (systolic/diastolic): The blood pressure measurement of the person, indicated as systolic pressure over diastolic pressure.

Heart Rate (bpm): The resting heart rate of the person in beats per minute. Daily Steps: The number of steps the person takes per day.

Sleep Disorder: The presence or absence of a sleep disorder in the person (None, Insomnia, Sleep Apnea).

Part 1: association between two categorical variables

1st research question:

Is there a significant association between gender and sleep disorder among the participants?

#Selecting variables
selected_columns <- c("Gender", "Sleep.Disorder")

mydata1 <- mydata[selected_columns]

head(mydata1)
##   Gender Sleep.Disorder
## 1   Male           None
## 2   Male           None
## 3   Male           None
## 4   Male    Sleep Apnea
## 5   Male    Sleep Apnea
## 6   Male       Insomnia
#Factoring
mydata1$GenderFactor <- factor(mydata1$Gender,
                        levels = c("Male", "Female"),
                        labels = c("Male", "Female"))

mydata1$Sleep.DisorderFactor <- factor(mydata1$Sleep.Disorder,
                        levels = c("None", "Sleep Apnea", "Insomnia"),
                        labels = c(0, 1, 2))
head(mydata1)
##   Gender Sleep.Disorder GenderFactor Sleep.DisorderFactor
## 1   Male           None         Male                    0
## 2   Male           None         Male                    0
## 3   Male           None         Male                    0
## 4   Male    Sleep Apnea         Male                    1
## 5   Male    Sleep Apnea         Male                    1
## 6   Male       Insomnia         Male                    2
#chi-squared test
results <- chisq.test(mydata1$GenderFactor, mydata1$Sleep.DisorderFactor,
                          correct = FALSE)
results
## 
##  Pearson's Chi-squared test
## 
## data:  mydata1$GenderFactor and mydata1$Sleep.DisorderFactor
## X-squared = 54.306, df = 2, p-value = 1.613e-12

Correct = FALSE because we have a 3x2 matrix

Null Hypothesis (H₀):There is no association between Gender and Sleep Disorder

Alternative Hypothesis (H₁):There is association between Gender and Sleep Disorder

We reject H0 at p < 0.001, so there is an association between Gender and Sleep Disorder.

#Observed frequencies
addmargins(results$observed)
##                     mydata1$Sleep.DisorderFactor
## mydata1$GenderFactor   0   1   2 Sum
##               Male   137  11  41 189
##               Female  82  67  36 185
##               Sum    219  78  77 374
#Expected frequencies
round(results$expected, 2)
##                     mydata1$Sleep.DisorderFactor
## mydata1$GenderFactor      0     1     2
##               Male   110.67 39.42 38.91
##               Female 108.33 38.58 38.09

Meaning of 38.09: If the effect doesn’t exist, we expect to have 38.09 womem with Insomnia.

#Difference between the observed and expected frequencies
round(results$res, 2)
##                     mydata1$Sleep.DisorderFactor
## mydata1$GenderFactor     0     1     2
##               Male    2.50 -4.53  0.33
##               Female -2.53  4.57 -0.34
#Distribution of observations in  proportions
addmargins(round(prop.table(results$observed),3))
##                     mydata1$Sleep.DisorderFactor
## mydata1$GenderFactor     0     1     2   Sum
##               Male   0.366 0.029 0.110 0.505
##               Female 0.219 0.179 0.096 0.494
##               Sum    0.585 0.208 0.206 0.999

Meaning of 0.096: Out of 100%, 9.6% of individuals are women and have insomnia.

#Distribution of Sleep Disorder type within each gender
addmargins(round(prop.table(results$observed, 1),3),2)
##                     mydata1$Sleep.DisorderFactor
## mydata1$GenderFactor     0     1     2   Sum
##               Male   0.725 0.058 0.217 1.000
##               Female 0.443 0.362 0.195 1.000

Meaning of 0.725: Out of all men, there are 72.5% of them that doesn´t have any sleep disorder.

#Distribution of gender within each Sleep Disorder type
addmargins(round(prop.table(results$observed, 2),3),1)
##                     mydata1$Sleep.DisorderFactor
## mydata1$GenderFactor     0     1     2
##               Male   0.626 0.141 0.532
##               Female 0.374 0.859 0.468
##               Sum    1.000 1.000 1.000

Meaning of 0.374: Out of those that don’t have any sleep disorder, 37.4% are women.

#Cramer's v test
install.packages("effectsize")
## Installing package into 'C:/Users/User/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'effectsize' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\User\AppData\Local\Temp\RtmpWqNBmY\downloaded_packages
library(effectsize)
effectsize::cramers_v(mydata1$GenderFactor, mydata1$Sleep.DisorderFactor)
## Cramer's V (adj.) |       95% CI
## --------------------------------
## 0.37              | [0.28, 1.00]
## 
## - One-sided CIs: upper bound fixed at [1.00].

The Cramer’s value of 0.37 indicates a Large association between Gender and Sleep Disorder.

#2x2 table with the observed results
observed_table <- table(mydata1$GenderFactor, mydata1$Sleep.DisorderFactor)
#odds for each category
odds_male <- observed_table["Male", "0"] / observed_table["Male", "1"]/ observed_table["Male", "2"]
odds_female <- observed_table["Female", "0"] / observed_table["Female", "1"]/ observed_table["Female", "2"]
#odds ratio
odds_ratio <- odds_male / odds_female
#results
print(observed_table)
##         
##            0   1   2
##   Male   137  11  41
##   Female  82  67  36
print(paste("Odds Ratio:", round(odds_ratio, 3)))
## [1] "Odds Ratio: 8.935"

Odds:

The odds of a female person have some Sleep Disorder ir 8.935 larger than the odds of male have a disorder.

#Non parametric test
fisher.test(mydata1$GenderFactor, mydata1$Sleep.DisorderFactor)
## 
##  Fisher's Exact Test for Count Data
## 
## data:  mydata1$GenderFactor and mydata1$Sleep.DisorderFactor
## p-value = 2.042e-13
## alternative hypothesis: two.sided

Null Hypothesis (H₀):Categorical variables are not associated

Alternative Hypothesis (H₁): Categorical variables are associated

We reject H0 at p < 0.001, so there is an association between Gender and Sleep Disorder.

Conclusion and answering of 1st research question:

Assumptions Pearson chi-square test:

  1. Observations must be independent.

  2. Check that all expected frequencies are greater than 5.

  3. In larger contingency tables (at least one categorical variable has more than two categories), up to 20% of the expected frequencies can be between 1 and 5, but this will reduce the power of the test.

Since all the assumptions are met, we will not use Fisher‘s Exact Probability Test of Independence.

So according with Pearson chi-square test:

There is association between the variables Gender and Sleep Disorder with a effect size of 0.37, considered Large.

Part 2: regression analysis

2nd research question:

What is the effect of age, sleep duration, physical activity level, stress level in an individual heart rate?

mydata <- read.csv("./Sleep&health_dataset.csv", header=TRUE, sep=",", dec=".")

head(mydata, 10)
##    Person.ID Gender Age           Occupation Sleep.Duration Quality.of.Sleep
## 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 Representative            5.9                4
## 5          5   Male  28 Sales Representative            5.9                4
## 6          6   Male  28    Software Engineer            5.9                4
## 7          7   Male  29              Teacher            6.3                6
## 8          8   Male  29               Doctor            7.8                7
## 9          9   Male  29               Doctor            7.8                7
## 10        10   Male  29               Doctor            7.8                7
##    Physical.Activity.Level Stress.Level BMI.Category Blood.Pressure Heart.Rate
## 1                       42            6   Overweight         126/83         77
## 2                       60            8       Normal         125/80         75
## 3                       60            8       Normal         125/80         75
## 4                       30            8        Obese         140/90         85
## 5                       30            8        Obese         140/90         85
## 6                       30            8        Obese         140/90         85
## 7                       40            7        Obese         140/90         82
## 8                       75            6       Normal         120/80         70
## 9                       75            6       Normal         120/80         70
## 10                      75            6       Normal         120/80         70
##    Daily.Steps Sleep.Disorder
## 1         4200           None
## 2        10000           None
## 3        10000           None
## 4         3000    Sleep Apnea
## 5         3000    Sleep Apnea
## 6         3000       Insomnia
## 7         3500       Insomnia
## 8         8000           None
## 9         8000           None
## 10        8000           None
#Selecting variables
selected_columns <- c("Person.ID", "Age", "Sleep.Duration", "Physical.Activity.Level", "Stress.Level", "Heart.Rate")

mydata2 <- mydata[selected_columns]

head(mydata2)
##   Person.ID Age Sleep.Duration Physical.Activity.Level Stress.Level Heart.Rate
## 1         1  27            6.1                      42            6         77
## 2         2  28            6.2                      60            8         75
## 3         3  28            6.2                      60            8         75
## 4         4  28            5.9                      30            8         85
## 5         5  28            5.9                      30            8         85
## 6         6  28            5.9                      30            8         85
summary(mydata2[c("Person.ID", "Age", "Sleep.Duration", "Physical.Activity.Level", "Stress.Level", "Heart.Rate")])
##    Person.ID           Age        Sleep.Duration  Physical.Activity.Level
##  Min.   :  1.00   Min.   :27.00   Min.   :5.800   Min.   :30.00          
##  1st Qu.: 94.25   1st Qu.:35.25   1st Qu.:6.400   1st Qu.:45.00          
##  Median :187.50   Median :43.00   Median :7.200   Median :60.00          
##  Mean   :187.50   Mean   :42.18   Mean   :7.132   Mean   :59.17          
##  3rd Qu.:280.75   3rd Qu.:50.00   3rd Qu.:7.800   3rd Qu.:75.00          
##  Max.   :374.00   Max.   :59.00   Max.   :8.500   Max.   :90.00          
##   Stress.Level     Heart.Rate   
##  Min.   :3.000   Min.   :65.00  
##  1st Qu.:4.000   1st Qu.:68.00  
##  Median :5.000   Median :70.00  
##  Mean   :5.385   Mean   :70.17  
##  3rd Qu.:7.000   3rd Qu.:72.00  
##  Max.   :8.000   Max.   :86.00
install.packages("car")
## Installing package into 'C:/Users/User/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'car' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\User\AppData\Local\Temp\RtmpWqNBmY\downloaded_packages
library(car)
## Loading required package: carData
scatterplot(Heart.Rate ~ Age, 
            ylab = "Heart rate in BPM", 
            xlab = "Age in years", 
            smooth = FALSE, 
            data = mydata2)

scatterplotMatrix(mydata2[ ,c(-1, -7)], smooth = FALSE)

Not all of dependent variables are normally distributed. Can we use them? Yes! Because we said that only standard errors should be normally distributed.

install.packages("Hmisc")
## Installing package into 'C:/Users/User/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'Hmisc' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\User\AppData\Local\Temp\RtmpWqNBmY\downloaded_packages
library(Hmisc)
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
## 
##     format.pval, units
rcorr(as.matrix(mydata2))#[ ,c(-1, -7)]))
##                         Person.ID   Age Sleep.Duration Physical.Activity.Level
## Person.ID                    1.00  0.99           0.30                    0.15
## Age                          0.99  1.00           0.34                    0.18
## Sleep.Duration               0.30  0.34           1.00                    0.21
## Physical.Activity.Level      0.15  0.18           0.21                    1.00
## Stress.Level                -0.39 -0.42          -0.81                   -0.03
## Heart.Rate                  -0.23 -0.23          -0.52                    0.14
##                         Stress.Level Heart.Rate
## Person.ID                      -0.39      -0.23
## Age                            -0.42      -0.23
## Sleep.Duration                 -0.81      -0.52
## Physical.Activity.Level        -0.03       0.14
## Stress.Level                    1.00       0.67
## Heart.Rate                      0.67       1.00
## 
## n= 374 
## 
## 
## P
##                         Person.ID Age    Sleep.Duration Physical.Activity.Level
## Person.ID                         0.0000 0.0000         0.0037                 
## Age                     0.0000           0.0000         0.0005                 
## Sleep.Duration          0.0000    0.0000                0.0000                 
## Physical.Activity.Level 0.0037    0.0005 0.0000                                
## Stress.Level            0.0000    0.0000 0.0000         0.5105                 
## Heart.Rate              0.0000    0.0000 0.0000         0.0080                 
##                         Stress.Level Heart.Rate
## Person.ID               0.0000       0.0000    
## Age                     0.0000       0.0000    
## Sleep.Duration          0.0000       0.0000    
## Physical.Activity.Level 0.5105       0.0080    
## Stress.Level                         0.0000    
## Heart.Rate              0.0000

Correlation between variables: We can see a strong negative correlation between Sleep duration and Stress level (-0.81). We can see a strong positive correlation between Physical Activity Level and Daily steps (0.77).

fit1 <- lm(Heart.Rate ~ Age + Sleep.Duration + Physical.Activity.Level + Stress.Level, data = mydata2)

vif(fit1)
##                     Age          Sleep.Duration Physical.Activity.Level 
##                1.262757                3.256354                1.153822 
##            Stress.Level 
##                3.406796
mean(vif(fit1))
## [1] 2.269932

Variance Inflation Factor Should be less than 5 -> all variables are below 5 We accept this model because there is not to strong multicolinearity.

mydata2$StdResid <- round(rstandard(fit1),3)
mydata2$CooksD <- round(cooks.distance(fit1),3)

hist(mydata2$StdResid,
     xlab = "Standardized residuals",
     ylab = "Frequency",
     main = "Histogram of standardized residuals")

We are checking if errors are normally distributed, through standardized residuals.

shapiro.test(mydata2$StdResid)
## 
##  Shapiro-Wilk normality test
## 
## data:  mydata2$StdResid
## W = 0.63457, p-value < 2.2e-16

Null Hypothesis (H₀): Errors are normally distributed

Alternative Hypothesis (H₁): Errors are not normally distributed

We reject H0 at p < 0.001, so there the erros are not normally distributed. Do we have a problem? No, because our sample is big sample size. In that way, we can proceed.

hist(mydata2$CooksD,
     xlab = "Cooks distance",
     ylab = "Frequency",
     main = "histogram of cooks distances")

head(mydata2[order(-mydata2$StdResid), c("Person.ID", "StdResid")], 15)
##     Person.ID StdResid
## 277       277    6.268
## 278       278    6.268
## 94         94    4.852
## 146       146    4.830
## 265       265    4.383
## 267       267    4.383
## 4           4    3.940
## 5           5    3.940
## 6           6    3.940
## 7           7    3.358
## 17         17    2.692
## 19         19    2.692
## 148       148    2.626
## 81         81    2.539
## 82         82    2.539
head(mydata2[order(-mydata2$CooksD), c("Person.ID", "CooksD")], 10)
##     Person.ID CooksD
## 277       277  0.091
## 278       278  0.091
## 4           4  0.049
## 5           5  0.049
## 6           6  0.049
## 94         94  0.028
## 7           7  0.024
## 1           1  0.020
## 146       146  0.019
## 81         81  0.017
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:Hmisc':
## 
##     src, summarize
## The following object is masked from 'package:car':
## 
##     recode
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
mydata2 <- mydata2 %>%
  filter(!Person.ID %in% c(4,5,6,7,94, 146, 265,267, 277, 278))

After analyzing the standard residuals and the Cooks Distance, it was decided to remove this units.

fit1 <- lm(Heart.Rate ~ Age + Sleep.Duration + Physical.Activity.Level + Stress.Level, data = mydata2)
mydata2$StdResid <- round(rstandard(fit1),3)
mydata2$CooksD <- round(cooks.distance(fit1),3)

hist(mydata2$StdResid,
     xlab = "Standardized residuals",
     ylab = "Frequency",
     main = "Histogram of standardized residuals")

shapiro.test(mydata2$StdResid)
## 
##  Shapiro-Wilk normality test
## 
## data:  mydata2$StdResid
## W = 0.77655, p-value < 2.2e-16

Null Hypothesis (H₀): Errors are normally distributed

Alternative Hypothesis (H₁): Errors are not normally distributed

We reject H0 at p < 0.001, so there the errors are not normally distributed. Once again, no problem because our sample is big sample size. In that way, we can proceed.

install.packages("olsrr")
## Installing package into 'C:/Users/User/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'olsrr' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\User\AppData\Local\Temp\RtmpWqNBmY\downloaded_packages
library(olsrr)
## 
## Attaching package: 'olsrr'
## The following object is masked from 'package:datasets':
## 
##     rivers
fit <- lm(Heart.Rate ~ Age + Sleep.Duration + Physical.Activity.Level + Stress.Level, data = mydata2)

mydata2$StdFitted <- scale(fit$fitted.values)

library(car)
scatterplot(StdResid ~ StdFitted, 
            ylab = "Standardized residuals", 
            xlab = "Standardized fitted values", 
            smooth = FALSE,
            regLine = FALSE,
            boxplots = FALSE,
            data = mydata2)

#Checking homoskedasticity
ols_test_breusch_pagan(fit1)
## 
##  Breusch Pagan Test for Heteroskedasticity
##  -----------------------------------------
##  Ho: the variance is constant            
##  Ha: the variance is not constant        
## 
##                  Data                  
##  --------------------------------------
##  Response : Heart.Rate 
##  Variables: fitted values of Heart.Rate 
## 
##         Test Summary          
##  -----------------------------
##  DF            =    1 
##  Chi2          =    5.135475 
##  Prob > Chi2   =    0.02344167

Null Hypothesis (H₀): Variance is constant

Alternative Hypothesis (H₁): Variance is not constant

We can reject H0 at p < 0.02.

So heteroskedasticity is violated. We need to use Robust Standard Errors.

install.packages("pastecs")
## Installing package into 'C:/Users/User/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'pastecs' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\User\AppData\Local\Temp\RtmpWqNBmY\downloaded_packages
library(pastecs)
## 
## Attaching package: 'pastecs'
## The following objects are masked from 'package:dplyr':
## 
##     first, last
round(stat.desc(mydata2[c("Heart.Rate", "Age", "Sleep.Duration", "Physical.Activity.Level","Stress.Level")], basic = FALSE), 2)
##              Heart.Rate   Age Sleep.Duration Physical.Activity.Level
## median            70.00 43.00           7.20                   60.00
## mean              69.78 42.30           7.14                   59.29
## SE.mean            0.18  0.45           0.04                    1.09
## CI.mean.0.95       0.36  0.89           0.08                    2.15
## var               11.88 74.51           0.63                  433.95
## std.dev            3.45  8.63           0.79                   20.83
## coef.var           0.05  0.20           0.11                    0.35
##              Stress.Level
## median               5.00
## mean                 5.38
## SE.mean              0.09
## CI.mean.0.95         0.18
## var                  3.14
## std.dev              1.77
## coef.var             0.33
library(Hmisc)
rcorr(as.matrix(mydata2[c("Heart.Rate", "Age", "Sleep.Duration", "Physical.Activity.Level","Stress.Level")], 
                type = "pearson"))
##                         Heart.Rate   Age Sleep.Duration Physical.Activity.Level
## Heart.Rate                    1.00 -0.22          -0.62                    0.19
## Age                          -0.22  1.00           0.33                    0.16
## Sleep.Duration               -0.62  0.33           1.00                    0.19
## Physical.Activity.Level       0.19  0.16           0.19                    1.00
## Stress.Level                  0.81 -0.41          -0.81                   -0.01
##                         Stress.Level
## Heart.Rate                      0.81
## Age                            -0.41
## Sleep.Duration                 -0.81
## Physical.Activity.Level        -0.01
## Stress.Level                    1.00
## 
## n= 364 
## 
## 
## P
##                         Heart.Rate Age    Sleep.Duration
## Heart.Rate                         0.0000 0.0000        
## Age                     0.0000            0.0000        
## Sleep.Duration          0.0000     0.0000               
## Physical.Activity.Level 0.0003     0.0029 0.0003        
## Stress.Level            0.0000     0.0000 0.0000        
##                         Physical.Activity.Level Stress.Level
## Heart.Rate              0.0003                  0.0000      
## Age                     0.0029                  0.0000      
## Sleep.Duration          0.0003                  0.0000      
## Physical.Activity.Level                         0.9072      
## Stress.Level            0.9072
install.packages("estimatr")
## Installing package into 'C:/Users/User/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'estimatr' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\User\AppData\Local\Temp\RtmpWqNBmY\downloaded_packages
library(estimatr)
fit1 <- lm_robust(Heart.Rate ~ Age + Sleep.Duration + Physical.Activity.Level + Stress.Level,  
                  se_type = "HC1",
                  data = mydata2)

summary(fit1)
## 
## Call:
## lm_robust(formula = Heart.Rate ~ Age + Sleep.Duration + Physical.Activity.Level + 
##     Stress.Level, data = mydata2, se_type = "HC1")
## 
## Standard error type:  HC1 
## 
## Coefficients:
##                         Estimate Std. Error t value  Pr(>|t|) CI Lower CI Upper
## (Intercept)             57.17705   2.416077 23.6652 2.828e-75 52.42561 61.92850
## Age                      0.03711   0.011918  3.1139 1.995e-03  0.01367  0.06055
## Sleep.Duration           0.04481   0.221568  0.2023 8.398e-01 -0.39092  0.48055
## Physical.Activity.Level  0.02959   0.004019  7.3636 1.236e-12  0.02169  0.03749
## Stress.Level             1.66589   0.111392 14.9552 1.196e-39  1.44683  1.88496
##                          DF
## (Intercept)             359
## Age                     359
## Sleep.Duration          359
## Physical.Activity.Level 359
## Stress.Level            359
## 
## Multiple R-squared:  0.6998 ,    Adjusted R-squared:  0.6965 
## F-statistic: 942.5 on 4 and 359 DF,  p-value: < 2.2e-16

By each aditional year of living, the heart rate on average increases by 0.04 bpm (p < 0.001), assuming all variables stay the same.

By each aditional hour of sleeping, the heart rate on average increases by 0.04 bpm (p < 0.001), assuming all variables stay the same.

By each aditional minute of physical activity, the heart rate on average increases by 0.03 bpm (p < 0.001), assuming all variables stay the same.

By each aditional level of stress, the heart rate on average increases by 1.66 bpm (p < 0.001), assuming all variables stay the same.

H0:R² = 0

H1: R² > 0

We reject H0 at p < 0.001, so the variables were statiscally significant.

Multiple correlation coefficient of determination - explains how well the model fits

69.9% of variability is explained by linear effect of Age, Sleep Duration, Physical Acitivity and Stress Level.

sqrt(summary(fit1)$r.squared)
## [1] 0.8365656

Linear relationship between Heart Rate and all other explanatory variables is strong.