Prepare

Location where data is stored: FitBit Fitness Tracker Data - datasets made available through Mobius. This public Kaggle data set contains personal fitness tracker from 30 fitbit users.

and the data is :

minuteIntensitiesNarrow_merged.csv dailyActivity_merged.csv dailyCalories_merged.csv dailyIntensities_merged.csv dailySteps_merged.csv heartrate_seconds_merged.csv hourlyCalories_merged.csv hourlyIntensities_merged.csv hourlySteps_merged.csv minuteCaloriesNarrow_merged.csv minuteCaloriesWide_merged.csv minuteIntensitiesWide_merged.csv minuteMETsNarrow_merged.csv minuteSleep_merged.csv minuteStepsNarrow_merged.csv minuteStepsWide_merged.csv sleepDay_merged.csv weightLogInfo_merged.csv ### Data type: Data was organized in a long format

Data credibility:

30 eligible Fitbit users consented to the submission of personal tracker data, including minute-level output for physical activity, heart rate, and sleep monitoring. It includes information about daily activity, steps, and heart rate that can be used to explore users’ habits. This data have some limitations, as its only small sample size of data

install and load necessary packages

# First, let's add the packages to be used
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
library(dplyr)
library(tidyverse)
library(lubridate) # converting to date formats
library(patchwork) # To display 2 charts together
## Warning: package 'patchwork' was built under R version 4.3.2
library(hrbrthemes)
## Warning: package 'hrbrthemes' was built under R version 4.3.2
## NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
##       Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
##       if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow

Loading datasets into the environment with the correct naming conventions.

activity = read.csv("C:/Users/abdal/OneDrive/Documents/Zoom/secound project/Fitabase Data/dailyActivity_merged.csv")
sleepDay = read.csv("C:/Users/abdal/OneDrive/Documents/Zoom/secound project/Fitabase Data/sleepDay_merged.csv")
weightloginfo = read.csv("C:/Users/abdal/OneDrive/Documents/Zoom/secound project/Fitabase Data/weightLogInfo_merged.csv")
hourly_intensity = read.csv("C:/Users/abdal/OneDrive/Documents/Zoom/secound project/Fitabase Data/hourlyIntensities_merged.csv")
hourly_steps = read.csv("C:/Users/abdal/OneDrive/Documents/Zoom/secound project/Fitabase Data/hourlySteps_merged.csv")

Viewing imported data

head(activity)
##           Id ActivityDate TotalSteps TotalDistance TrackerDistance
## 1 1503960366    4/12/2016      13162          8.50            8.50
## 2 1503960366    4/13/2016      10735          6.97            6.97
## 3 1503960366    4/14/2016      10460          6.74            6.74
## 4 1503960366    4/15/2016       9762          6.28            6.28
## 5 1503960366    4/16/2016      12669          8.16            8.16
## 6 1503960366    4/17/2016       9705          6.48            6.48
##   LoggedActivitiesDistance VeryActiveDistance ModeratelyActiveDistance
## 1                        0               1.88                     0.55
## 2                        0               1.57                     0.69
## 3                        0               2.44                     0.40
## 4                        0               2.14                     1.26
## 5                        0               2.71                     0.41
## 6                        0               3.19                     0.78
##   LightActiveDistance SedentaryActiveDistance VeryActiveMinutes
## 1                6.06                       0                25
## 2                4.71                       0                21
## 3                3.91                       0                30
## 4                2.83                       0                29
## 5                5.04                       0                36
## 6                2.51                       0                38
##   FairlyActiveMinutes LightlyActiveMinutes SedentaryMinutes Calories
## 1                  13                  328              728     1985
## 2                  19                  217              776     1797
## 3                  11                  181             1218     1776
## 4                  34                  209              726     1745
## 5                  10                  221              773     1863
## 6                  20                  164              539     1728
head(weightloginfo)
##           Id                  Date WeightKg WeightPounds Fat   BMI
## 1 1503960366  5/2/2016 11:59:59 PM     52.6     115.9631  22 22.65
## 2 1503960366  5/3/2016 11:59:59 PM     52.6     115.9631  NA 22.65
## 3 1927972279  4/13/2016 1:08:52 AM    133.5     294.3171  NA 47.54
## 4 2873212765 4/21/2016 11:59:59 PM     56.7     125.0021  NA 21.45
## 5 2873212765 5/12/2016 11:59:59 PM     57.3     126.3249  NA 21.69
## 6 4319703577 4/17/2016 11:59:59 PM     72.4     159.6147  25 27.45
##   IsManualReport        LogId
## 1           True 1.462234e+12
## 2           True 1.462320e+12
## 3          False 1.460510e+12
## 4           True 1.461283e+12
## 5           True 1.463098e+12
## 6           True 1.460938e+12
head(sleep)
##   extra group ID
## 1   0.7     1  1
## 2  -1.6     1  2
## 3  -0.2     1  3
## 4  -1.2     1  4
## 5  -0.1     1  5
## 6   3.4     1  6
head(hourly_intensity)
##           Id          ActivityHour TotalIntensity AverageIntensity
## 1 1503960366 4/12/2016 12:00:00 AM             20         0.333333
## 2 1503960366  4/12/2016 1:00:00 AM              8         0.133333
## 3 1503960366  4/12/2016 2:00:00 AM              7         0.116667
## 4 1503960366  4/12/2016 3:00:00 AM              0         0.000000
## 5 1503960366  4/12/2016 4:00:00 AM              0         0.000000
## 6 1503960366  4/12/2016 5:00:00 AM              0         0.000000
head(hourly_steps)
##           Id          ActivityHour StepTotal
## 1 1503960366 4/12/2016 12:00:00 AM       373
## 2 1503960366  4/12/2016 1:00:00 AM       160
## 3 1503960366  4/12/2016 2:00:00 AM       151
## 4 1503960366  4/12/2016 3:00:00 AM         0
## 5 1503960366  4/12/2016 4:00:00 AM         0
## 6 1503960366  4/12/2016 5:00:00 AM         0

Cleaning the data

# Always check for: 
print('Data duplicates')
## [1] "Data duplicates"
sum(duplicated(activity))
## [1] 0
sum(duplicated(sleepDay))
## [1] 3
sum(duplicated(weightloginfo))
## [1] 0
sum(duplicated(hourly_intensity))
## [1] 0
sum(duplicated(hourly_steps))
## [1] 0
print('Data nulls')
## [1] "Data nulls"
sum(is.na(activity))
## [1] 0
sum(is.na(sleepDay))
## [1] 0
sum(is.na(weightloginfo))
## [1] 65
sum(is.na(hourly_intensity))
## [1] 0
sum(is.na(hourly_steps))
## [1] 0

Now remove the duplicates and nulls

# Now let's remove the duplicates and nulls

clean_sleepDay = sleepDay[!duplicated(sleepDay), ]
clean_weight = na.omit(weightloginfo)
clean_hourly_steps = na.omit(hourly_steps)

Analysis

format the date, and adding month, day columns

# Can see that date is a chr, need to change to date type
# Make a new, cleaned df (clean_) to ensure changes made aren't done to original df 
# as.POSIXct is similar to as.Date but has time as well (need to specify format in both: "%m/%d/%Y [%I:%M:%S %p]")

# activity 
clean_activity = activity %>% 
  rename(Date = ActivityDate) %>%
  mutate(
    Date = as.Date(activity$ActivityDate, "%m/%d/%y"),
    day = weekdays(Date),
    month = months(Date))

# sleep 
clean_sleepDay <- sleepDay %>%
  rename(Date = SleepDay) %>% 
  mutate(
    Date = as.POSIXct(Date, format = '%m/%d/%Y %I:%M:%S %p'), # Converts Date to a dttm type the p specifies that it's in 12 hr format 
    Time = format(Date, format = '%H:%M:%S'), # New Time col made using format(), p removed for 24hr instead of 12hr format
    Day = weekdays(Date),
    Month = months(Date)
  ) 

#hourly_steps 
clean_hourly_steps <- hourly_steps %>%
  rename(Date = ActivityHour) %>%
  mutate(
    Date = as.POSIXct(Date, format = '%m/%d/%Y %I:%M:%S %p'),
    Time = format(Date, format = '%H:%M:%S'),
    Day = weekdays(Date),
    Month = months(Date)
  )

after the clean process let’s check the data

clean_activity %>% 
  select(TotalSteps,
         TotalDistance,
         SedentaryMinutes) %>% 
  summary()
##    TotalSteps    TotalDistance    SedentaryMinutes
##  Min.   :    0   Min.   : 0.000   Min.   :   0.0  
##  1st Qu.: 3790   1st Qu.: 2.620   1st Qu.: 729.8  
##  Median : 7406   Median : 5.245   Median :1057.5  
##  Mean   : 7638   Mean   : 5.490   Mean   : 991.2  
##  3rd Qu.:10727   3rd Qu.: 7.713   3rd Qu.:1229.5  
##  Max.   :36019   Max.   :28.030   Max.   :1440.0
clean_sleepDay %>% 
  select(TotalSleepRecords,
         TotalMinutesAsleep,
         TotalTimeInBed) %>%
  summary()
##  TotalSleepRecords TotalMinutesAsleep TotalTimeInBed 
##  Min.   :1.000     Min.   : 58.0      Min.   : 61.0  
##  1st Qu.:1.000     1st Qu.:361.0      1st Qu.:403.0  
##  Median :1.000     Median :433.0      Median :463.0  
##  Mean   :1.119     Mean   :419.5      Mean   :458.6  
##  3rd Qu.:1.000     3rd Qu.:490.0      3rd Qu.:526.0  
##  Max.   :3.000     Max.   :796.0      Max.   :961.0

Visualizations

Sedentary minutes vs. Calories

#after that we'll start Plotting
ggplot(data=clean_activity, aes(x=SedentaryMinutes, y=Calories)) + 
  geom_point()+
  geom_rug( color="red")+
  geom_smooth()+ 
  labs(title = "                    Sedentary minutes vs. Calories",
       subtitle = "        Users spend an average of 991 minutes (16-17 hours) of sedentary activity",
       caption = "Data source: FitBit Fitness Tracker Data")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Total Steps vs. Calories

ggplot(data=clean_activity, aes(x=TotalSteps, y=Calories)) + 
  geom_point() + geom_smooth() + labs(title="Total Steps vs. Calories", caption = "Data source: FitBit Fitness Tracker Data")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Total Minutes Asleep vs. Total Time in Bed

ggplot(data = clean_sleepDay, mapping = aes(x=TotalMinutesAsleep, y=TotalTimeInBed))+
  geom_point() + geom_smooth() + labs(title="Total Minutes Asleep vs. Total Time in Bed", caption = "Data source: FitBit Fitness Tracker Data")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

clean_sleepDay %>% 
  group_by(TotalTimeInBed) %>% 
  summarize(avgsleeptime = mean(TotalMinutesAsleep)) %>% 
  ggplot() +
  geom_col(mapping = aes(x = TotalTimeInBed  , y = avgsleeptime, fill = avgsleeptime)) +
  scale_fill_gradient(low = "lightblue", high = "darkblue") + 
  theme(axis.text.x = element_text(angle = 0))+
  labs(title = "                               Minutes in bed vs Total minutes asleep",
       subtitle = "                                             On average, users slept about 7 hours",
       caption = "Data source: FitBit Fitness Tracker Data")

Total intesivity over time

# Total intesivity over time
clean_hourly_steps %>% 
  group_by(Time) %>% 
  summarize(avgsteps = mean(StepTotal)) %>% 
  ggplot() +
  geom_col(mapping = aes(x = Time , y = avgsteps, fill = avgsteps)) +
  scale_fill_gradient(low = "darkblue", high = "orange") + 
  theme(axis.text.x = element_text(angle = 90))+
  labs(title = "                                           Total intesivity over time",
       caption = "Data source: FitBit Fitness Tracker Data")

Share

  1. We are seeing a trend where more steps per day translates into more calories burned.

  2. I see positive correlation here between Total Steps and Calories, which is obvious - the more active we are, the more calories we burn. (or) This viz shows that users spend a lot of minutes sedentary in a day. Also shows a trend of more steps being taken leading to more calories burned (likely to be a causal relationship).

  3. Can see that total time in bed correlates with the total amount of sleep. If users want more sleep, could suggest them to set a reminder to sleep earlier.

  4. We observed a trend where people are active between 6:00 and 22:00 - We noticed the highest intensity between 20:00 and 19:00, most likely people go to the gym or doing exercise in these hours.

Act

Recommendations for Bellabeat app

Sleep

-Create a notification system to notifies users to optimise their evening habits and create reminders (for example, “turn off your phone to limit screen time and blue light exposure”). -Update the app to track sleep and provide information such as how long you sleep, how often you wake up during the night.

Steps

Most activity happens between 5 pm and 7 pm - I suppose, that people go to a gym or for a walk after finishing work. Bellabeat can use this time to remind and motivate users to go for a run or walk.Create dashboards to track progress for people who spend more time sitting, and add notifications and set up challenges to encourage them.

Diet control

If users want to lose weight, it’s probably a good idea to control daily calorie consumption. Bellabeat can suggest some ideas for low-calorie lunch and dinner.

Thank you for your interest to my Bellabeat Case Study!