{"metadata":{"kernelspec":{"name":"ir","display_name":"R","language":"R"},"language_info":{"name":"R","codemirror_mode":"r","pygments_lexer":"r","mimetype":"text/x-r-source","file_extension":".r","version":"4.0.5"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# BellaBeat Fitness Tracker.\n![Bellabeat](https://www.stableton.com/wp-content/webp-express/webp-images/uploads/2021/05/Bellabeat-Blog-Picture1.png.webp)\n\n# Introduction\n\nBellabeat is a high tech manufacturer of health-focused products for women. Bellabeat is a successful small company, but they have potential to become larger player in the global smart device market. Bellabeat’s device is mainly focused on collecting data on activity, sleep, stress and reproductive health. Bellabeat has empowered women with knowledge about their own health and habits. Since it was found in 2013, Bellabeat has grown rapidly and quickly positioned itself as a teck driven wellness company for women. \nBellabeat Product includes, following devices and explained  what they are used for.\n\n* **Bellabeat app:** The Bellabeat app provides users with health data related to their activity, sleep, stress, menstrual cycle, and mindfulness habits. This data can help users better understand their current habits and make healthy decisions. The Bellabeat app connects to their line of smart wellness products.\n\n* **Leaf:** Bellabeat’s classic wellness tracker can be worn as a bracelet, necklace, or clip. The Leaf tracker connects to the Bellabeat app to track activity, sleep, and stress. \n\n* **Time:** This wellness watch combines the timeless look of a classic timepiece with smart technology to track user activity, sleep, and stress. The Time watch connects to the Bellabeat app to provide you with insights into your daily wellness. \n\n* **Spring:** This is a water bottle that tracks daily water intake using smart technology to ensure that you are appropriately hydrated throughout the day. The Spring bottle connects to the Bellabeat app to track your hydration levels. \n\n* **Bellabeat Membership:** Bellabeat also offers a subscription-based membership program for users. Membership gives users 24/7 access to fully personalized guidance on nutrition, activity, sleep, health and beauty, and mindfulness based on their lifestyle and goals.\n\n*For more information you can visit their official website* [BellaBeat](https://bellabeat.com/).\n\n","metadata":{}},{"cell_type":"markdown","source":"# Business Challenge:\n\nBellabeat is a fast-growing company, and it has potential to become rival player in smart devices. To increase its business scale and, to make better strategies for market, We wants to find out , How customers are using FitBit device. \nAre they using it on any special days of week, or are they using it generally as fancy wearing?\nWhich is most significant pattern observed in Bellabeat data and how can we use these insights to improve our Business.","metadata":{}},{"cell_type":"markdown","source":"# Question to Investigate.\n\n1.  What are some trends in smart device usage?\n1.  How could these trends apply to Bellabeat customers? \n1.  How could these trends help influence Bellabeat marketing strategy?","metadata":{}},{"cell_type":"markdown","source":"# Data Source:\nThe required data is available on **Kaggle**, and is provided by **FitBit** **Fitness Tracker Data.** \n\n* Data is from Reliable source.\n* It is publicly available to all.\n* It is original data source.\n* It is Comprehensive.\n* Data is credible as it is from original source.\n* Data is not outdated, as it is freshly recorded in 2 or 3 years ago.\n* Data Includes fitness track from 30 users.","metadata":{}},{"cell_type":"markdown","source":"# Prepare Data.\n\n* Data available from Kaggle website , and can be accessed from [here](https://www.kaggle.com/arashnic/fitbit/download) \n* Data is of large size of about 300 MBs, thus it won’t be transformed or cleaned using Spreadsheet or MS-Excel.\n* Our Data need thoroughly cleaning, thus R- Could be best tool for it, as SQL doesn’t have every function we need here,     But for Double security of Data we can store them on Cloud using our SQL servers.","metadata":{}},{"cell_type":"markdown","source":"# Processing Data\n*Here we will import all required Packages from R, which includes tidyverse.*\n* **tidyr** **<-** for data cleaning\n* **DPlyr <-** for Selecting and filtering.\n* **readr <-** for loading & reading data.\n* **ggplot2 <-** for data visulization.\n* **lubridate <-** for Date transformation.\n* **tibble,purr, stringr, forcats,stats,hms**","metadata":{}},{"cell_type":"markdown","source":"## Importing Packages","metadata":{}},{"cell_type":"code","source":"  install.packages(\"tidyverse\") # metapackage of all tidyverse packages\n  install.packages(\"skimr\")\n  install.packages(\"shiny\")\n  install.packages(\"janitor\")\n  install.packages(\"here\")","metadata":{"execution":{"iopub.status.busy":"2021-09-02T07:30:42.129662Z","iopub.execute_input":"2021-09-02T07:30:42.131373Z","iopub.status.idle":"2021-09-02T07:30:42.142959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Loading Packages to Our System.","metadata":{}},{"cell_type":"code","source":"library(tidyverse)\nlibrary(skimr)\nlibrary(shiny)\nlibrary(janitor)\nlibrary(here)\nlibrary(hms)\nlibrary(lubridate)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T07:30:42.145493Z","iopub.execute_input":"2021-09-02T07:30:42.14695Z","iopub.status.idle":"2021-09-02T07:30:42.171761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Loading our Data set into system.","metadata":{}},{"cell_type":"code","source":"daily_activity <- read_csv(\"../input/fitbit-data-set/activity.csv\")\nhourly_cal <- read_csv(\"../input/fitbit-data-set/cal.csv\")\nhourly_intensity <- read_csv(\"../input/fitbit-data-set/Intense.csv\")\nhourly_steps <- read_csv(\"../input/fitbit-data-set/stp.csv\")\nsleep_day <- read_csv(\"../input/fitbit-data-set/slp.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-09-02T07:30:42.174405Z","iopub.execute_input":"2021-09-02T07:30:42.175909Z","iopub.status.idle":"2021-09-02T07:30:42.395714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Checking Data sets**","metadata":{}},{"cell_type":"code","source":"glimpse(daily_activity)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T07:30:42.406224Z","iopub.execute_input":"2021-09-02T07:30:42.408047Z","iopub.status.idle":"2021-09-02T07:30:42.428956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"glimpse(sleep_day)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T07:30:42.431355Z","iopub.execute_input":"2021-09-02T07:30:42.432748Z","iopub.status.idle":"2021-09-02T07:30:42.450412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Header Names are written in CamelCase, we would convert it into Snake_Case. *","metadata":{}},{"cell_type":"code","source":"Activity<- clean_names(daily_activity)\nCalories <- clean_names(hourly_cal)\nintensity <- clean_names(hourly_intensity)\nSteps <- clean_names(hourly_steps)\nSleep <- clean_names(sleep_day)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T07:30:42.452803Z","iopub.execute_input":"2021-09-02T07:30:42.454316Z","iopub.status.idle":"2021-09-02T07:30:42.606348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"** Date in Activity is written in Text String Format -> MM/DD/YYYY: **","metadata":{}},{"cell_type":"code","source":"glimpse(hourly_cal)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T07:30:42.610135Z","iopub.execute_input":"2021-09-02T07:30:42.611851Z","iopub.status.idle":"2021-09-02T07:30:42.630189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This data was collected from 30 devices of different people so, In order to check for consistency of our data. We need to check If Devices **ID count = 30** or not. SO that we could be sure from **Duplicacy** or **inconsistency** of Datafeilds.\n","metadata":{}},{"cell_type":"code","source":"count_check <- data.frame(data_name = c(\"daily_activity\",\n                                        \"hourly_cal\",\n                                        \"hourly_intensity\",\n                                        \"hourly_steps\",\n                                        \"sleep_day\"),\n                          Total = c(n_distinct(daily_activity$id),\n                                    n_distinct(hourly_cal$id),\n                                    n_distinct(hourly_intensity$id),\n                                    n_distinct(hourly_steps$id),\n                                    n_distinct(sleep_day$id)))\nhead(count_check, 10)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T07:30:42.632659Z","iopub.execute_input":"2021-09-02T07:30:42.634073Z","iopub.status.idle":"2021-09-02T07:30:42.666005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* IDs Count is not equal to 30, thus it might contain some duplicate data or missing data.\n\n    **Checking Day Range**","metadata":{}},{"cell_type":"code","source":"date_range <- data.frame(dates=c(min(Activity$activity_date),\n                max(Activity$activity_date)),val = c(\"Min\",\"Max\"))\ndate_range","metadata":{"execution":{"iopub.status.busy":"2021-09-02T07:30:42.668529Z","iopub.execute_input":"2021-09-02T07:30:42.670062Z","iopub.status.idle":"2021-09-02T07:30:42.69324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Data was collected from 12/04/2016 to 12/05/2016. Therefor we should check for Count of days.","metadata":{}},{"cell_type":"code","source":"check_day <- group_by(Activity,id) %>%\n                        summarise(total_day = n_distinct(activity_date))\narrange(check_day,total_day)\nhead(check_day)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T07:30:42.695662Z","iopub.execute_input":"2021-09-02T07:30:42.697147Z","iopub.status.idle":"2021-09-02T07:30:42.781647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Exploring Sleep Dataset.","metadata":{}},{"cell_type":"code","source":"summary(Sleep)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T07:30:42.784013Z","iopub.execute_input":"2021-09-02T07:30:42.785519Z","iopub.status.idle":"2021-09-02T07:30:42.804509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count(Sleep,n_distinct(Sleep$id))","metadata":{"execution":{"iopub.status.busy":"2021-09-02T07:30:42.80693Z","iopub.execute_input":"2021-09-02T07:30:42.808394Z","iopub.status.idle":"2021-09-02T07:30:42.837192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Observations\n  After reviewing the data, following observations are made:_\n* date_time needs to be CAST to dttm.\n* Normal Sleep Time is 419 minutes.\n* Minimum Sleep Time is 58 minutes.\n* Maximum Sleep Time is 796 minutes.\n* Only 24 IDs have Sleep Record.\n* Total Participants are 33.\n","metadata":{}},{"cell_type":"markdown","source":"# Transforming Data\n1. **Create** New Date fields.\n2. **Removing** unwanted Variables.\n3. **Filter** participants with less than 100 minutes of *Sleep Time*.\n\n*Transforming Daily Data*","metadata":{}},{"cell_type":"code","source":"daily <- select(Activity,-tracker_distance)\ndaily$date <- mdy(daily$activity_date)\ndaily$week_day <- wday(daily$date,label = TRUE)\ndaily$week_no <- week(daily$date)\ndaily<- filter( daily, id!=4057192912)\n\n## reviewing Created Datafields\nhead(daily,10)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T07:30:42.839788Z","iopub.execute_input":"2021-09-02T07:30:42.841272Z","iopub.status.idle":"2021-09-02T07:30:42.913309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Transforming Hourly Data**","metadata":{}},{"cell_type":"code","source":"hourly<- inner_join(inner_join(Calories, intensity, by = c(\"id\", \"activity_hour\")),Steps, by = c(\"id\", \"activity_hour\"))\nhourly$activity_hour <- mdy_hms(hourly$activity_hour)\nhourly$date <- as_date(hourly$activity_hour)\nhourly$time <- as_hms(hourly$activity_hour)\nhourly <- filter(hourly, id != 4057192912)\nhead(hourly)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T07:36:27.619805Z","iopub.execute_input":"2021-09-02T07:36:27.621615Z","iopub.status.idle":"2021-09-02T07:36:27.77588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Sleeping           <- Sleep\nSleeping$sleep_day <- mdy_hms(Sleeping$sleep_day)\nSleeping$date      <- as_date(Sleeping$sleep_day)\nSleeping           <- filter(Sleeping, id!=4057192912, total_minutes_asleep > 100)\nhead(Sleeping)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T07:30:42.917009Z","iopub.execute_input":"2021-09-02T07:30:42.918621Z","iopub.status.idle":"2021-09-02T07:30:42.978337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Analysis\nWe can do analysis by  plotting visuals of different data fields.","metadata":{}},{"cell_type":"code","source":"plot_style <- theme(plot.title = element_text(face = \"bold\", size = 18),\n                    plot.subtitle = element_text(margin = margin(b = 20)),\n                    axis.title.y = element_text(margin = margin(r = 10)),\n                    axis.title.x = element_text(margin = margin(t = 10)),\n                    plot.margin = margin(10,10,10,10),\n                    legend.title = element_text(face = \"bold\"))","metadata":{"execution":{"iopub.status.busy":"2021-09-02T07:30:42.981786Z","iopub.execute_input":"2021-09-02T07:30:42.983426Z","iopub.status.idle":"2021-09-02T07:30:42.99807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"** Average daily steps within Week days **","metadata":{}},{"cell_type":"code","source":"average_daily_steps_df <- group_by(daily,week_day) %>% \n  summarise(average_steps = mean(total_steps))\ntue_steps_df <- filter(average_daily_steps_df, week_day == \"Tue\")\nsat_steps_df <- filter(average_daily_steps_df, week_day == \"Sat\")\ntue_steps <- tue_steps_df$average_steps\nsat_steps <- sat_steps_df$average_steps\nggplot(average_daily_steps_df)+\n  geom_bar(aes(week_day, average_steps), stat = \"identity\", fill = \"royalblue3\")+\n  labs(title = \"How Many Steps People Walk Each Day?\", \n       subtitle = \"Average daily steps from Monday to Sunday\")+\n  ylab(\"Average steps\")+\n  xlab(NULL)+\n  ylim(0, 10000)+\n  plot_style","metadata":{"execution":{"iopub.status.busy":"2021-09-02T07:30:43.000723Z","iopub.execute_input":"2021-09-02T07:30:43.002304Z","iopub.status.idle":"2021-09-02T07:30:43.547291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"*Average Daily Steeps -minutes*","metadata":{}},{"cell_type":"code","source":"# Pivot daily_df\npivot_daily_df <- daily %>%\n  pivot_longer(c(very_active_minutes, fairly_active_minutes,\n                 lightly_active_minutes, sedentary_minutes), \n               names_to = \"active_type\", values_to = \"active_minutes\")\n# Summarize data\ndaily_active_df <- select(pivot_daily_df, id, week_day, active_type, active_minutes)\naverage_daily_active_df <- summarise(group_by(daily_active_df, week_day, active_type), \n                                     average_active_minutes = mean(active_minutes))\n# Visualization\nfilter(average_daily_active_df, active_type == \"very_active_minutes\" |active_type == \"fairly_active_minutes\") %>% \nggplot()+\n  geom_bar(aes(week_day, average_active_minutes, fill = active_type), \n           stat = \"identity\", position = \"stack\")+\n  labs(title = \"How Long People Exercise Each Day?\", \n       subtitle = \"Average very active (mins) and fairly active (mins)\")+\n  ylab(\"Average active minutes\")+\n  xlab(NULL)+\n  ylim(0,60)+\n  plot_style","metadata":{"execution":{"iopub.status.busy":"2021-09-02T07:30:43.549771Z","iopub.execute_input":"2021-09-02T07:30:43.551183Z","iopub.status.idle":"2021-09-02T07:30:43.973338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Type of Users\nAccording to the 2018 Physical Activity Guidelines for Americans, 2nd edition, adults should do at least 150 minutes to 300 minutes a week of moderate-intensity, or 75 minutes to 150 minutes a week of vigorous-intensity aerobic physical activity.\n\nA general rule of thumb is that 2 minutes of moderate-intensity activity counts the same as 1 minute of vigorous-intensity activity.\n\nIn this case, \n*     **very_active = Vigorous Intensity**\n*     **fairly_active = Moderate Intensity**. \n  \n  *Reference: Fitbase data dictionary*\n\nFrom above information, we can combine very_active_miutes and fairly_active_minutes as Equivalent moderate minute eq_moderate_mins.","metadata":{}},{"cell_type":"code","source":"weekly_active_mins <- \n  summarise(group_by(daily, id, week_no), \n                           very_active_mins = sum(very_active_minutes), \n                           fairly_active_mins = sum(fairly_active_minutes)) %>% \n  mutate(eq_moderate_mins = 2 * very_active_mins + fairly_active_mins)\nhead(weekly_active_mins)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T07:30:43.975844Z","iopub.execute_input":"2021-09-02T07:30:43.977749Z","iopub.status.idle":"2021-09-02T07:30:44.019496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Then, grouping participant based on their weekly active minutes. The result shows that there are 3 types of participants:\n\n* __High active__ = weekly active > 300 minutes\n\n* __Medium active__ = weekly active > 150 minutes\n\n* __Low active__ = weekly active < 150 minutes","metadata":{}},{"cell_type":"code","source":"participant_type_df <- \n  summarise(group_by(weekly_active_mins, id),\n                            average_active_mins = mean(eq_moderate_mins)) %>% \n  mutate(active = ifelse(average_active_mins > 300, \"high\",\n                          ifelse(average_active_mins > 150, \"medium\", \"low\")))\n\ngroup_by(participant_type_df, active) %>% \n  summarise(total = n()) %>% \n  mutate(ratio = total/sum(total))","metadata":{"execution":{"iopub.status.busy":"2021-09-02T07:30:44.022242Z","iopub.execute_input":"2021-09-02T07:30:44.023742Z","iopub.status.idle":"2021-09-02T07:30:44.068969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n\n**How Active people Walks.**","metadata":{}},{"cell_type":"code","source":"daily_df_merge <- merge(daily, participant_type_df, by = \"id\") %>% \n  select(id, activity_date, week_day, total_steps, calories, active) %>% \n  group_by(active) %>% \n  summarise(average_steps = mean(total_steps), average_cal = mean(calories))\n    \nggplot(daily_df_merge)+\n  geom_bar(aes(active, average_steps), stat = \"identity\", fill = \"royalblue3\")+\n  labs(title = \"How Active People Walk?\", subtitle = \"Active type VS average daily steps\")+\n  xlab(\"Active type\")+\n  ylab(\"Average daily steps\")+\n  ylim(0, 12000)+\n  plot_style","metadata":{"execution":{"iopub.status.busy":"2021-09-02T07:30:44.071227Z","iopub.execute_input":"2021-09-02T07:30:44.072573Z","iopub.status.idle":"2021-09-02T07:30:44.308285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**How much Calories do Active peope Burn ?**","metadata":{}},{"cell_type":"code","source":"ggplot(daily_df_merge)+\n  geom_bar(aes(active, average_cal), stat = \"identity\", fill = \"brown2\")+\n  labs(title = \"How Many Calories Active People Burn?\", subtitle = \"Active type VS average daily calories\")+\n  xlab(\"Activity type\")+\n  ylab(\"Average calories\")+\n  ylim(0, 3000)+\n  plot_style","metadata":{"execution":{"iopub.status.busy":"2021-09-02T07:30:44.311906Z","iopub.execute_input":"2021-09-02T07:30:44.31344Z","iopub.status.idle":"2021-09-02T07:30:44.54242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Active People excercise time**","metadata":{}},{"cell_type":"code","source":"merge(hourly, participant_type_df) %>% \n  ggplot()+\n  stat_summary(aes(time, calories, color = active), size = 1, fun = mean, geom = \"line\")+\n  labs(title = \"When do Active People Exercise?\", \n       subtitle = \"Average hourly calories burn VS time of days\", \n       x = \"Time of day\", y = \"Average hourly calories\")+\n  plot_style","metadata":{"execution":{"iopub.status.busy":"2021-09-02T07:36:35.599031Z","iopub.execute_input":"2021-09-02T07:36:35.600929Z","iopub.status.idle":"2021-09-02T07:36:36.178471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Key Findings.\n* Active people takes more than 10K  steps each day.\n* Normal people takes about 7.5K steps each day.\n* Less Active people takes about 5K steps each day.\n* Active people burn more calories than their normal everyday intake.\n* People are most likely to go for exercise in between ( 04:00 PM )- ( 06:00 PM ).\n","metadata":{}},{"cell_type":"markdown","source":"# Insights\n*Get Healthier With Bellabeat*\n\n* According to the 2018 Physical Activity Guidelines for Americans, 2nd edition, adults should do at least 150 minutes to 300 minutes a week of moderate-intensity. The results show that 25% of customer below the recommendation, 37.5% follow the 150 minutes recommendation and another 37.5% above 300 minutes a week. By adding notification about this data, Bellabeat apps could help improve customer healthy.\n\n* Less Active people takes about 5K steps each day. May be they use car, bike or any other vehicals  for going to office or they less likely to take morning walks.Since all data is from women, so there are chances that low active persons are housewives and ususaly remain indoor and thus their steps are not recorded. So we can focus on them , and motivate or acknowledge them about daily workout.\n\n* Active people burn more calories than their normal everyday intake. They might be targeting to weight loss and are also they are activily participating in daily excersise, and morning walks. We can assist them with proper guidens in our fitness program. \n\n* Most  of women's excerice time is between ( 04:00 PM )- ( 06:00 PM ). So, we can focus our advertisements or programs from 10:00 AM to 04:00 PM. So that our advertisements or workout programs could make bigger impact before they prepare for their daily workout session, if we focused our ads in night time, then its possible that they would notice it , but would forget it by next morning.\n","metadata":{}},{"cell_type":"markdown","source":"---\n\n\n\n\n*Have questions or comments? Visit the [Learn Discussion forum](https://www.kaggle.com/learn-forum/161321) to chat with other Learners.*","metadata":{}}]}