Loading Data into R

Let’s load our data, fst style. Remember the data needs to be in the folder where you’re working from. You can enter getwd() to check your working directory.

# List of packages
packages <- c("tidyverse", "broom", "infer", "fst", "modelsummary", "effects", "survey", "interactions") # add any you need here

# Install packages if they aren't installed already
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)

# Load the packages
lapply(packages, library, character.only = TRUE)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ 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
## Warning: package 'infer' was built under R version 4.3.3
## Warning: package 'effects' was built under R version 4.3.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.3.3
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
## Warning: package 'survey' was built under R version 4.3.3
## Loading required package: grid
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## 
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## 
## Loading required package: survival
## 
## Attaching package: 'survey'
## 
## The following object is masked from 'package:graphics':
## 
##     dotchart
## [[1]]
##  [1] "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"     "readr"    
##  [7] "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics" 
## [13] "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[2]]
##  [1] "broom"     "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"    
##  [7] "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"    
## [13] "graphics"  "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[3]]
##  [1] "infer"     "broom"     "lubridate" "forcats"   "stringr"   "dplyr"    
##  [7] "purrr"     "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse"
## [13] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"  
## [19] "base"     
## 
## [[4]]
##  [1] "fst"       "infer"     "broom"     "lubridate" "forcats"   "stringr"  
##  [7] "dplyr"     "purrr"     "readr"     "tidyr"     "tibble"    "ggplot2"  
## [13] "tidyverse" "stats"     "graphics"  "grDevices" "utils"     "datasets" 
## [19] "methods"   "base"     
## 
## [[5]]
##  [1] "modelsummary" "fst"          "infer"        "broom"        "lubridate"   
##  [6] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [11] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [16] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [21] "base"        
## 
## [[6]]
##  [1] "effects"      "carData"      "modelsummary" "fst"          "infer"       
##  [6] "broom"        "lubridate"    "forcats"      "stringr"      "dplyr"       
## [11] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [16] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [21] "datasets"     "methods"      "base"        
## 
## [[7]]
##  [1] "survey"       "survival"     "Matrix"       "grid"         "effects"     
##  [6] "carData"      "modelsummary" "fst"          "infer"        "broom"       
## [11] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [16] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [21] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [26] "methods"      "base"        
## 
## [[8]]
##  [1] "interactions" "survey"       "survival"     "Matrix"       "grid"        
##  [6] "effects"      "carData"      "modelsummary" "fst"          "infer"       
## [11] "broom"        "lubridate"    "forcats"      "stringr"      "dplyr"       
## [16] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [21] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [26] "datasets"     "methods"      "base"
ess <- read.fst("All-ESS-Data.fst")

End of Tutorial

Homework 4 (5%): due by next lecture on March 19th

Instructions: Start a new R markdown for the homework and call it “Yourlastname_Firstname_Homework_4”.

Copy everything below from Task 1 to Task 5. Keep the task prompt and questions, and provide your code and answer underneath.

Remember: you need all the steps for your code to work, including loading your data – otherwise it will not knit.

To generate a new code box, click on the +C sign above. Underneath your code, provide your answer to the task question.

When you are done, click on “Knit” above, then “Knit to Html”. Wait for everything to compile. If you get an error like “Execution halted”, it means there are issues with your code you must fix. When all issues are fixed, it will prompt a new window. Then click on “Publish” in the top right, and then Rpubs (the first option) and follow the instructions to create your Rpubs account and get your Rpubs link for your document (i.e., html link as I provide for the tutorial).

Note: Make sure to provide both your markdown file and R pubs link. If you do not submit both, you will be penalized 2 pts. out of the 5 pts. total.

Task 1

Graph the histogram distribution using the populist scale (as we did in the tutorial) for the country of Sweden. What do you note?

Answer to Task 1

sweden_data <- read.fst("sweden_data.fst")


sweden_data$trstplt <- ifelse(sweden_data$trstplt > 10, NA, sweden_data$trstplt)
sweden_data$trstprl <- ifelse(sweden_data$trstprl > 10, NA, sweden_data$trstprl)
sweden_data$trstprt <- ifelse(sweden_data$trstprt > 10, NA, sweden_data$trstprt)

sweden_data$trust <- scales::rescale(sweden_data$trstplt + sweden_data$trstprl + sweden_data$trstprt, na.rm = TRUE, to = c(0, 100))

sweden_data$populist <- scales::rescale(sweden_data$trust, na.rm = TRUE, to=c(100,0))
ggplot(sweden_data, aes(x = populist)) + geom_histogram(bins = 30, fill = "blue", color = "black") + theme_minimal() + labs(title = "Distribution of Populist Scale for Sweden", x = "Populist Scale", y = "Count")
## Warning: Removed 2503 rows containing non-finite values (`stat_bin()`).

Now with the information above, we are able to interpret the histogram. It is clear based on this information that the data shows an increase in count at the population scale of roughly 50, and the count exceeding 2000. This is different from most of the counts being below 1000 based on the rest of the population scale.

Task 2

Run a linear regression with populist attitudes as the outcome, educational attainment (recoded as a binary “BA or more” and “No BA”), and using the Swedish data. Print the intercept and coefficients only and interpret. Then do a tidy model table displaying the p-value (with digits = 3) and interpret.

Answer to Task 2

sweden_data <- sweden_data %>%
  mutate(
 
    educ.ba = case_when(
      essround < 5 & edulvla == 5 ~ "BA or more",
      essround >= 5 & edulvlb > 600 ~ "BA or more",
      TRUE ~ "NO BA"
    ),
    
    edulvla = ifelse(edulvla %in% c(77, 88, 99), NA_integer_, edulvla),
    edulvlb = ifelse(edulvlb %in% c(5555, 7777, 8888), NA_integer_, edulvlb),
    
    educ.ba = factor(educ.ba, levels = c("NO BA", "BA or more")), 
)
sweden_pop_model <- lm(populist ~ educ.ba, data = sweden_data)
coefficients_sweden <- coef(sweden_pop_model)
print(coefficients_sweden)
##       (Intercept) educ.baBA or more 
##         51.358680         -7.868121
tidy(sweden_pop_model)
## # A tibble: 2 × 5
##   term              estimate std.error statistic   p.value
##   <chr>                <dbl>     <dbl>     <dbl>     <dbl>
## 1 (Intercept)          51.4      0.181     283.  0        
## 2 educ.baBA or more    -7.87     0.350     -22.5 2.58e-110

The intercept for this question is 51.358680 and the educ.baBA or more is -7.868. What we can interpret by this is that the populist values in Sweden are 51.358680.

Task 3

Now using the Italian dataset and the authoritarian values scale (as we did in the tutorial), graph the average by survey year. Interpret.

italy_data <- read.fst("italy_data.fst")
italy_data <- italy_data %>%
  mutate(behave = ipbhprp, secure = impsafe, safety = ipstrgv, tradition = imptrad, rules = ipfrule) %>%
  mutate(across(c("behave", "secure", "safety", "tradition", "rules"), 
                ~ na_if(.x, 7) %>% na_if(8) %>% na_if(9))) %>%
  
  mutate(across(c("behave", "secure", "safety", "tradition", "rules"), ~ 7 - .x ))

italy_data$auth <- scales::rescale(italy_data$behave + italy_data$secure + italy_data$safety + italy_data$tradition + italy_data$rules, to=c(0,100), na.rm=TRUE)


italy_data <- italy_data %>% filter(!is.na(auth))
table(italy_data$secure)
## 
##    1    2    3    4    5    6 
##   54  139  626 1856 2876 2881
table(italy_data$auth)
## 
##   0  12  16  20  24  28  32  36  40  44  48  52  56  60  64  68  72  76  80  84 
##   7   4   4   9  13  20  27  57 113 155 163 323 367 596 647 789 823 897 992 733 
##  88  92  96 100 
## 632 465 299 297
italy_data$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020) 
for(i in 1:10){
  italy_data$year[italy_data$essround == i] <- replacements[i]
}
auth_avg <- italy_data %>%
  group_by(year) %>%
  summarize(auth_avg = mean(auth, na.rm = TRUE))

plot_auth <- ggplot(auth_avg, aes(x = year, y = auth_avg)) + geom_point(aes(color = auth_avg), alpha = 1.0) + geom_line(aes(group = 1), color = "blue", linetype = "dashed") +
  
  labs(title = "Authoritarian Scale Average by Year (Italy)", x = "Survey Year", Y = "Authoritatian Scale Average") + theme_minimal() + theme(legend.position = "none") + scale_y_continuous(limits = c(0,100))

print(plot_auth)

What we can interpret from this output is that the auth_avg has seen a slow decrease from slightly above 75 to slightly below 75 in the years from 2005 to 2020. However, the results have not seen any extensive change over the years. With this in mind, there is reasonable evidence to conclude that the authoritarian scale average in Italy has remained mostly the same overtime.

Task 4

Now run a linear regression model, using the modelsummary package, with authoritarian values as the outcome, and generations as the predictor/potential explanatory variable, and using the Italian data again. Rename the coefficients using the coef_rename function. Interpret the coefficients, whether they are statistically significant, and the adjusted R-squared.

Answer to Task 4

italy_data <- read.fst("italy_data.fst")
model_data <- italy_data %>%
  mutate(
    
    age = ifelse(agea == 999, NA, agea), 
    cohort = ifelse(yrbrn < 1930 | yrbrn > 2000, NA, yrbrn), 
    gen = case_when(yrbrn %in% 1900:1945 ~ "1",
                    yrbrn %in% 1946:1964 ~ "2",
                    yrbrn %in% 1965:1979 ~ "3",
                    yrbrn %in% 1980:1996 ~ "4",
                    TRUE ~ as.character(cohort)
                    ),
    gen = factor(gen, 
                 levels = c("1", "2", "3", "4"), 
                 labels = c("Interwar", "Baby Boomers", "Gen X", "Millennials"))
  )

 table(model_data$gen)
## 
##     Interwar Baby Boomers        Gen X  Millennials 
##         1470         3179         2636         2031

Task 5

Now visualize, using the modelplot() function from the modelsummary package, coefficient estimates and 95% confidence intervals for the following three models:

Model 1 = populist attitudes scale as the outcome; left/right as the single predictor (omitting moderates as we did in the tutorial), and using data for Greece.

Model 2 = populist attitudes scale as the outcome; gender as the single predictor, and using data for Greece.

Model 3 = populist attitudes scale as the outcome; generations as the single predictor (using the four generational category recode we did in the tutorial), and using data for Greece. Interpret.

Answer to Task 5

greece_data <- read.fst("greece_data.fst")
greece_data$trstplt <- ifelse(greece_data$trstplt > 10, NA, greece_data$trstplt)
greece_data$trstprl <- ifelse(greece_data$trstprl > 10, NA, greece_data$trstprl)
greece_data$trstprt <- ifelse(greece_data$trstprt > 10, NA, greece_data$trstprt)

greece_data$trust <- scales::rescale(greece_data$trstplt + greece_data$trstprl + greece_data$trstprt, na.rm = TRUE, to = c(0,100))

greece_data$populist <- scales::rescale(greece_data$trust, na.rm = TRUE, to=c(100,0))
model_data <- greece_data %>%
  mutate(
    gndr = case_when(
      gndr == 1 ~ "Male",
      gndr == 2 ~ "Female",
      gndr == 9 ~ NA_character_,
      TRUE ~ as.character(gndr)
    ),
    
    lrscale = case_when(
      lrscale %in% 0:4 ~ "Left",
      lrscale %in% 6:10 ~ "Right",
      lrscale %in% c(77, 88, 99) ~ NA_character_, TRUE ~ NA_character_
    ),
    
    age = ifelse(agea == 999, NA, agea), cohort = ifelse(yrbrn < 1930 | yrbrn > 2000, NA, yrbrn), 
    gen = case_when(yrbrn %in% 1900:1945 ~ "1",
                    yrbrn %in% 1946:1964 ~ "2",
                    yrbrn %in% 1965:1979 ~ "3",
                    yrbrn %in% 1980:1996 ~ "4",
                    TRUE ~ as.character(cohort)
  ),                  
    gen = factor(gen, levels = c("1", "2", "3", "4"), 
                 labels = c("Interwar", "Baby Boomers", "Gen X", "Millennials")),
                    
  )
models <- list(
  "Model 1" = lm(populist ~ lrscale, data = model_data),
  "Model 2" = lm(populist ~ gndr, data = model_data),
  "Model 3" = lm(populist ~ gen, data = model_data))
modelsummary(models, fmt = 1, 
             estimate = c("{estimate}) ({std.error}) {stars}", 
                          "{estimate}) ({std.error}) {stars}",
                          "{estimate}) ({std.error}) {stars}"), statistic = NULL)
Model 1  Model 2  Model 3
(Intercept) 72.0) (0.5) *** 70.0) (0.3) *** 65.8) (0.5) ***
lrscaleRight −8.8) (0.6) ***
gndrMale −1.0) (0.5) *
genBaby Boomers 3.5) (0.7) ***
genGen X 5.6) (0.7) ***
genMillennials 5.8) (0.7) ***
Num.Obs. 4936 9823 9562
R2 0.037 0.000 0.009
R2 Adj. 0.036 0.000 0.009
AIC 44723.8 88873.2 86473.9
BIC 44743.3 88894.8 86509.7
Log.Lik. −22358.894 −44433.590 −43231.944
RMSE 22.44 22.30 22.25
modelplot(models, coef_omit = 'Interc')

With this data, there is a lot we can interpret. Some relevant information derived from the data is the intercepts for model 1 is (72.0) (0.5), for model 2 it is (70.0) and (0.3) and model 3 it is (65.8) (0.5). The R2 for model 1 is 0.037, for model 2 it is 0.000 and for model 3 it is 0.009. the adjusted r2 for model 1 is 0.036, for model 2 it is 0.000 and for model 3 it is 0.009. With this in mind, what we can interpret is that genMillenials, genGenX and genBaby Boomers are shown to be more likely to have populist views. Whereas gndrMale and lrscaleRight are less likely to have populist views.

End