packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer", 
              "fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr")
              
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)

lapply(packages, library, character.only = TRUE)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## Loading required package: viridisLite
## 
## 
## Attaching package: 'kableExtra'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     group_rows
## [[1]]
##  [1] "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"     "readr"    
##  [7] "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics" 
## [13] "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[2]]
##  [1] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
##  [6] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [11] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [16] "datasets"     "methods"      "base"        
## 
## [[3]]
##  [1] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
##  [6] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [11] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [16] "datasets"     "methods"      "base"        
## 
## [[4]]
##  [1] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
##  [6] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [11] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [16] "utils"        "datasets"     "methods"      "base"        
## 
## [[5]]
##  [1] "fst"          "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
##  [6] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [11] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [16] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[6]]
##  [1] "viridis"      "viridisLite"  "fst"          "RColorBrewer" "modelsummary"
##  [6] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [11] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [16] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [21] "methods"      "base"        
## 
## [[7]]
##  [1] "knitr"        "viridis"      "viridisLite"  "fst"          "RColorBrewer"
##  [6] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
## [11] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [16] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [21] "datasets"     "methods"      "base"        
## 
## [[8]]
##  [1] "kableExtra"   "knitr"        "viridis"      "viridisLite"  "fst"         
##  [6] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
## [11] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [16] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [21] "utils"        "datasets"     "methods"      "base"        
## 
## [[9]]
##  [1] "rmarkdown"    "kableExtra"   "knitr"        "viridis"      "viridisLite" 
##  [6] "fst"          "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
## [11] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [16] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [21] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[10]]
##  [1] "ggridges"     "rmarkdown"    "kableExtra"   "knitr"        "viridis"     
##  [6] "viridisLite"  "fst"          "RColorBrewer" "modelsummary" "lubridate"   
## [11] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [16] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [21] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [26] "base"        
## 
## [[11]]
##  [1] "ggridges"     "rmarkdown"    "kableExtra"   "knitr"        "viridis"     
##  [6] "viridisLite"  "fst"          "RColorBrewer" "modelsummary" "lubridate"   
## [11] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [16] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [21] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [26] "base"        
## 
## [[12]]
##  [1] "questionr"    "ggridges"     "rmarkdown"    "kableExtra"   "knitr"       
##  [6] "viridis"      "viridisLite"  "fst"          "RColorBrewer" "modelsummary"
## [11] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [16] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [21] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [26] "methods"      "base"
setwd("C:/Users/jpcha/Desktop/SOC202")

library(fst)

ess <- read_fst ("All-ESS-Data.fst")

##Task one:

table(ess$essround)
## 
##     1     2     3     4     5     6     7     8     9    10 
## 42359 47537 43000 56752 52458 54673 40185 44387 49519 59685
ess$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
  ess$year[ess$essround == i] <- replacements[i]
}

table(ess$nwsptot) 
## 
##     0     1     2     3     4     5     6     7    77    88    99 
## 70283 73250 65009 18684  7635  2890  1440  2004    36   620   255
#Newspaper reading, total time on average weekday  
#results: 
##     0     1     2     3     4     5     6     7    77    88    99 
## 70283 73250 65009 18684  7635  2890  1440  2004    36   620   255 

table(ess$rdtot)
## 
##     0     1     2     3     4     5     6     7    77    88    99 
## 58733 36957 38032 19077 16108 10644  9737 51596    34   978   210
#Radio listening, total time on average weekday
#results:
##     0     1     2     3     4     5     6     7    77    88    99 
## 58733 36957 38032 19077 16108 10644  9737 51596    34   978   210 

table(ess$tvtot)
## 
##     0     1     2     3     4     5     6     7    77    88    99 
## 13055 18377 45705 46056 55550 43202 40829 73214    46   787   143
#TV watching, total time on average weekday
#results:
##    0     1     2     3     4     5     6     7    77    88    99 
##13055 18377 45705 46056 55550 43202 40829 73214    46   787   143 

spain_data <- ess %>%
  filter(cntry == "ES") %>%
  mutate(
    nwsptot = ifelse(nwsptot %in% c(77, 88, 99), NA, nwsptot),
    rdtot = ifelse(rdtot %in% c(77, 88, 99), NA, rdtot),
    tvtot = ifelse(tvtot %in% c(77, 88, 99), NA, tvtot),
  )

#Checking my work:

table(spain_data$nwsptot)
## 
##    0    1    2    3    4    5    6    7 
## 4638 2689 1777  393  115   51   19   31
#results:
##    0    1    2    3    4    5    6    7 
## 4638 2689 1777  393  115   51   19   31 

table(spain_data$rdtot)
## 
##    0    1    2    3    4    5    6    7 
## 3378 1298 1417  818  562  390  313 1541
#results: 
##    0    1    2    3    4    5    6    7 
## 3378 1298 1417  818  562  390  313 1541

table(spain_data$tvtot)
## 
##    0    1    2    3    4    5    6    7 
##  364  836 2251 2211 2251 1839 1287 2494
#results: 
##   0    1    2    3    4    5    6    7 
## 364  836 2251 2211 2251 1839 1287 2494

#My Summary Data Table:

datasummary_skim(spain_data %>% select(nwsptot, rdtot, tvtot))
Unique (#) Missing (%) Mean SD Min Median Max
nwsptot 9 50 0.9 1.1 0.0 1.0 7.0
rdtot 9 50 2.4 2.6 0.0 2.0 7.0
tvtot 9 30 4.1 2.0 0.0 4.0 7.0

#I notice that 50% of newspaper and radio, and 30% of tv respondents answered either “refusal,” “don’t know,” or “no answer”. These are the missing variables. I also notice that tvtot have the highest mean and median and nwsptot has the lowest mean and median, which does not surprise me. I could have guessed that people spend more time watching tv than reading the newspaper.

##Task two:

totaltime_by_year <- spain_data %>%
  group_by(year) %>%
  summarize(mean_totaltime = mean(tvtot, na.rm = TRUE))
totaltime_by_year
## # A tibble: 10 × 2
##     year mean_totaltime
##    <dbl>          <dbl>
##  1  2002           4.53
##  2  2004           4.22
##  3  2006           4.00
##  4  2008           4.02
##  5  2010           3.95
##  6  2012           3.92
##  7  2014           4.08
##  8  2016         NaN   
##  9  2018         NaN   
## 10  2020         NaN
ggplot(totaltime_by_year, aes(x = year, y = mean_totaltime)) +
  geom_line(color = "purple", size = 1) + 
  geom_point(color = "hotpink", size = 3) + 
  labs(title = "Total Tv-Watching Time on Average Weekday (2002-2020)", 
       x = "Survey Year", 
       y = "Total Time (0-7)") +
  ylim(0, 7) +  
  theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Removed 3 rows containing missing values (`geom_line()`).
## Warning: Removed 3 rows containing missing values (`geom_point()`).

#To my surprise, time spent watching tv has decreased. This might be due to new forms of entertainment becoming more popular.

##Task three:

ess_selected <- ess %>%
  filter(cntry %in% c("ES", "GB", "FR")) %>%
  mutate(tvtot = ifelse(tvtot %in% c(77, 88, 99), NA, tvtot))


task3plot <- ggplot(ess_selected, aes(x = reorder(cntry, -tvtot, FUN=mean), y = tvtot, fill = cntry)) +
  geom_boxplot() +
  theme_minimal() + 
  theme(legend.position = "none") + 
  labs(title = "Boxplot comparison for total tv-watching time (Spain, UK, France)", 
       x = "Country", 
       y = "Scale (0-7)")

task3plot
## Warning: Removed 17299 rows containing non-finite values (`stat_boxplot()`).

#As I would have guessed, the UK spends the most time watching tv compared to Spain and France.

##Task four:

spain_data <- spain_data %>%
  mutate(
    edulvla = case_when(
      essround < 5 & edulvla == 55 ~ NA_real_,
      TRUE ~ edulvla
    ),
    
    edulvlb = case_when(
      essround >= 5 & edulvlb == 5555 ~ NA_real_,
      TRUE ~ edulvlb
    ),

    educ_level = case_when(
      essround < 5 & edulvla == 5 ~ "BA",
      essround >= 5 & edulvlb > 600 ~ "BA",
      TRUE ~ "No BA"
    )
  )

table(spain_data$educ_level)
## 
##    BA No BA 
##  4203 15249
tvtotedu <- datasummary_crosstab(tvtot ~ educ_level, data = spain_data)
tvtotedu
tvtot BA No BA All
0 N 94 270 364
% row 25.8 74.2 100.0
1 N 229 607 836
% row 27.4 72.6 100.0
2 N 589 1662 2251
% row 26.2 73.8 100.0
3 N 522 1689 2211
% row 23.6 76.4 100.0
4 N 448 1803 2251
% row 19.9 80.1 100.0
5 N 315 1524 1839
% row 17.1 82.9 100.0
6 N 162 1125 1287
% row 12.6 87.4 100.0
7 N 176 2318 2494
% row 7.1 92.9 100.0
All N 4203 15249 19452
% row 21.6 78.4 100.0
table(spain_data$tvtot, spain_data$educ_level) %>%
  cprop()
##        
##         BA    No BA All  
##   0       3.7   2.5   2.7
##   1       9.0   5.5   6.2
##   2      23.2  15.1  16.6
##   3      20.6  15.4  16.3
##   4      17.7  16.4  16.6
##   5      12.4  13.9  13.6
##   6       6.4  10.2   9.5
##   7       6.9  21.1  18.4
##   Total 100.0 100.0 100.0

#second socio-demographic varibale:

spain_data <- spain_data %>%
  mutate(religion = case_when(
    rlgblg == 2 ~ "No",
    rlgblg == 1 ~ "Yes",
    rlgblg %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(rlgblg)
  ))

# checking my work:
table(spain_data$religion)
## 
##    No   Yes 
##  5222 11857
tvtotrlg <- datasummary_crosstab(tvtot ~ religion, data = spain_data)
tvtotrlg
tvtot No Yes All
0 N 159 202 364
% row 43.7 55.5 100.0
1 N 294 537 836
% row 35.2 64.2 100.0
2 N 777 1461 2251
% row 34.5 64.9 100.0
3 N 733 1469 2211
% row 33.2 66.4 100.0
4 N 666 1568 2251
% row 29.6 69.7 100.0
5 N 504 1328 1839
% row 27.4 72.2 100.0
6 N 320 962 1287
% row 24.9 74.7 100.0
7 N 510 1973 2494
% row 20.4 79.1 100.0
All N 5222 11857 19452
% row 26.8 61.0 100.0
table(spain_data$tvtot, spain_data$religion) %>%
  cprop()
##        
##         No    Yes   All  
##   0       4.0   2.1   2.7
##   1       7.4   5.7   6.2
##   2      19.6  15.4  16.6
##   3      18.5  15.5  16.4
##   4      16.8  16.5  16.6
##   5      12.7  14.0  13.6
##   6       8.1  10.1   9.5
##   7      12.9  20.8  18.4
##   Total 100.0 100.0 100.0

#People who do not have BA spend more time watching tv than those who do have a BA. People who do belong to a religion spend more time watching tv than those who don not. I would assume that people with a BA use more of their time productively or are perhaps just busier than people who no not have a BA.I am not sure why people who belong to a religion spend more time watching tv.

##Task five:

df <- spain_data %>%
  filter(!is.na(educ_level) & !is.na(tvtot))

df <- df %>%
  mutate(tvtot = case_when(
    tvtot == 1 ~ "Yes",
    tvtot == 0 ~ "No",
    TRUE ~ as.character(tvtot)  
  ))

table(df$tvtot)
## 
##    2    3    4    5    6    7   No  Yes 
## 2251 2211 2251 1839 1287 2494  364  836
table(df$tvtot, df$educ_level) %>%
  cprop() %>%
  as.data.frame() %>%
  filter(Var1 != "Total",
         Var2 != "All") %>%
  ggplot(aes(x=Var1, y=Freq, fill=Var2)) +
  geom_col(position = "dodge") +
  labs(title="Time Spent Watching TV on an Average Weekday in Spain",
       y = "Conditional Percentage",
       x = "Time Spent",
       fill = "At least BA vs. Not")

#More people with a BA spend no time watching tv, than people without a BA spend watching tv.

##This concludes Homework 5.