Data visualization on activity tracking device
The skills the author demoed here can be learned through taking Data Science with Machine Learning bootcamp with NYC Data Science Academy.
I. Introduction
Nowadays, there are wide selections and different price options for wearable activity tracking devices, or βwearablesβ. Most of these tracking devices harness a 3-axis accelerometer to understand the userβs motions. By analyzing acceleration data, the trackers provide information about frequency, duration, intensity, and patterns of movement to determine a multitude of health metrics, such as steps taken, distance traveled, calories burned, and sleep quality.
Another advantage of owning this device for data and fitness enthusiasts is the ability to log their food, activities, and weight over time with the possibility to set daily and weekly goals for themselves for steps, calories burned and distance walked. One of the most popular wearable tracking devices is Fitbit. Like most wearable activity tracking devices, Β there is the option to synchronize your data to your user account via Fitbit Connect. In this work, we consider Steps as our health and fitness goal that we wish to predict.
II. The Data
Fitbit users can extract their data by logging into their Fitbit Connect account as shown in Fig.1 below, where data for one year of activities and sleep could be extracted in CSV format.
Fig. 1 : Data Extraction for Fitbit userβs webpage
After gathering aΒ one year activities and sleep data, an exploratory data analysis is achieved using Python in order to determine the assess the relationship or determine the patterns between different type of measurements from May 8 2015 to May 7 2016. For each day, the extracted data has the following features (columns of our datasets):
- Calorie burned
- Steps
- Distance
- Floors
- Sedentary MinutesΒ
- Lightly Active MinutesΒ
- Fairly Active MinutesΒ
- Very Active MinutesΒ
- Activity Calories
- Minutes of Sleep
- Minutes of Being Awake
- Number of Awakenings
- Length Of Rest In Minutes
III. Exploratory data analysis
First we take a look to the distribution of our raw data as shown in Fig.2 below.
Fig. 2Β :Β Raw Data
Based on the raw data from Fig.2, we can see the following:
- There are no missing values in the dataset
- Some of the data are zero, this is because Fitbit is not worn, is charging or due to some failures in synchronizing the data
- Sedentary minutes are longer than activity minutes
- Light activity has a distribution that resembles the gaussian distribution
- There are less minutes fairly active or very active days. Distribution appear as a log normal distribution
- Floors feature describes elevation or climbing and seems to have a log normal distribution
- On average Sleep is around 5 to 6 hours
- Daily Steps vary between 5000 and 20000 steps equivalent to a distance up to 12 miles
In this dataset, we distinguish 3 types of health or fitness goals: Steps, Calories burned, Hours of Sleep, and Sleep efficiency. To assess the sleep quality for a specific day, we define Sleep efficiency as the percentage of Minutes of Sleep over Length Of Rest In Minutes.
In the following section, these goals are represented over days of the week, months and grouped by work days (from Monday to Friday) and weekends (Saturday and Sunday).
Β 1- Goals observations
Β Β Β Β Β Β Β Β 1.a - variations by days of the week
Most of the global recommendations on physical activity for health are targeted toward meeting certain daily goals. Fig.3 below shows the daily achieved goals.
Fig. 3Β : Daily goals
- On average, the Fitbit user achieve between 8500 to nearly 12000 steps by day. According to Fitbit, the daily recommended steps for an adult are 10000 steps, which is equivalent to 30 min of daily exercise. Saturday is the most active day.
- On average, the Fitbit user sleep between 5-6 hours and sleep the longest on Thursday. This is below the recommended by The Journal of the National Sleep Foundation. For an adult it is between 7-9 hours
- The sleep efficiency seems constant around 85% and is also considered by The Journal of the National Sleep Foundation as a normal sleep cycle.
- Calories burned are around 3000 Β which is more than the average person According to World Health Organisation, is between 1,600 and 2,000 calories per day.
Β Β Β Β Β Β 1.b- Goals parameters- variations by months
Although there are no global recommendations on physical activity for health, it is interesting to observe the evolution of goals parameters by month. We can see that October was the least active month, with few hours of sleep despite having the highest sleep efficiency. From Fig.4, we can also observe that the Fitbit user is more active during Spring and Summer months with higher hours of sleep but low sleep efficiency.
Fig. 4Β : MonthlyΒ goals
Β Β Β Β Β Β 1.c- Goals parameters- variations by workdays or weekend
A new feature is created to group days by workdays (from Monday to Friday) and Weekends (Saturday and Sunday). Fig.5 shows the achieved goals parameters. During the workdays, the Fitbit users improveΒ all the health goals comparing to weekends.
Fig. 5Β : Workdays and WeekendΒ goals
Β 2.Data interaction
In the following section, the relationship between several features in the dataset is observed. Along with the feature that distinguishes between workdays and weekends, other features are added to the datasets such as: Yesterday sleep hours and Yesterday's sleep efficiency in order to assess the effects of a previous night sleep on the goals. The following features: Days, Months and Weekdays have been encoded.
In this work, we consider Steps as our health and fitness goal that we wish to predict.
Β Β Β Β Β Β 2.a - Correlation
First, we look at the trend shared by predictors, i.e the features that will be used to predict Steps. We compute the correlation matrix shown in Fig.6.
Fig. 6Β :Β Correlation matrix
From Fig.6, we can observe some strong correlation between some sleep predictors. Β Distance is strongly correlated to Floors and both are inter-correlated to Minutes very active.
Β Β 3- Data analysis insights
- In order to build our steps prediction model, we choose only to keep the variables Hours Sleep and Yesterday Sleep which respectively describe the sleep quality for the same day and day before.
- Our data shows some strong correlation between predictors such as Distance and Minutes very active (0.76 correlation). Β If two predictors are strongly correlated (correlation coefficient>0.76 )Β we choose to keep only one in order to avoid redundant information in our data.
Fig.8 shows the relationship between Steps and other predictors. We can see that there is a strong linear relationship between Steps and the predictors. Distance according to Fitbit is calculated by multiplying your walking steps by your walking stride length.
Fig. 8Β : Relationship between Steps and predictors
II. Steps prediction and Evaluation
In order to predict the Steps, we split our data into train (75% of the dataset) and test (25% of the dataset). The train data has the following predictors:
- Calories burned
- Distance
- Floors
- Sedentary MinutesΒ
- Lightly Active MinutesΒ
- Fairly Active MinutesΒ
- Very Active Minutes
- Activity Calories
- Work_or_Weekend
- Hours Sleep
- Yesterday Sleep
- Months encoded
We fit different machine learning algorithms on our train dataset and make corresponding predictions on our test dataset. First we assess variable importance using linear regression and test p values for each predictor. Results are shown in Table.I
TableΒ IΒ :Β Feature importance and p-value
In order to measure models accuracy, we compute theΒ RMSE and R2 as shown in Table.II.
TableΒ II :Β Prediction Scores for different Machine Learning
From Table II, simple linear regression seems to be well suited for the nature of this prediction problem as it has a lower value of RMSE in this case we have an error of 3437.32 steps. No improvement is observed using regularization such as Lasso or Ridge.
Fig.9 shows the comparison between Predicted Steps and Steps from the test dataset.
Fig. 9Β :Β Comparison between models- Predicted Steps and Test Steps
III.Β Discussion
Considering the nature of our data, we have some multicollinearity and also a strong linear relationship between some features and our response variable Steps. Multicollinearity is important in regression analysis as it may cause unstable estimated coefficients and a loss of the model interpretability as it depends on the data used to train our model.
In order to have a constrained model, we introduce regularization of the linear regression using Ridge and Lasso methods to predict the Steps based predominantly on the most important features. Regularization, in this case, shrinks the least important coefficients (Ridge) or can eliminate them completely (Lasso) in order to improve generalization of the model. For this model, regularization doesn't improve interpretability or the predictive power of the model.
As for the Random Forest Regressor, it fails in predicting Steps especially for small values as observed in Fig.9. In the exploratory analysis, the mean daily Steps is between 8000 and 12000. The poor results of Random Forest Regressor are due to the nature of the algorithm being a nonlinear algorithm. In our case, our data is very linear and thus we need a lot of branches per tree to get a good approximation.
VI.Β Conclusion
In this work, the relationship between the measured Fitbit parameters was explored. We choose to predict the number of steps based on multiple features. Despite an apparent multicollinearity, simple linear regression seems to give better results predicting the number of steps.