#loading in packages
packages <- c("tidyverse", "infer", "fst", "modelsummary", "effects", "survey", "MASS", "kableExtra", "flextable", "equatiomatic")
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: carData
##
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
##
## 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
##
##
##
## Attaching package: 'MASS'
##
##
## The following object is masked from 'package:dplyr':
##
## select
##
##
##
## Attaching package: 'kableExtra'
##
##
## The following object is masked from 'package:dplyr':
##
## group_rows
##
##
##
## Attaching package: 'flextable'
##
##
## The following objects are masked from 'package:kableExtra':
##
## as_image, footnote
##
##
## The following object is masked from 'package:purrr':
##
## compose
## [[1]]
## [1] "lubridate" "forcats" "stringr" "dplyr" "purrr" "readr"
## [7] "tidyr" "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [13] "grDevices" "utils" "datasets" "methods" "base"
##
## [[2]]
## [1] "infer" "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [7] "readr" "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [13] "graphics" "grDevices" "utils" "datasets" "methods" "base"
##
## [[3]]
## [1] "fst" "infer" "lubridate" "forcats" "stringr" "dplyr"
## [7] "purrr" "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [13] "stats" "graphics" "grDevices" "utils" "datasets" "methods"
## [19] "base"
##
## [[4]]
## [1] "modelsummary" "fst" "infer" "lubridate" "forcats"
## [6] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [11] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [16] "grDevices" "utils" "datasets" "methods" "base"
##
## [[5]]
## [1] "effects" "carData" "modelsummary" "fst" "infer"
## [6] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [11] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [16] "stats" "graphics" "grDevices" "utils" "datasets"
## [21] "methods" "base"
##
## [[6]]
## [1] "survey" "survival" "Matrix" "grid" "effects"
## [6] "carData" "modelsummary" "fst" "infer" "lubridate"
## [11] "forcats" "stringr" "dplyr" "purrr" "readr"
## [16] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [21] "graphics" "grDevices" "utils" "datasets" "methods"
## [26] "base"
##
## [[7]]
## [1] "MASS" "survey" "survival" "Matrix" "grid"
## [6] "effects" "carData" "modelsummary" "fst" "infer"
## [11] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [16] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [21] "stats" "graphics" "grDevices" "utils" "datasets"
## [26] "methods" "base"
##
## [[8]]
## [1] "kableExtra" "MASS" "survey" "survival" "Matrix"
## [6] "grid" "effects" "carData" "modelsummary" "fst"
## [11] "infer" "lubridate" "forcats" "stringr" "dplyr"
## [16] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [21] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [26] "datasets" "methods" "base"
##
## [[9]]
## [1] "flextable" "kableExtra" "MASS" "survey" "survival"
## [6] "Matrix" "grid" "effects" "carData" "modelsummary"
## [11] "fst" "infer" "lubridate" "forcats" "stringr"
## [16] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [21] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [26] "utils" "datasets" "methods" "base"
##
## [[10]]
## [1] "equatiomatic" "flextable" "kableExtra" "MASS" "survey"
## [6] "survival" "Matrix" "grid" "effects" "carData"
## [11] "modelsummary" "fst" "infer" "lubridate" "forcats"
## [16] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [21] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [26] "grDevices" "utils" "datasets" "methods" "base"
#ess data load in
ess <- read_fst("All-ESS-Data.fst")
uk_data <- ess %>% filter(cntry == "GB")
write.csv(uk_data, file="./essukdata.csv")
#making data just for the UK
uk_data <- read_csv("./essukdata.csv")
## New names:
## Rows: 20979 Columns: 2666
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (26): name, proddate, cntry, cntbrth, cntbrtha, cntbrthb, cntbrthc, c... dbl
## (1550): ...1, essround, edition, idno, dweight, pspwght, pweight, anwei... lgl
## (1077): prtvde1, prtvade1, prtvbde1, prtvcde1, prtvdde1, prtvede1, prtv... dttm
## (13): inwds, inwde, ainws, ainwe, binwe, cinwe, dinwe, finwe, ginwe, ...
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## • `` -> `...1`
df <- uk_data
uk_data <- uk_data %>%
mutate(
# Set 'other' to NA for rounds 1-4
edulvla = case_when(
essround < 5 & edulvla == 55 ~ NA_real_,
TRUE ~ edulvla
),
# Set 'other' to NA for rounds 5 and later
edulvlb = case_when(
essround >= 5 & edulvlb == 5555 ~ NA_real_,
TRUE ~ edulvlb
),
# Create educ_level column
educ_level = case_when(
essround < 5 & edulvla == 5 ~ "BA",
essround >= 5 & edulvlb > 600 ~ "BA",
TRUE ~ "No BA"
)
)
unique(uk_data$educ_level)
## [1] "BA" "No BA"
table(uk_data$educ_level)
##
## BA No BA
## 6116 14863
#creating summary table for data
uk_data$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012)
for(i in 1:10){
uk_data$year[uk_data$essround == i] <- replacements[i]
}
uk_data_table <- uk_data %>%
mutate(
educ_level = ifelse(educ_level %in% c(77, 88, 99), NA, educ_level),
polcmpl = ifelse(polcmpl %in% c(7, 8, 9), NA, polcmpl)
)
summary_table <- datasummary_skim(uk_data_table %>%
dplyr::select(polcmpl, educ_level) %>%
mutate(education = ifelse(educ_level == "BA", 1, 0)),
output = "flextable")
## Warning: The histogram argument is only supported for (a) output types "default",
## "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
## ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
## compiled to PDF (via kableExtra) or HTML (via kableExtra or gt). Use
## `histogram=FALSE` to silence this warning.
summary_table
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
polcmpl | 6 | 59 | 3.2 | 1.1 | 1.0 | 3.0 | 5.0 |
education | 2 | 0 | 0.3 | 0.5 | 0.0 | 0.0 | 1.0 |
df$year <- NA
replacements <- c(2002, 2004, 2006, 2008)
for(i in 1:10){
df$year[df$essround == i] <- replacements[i]
}
uk_data_table_subset <- uk_data %>%
mutate(
educ_level = ifelse(educ_level %in% c(77, 88, 99), NA, educ_level),
polcmpl = ifelse(polcmpl %in% c(7, 8, 9), NA, polcmpl),
)
summary_table <- datasummary_skim(uk_data_table_subset %>% dplyr:: select(educ_level, polcmpl), output = "flextable")
## Warning: The histogram argument is only supported for (a) output types "default",
## "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
## ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
## compiled to PDF (via kableExtra) or HTML (via kableExtra or gt). Use
## `histogram=FALSE` to silence this warning.
uk_data_v2 <- uk_data_table_subset %>%
rename(
`Politics are too Complicated to Understand` = polcmpl,
`Education Level` = educ_level
)
summary_table_v2 <- datasummary_skim(uk_data_v2 %>% dplyr:: select(`Politics are too Complicated to Understand`,`Education Level`), output = "flextable")
## Warning: The histogram argument is only supported for (a) output types "default",
## "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
## ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
## compiled to PDF (via kableExtra) or HTML (via kableExtra or gt). Use
## `histogram=FALSE` to silence this warning.
summary_table_v2
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
Politics are too Complicated to Understand | 6 | 59 | 3.2 | 1.1 | 1.0 | 3.0 | 5.0 |
avg_polcmpl_by_year <- aggregate(polcmpl ~ year + educ_level, data=uk_data_table_subset, mean)
# Storing plot as p1
p1 <- ggplot(avg_polcmpl_by_year, aes(x=year, y=polcmpl, color=as.factor(educ_level))) +
geom_line(aes(group=educ_level)) +
labs(title="Mean Politics are too Complicated to Understand",
subtitle = "for those who have a BA vs. No BA in the UK",
x="Survey Year",
y="Average Polcmpl") +
scale_color_discrete(name="Education", labels=c("No BA", " BA")) +
theme_minimal()
p1
#re-coding variables to have categorical values
uk_data <- uk_data %>%
mutate(
polcmpl = case_when(
polcmpl == 1 ~ "Always",
polcmpl == 2 ~ "Seldom",
polcmpl == 3 ~ "Occasionally",
polcmpl == 4 ~ "Regularly",
polcmpl == 5 ~ "Frequently",
polcmpl %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(polcmpl)
))
unique(uk_data$polcmpl)
## [1] "Seldom" "Occasionally" "Regularly" "Always" "Frequently"
## [6] NA
table(uk_data$polcmpl)
##
## Always Frequently Occasionally Regularly Seldom
## 715 1353 3052 2180 1339
##Context
#creating summary table for data
uk_data$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012)
for(i in 1:10){
uk_data$year[uk_data$essround == i] <- replacements[i]
}
uk_data_table <- uk_data %>%
mutate(
educ_level = ifelse(educ_level %in% c(77, 88, 99), NA, educ_level),
polcmpl = ifelse(polcmpl %in% c(7, 8, 9), NA, polcmpl)
)
summary_table <- datasummary_skim(uk_data_table %>%
dplyr::select(polcmpl, educ_level) %>%
mutate(education = ifelse(educ_level == "BA", 1, 0)),
output = "flextable")
## Warning: The histogram argument is only supported for (a) output types "default",
## "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
## ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
## compiled to PDF (via kableExtra) or HTML (via kableExtra or gt). Use
## `histogram=FALSE` to silence this warning.
summary_table
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
education | 2 | 0 | 0.3 | 0.5 | 0.0 | 0.0 | 1.0 |
#did not end up using
#df$year <- NA
#replacements <- c(2002, 2004, 2006, 2008)
#for(i in 1:10){
# df$year[df$essround == i] <- replacements[i]
#}
#complicated_avg <- df %>%
# group_by(year) %>%
# summarize(complicated_avg = mean(polcmpl, na.rm = TRUE))
#plot_zoom_complicated <- ggplot(complicated_avg, aes(x = year, y = complicated_avg)) +
# geom_point(aes(size = complicated_avg, color = complicated_avg), alpha = 1) +
# geom_line(aes(group = 1), color = "lavender", linetype = "dashed") +
# labs(title = "Politics too Complicated to Understand",
# x = "Survey Year",
# y = "Complicated Average") +
# theme_minimal() +
# theme(legend.position = "none")
#print(plot_zoom_complicated)
#plot_full_scale_complicated <- ggplot(complicated_avg, aes(x = year, y = complicated_avg)) +
# geom_point(aes(size = complicated_avg, color = complicated_avg), alpha = 1) +
# geom_line(aes(group = 1), color = "purple", linetype = "dashed") +
# labs(title = "Authoritarian Scale Average by Year (France)",
# x = "Survey Year",
# y = "Authoritarian Scale Average") +
# theme_minimal() +
# theme(legend.position = "none") +
# scale_y_continuous(limits = c(0, 100))
#print(plot_full_scale_complicated)
##Findings - null distribution
#Step 1 - calculating the test statistic
test_stat <- uk_data %>%
specify(explanatory = educ_level,
response = polcmpl) %>%
hypothesize(null = "independence") %>%
calculate(stat = "Chisq")
## Warning: Removed 12340 rows containing missing values.
print(test_stat$stat)
## X-squared
## 350.086
#simulating the null distribution
null_distribution <- uk_data %>%
specify(explanatory = educ_level,
response = polcmpl) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>% # only line we are adding, use reps = 1000 as standard
calculate(stat = "Chisq")
## Warning: Removed 12340 rows containing missing values.
#calculating the p-value
p_val <- null_distribution %>%
get_pvalue(obs_stat = test_stat, direction = "two-sided")
## Warning: Please be cautious in reporting a p-value of 0. This result is an
## approximation based on the number of `reps` chosen in the `generate()` step.
## See `?get_p_value()` for more information.
p_val
## # A tibble: 1 × 1
## p_value
## <dbl>
## 1 0
#visualization
null_distribution %>%
visualize() +
shade_p_value(obs_stat = test_stat, direction = "two-sided")
## Warning: Chi-Square usually corresponds to right-tailed tests. Proceed with
## caution.
#visualization of null distribution with confidence interval
conf_int <- null_distribution %>%
get_confidence_interval(level = 0.95, type = "percentile")
null_distribution %>%
visualize() +
shade_p_value(obs_stat = test_stat, direction = "two-sided") +
shade_confidence_interval(endpoints = conf_int)
## Warning: Chi-Square usually corresponds to right-tailed tests. Proceed with
## caution.
#Regression
df <-uk_data %>%
mutate(education = ifelse(educ_level == "BA", 1, 0 ))
df <- uk_data %>%
mutate(confidentpol = ifelse(polcmpl>5, NA, 6-polcmpl))
#did not use this
#df %>%
# ggplot(aes(x = hinctnta, y = confidentpol)) +
# geom_smooth() +
# theme_minimal()
#df %>%
# ggplot(aes(x = hinctnta, y = confidentpol)) +
# geom_smooth() +
# facet_wrap(~educ_level)
ds <- df
df <- df %>%
mutate(
education = educ_level,
polcomplicated = case_when(
polcmpl %in% c("Never", "Seldom", "Occasionally") ~ 0,
polcmpl %in% c("Regularly", "Frequently") ~ 1,
T~NA),
income = case_when(
hinctnta %in% c(77, 88, 99) ~ NA_real_,
TRUE ~ hinctnta
)
)
questionr::freq(df$polcomplicated)
## n % val%
## 0 4391 20.9 55.4
## 1 3533 16.8 44.6
## NA 13055 62.2 NA
table(df$year, df$education)
##
## BA No BA
## 2002 662 1390
## 2004 354 1543
## 2006 946 1448
## 2008 1038 1314
## 2010 456 1966
## 2012 457 1829
table(df$year, df$polcomplicated)
##
## 0 1
## 2002 1051 822
## 2004 953 814
## 2006 1196 979
## 2008 1191 918
## 2010 0 0
## 2012 0 0
table(df$year, df$income)
##
## 1 2 3 4 5 6 7 8 9 10
## 2002 0 0 0 0 0 0 0 0 0 0
## 2004 0 0 0 0 0 0 0 0 0 0
## 2006 0 0 0 0 0 0 0 0 0 0
## 2008 256 236 206 179 155 175 215 185 164 229
## 2010 312 243 194 161 162 176 182 176 146 154
## 2012 271 239 200 139 128 160 156 176 151 177
df <- df %>% filter(!is.na(education))
df <- df %>% filter(!is.na(polcomplicated))
df <- df %>% filter(!is.na(income))
questionr::freq(df$year)
## n % val%
## 2008 1810 100 100
d <- df %>%
# Convert factors/characters to numeric
mutate(
education_num = as.numeric(factor(education, levels = c("No BA", "BA"))),
income_num = as.numeric(income),
polcomplicated_num = as.numeric(polcomplicated)
)
table1 <- datasummary_skim(d %>%
dplyr::select(education_num, polcomplicated_num, income_num),
output = "flextable")
## Warning: The histogram argument is only supported for (a) output types "default",
## "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
## ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
## compiled to PDF (via kableExtra) or HTML (via kableExtra or gt). Use
## `histogram=FALSE` to silence this warning.
table1
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
education_num | 2 | 0 | 1.4 | 0.5 | 1.0 | 1.0 | 2.0 |
polcomplicated_num | 2 | 0 | 0.4 | 0.5 | 0.0 | 0.0 | 1.0 |
income_num | 10 | 0 | 5.3 | 3.0 | 1.0 | 5.0 | 10.0 |
d <- d %>%
rename(
`Education Level` = education_num,
`Income` = income_num,
`How Complicated Politics` = polcomplicated_num,
)
table1_v2 <- datasummary_skim(d %>% dplyr::select(`Education Level`,`Income`, `How Complicated Politics`), title = "Table 1. Descriptive statistics of main variables", output = "flextable")
## Warning: The histogram argument is only supported for (a) output types "default",
## "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
## ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
## compiled to PDF (via kableExtra) or HTML (via kableExtra or gt). Use
## `histogram=FALSE` to silence this warning.
table1_v2
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
Education Level | 2 | 0 | 1.4 | 0.5 | 1.0 | 1.0 | 2.0 |
Income | 10 | 0 | 5.3 | 3.0 | 1.0 | 5.0 | 10.0 |
How Complicated Politics | 2 | 0 | 0.4 | 0.5 | 0.0 | 0.0 | 1.0 |
#interaction
df$education <- as.factor(df$education) %>%
relevel("No BA")
df$weight <- df$dweight * df$pweight
model1 <- glm(polcomplicated ~ education,
family = binomial(link="logit"),
data = df,
weights = weight)
## Warning in eval(family$initialize): non-integer #successes in a binomial glm!
model2 <- glm(polcomplicated ~ education + income,
family = binomial(link="logit"),
data = df, weights = weight)
## Warning in eval(family$initialize): non-integer #successes in a binomial glm!
model3 <- glm(polcomplicated ~ education * income,
family = binomial(link="logit"),
data = df, weights = weight)
## Warning in eval(family$initialize): non-integer #successes in a binomial glm!
summary(model1)
##
## Call:
## glm(formula = polcomplicated ~ education, family = binomial(link = "logit"),
## data = df, weights = weight)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.04196 0.04392 0.955 0.339
## educationBA -0.64203 0.06665 -9.633 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 5230.8 on 1809 degrees of freedom
## Residual deviance: 5136.5 on 1808 degrees of freedom
## AIC: 4713.1
##
## Number of Fisher Scoring iterations: 4
summary(model2)
##
## Call:
## glm(formula = polcomplicated ~ education + income, family = binomial(link = "logit"),
## data = df, weights = weight)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.25882 0.07128 3.631 0.000282 ***
## educationBA -0.54075 0.07152 -7.560 4.02e-14 ***
## income -0.04633 0.01198 -3.869 0.000109 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 5230.8 on 1809 degrees of freedom
## Residual deviance: 5121.5 on 1807 degrees of freedom
## AIC: 4701.9
##
## Number of Fisher Scoring iterations: 4
summary(model3)
##
## Call:
## glm(formula = polcomplicated ~ education * income, family = binomial(link = "logit"),
## data = df, weights = weight)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.21896 0.08582 2.551 0.01073 *
## educationBA -0.42160 0.15992 -2.636 0.00838 **
## income -0.03782 0.01574 -2.402 0.01629 *
## educationBA:income -0.02016 0.02423 -0.832 0.40542
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 5230.8 on 1809 degrees of freedom
## Residual deviance: 5120.8 on 1806 degrees of freedom
## AIC: 4703
##
## Number of Fisher Scoring iterations: 4
#regression table
modelsummary(
list(model1, model2, model3),
fmt = 3,
estimate = c( "{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}"),
statistic = NULL,
coef_omit = "Intercept")
| (1) | (2) | (3) | |
|---|---|---|---|
| educationBA | −0.642 (0.067)*** | −0.541 (0.072)*** | −0.422 (0.160)** |
| income | −0.046 (0.012)*** | −0.038 (0.016)* | |
| educationBA × income | −0.020 (0.024) | ||
| Num.Obs. | 1810 | 1810 | 1810 |
| AIC | 4713.1 | 4701.9 | 4703.0 |
| BIC | 4724.1 | 4718.4 | 4725.0 |
| Log.Lik. | −2354.542 | −2347.938 | −2347.482 |
| RMSE | 0.49 | 0.49 | 0.49 |
#other stuff that didn’t work
#df <- df %>%
# mutate(politics = polcmpl,
# income = hinctnta,
# education = educ_level) %>%
# mutate(across(c("politics", "education", "income"),
# ~ na_if(.x, 7) %>% na_if(8) %>% na_if(9))) %>%
# Apply the reverse coding
# mutate(across(c("politics", "education", "income"), ~ 7 - .x ))
# Now you can calculate 'schwartzauth' after the NA recoding
#df$auth <- scales::rescale(df$behave +
# df$politics +
# df$education +
# df$income, to=c(0,100), na.rm=TRUE)
#df <- df %>% filter(!is.na(auth))
#uk_data <- uk_data %>%
# rename(
# `Education level` = educ_level,
# `Politics are complicated` = polcmpl
# )
#d <- d %>%
# rename(
# `Political Identity` = polID_num,
# `Generations` = gen_num,
# `Educational attainment` = educ_ba_num,
# `Religious` = religious_num,
# `Authoritarian` = auth
# )
# table1_v2 <- datasummary_skim(d %>% dplyr::select(`Political Identity`,`Generations`, `Educational attainment`, `Religious`, `Authoritarian`), title = "Table 1. Descriptive statistics of main variables", output = "flextable")