library('tidyr')
library('readr')
library('dplyr')
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library('ggplot2')
library('forcats')
library('tidyquant')
## Loading required package: lubridate
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## ######################### Warning from 'xts' package ##########################
## #                                                                             #
## # The dplyr lag() function breaks how base R's lag() function is supposed to  #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or       #
## # source() into this session won't work correctly.                            #
## #                                                                             #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop           #
## # dplyr from breaking base R's lag() function.                                #
## #                                                                             #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning.  #
## #                                                                             #
## ###############################################################################
## 
## Attaching package: 'xts'
## The following objects are masked from 'package:dplyr':
## 
##     first, last
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
responses <- read.csv("/cloud/project/multipleChoiceResponses.csv", header = TRUE, stringsAsFactors = FALSE)
## Warning in scan(file = file, what = what, sep = sep, quote = quote, dec = dec,
## : EOF within quoted string
selected_columns <- responses %>%
  select(
    starts_with("Learning"),
    starts_with("Working"),
    starts_with("Age"),
    starts_with("EmploymentStatus"),
    starts_with("CurrentJob"),
    starts_with("MLMethod"),
    starts_with("Formal")
  )

char_columns <- sapply(selected_columns, is.character)
selected_columns[, char_columns] <- lapply(selected_columns[, char_columns], as.factor)

# 1.2
factor_levels <- data.frame(
  variable = names(selected_columns),
  num_levels = sapply(selected_columns, nlevels)
)

# 1.2
print(factor_levels)
##                                                                        variable
## LearningDataScience                                         LearningDataScience
## LearningPlatformSelect                                   LearningPlatformSelect
## LearningPlatformUsefulnessArxiv                 LearningPlatformUsefulnessArxiv
## LearningPlatformUsefulnessBlogs                 LearningPlatformUsefulnessBlogs
## LearningPlatformUsefulnessCollege             LearningPlatformUsefulnessCollege
## LearningPlatformUsefulnessCompany             LearningPlatformUsefulnessCompany
## LearningPlatformUsefulnessConferences     LearningPlatformUsefulnessConferences
## LearningPlatformUsefulnessFriends             LearningPlatformUsefulnessFriends
## LearningPlatformUsefulnessKaggle               LearningPlatformUsefulnessKaggle
## LearningPlatformUsefulnessNewsletters     LearningPlatformUsefulnessNewsletters
## LearningPlatformUsefulnessCommunities     LearningPlatformUsefulnessCommunities
## LearningPlatformUsefulnessDocumentation LearningPlatformUsefulnessDocumentation
## LearningPlatformUsefulnessCourses             LearningPlatformUsefulnessCourses
## LearningPlatformUsefulnessProjects           LearningPlatformUsefulnessProjects
## LearningPlatformUsefulnessPodcasts           LearningPlatformUsefulnessPodcasts
## LearningPlatformUsefulnessSO                       LearningPlatformUsefulnessSO
## LearningPlatformUsefulnessTextbook           LearningPlatformUsefulnessTextbook
## LearningPlatformUsefulnessTradeBook         LearningPlatformUsefulnessTradeBook
## LearningPlatformUsefulnessTutoring           LearningPlatformUsefulnessTutoring
## LearningPlatformUsefulnessYouTube             LearningPlatformUsefulnessYouTube
## LearningDataScienceTime                                 LearningDataScienceTime
## LearningCategorySelftTaught                         LearningCategorySelftTaught
## LearningCategoryOnlineCourses                     LearningCategoryOnlineCourses
## LearningCategoryWork                                       LearningCategoryWork
## LearningCategoryUniversity                           LearningCategoryUniversity
## LearningCategoryKaggle                                   LearningCategoryKaggle
## LearningCategoryOther                                     LearningCategoryOther
## Age                                                                         Age
## EmploymentStatus                                               EmploymentStatus
## CurrentJobTitleSelect                                     CurrentJobTitleSelect
## MLMethodNextYearSelect                                   MLMethodNextYearSelect
## FormalEducation                                                 FormalEducation
##                                         num_levels
## LearningDataScience                            444
## LearningPlatformSelect                        3435
## LearningPlatformUsefulnessArxiv                430
## LearningPlatformUsefulnessBlogs                421
## LearningPlatformUsefulnessCollege              405
## LearningPlatformUsefulnessCompany              455
## LearningPlatformUsefulnessConferences          434
## LearningPlatformUsefulnessFriends              421
## LearningPlatformUsefulnessKaggle               440
## LearningPlatformUsefulnessNewsletters          391
## LearningPlatformUsefulnessCommunities          428
## LearningPlatformUsefulnessDocumentation        433
## LearningPlatformUsefulnessCourses              410
## LearningPlatformUsefulnessProjects             446
## LearningPlatformUsefulnessPodcasts             453
## LearningPlatformUsefulnessSO                   414
## LearningPlatformUsefulnessTextbook             450
## LearningPlatformUsefulnessTradeBook            427
## LearningPlatformUsefulnessTutoring             433
## LearningPlatformUsefulnessYouTube              434
## LearningDataScienceTime                        419
## LearningCategorySelftTaught                    459
## LearningCategoryOnlineCourses                  451
## LearningCategoryWork                           462
## LearningCategoryUniversity                     473
## LearningCategoryKaggle                         445
## LearningCategoryOther                          468
## Age                                            489
## EmploymentStatus                               420
## CurrentJobTitleSelect                          425
## MLMethodNextYearSelect                         475
## FormalEducation                                452
# 1.2
top5_levels <- factor_levels %>%
  arrange(desc(num_levels)) %>%
  head(5)

# 1.2
print(top5_levels)
##                                              variable num_levels
## LearningPlatformSelect         LearningPlatformSelect       3435
## Age                                               Age        489
## MLMethodNextYearSelect         MLMethodNextYearSelect        475
## LearningCategoryUniversity LearningCategoryUniversity        473
## LearningCategoryOther           LearningCategoryOther        468
# 1.3 
current_job_levels <- factor_levels %>%
  filter(variable == "CurrentJobTitleSelect")

# 1.3
print(current_job_levels)
##                                    variable num_levels
## CurrentJobTitleSelect CurrentJobTitleSelect        425
# 1.4 
current_job_levels <- factor_levels %>%
  filter(variable == "CurrentJobTitleSelect")

print(current_job_levels)
##                                    variable num_levels
## CurrentJobTitleSelect CurrentJobTitleSelect        425
library(ggplot2)

employer_industry_plot <- ggplot(responses, aes(x = EmployerIndustry)) +
  geom_bar() +
  coord_flip()
#question_2
#2.1
library(ggplot2)

responses <- read.csv("/cloud/project/multipleChoiceResponses.csv", header = TRUE, stringsAsFactors = FALSE)
## Warning in scan(file = file, what = what, sep = sep, quote = quote, dec = dec,
## : EOF within quoted string
employer_industry_plot <- ggplot(responses, aes(x = EmployerIndustry)) +
  geom_bar(stat = "count") +
  coord_flip()

print(employer_industry_plot)

#2.2
library(ggplot2)

responses <- read.csv("/cloud/project/multipleChoiceResponses.csv", header = TRUE, stringsAsFactors = FALSE)
## Warning in scan(file = file, what = what, sep = sep, quote = quote, dec = dec,
## : EOF within quoted string
filtered_responses <- na.omit(responses[c("Age", "EmployerIndustry")])

filtered_plot <- ggplot(filtered_responses, aes(x = EmployerIndustry)) +
  geom_bar(stat = "count") +
  coord_flip()

print(filtered_plot)