"""
# Background

"""
"""
##### This project is a part of the Google Data Analytics Certificate program. For this project I have used the publically available data presented by "Motivate International Inc." refered by the project instruction menaul. 
"""
"""
##### This project will cover all six data analysis stages,
* Ask
* Prepare
* Process
* Analyze
* Share
* Act
"""
"""
# ASK***
"""
"""
##### The first stage "ASK" has 2 key tasks 

1. **Identifying the business task**

    Cyclistic is already a well-known bike-sharing company that is operating for quite a few years now. Right now it has 2 types of users Annual Members and Causal(non-member users). According to financial analysts, the annual members are much more profitable than the causal riders. According to the marketing director Lily Moreno, maximization of the annual membership is key to long-term success. The aim of the task is to design marketing strategies that will convert casual riders to annal members. 
    
2. **Consider key satakeholders**

     The marketing director Lily Moreno, assigned me to identify how these two different types of users use Cyclistic bikes differently. The Cyclistic marketing analytics team is responsible for creating cyclistic marketing strategies by collecting, analyzing, and reporting data. Finally, the marketing executive team will decide whether or not the new marketing strategy will be implemented or not. 
     
   **Deliverable**

       A clear statement of the business task. The business taks is to provide insight to the marketing analyst team by analyzing the Cyclistic historical bike trip data to better understand how annual members and casual riders differ, why casual riders would buy a membership, and how digital media could affect their marketing tactics.
 
   
"""
"""
# PREPARE***
"""
"""
##### There are 4 key tasks for the preparation stage. Download data and store it appropriately, Identify how it’s organized, Sort and filter the data, and Determine the credibility of the data. For this project, I have used the publically available data presented by "Motivate International Inc." I have downloaded the latest 12 months Cyclistic bike-sharing Zipped data according to the instruction and up-loaded it into Kaggle under the name of "bike-share capstone project". The latest 12 months data seems to be organized. However, there are some minor changes and missing values which will be identified. From the huge data set, we have already shorted and filtered the latest 12 months' data. Determine the credibility of the data. Data was imported from https://divvy-tripdata.s3.amazonaws.com/index.html which is a credible source. 

**Deliverable**
   
  A description of all data sources used. The monthly .csv files as data (April 2020 to March 2021) has been imported from https://divvy-tripdata.s3.amazonaws.com/index.html.



"""
# This R environment comes with many helpful analytics packages installed
# It is defined by the kaggle/rstats Docker image: https://github.com/kaggle/docker-rstats
# For example, here's a helpful package to load

library(tidyverse) # metapackage of all tidyverse packages

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

list.files(path = "../input/bike-share-capstone-project")

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
"""
### Installing packages
"""
install.packages("tidyverse")
install.packages("lubridate")
install.packages("janitor")
install.packages("ggplot2")
"""
### Installing libraries 
"""
library(tidyverse)
library(lubridate)
library(janitor)
library(ggplot2)
library(data.table)
library(dplyr)
"""
### Modifying R options to disable scientific notation in advance
"""
options(scipen = 999)
"""
### Importing the .csv data 

"""
td1 <- read_csv("../input/bike-share-capstone-project/202004-divvy-tripdata.csv")
td2 <- read_csv("../input/bike-share-capstone-project/202005-divvy-tripdata.csv")
td3 <- read_csv("../input/bike-share-capstone-project/202006-divvy-tripdata.csv")
td4 <- read_csv("../input/bike-share-capstone-project/202007-divvy-tripdata.csv")
td5 <- read_csv("../input/bike-share-capstone-project/202008-divvy-tripdata.csv")
td6 <- read_csv("../input/bike-share-capstone-project/202009-divvy-tripdata.csv")
td7 <- read_csv("../input/bike-share-capstone-project/202010-divvy-tripdata.csv")
td8 <- read_csv("../input/bike-share-capstone-project/202011-divvy-tripdata.csv")
td9 <- read_csv("../input/bike-share-capstone-project/202012-divvy-tripdata.csv")
td10 <- read_csv("../input/bike-share-capstone-project/202101-divvy-tripdata.csv")
td11 <- read_csv("../input/bike-share-capstone-project/202102-divvy-tripdata.csv")
td12 <- read_csv("../input/bike-share-capstone-project/202103-divvy-tripdata.csv")
"""
##### Note: "td" stands for "trip data" and numbers 1,2,3...12 stands for months accordingly, so "td1" means trip data "202004-divvy-tripdata.csv" and all the other numbers goes accordingly
"""
"""
# PROCESS***
"""
"""
##### I choose R as the tool for this analytic project as it contains really big amount of data. R is a strong tool to handle enormous data and for smooth visualization.  
"""
"""
### Checking for inconsistency by examining the data structure 
"""
str(td1)
str(td2)
str(td3)
str(td4)
str(td5)
str(td6)
str(td7)
str(td8)
str(td9)
str(td10)
str(td11)
str(td12)
"""
#### Result: after conducting the examination on the data structures the inconsistency has been found. The "start_station_id" and "end_station_id" suppose to be character, but in td1, td2, td3, td4, td5, td6, td7,and td8 they are numeric and that need to be fixed.
"""
"""
### Transforming"start_station_id" and "end_station_id" data type to character
"""
td1 <- mutate(td1,start_station_id = as.character(start_station_id))
td2 <- mutate(td2,start_station_id = as.character(start_station_id))
td3 <- mutate(td3,start_station_id = as.character(start_station_id))
td4 <- mutate(td4,start_station_id = as.character(start_station_id))
td5 <- mutate(td5,start_station_id = as.character(start_station_id))
td6 <- mutate(td6,start_station_id = as.character(start_station_id))
td7 <- mutate(td7,start_station_id = as.character(start_station_id))
td8 <- mutate(td8,start_station_id = as.character(start_station_id))


td1 <- mutate(td1,end_station_id = as.character(end_station_id))
td2 <- mutate(td2,end_station_id = as.character(end_station_id))
td3 <- mutate(td3,end_station_id = as.character(end_station_id))
td4 <- mutate(td4,end_station_id = as.character(end_station_id))
td5 <- mutate(td5,end_station_id = as.character(end_station_id))
td6 <- mutate(td6,end_station_id = as.character(end_station_id))
td7 <- mutate(td7,end_station_id = as.character(end_station_id))
td8 <- mutate(td8,end_station_id = as.character(end_station_id))
"""
### Combining all 12 months data into one data frame
"""
trip_data <- rbind(td1,td2,td3,td4,td5,td6,td7,td8,td9,td10,td11,td12)
"""
### Inspecting the combined data frame that has been created recently by calling list of column name
"""
colnames(trip_data) 
"""
### Removing empty rows and columns 
"""
trip_data <-janitor:: remove_empty(trip_data, which = c("rows"))
trip_data <-janitor:: remove_empty(trip_data, which = c("cols"))
"""
### Inspecting the numbers of rows in the data frame
"""
nrow(trip_data)
"""
### Inspecting the column names and data types
"""
str(trip_data)
"""
### Inspecting the statistical summery of data frame 
"""
summary(trip_data)
"""
### Inspecting the number of different member types
"""
table(trip_data$member_casual)
"""
### Renaming member_casual to rider_type to have a better understanding of the column names
"""
trip_data <- trip_data %>% rename(rider_type = member_casual)
"""
### Inspecting the rideable bikes types
"""
table(trip_data$rideable_type)
"""
### Adding date and time data into the data frame
"""
trip_data$started_at <- lubridate::ymd_hms(trip_data$started_at)
trip_data$ended_at <- lubridate::ymd_hms(trip_data$ended_at)
"""
### Adding date field to the data set for future analysis and visualization purpose 


"""
trip_data$date <- as.Date(trip_data$started_at)
"""
### Creating hour field into the data frame
"""
trip_data$start_hour <- lubridate::hour(trip_data$started_at)
trip_data$end_hour <- lubridate::hour(trip_data$ended_at)
"""
### Adding months and days of the week field in the data frame
"""
trip_data$months <- months.Date(trip_data$started_at)
trip_data$day_of_week <- weekdays.Date(trip_data$started_at)
"""
### Calculating trip length in minutes 
"""
trip_data$trip_length <- difftime(trip_data$ended_at, trip_data$started_at, units = "mins")
"""
##### Note: Trip lengths are in minutes but the data type is not numeric 
"""
"""
### Transforming trip length to numeric to ensure the execution of smooth calculation
"""
trip_data$trip_length <- as.numeric(as.character(trip_data$trip_length))
"""
### Inspecting the transformed format
"""
is.numeric(trip_data$trip_length)
"""
### Inspecting column structure in the data frame 
"""
str(trip_data)
"""
### Creating new version of the data 
"""
trip_data_V2 <- trip_data[!(trip_data$start_station_name == "HQ QR" | trip_data$trip_length < 0),]
"""
### Removing NA for further cleaning the data and creating cleaned version of the data 
"""
cleaned_trip_data <- drop_na(trip_data_V2)
"""
### Inspecting summary of the clean data
"""
summary(cleaned_trip_data)
"""
### Varying if all NA has been removed
"""
sum(is.na(cleaned_trip_data))
"""
### Calling a tibble to have a sink pick of the new data frame
"""
tibble(cleaned_trip_data)
"""
# ANALYZE***
"""
"""
##### Key Tasks

1. Aggregate your data so it’s useful and accessible.
2. Organize and format your data.
3. Perform calculations.
4. Identify trends and relationships.

**Deliverable**

A summary of your analysis
"""
"""
### Descriptive analysis of tripe_length in minutes

### Calculating the average trip time (total trip length divided by the total number of riders)
"""
mean(cleaned_trip_data$trip_length)
"""
### Calculating the longest ride 
"""
max(cleaned_trip_data$trip_length)
median(cleaned_trip_data$trip_length)
min(cleaned_trip_data$trip_length)
"""
### Summary of the trip length data
"""
summary(cleaned_trip_data$trip_length)
"""
### Finding out mean trip length according to membership type 
"""
aggregate(cleaned_trip_data$trip_length ~ cleaned_trip_data$rider_type, FUN = mean)
"""
### Finding out average trip length according to membership type
"""
aggregate(cleaned_trip_data$trip_length ~ cleaned_trip_data$rider_type, FUN = median)
"""
### Statistical summary of the trip duration according to the customer type
"""
cleaned_trip_data %>% group_by(rider_type)%>%
  summarise(min_trip_length = min(trip_length), median_trip_length = median(trip_length),
            max_trip_length = max(trip_length), mean_trip_length = mean(trip_length))
"""
### Finding out mean trip length by months and days of week according to membership type 
"""
aggregate(cleaned_trip_data$trip_length ~ cleaned_trip_data$rider_type 
          + cleaned_trip_data$day_of_week + cleaned_trip_data$months, FUN = mean)
"""
##### Note: The order of the day and months are not according to the order and we need to fix it for smooth visualization 
"""
"""
### Fixing the order of months and the day_of_week for the preparation of smooth visualization 
"""
cleaned_trip_data$months <- ordered(cleaned_trip_data$months, 
                                         levels = c("April","May","June","July",
                                                    "August","September","October",
                                                    "November","December","January","February","March"))
cleaned_trip_data$day_of_week <- ordered(cleaned_trip_data$day_of_week, 
                        levels = c("Monday","Tuesday","Wednesday",
                                   "Thursday","Friday","Saturday","Sunday"))
"""
### Checking for the test trips that company made due to quality check and R&D purpose 
"""
nrow(subset(cleaned_trip_data, start_station_name %like% "TEST"))
nrow(subset(cleaned_trip_data, start_station_name %like% "Test"))
nrow(subset(cleaned_trip_data, start_station_name %like% "test"))
"""
### Removing the test trips form data to ensure the accuracy of the analysis 
"""
cleaned_trip_data <- cleaned_trip_data[!(cleaned_trip_data$start_station_name %like% "TEST"),]
"""
### Inspection if the TEST data has been removed



"""
nrow(subset(cleaned_trip_data, start_station_name %like% "TEST"))
"""
### Checking the data frame


"""
glimpse(cleaned_trip_data)
"""
### Inspecting the distinct value 


"""
table(cleaned_trip_data$rider_type)
"""
# SHARE***
## VISUALIZATION***
"""
"""
### Finding out the demand of rideable types according to the number of trips taken bye the types of customers



"""
cleaned_trip_data %>% group_by(rideable_type, rider_type) %>% summarise(number_of_trips = n()) %>%
  ggplot(aes(x = rideable_type, y= number_of_trips, fill = rider_type)) + geom_col() +
  labs(title = "Demand of Rideable type vs number of trips base on rider types")
"""
##### According to the visualized data, Docked bikes seemed to be the most popular bike among both casual and member riders. Classic bikes and Electric bikes are popular among the member riders. If electric bikes are more expensive to ride for casual riders then the company can consider reducing the riding cost for non-menber riders to increase the demand for electric bikes at least as a campaign. That can possibly attract causal riders to become a member.     
"""
"""
### The total number of trips by hours for the 12 months including all stations


"""
cleaned_trip_data %>% count(start_hour, sort = T) %>% 
  ggplot(aes(x= start_hour, y = n)) + geom_line(size = 1) + 
  labs(title = "Count of Trips by Hours for Pervious 12 Months", x = "Start Hour of Trips",
       y = "Count of Trips")
"""
##### According to the visualized data, bike demands start rising from 500 hours and the demand keeps increasing throughout the day. The demands are at peak between 1700 to 1800 hours, after that it starts decreasing.
"""
"""
### Average trip duration in a day according to the rider type


"""
cleaned_trip_data %>% 
  group_by(rider_type, day_of_week) %>% summarise(average_trip_length = mean(trip_length)) %>%
                              ggplot(aes(x = day_of_week, y = average_trip_length, fill = rider_type)) + 
  geom_col(width = 1, position = position_dodge(width = 1)) + labs(title = "Average Trip Duration in a Day based on rider type",
                                                                   x = "Day of the Week", y = "Average Trip Duration")
"""
##### According to the visualized data, the average trip duration of casual riders seems to be more than double of member riders. That means there are more non-member users than member users. That gives a hint there is a huge amount of non-member users that can be converted as members with proper marketing. The visualization may seem like causal riders are taking longer trips than the member riders which is true to some extent but does not have to possibly be true every time. I could possibly mean there are more casual users than members. The column chart also shows that there are more users during the weekends than on weekdays.
"""
"""
### Total number of trips in a month according to the type of riders 


"""
cleaned_trip_data %>% group_by(rider_type, months) %>% summarise(number_of_trips = n()) %>% arrange(rider_type,months) %>%
  ggplot(aes(x = months, y = number_of_trips, fill = rider_type)) + 
  labs(title = "Total number of trips in a month") + geom_col(width = 1, position = position_dodge(width = 1))+
  theme(axis.text.x = element_text(angle = 30))
"""
##### According to the visualized data, among both types of users bike rides are most popular during the months of summer which is obvious. Then it starts decreasing during the Autumn. The demand is lowes during the Winter season. Then it starts increasing again during the Spring. 

##### If the maintenance cost is expensive, the company can stop their operation during the Winter-time and resume it again from Spring. By that time company can put the resource and effort more into R&D to make the service better. 
"""
"""
### Total number of trips in a day of week according to the type of riders 

"""
cleaned_trip_data %>% group_by(rider_type, day_of_week) %>% summarise(number_of_trips = n()) %>% arrange(rider_type,day_of_week) %>%
  ggplot(aes(x = day_of_week, y = number_of_trips, fill = rider_type)) + 
  labs(title = "Total number of trips in a week based on rider type") + geom_col(width = 1, position = position_dodge(width = 1))+
  theme(axis.text.x = element_text(angle = 20))
"""
##### According to the visualized data, the maximum number of trips taken by the member users during the weekdays. During the weekend the non-member users are taking almost the same amount or more rides. The number of trips taken by the member users is almost static or close during the whole week. On the other hand, the riding behavior of casual users seems to be increasing gradually from the beginning of the week and touch the peak during the weekend.
"""
"""
### Number of trips according to the rider type and trip date 


  
"""
cleaned_trip_data %>%
  group_by(rider_type,date) %>%
  summarise(number_of_rides = n(), average_duration = mean(trip_length)) %>%
  arrange(rider_type, date) %>% ggplot(aes(x= date, y= number_of_rides, group = rider_type)) + 
  geom_line(aes(color= rider_type)) + labs(title = "Number of Rides by Ride Date and Rider type", X = "Ride Date", y = "Rider Type") 

"""
##### According to the visualized data, over the whole 12 months, both member and casual riders took almost the same amount of rides on average. If we look at the graph it may seem the causal riders took more rides than the members, but if we take a closer look into the graph we can easily notice the number of trips that member users took is more constant than the non-member users. The causal riders are at peak only during the summer months where the member users are more constant to use the service through the whole 12 months compared to the casual users. 
"""
"""
### Creating .csv files by weeks and months for other visualization tools.
"""
trip_data_by_week <- aggregate(cleaned_trip_data$trip_length ~ cleaned_trip_data$rider_type + 
                                         cleaned_trip_data$day_of_week, FUN = mean)
write.csv(cleaned_trip_data, "Trip Data by Week.csv", row.names = F)
trip_data_by_months <- aggregate(cleaned_trip_data$trip_length ~ cleaned_trip_data$rider_type +
                                                + cleaned_trip_data$months, FUN = mean)
write.csv(cleaned_trip_data, "Trip Data by months.csv", row.names = F)
"""
# ACT***
"""
"""
## Key Findings

* Member riders constantly use the service throughout the week, when Causal riders are more likely to use the service during the weekends.
* Casual riders seem to have longer rides than member riders.
* For the obvious reason, both types of customers use the bikes most during the summer season and least in winter.
* The demand for the bikes is in peak at evening between 1700 to 1800 hours.
* Docked bikes are the most popular bike among both types of users compared to classic and electric bikes.
"""
"""
## Recommendations 

* The company should send out attractive promotions to attract more members.
* Off-peak hours price should be discounted (that may attract younger age group customers more in addition).
* the company can introduce different levels of membership (that can brings benefits like reduced riding or fees) that can be achieved through membership longevity. 
"""
"""
## Additional data you could lead to more deep and detail analysis

* Age group data: This could be used to understand the customers better and also can be used to stand out different promotional offers, such as off-peak hours offers.
* occupational data: This could create different kinds of membership offers, such as Student members, Career partners members (e.g. private courier services, food delivery services), and General members (that includes every one but Students and courier partners).
"""
"""
## Resources

* Google Data Analytics Professional Certificate Program
* Stake Overflow
* Kaggle Community
"""
"""
****************
"""