Description

This package is used to do analysis on some resting heart rate data I have been collecting. It creates target heart ranges for excersing. Also it compares heart rate to it's moving average. The package also tries to fit some linear models. It can also create a scatter plot with the correlation listed as a title.

Objective

I was hoping with this analysis to see if there was a relationship between my resting heart rate. I also want to look at some patterns with my resting heart rate.

Experimental Design

So I measured 10 Variable. 1) Date is just the date the rest of the variables were measured. 2) Day is just the number of days from the first day. 3) Resting Heart Rate was measured every morning (that I remembered) by counting my heart rate for 1 min. The first day I tried to use a resting heart rate monitor but it didn't seem to be working. 4) Miles Walking was the number of miles that my health app on my phone collected each day. 5) Inf is the number of hours that I studied for my Statistical Inference class. 6) Computing is the number of hours that I studied for Introduction to Statistical Computing. 7) Probability is the number of hours that I studied for Introduction to Probability. 8) Other is any other hours of studying that I wasn't for those classes. 9) Estimated Sleep is the amount of sleep that I had the night before I measured my heart rate. 10) Hrs on Hwk is just the summation of 5), 6), 7), and 8).

My data set is really only representative of heart rate data for myself. It would be interesting to extend this to other people and see if others get similar results to me. There are many possible confounders. For instance, this experiment is not blinded. I was the one counting my own heart rate and I started analysis before I finished collecting data. Therefore, this could have affected the results of my study. Also there were days that I forgot to count my resting heart rate. Therefore, these missing data could have affected my results as well.

library(knitr)
library(heartrate)
library(googlesheets)
library(tidyverse)
library(forecast)

Here are some examples of what you can do with my packages and some basic analysis:

Sample Dataset

I created a sample data set. It contains all of my data collected. Here is just the first couple of rows:

data("tester")
tester

Updating Dataset

I created a function that will automatically pull in the newest data. It is called updating_heartRate. This is useful so that I can continually use the function to update my data.

tester <- updating_heartRate()
tester

target_ranges

I created a function that will create target work out ranges based off \url{https://www.active.com/fitness/articles/how-to-calculate-your-training-heart-rate-zones}. I think this could be useful if I tracked my heart rate during work outs I could see if my heart rates during the work out were in my target heart range. Here is an example of that:

ranged <- target_ranges(tester$`Resting Heart Rate`, time_data = tester$Date,
                        age = 24)
plot(ranged[,1], ranged[,2], type = "l", ylim = c(min(ranged[,2]),
                                                  max(ranged[,3])),
     xlab = "Date",
     ylab = "Heart Rate (Beats Per Min)" )
lines(ranged[,1], ranged[,3], type = "l")

stress_plot

I created a function that immediately plots the difference between the actual data and a moving average. This would allow one to see if they seem to be above their moving average. My data for the most part seems to be pretty much the same (it might be increasing slightly) except the first data point was really low. The first data point is really low because I was using a heart rate monitor that I think is broken.

plot_stressed(tester$`Resting Heart Rate`, tester$Date, xlab = "Date",
              ylab = " Resting Heart Rate (Beats Per Min)")

plot_lms

This function produces a series of scatter plots that match up with the columns selected. It then fits a linear regression and posts the equation for the line at the top of each scatter plot. I was also able to now add the p-value for the coefficient for the slope of the line to the plot at the bottom. I would like to add some way of doing multiple linear regression here and maybe have a way to post if the slope of the line is statistically significant. It doesn't seem that any of these linear regressions seem to fit the data very well. Also none of the p-values are significant.

par(mar= c(5.0, 4.0, 3.0, 2))
tester %>% plot_lms(., col_names = c("Hrs on Hwk", "Miles walking", "Estimated Sleep"),
         response_variable = "Resting Heart Rate", multivariate = TRUE)

I then tried an compare the resting heart rate to the previous day's data. Again, there doesn't seem to be any clear fit to the data. Also none of the p-values for the coefficient of the slope are statistically significant either.

par(mar= c(5.0, 4.0, 3.0, 2))
tester <- tester %>% mutate(lag_sleep = lag(tester$"Estimated Sleep", 1),
                            lag_walking= lag(tester$"Miles walking", 1),
                            lag_hwk_total = lag(tester$"Hrs on Hwk", 1))
tester %>% plot_lms(., col_names = c("lag_sleep", "lag_walking", "lag_hwk_total"),
         response_variable = "Resting Heart Rate")

plot_cor

This function produces a series of scatter plots that match up with the columns selected but this time it prints the correlation of the variable of the columns selected with the response variable. As you can see for these three variables there isn't a strong correlation between them and resting heart rate.

par(mar = c(4.1, 4.1,4.1,1.1))
tester %>% plot_cor(. ,col_names = c("Hrs on Hwk", "Miles walking", "Estimated Sleep"),
         response_variable = "Resting Heart Rate")

Other Analysis of the Resting Heart Rate Data.

The first point in the resting heart rate looks like it might be an outlier. Therefore, I thought about removing it. Also the first day I tried to use a heart rate monitor but I don't believe that it functions correctly.

tester %>% select("Resting Heart Rate") %>%  boxplot() 
tester2 <- tester[-1,]

This is just a plot of my resting heart rate. It looks like the plot might be cyclical. So I thought to compare across days of the week.

plot(tester2$Date, tester2$"Resting Heart Rate", type = "l",
     ylab = "Resting Heart Rate(Beats Per Min)", xlab = "Date")

Interestingly, it seems that my resting heart rate is high on Sunday. I'm not sure why that would be. This would probably something I would continually look at.

days_of_week <- as.factor(c("T", "W", "Th", "F", "S", "Su", "M"))
days_of_week <- rep(days_of_week, 100)[1:nrow(tester2)]
tester2$days_of_week <- days_of_week


mean_ave <- by(tester2$`Resting Heart Rate`, tester2$days_of_week, function(x) mean(x, na.rm = TRUE))
mean_ave_df <- as.data.frame(do.call(rbind, list(mean_ave)))
kable(mean_ave_df)

Although, currently if we look at the the Tukey Multiple comparison test with a 95% family wise confidence level. We see that none of the of comparisons are statistically significant.

saver <- TukeyHSD(aov(tester2$`Resting Heart Rate` ~ tester2$days_of_week))
kable(saver$`tester2$days_of_week`)

Now I though I'd look at some of the variables I created before but now with the data set without the outlier and looking at the lag of sleep, miles walking, and homework totals. This time I'm going to look at them all together or in other words fit a multivariate regression model. Again, we see that none of these covariates are statistically significant.

tester2 %>% plot_lms(., col_names = c("lag_sleep", "lag_walking", "lag_hwk_total"),
                    response_variable = "Resting Heart Rate", 
                    multivariate = TRUE)

Then I thought to just look at the individual classes and how much time I spend on those classes. Again I didn't see any statistically significant p-values for the correlation coefficient.

tester2 %>% plot_lms(., col_names = c("Inf", "Computing", "Probability", "Other"),
                    response_variable = "Resting Heart Rate")

Then again, I thought maybe if I looked at the previous days homework for each one of those classes I would see some evidence. These variables in general seemed to even have larger p-values and not to fit the data well.

tester2 <- tester2 %>% mutate(lag_inf = lag(tester2$"Inf", 1),
                            lag_comp= lag(tester2$"Computing", 1),
                            lag_prob = lag(tester2$"Probability", 1),
                            lag_other = lag(tester2$Other))

tester2 %>% plot_lms(., col_names = c("lag_inf", "lag_comp", "lag_prob", "lag_other"),
                    response_variable = "Resting Heart Rate")

Conclusion

This analysis was very interesting to me. I found it interesting that none of the variables seem to be highly correlated with my resting heart rate. In the future, I hope that I can track more variables. For instance, I would like to see if meditation or working out had a relationship with my resting heart rate. I found it interesting that my heart rate was actually the highest on Sunday. I won't have guessed that. I think some of the plot functions I created could be useful in other setting. For instance, stress plot function also allows me to determine target heart rate. Overall, I think this is a good start for further research into this subject.

Limitations

There are many limitations to my analysis. For one instance, I only collected data on me, so it probably doesn't extend to everyone else. Also, I wasn't always great at keeping track of the variables I was interested in, so it probably isn't very accurate. I started off thinking I would use a heart rate monitor, but then it broke. So I had to keep track of it by counting the beats myself. This probably created some bias. Also I wish I knew more about time series analysis because that could be used in this setting. The length of the study was short. It would be intriguing to see how things were different if I did this for longer and added in more variables. Overall, I found this study really interesting and made me realize I should start tracking other things in my life.



coreykimzey/heartrate documentation built on May 28, 2019, 7:47 p.m.