#
# ESI Management :: Reports
#

library(RMySQL)
#library(sqldf)
library(feather)
library(ggplot2)
library(dplyr)
library(reshape2)
library(lubridate)
library(plotly) 


# 1. Load data ------------------------------------------------------------

# 1.1. Load one "table" function
db_load <- function(table){
  rs = dbSendQuery(mydb, paste0("select * from ", table))
  df = fetch(rs, n=-1)
  write_feather(df, paste0("data/", table))  
}

# 1.2. Connection
mydb <-  dbConnect(MySQL(), user='analyst', password='exa_analyst1&', 
                   dbname='esi_management', 
                   host='lecanaldb.c12hbxfn3xzn.eu-west-1.rds.amazonaws.com',
                   port=3306)

# 1.3. Read tables 

# 1.3.1. from web
# df_entity <- db_load("wp_esi_entity")
# df_tag <- db_load("wp_esi_tag")
# df_tag_entity <- db_load("wp_esi_tag_entity")
# df_investment <- db_load("wp_esi_investment")

# 1.3.2. from cach
df_entity <- read_feather("data/wp_esi_entity")
df_tag <- read_feather("data/wp_esi_tag")
df_tag_entity <- read_feather("data/wp_esi_tag_entity")
df_investment <- read_feather("data/wp_esi_investment")

# 1.4. Close connection
#on.exit(dbDisconnect(mydb))
dbDisconnect(mydb)

# 1.5. Packages management
detach("package:RMySQL", unload=TRUE)
library(sqldf)

#
# Q1: What is a distribution of AI companies by countries ? ----
#

# Q1.1. Create data frame
df_q1 <- df_entity %>% 
  group_by(country) %>% 
  summarise(sum = n()) %>% 
  arrange(-sum) %>% 
  mutate(country = ifelse(country=="", "N/A", country))  

# Q1.2. Countries with more than 100 companies
df_q1 %>% 
  filter(sum > 100) %>% 
  ggplot(aes(reorder(country, sum), sum, fill=factor(sum))) +
  ggtitle("Countries with more than 100 companies") +
  ylab("Number of companies") +
  xlab("Countries") +
  geom_bar(stat="identity") +
  scale_fill_discrete(name = "Number") +
  theme_bw() +
  coord_flip()

# Q1.3. Countries with 10-100 companies
df_q1 %>% 
  filter(sum %in% 10:100) %>% 
  ggplot(aes(reorder(country, sum), sum, fill=factor(sum))) +
  ggtitle("Countries with 10-100 companies") +
  xlab("Countries") +
  ylab("Number of companies") +
  geom_bar(stat="identity") +
  scale_fill_discrete(name = "Number") +
  theme_bw() +
  coord_flip()

# Q1.4. Countries with 1-10 companies
df_q1 %>% 
  filter(sum %in% 1:10) %>% 
  ggplot(aes(reorder(country, sum), sum, fill=factor(sum))) +
  ggtitle("Countries with 1-10 companies") +
  xlab("Countries") +
  ylab("Number of companies") +  
  geom_bar(stat="identity") +
  scale_fill_discrete(name = "Number") +
  theme_bw() +
  coord_flip()

# Q1.5. Save data into the file
write.csv(df_q1, "q1_countries.csv", row.names = FALSE)

# Q1.6. Clean memory
rm(list=c("df_q1"))

#
# Q2: What's the investment per tags -----
#

# Q2.1. Join data
s1 <- "select df_entity.*, df_tag_entity.* from df_entity left join df_tag_entity on df_entity.id=df_tag_entity.entity_id"
s2 <- "select x.*, df_tag.* from x left join df_tag on x.tag_id = df_tag.id"
x <- sqldf(s1)
x <- sqldf(s2)

# Q2.2. Change column names
colnames(x) <- c("entity_id", "name", "description", "url", "is_investor", "country", 
                 "city", "entity_type_id", "founded_in", "employees", "acquired_by", 
                 "acquired_at", "acquired_amount", "total_raised", "latest_round_type", 
                 "facebook_link", "twitter_link", "linkedin_link", "status", "tag_id", 
                 "entity_id_1", "tag_id_1", "tag_name", "description_1", "parent_1")

# Q2.3. Select features
x <- x %>% 
  subset(select = c("entity_id", "total_raised", "tag_id")) %>% 
  filter(!is.na(tag_id))

# Q2.4. Transform data set
xt <- dcast(x, tag_id ~ total_raised)

# Q2.5. Create tags column
xt_tags <- xt %>% 
  subset(select = c("tag_id"))

# Q2.6. For one tag function
one_tag <- function(id){
  # 1. basic data set
  xt_case <- xt %>% 
    filter(tag_id == id) %>%
    subset(select=2:length(colnames(xt))) %>% 
    t()
  
  # 2. add "total_raised"
  rnames <- as.data.frame(row.names(xt_case))
  colnames(rnames) <- c("total_raised")
  
  xt_case <- cbind(rnames, xt_case)
  row.names(xt_case) <- NULL
  
  colnames(xt_case) <- c("total_raised", "sum_entities")
  
  xt_case <- xt_case %>% 
    mutate(total_raised = as.numeric(as.character(total_raised))) %>% 
    filter(!is.na(total_raised)) %>% 
    mutate(sum_total_raised = total_raised * sum_entities) %>% 
    subset(select = c("sum_total_raised"))
  
  return(xt_case)
} # one_tag

# Q2.7. init result data set
z <- one_tag(unique(xt_tags$tag_id)[1])  
colnames(z) <- c(unique(xt_tags$tag_id)[1])

# Q2.8. for loop
end_for <- unique(xt_tags$tag_id)[2:length(unique(xt_tags$tag_id))]
for(i in end_for){
  tmp <- one_tag(i)
  colnames(tmp) <- c(i)
  z <- cbind(z, tmp)
}

# Q2.9. transpose
z <- t(z)

# Q2.10. sum total_raised
xt_sum_total_raised <- as.data.frame(z) %>% 
  mutate(sum_total_raised = rowSums(.)) %>% 
  subset(select = c("sum_total_raised"))

# Q2.11. Merge data sets
xt_res <- cbind(xt_tags, xt_sum_total_raised)

# Q2.12. Add tag names
s <- "select xt_res.*, df_tag.* from xt_res left join df_tag on xt_res.tag_id = df_tag.id"
df_res_final <- sqldf(s)

df_res_final <- df_res_final %>% 
  subset(select = c("tag_id", "name", "sum_total_raised"))

#
# Q2.13. Draw graphs
#

# > 1,000,000,000
df_res_final %>% 
  filter(sum_total_raised > 1000000000) %>% 
  ggplot(aes(reorder(name, sum_total_raised), sum_total_raised, fill=factor(sum_total_raised))) +
  ggtitle("Total raised of companies per Tag (> 1,000,000,000)") +
  xlab("Tags") +
  ylab("Total raised of company") +
  geom_bar(stat="identity") +
  scale_fill_discrete(name = "Number") +
  theme_bw() +
  coord_flip()

# > 1,000,000  1,000,000,000
df_res_final %>% 
  filter((sum_total_raised > 1000000) &  (sum_total_raised < 1000000000)) %>% 
  ggplot(aes(reorder(name, sum_total_raised), sum_total_raised, fill=factor(sum_total_raised))) +
  ggtitle("Total raised of companies per Tag (1,000,000 - 1,000,000,000)") +
  xlab("Tags") +
  ylab("Total raised of company") +
  geom_bar(stat="identity") +
  scale_fill_discrete(name = "Number") +
  theme_bw() +
  coord_flip()

# < 1,000,000
df_res_final %>% 
  filter(sum_total_raised <= 1000000) %>% 
  ggplot(aes(reorder(name, sum_total_raised), sum_total_raised, fill=factor(sum_total_raised))) +
  ggtitle("Total raised of companies per Tag (< 1,000,000)") +
  xlab("Tags") +
  ylab("Total raised of company") +
  geom_bar(stat="identity") +
  scale_fill_discrete(name = "Number") +
  theme_bw() +
  coord_flip()

write.csv(df_res_final, "q2_total_raised_per_tag.csv", row.names = FALSE)

#
# Q3: What is the number of companies per tags? ----
#

# Q3.1. Join data
s1 <- "select df_entity.*, df_tag_entity.* from df_entity left join df_tag_entity on df_entity.id=df_tag_entity.entity_id"
s2 <- "select x.*, df_tag.* from x left join df_tag on x.tag_id = df_tag.id"
x <- sqldf(s1)
x <- sqldf(s2)

colnames(x)
# Q3.2. Change column names
colnames(x) <- c("entity_id", "name", "description", "url", "is_investor", "country", 
                 "city", "entity_type_id", "founded_in", "employees", "acquired_by", 
                 "acquired_at", "acquired_amount", "total_raised", "latest_round_type", 
                 "facebook_link", "twitter_link", "linkedin_link", "status", "tag_id", 
                 "entity_id_1", "tag_id_1", "tag_name", "description_1", "parent_1")

# Q3.3. Select features
x <- x %>% 
  subset(select = c("entity_id", "total_raised", "tag_id", "tag_name"))

# Q3.4. Transform data set
xt <- dcast(x, entity_id ~ tag_id)

xt[!is.na(xt)] <- 1

# change column types
xt[ , colnames(xt)] <-  apply(xt[ , colnames(xt)], 2, function(x) as.numeric(as.character(x)))

# calculate sums
xt["total", ] <- colSums(xt, na.rm = T)
xt <- xt["total", ]

# remove feature
xt$entity_id <- NULL

# transpose
xt <- t(xt)

# add "tag_id"
rnames <- as.data.frame(row.names(xt))
colnames(rnames) <- c("tag_id")

# merge
xt <- cbind(rnames, xt)

# change row names
row.names(xt) <- NULL

# add tag names
xt <- sqldf("select xt.*, df_tag.* from xt left join df_tag on xt.tag_id = df_tag.id")

# select features and arrange
xt <- xt %>% 
  subset(select = c("name", "total")) %>% 
  arrange(-total)

#
# Draw graphs
#

# > 100 companies
xt %>% 
  filter(total > 100) %>% 
  ggplot(aes(reorder(name, total), total, fill=factor(total))) +
  ggtitle("Companies per Tag (> 100)") +
  xlab("Tags") +
  ylab("Number of companies") +  
  geom_bar(stat="identity") +
  scale_fill_discrete(name = "Number") +
  theme_bw() +
  coord_flip()

# 10:100
xt %>% 
  filter(total %in% 10:100) %>% 
  ggplot(aes(reorder(name, total), total, fill=factor(total))) +
  ggtitle("Companies per Tag (10-100)") +
  xlab("Tags") +
  ylab("Number of companies") +  
  geom_bar(stat="identity") +
  scale_fill_discrete(name = "Number") +
  theme_bw() +
  coord_flip()

# 1:10
xt %>% 
  filter(total %in% 1:10) %>% 
  ggplot(aes(reorder(name, total), total, fill=factor(total))) +
  ggtitle("Companies per Tag (1-10)") +
  xlab("Tags") +
  ylab("Number of companies") +
  geom_bar(stat="identity") +
  scale_fill_discrete(name = "Number") +
  theme_bw() +
  coord_flip()

write.csv(xt, "q3_companies_per_tag.csv", row.names = FALSE)

#
# Q4: What is the average size of company per tag? -------------------------------------
# The formula for calculation per tag is
# Average size of company = Sum employees / number companies

# Q4.1. Join data
s1 <- "select df_entity.*, df_tag_entity.* from df_entity left join df_tag_entity on df_entity.id=df_tag_entity.entity_id"
s2 <- "select x.*, df_tag.* from x left join df_tag on x.tag_id = df_tag.id"
x <- sqldf(s1)
x <- sqldf(s2)

# Q4.2. Change column names
colnames(x) <- c("entity_id", "name", "description", "url", "is_investor", "country", 
                 "city", "entity_type_id", "founded_in", "employees", "acquired_by", 
                 "acquired_at", "acquired_amount", "total_raised", "latest_round_type", 
                 "facebook_link", "twitter_link", "linkedin_link", "status", "tag_id", 
                 "entity_id_1", "tag_id_1", "tag_name", "description_1", "parent_1")

# Q4.3. Select features
x <- x %>% 
  subset(select = c("entity_id", "employees", "tag_id")) %>% 
  filter(!is.na(tag_id))

# Q3.4. Transform data set
xt <- dcast(x, tag_id ~ employees)

# Q3.6. Create tags column
xt_tags <- xt %>% 
  subset(select = c("tag_id"))

#
# Q3.7. Create "sum entities"
#
xt_sum_entities <- xt %>% 
  subset(select=2:length(colnames(xt))) %>% 
  mutate(sum_entities = rowSums(.)) %>% 
  subset(select = c("sum_entities"))

#
# Q3.8. Create "sum employees"
#

# For one tag function
one_tag <- function(id){
  # 1. basic data set
  xt_case <- xt %>% 
    filter(tag_id == id) %>%
    subset(select=2:length(colnames(xt))) %>% 
    t()
  
  # 2. add "employees"
  rnames <- as.data.frame(row.names(xt_case))
  colnames(rnames) <- c("employees")
  
  xt_case <- cbind(rnames, xt_case)
  row.names(xt_case) <- NULL
  
  colnames(xt_case) <- c("employees", "sum_entities")
  
  xt_case <- xt_case %>% 
    mutate(employees = as.numeric(as.character(xt_case$employees)),
           sum_employees = employees * sum_entities) %>% 
    subset(select = c("sum_employees"))
  
  return(xt_case)
}

# init result data set
z <- one_tag(unique(xt_tags$tag_id)[1])  
colnames(z) <- c(unique(xt_tags$tag_id)[1])

# for loop
end_for <- unique(xt_tags$tag_id)[2:length(unique(xt_tags$tag_id))]
for(i in end_for){
  tmp <- one_tag(i)
  colnames(tmp) <- c(i)
  z <- cbind(z, tmp)
}

# transpose
z <- t(z)

# 1. sum employees
xt_sum_employees <- as.data.frame(z) %>% 
  mutate(sum_employees = rowSums(.)) %>% 
  subset(select = c("sum_employees"))


# Q3.9. Merge data sets
xt_res <- cbind(xt_tags, xt_sum_entities)
xt_res <- cbind(xt_res, xt_sum_employees)

# Q3.10. Calculate average size
xt_res <- xt_res %>%  
  mutate(sum_entities = as.numeric(sum_entities),
         sum_employees = as.numeric(sum_employees),
         avr_size = round(sum_employees / sum_entities)) %>% 
  subset(select = c("tag_id", "avr_size"))
  
# Q3.10. Add tag names

s <- "select xt_res.*, df_tag.* from xt_res left join df_tag on xt_res.tag_id = df_tag.id"
df_res_final <- sqldf(s)

df_res_final <- df_res_final %>% 
  subset(select = c("tag_id", "name", "avr_size"))
  
#
# Q3.11. Draw graphs
#

# > 1000
df_res_final %>% 
  filter(avr_size > 1000) %>% 
  ggplot(aes(reorder(name, avr_size), avr_size, fill=factor(avr_size))) +
  ggtitle("Average size of companies per Tag (> 1000)") +
  xlab("Tags") +
  ylab("Average number of employees") +
  geom_bar(stat="identity") +
  scale_fill_discrete(name = "Number") +
  theme_bw() +
  coord_flip()

# 100: 1000
df_res_final %>% 
  filter(avr_size %in% 100:1000) %>% 
  ggplot(aes(reorder(name, avr_size), avr_size, fill=factor(avr_size))) +
  ggtitle("Average size of companies per Tag (100 - 1000)") +
  xlab("Tags") +
  ylab("Average number of employees") +
  geom_bar(stat="identity") +
  scale_fill_discrete(name = "Number") +
  theme_bw() +
  coord_flip()

# 50: 100
df_res_final %>% 
  filter(avr_size %in% 50:100) %>% 
  ggplot(aes(reorder(name, avr_size), avr_size, fill=factor(avr_size))) +
  ggtitle("Average size of companies per Tag (50 - 100)") +
  xlab("Tags") +
  ylab("Average number of employees") +
  geom_bar(stat="identity") +
  scale_fill_discrete(name = "Number") +
  theme_bw() +
  coord_flip()

# 1: 50
df_res_final %>% 
  filter(avr_size %in% 1:50) %>% 
  ggplot(aes(reorder(name, avr_size), avr_size, fill=factor(avr_size))) +
  ggtitle("Average size of companies per Tag (1 - 50)") +
  xlab("Tags") +
  ylab("Average number of employees") +
  geom_bar(stat="identity") +
  scale_fill_discrete(name = "Number") +
  theme_bw() +
  coord_flip()

# Q3.12. Save results into the file
write.csv(df_res_final, "q4_avr_size_per_tag.csv", row.names = FALSE)

#
# Q5: What is the average age of company per tag? -----
#

# Q5.1. Join data
s1 <- "select df_entity.*, df_tag_entity.* from df_entity left join df_tag_entity on df_entity.id=df_tag_entity.entity_id"
s2 <- "select x.*, df_tag.* from x left join df_tag on x.tag_id = df_tag.id"
x <- sqldf(s1)
x <- sqldf(s2)

# Q5.2. Change column names
colnames(x) <- c("entity_id", "name", "description", "url", "is_investor", "country", 
                 "city", "entity_type_id", "founded_in", "employees", "acquired_by", 
                 "acquired_at", "acquired_amount", "total_raised", "latest_round_type", 
                 "facebook_link", "twitter_link", "linkedin_link", "status", "tag_id", 
                 "entity_id_1", "tag_id_1", "tag_name", "description_1", "parent_1")

# Q5.3. Check if a value is the date
is_date <- function(mydate, date.format = "%Y-%m-%d") {
  tryCatch(!is.na(as.Date(mydate, date.format)),  
           error = function(err) {FALSE})  
}

# fix data bugs
# x <- x %>% 
#   filter(! is_date(founded_in)) %>% 
#   subset(select = c("entity_id", "name", "founded_in")) %>% 
#   unique() %>% 
#   write.csv("bugs_founded_in.csv", row.names=F)

# Q5.4. Select features
# year > 1900
x <- x %>% 
  subset(select = c("entity_id", "founded_in", "tag_id")) %>% 
  filter(!is.na(tag_id) & is_date(founded_in) & (year(as.Date(founded_in))>1900)) %>% 
  mutate(age = interval(as.Date(founded_in), today()) / years(1)) %>% 
  mutate(age = round(age, 2)) %>% 
  subset(select = c("entity_id", "age", "tag_id"))

# Q5.5. Transform data set
xt <- dcast(x, tag_id ~ age)

# Q5.6. Create tags column
xt_tags <- xt %>% 
  subset(select = c("tag_id"))

#
# Q5.7. Create "sum entities"
#
xt_sum_entities <- xt %>% 
  subset(select=2:length(colnames(xt))) %>% 
  mutate(sum_entities = rowSums(.)) %>% 
  subset(select = c("sum_entities"))

#
# Q5.8. Create "sum age" # instead 'employees'
#

# For one tag function
one_tag <- function(id){
  # 1. basic data set
  xt_case <- xt %>% 
    filter(tag_id == id) %>%
    subset(select=2:length(colnames(xt))) %>% 
    t()
  
  # 2. add "age"
  rnames <- as.data.frame(row.names(xt_case))
  colnames(rnames) <- c("age")
  
  xt_case <- cbind(rnames, xt_case)
  row.names(xt_case) <- NULL
  
  colnames(xt_case) <- c("age", "sum_entities")
  
  xt_case <- xt_case %>% 
    mutate(age = as.numeric(as.character(xt_case$age)),
           sum_age = age * sum_entities) %>% 
    subset(select = c("sum_age"))
  
  return(xt_case)
}

# init result data set
z <- one_tag(unique(xt_tags$tag_id)[1])  
colnames(z) <- c(unique(xt_tags$tag_id)[1])

# for loop
end_for <- unique(xt_tags$tag_id)[2:length(unique(xt_tags$tag_id))]
for(i in end_for){
  tmp <- one_tag(i)
  colnames(tmp) <- c(i)
  z <- cbind(z, tmp)
}

# transpose
z <- t(z)

# 1. sum age
xt_sum_age <- as.data.frame(z) %>% 
  mutate(sum_age = rowSums(.)) %>% 
  subset(select = c("sum_age"))


# Q3.9. Merge data sets
xt_res <- cbind(xt_tags, xt_sum_entities)
xt_res <- cbind(xt_res, xt_sum_age)

# Q3.10. Calculate average age
xt_res <- xt_res %>%  
  mutate(sum_entities = as.numeric(sum_entities),
         sum_age = as.numeric(sum_age),
         avr_age = round(sum_age / sum_entities), 2) %>% 
  subset(select = c("tag_id", "avr_age"))

# Q3.10. Add tag names

s <- "select xt_res.*, df_tag.* from xt_res left join df_tag on xt_res.tag_id = df_tag.id"
df_res_final <- sqldf(s)

df_res_final <- df_res_final %>% 
  subset(select = c("tag_id", "name", "avr_age"))

#
# Q5.11. Draw graphs
#

# > 10
df_res_final %>% 
  filter(avr_age > 10) %>% 
  ggplot(aes(reorder(name, avr_age), avr_age, fill=factor(avr_age))) +
  ggtitle("Average age of companies per Tag (> 10)") +
  xlab("Tags") +
  ylab("Average age of company") +
  geom_bar(stat="identity") +
  scale_fill_discrete(name = "Number") +
  theme_bw() +
  coord_flip()

# 5:10
df_res_final %>% 
  filter(avr_age %in% 5:10) %>% 
  ggplot(aes(reorder(name, avr_age), avr_age, fill=factor(avr_age))) +
  ggtitle("Average age of companies per Tag (5 - 10)") +
  xlab("Tags") +
  ylab("Average age of company") +
  geom_bar(stat="identity") +
  scale_fill_discrete(name = "Number") +
  theme_bw() +
  coord_flip()

# 1:5
df_res_final %>% 
  filter(avr_age %in% 1:5) %>% 
  ggplot(aes(reorder(name, avr_age), avr_age, fill=factor(avr_age))) +
  ggtitle("Average age of companies per Tag (1 - 5)") +
  xlab("Tags") +
  ylab("Average age of company") +
  geom_bar(stat="identity") +
  scale_fill_discrete(name = "Number") +
  theme_bw() +
  coord_flip()

# Q5.12. Save results into the file
write.csv(df_res_final, "q5_avr_age_per_tag.csv", row.names = FALSE)

# Q6. What is the company size vs Investment (2 dimensions)? ----------------------------

# Q6.1. Extract data
s1 <- "select df_entity.*, df_tag_entity.* from df_entity left join df_tag_entity on df_entity.id=df_tag_entity.entity_id"
s2 <- "select x.*, df_tag.* from x left join df_tag on x.tag_id = df_tag.id"
x <- sqldf(s1)
x <- sqldf(s2)

# Q6.2. Change column names
colnames(x) <- c("entity_id", "name", "description", "url", "is_investor", "country", 
                 "city", "entity_type_id", "founded_in", "employees", "acquired_by", 
                 "acquired_at", "acquired_amount", "total_raised", "latest_round_type", 
                 "facebook_link", "twitter_link", "linkedin_link", "status", "tag_id", 
                 "entity_id_1", "tag_id_1", "tag_name", "description_1", "parent_1")

# Q6.3. Select features
x <- x %>% 
  subset(select = c("employees",  "total_raised"))
  
# Q6.4. Bin features

x <- x %>% 
  mutate(employees_bin = cut(employees, c(-Inf, 51, 101, 1001, Inf), 
                       labels = c("1:50", "50:100", "100:1000", ">1000")),
         total_raised_bin = cut(total_raised, c(-Inf, 1000000, 1000000000, Inf),
                        labels = c("< 1,000,000", "1,000,000 - 1,000,000,000", "> 1,000,000,000"))) %>% 
  subset(select = c("employees_bin", "total_raised_bin"))


# Q6.5. Transform data set
xt <- dcast(x, employees_bin ~ total_raised_bin)

# Q6.6. Save results into the file
write.csv(xt, "q6_company_size_vs_investment.csv", row.names = FALSE)

#
# Q8. What are the top 50 investments for "ultrasound" tag in the last 4 years? --------
#

# Q8.1. Join data
s1 <- "select df_entity.*, df_tag_entity.* from df_entity left join df_tag_entity on df_entity.id=df_tag_entity.entity_id"
s2 <- "select x.*, df_tag.* from x left join df_tag on x.tag_id = df_tag.id"
x <- sqldf(s1)
x <- sqldf(s2)

# Q8.2. Change column names
colnames(x) <- c("entity_id", "entity_name", "description", "url", "is_investor", "country", 
                 "city", "entity_type_id", "founded_in", "employees", "acquired_by", 
                 "acquired_at", "acquired_amount", "total_raised", "latest_round_type", 
                 "facebook_link", "twitter_link", "linkedin_link", "status", "tag_id", 
                 "entity_id_1", "tag_id_1", "tag_name", "description_1", "parent_1")

# Q8.3. Select features
x <- x %>% 
  subset(select = c("entity_id", "entity_name", "tag_id")) %>% 
  filter(!is.na(tag_id))

# Q8.4. Add tag names
s <- "select x.*, df_tag.name from x left join df_tag on x.tag_id = df_tag.id"
x <- sqldf(s)

# Q8.5. Set tag for search
tag_for_search <- "ultrasound"
df_by_tag <- x %>% 
  filter(tolower(name) == tag_for_search)

# Q8.6. Select investments by tag
s <- "select df_by_tag.*, df_investment.* from df_investment left join df_by_tag on df_by_tag.entity_id = df_investment.entity_id"
y <- sqldf(s)
colnames(y) <- c("entity_id", "entity_name", "tag_id", "name", "id", "amount", "currency", "investment_date",
                 "entity_id_1", "investment_type_id", "crunchbase_url")

# Q8.7. Clean and select
y <- y %>% 
  filter(!is.na(entity_id)) %>% 
  subset(select = c("entity_id", "entity_name", "tag_id", "name", "amount", "currency", "investment_date")) %>% 
  mutate(investment_year = year(investment_date)) %>% 
  #
  # select for last 4 years only
  #
  filter(investment_year %in% year(today()):(year(today()) - 4)) %>% 
  subset(select = c("entity_id", "entity_name", "tag_id", "name", "amount", "currency", "investment_year", "investment_date")) %>% 
  arrange(amount) %>% 
  unique() %>% 
  #
  # Sum by years for entity
  #
  group_by(investment_year, entity_name, name, currency) %>% 
  mutate(amount = as.numeric(amount)) %>% 
  summarise(sum_amount = sum(amount))

# Q8.8. 

# Q8._. Save results into the file
write.csv(y, "q8_investments_by_tag.csv", row.names = FALSE)



# Q9. Find and remove several duplicates for investments in the database

# Undef: Can you try to IDs of certificates sometimes the date is not exactly 
# the same could be one day before one day after your mouth or soup be somewhat similar


