#load required packages for analysis
library(ggplot2)
library(readr)
library(dplyr)

# Read train and Test data files
train <- read_csv('../input/train.csv')
glimpse(train)
test <- read_csv('../input/test.csv')
glimpse(test)
Business_Counts <- train %>% 
    group_by(place_id) %>% 
    summarize(Count=length(x)) %>% 
    arrange(-Count) 
ggplot(Business_Counts,aes(x=as.factor(place_id)),y=Count) + geom_bar()
# take top ten business
Top10_Business <- Business_Counts %>% 
    top_n(n=10)

head(Top10_Business)
# Get only top business data from train 
Top10_Business_Data <- train %>% 
    inner_join(Top10_Business, by = 'place_id') %>% 
    select(-row_id,-Count) %>% mutate(hour = floor((time/60)) %% 24,
                           qturday = floor(time+120 / (6*60)) %% 4,
                           weekday = floor(time/5040) %% 7,
                           month = floor(time/43200) %% 30)

head(Top10_Business_Data)
# Check how distributed the business
Top10_Business_Data %>% 
    mutate(place_id = as.factor(place_id)) %>%
    ggplot(aes(x, y )) +
        geom_point(aes(color = place_id,size = 1 / accuracy), alpha = 0.6) + 
        theme_minimal() +
        xlim(0, 10) + 
        ylim(0, 10) + 
        theme(legend.position = "none") +
        ggtitle("Check-ins colored by place_id")
Top10_Business_Data %>% 
  mutate(place_id = as.factor(place_id)) %>%
  ggplot(aes(as.numeric(time), y )) +
  geom_point(aes(color = place_id,size = 1 / accuracy), alpha = 0.6) + 
  theme_minimal() +
  #xlim(0, 10) + 
  ylim(0, 10) + 
  theme(legend.position = "none") +
  ggtitle("Check-ins colored by place_id")

Top10_Business_Data %>% 
  mutate(place_id = as.factor(place_id)) %>%
  ggplot(aes(as.numeric(hour), y )) +
  geom_point(aes(color = place_id,size = 1 / accuracy), alpha = 0.6) + 
  theme_minimal() +
  #xlim(0, 10) + 
  ylim(0, 10) + 
  theme(legend.position = "none") +
  ggtitle("Check-ins colored by place_id")

Top10_Business_Data %>% 
  mutate(place_id = as.factor(place_id)) %>%
  ggplot(aes(as.numeric(qturday), y )) +
  geom_point(aes(color = place_id,size = 1 / accuracy), alpha = 0.6) + 
  theme_minimal() +
  #xlim(0, 10) + 
  ylim(0, 10) + 
  theme(legend.position = "none") +
  ggtitle("Check-ins colored by place_id")

Top10_Business_Data %>% 
  mutate(place_id = as.factor(place_id)) %>%
  ggplot(aes(as.numeric(weekday), y )) +
  geom_point(aes(color = place_id,size = 1 / accuracy), alpha = 0.6) + 
  theme_minimal() +
  #xlim(0, 10) + 
  ylim(0, 10) + 
  theme(legend.position = "none") +
  ggtitle("Check-ins colored by place_id")


Top10_Business_Data %>%   
  filter(place_id == 8772469670) %>%
  # mutate(place_id = as.factor(place_id)) %>%
  ggplot(aes(as.numeric(time), y )) +
  geom_point(aes(color = place_id,size = 1 / accuracy), alpha = 0.6) + 
  theme_minimal() +
  #xlim(0, 10) + 
  #ylim(0, 10) + 
  theme(legend.position = "none") +
  ggtitle("Check-ins colored by place_id")

# feature extraction
Top10_Business_Data <- Top10_Business_Data %>% 
    mutate(hour = (time/60) %% 24 , day = (time/1440), weekday = (time/1440) %% 7) 
summary(Top10_Business_Data)
Sample_Business_IDs <- Business_Counts %>% 
    sample_n(10)

print(Sample_Business_IDs)

# Get only top business data from train 
Sampled_BusinessIDs_Data <- train %>% 
    inner_join(Sample_Business_IDs, by = 'place_id') %>% 
    select(-row_id,-Count)
head(Top10_Business_Data)
