#first, we need to load the necessary file and data i need to proceed the analysis.
packages <- c("tidyverse", "infer", "fst", "modelsummary", "broom", "stargazer", "remotes")
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.4 ✔ readr 2.1.4
## ✔ 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
##
## Please cite as:
##
##
## Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##
## R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
## * checking for file ‘/private/var/folders/fs/2z898qp57xqdt_rxc_r7cqqw0000gn/T/RtmpQjGfuY/remotes558860d5806/datalorax-equatiomatic-39e8c3e/DESCRIPTION’ ... OK
## * preparing ‘equatiomatic’:
## * checking DESCRIPTION meta-information ... OK
## * checking for LF line-endings in source and make files and shell scripts
## * checking for empty or unneeded directories
## * building ‘equatiomatic_0.3.1.tar.gz’
#set the work directory to my soc202 folder where the ess dataset is stored
setwd("~/Documents/Soc202")
getwd()
## [1] "/Users/leo/Documents/Soc202"
ess <- read_fst("All-ESS-Data.fst")
# trstep: Trust in the European Parliament
# wrkprty: Worked in political party or action group last 12 months
# hinctnta - Household's totalnet income,all sources
# Set trstep & hinctnta to NA if outside 0-10, wrkprty to NA if not 1 or 2
germany_data <- ess %>%
filter(cntry == "GR") %>%
mutate(
trstep = ifelse(trstep %in% 0:10, trstep, NA),
hinctnta = ifelse(hinctnta %in% 0:10, hinctnta, NA),
wrkprty= ifelse(wrkprty %in% 1:2, wrkprty, NA),
) %>%
# Remove rows with any NAs in hinctnta, trstep, wrkprty
filter(!is.na(hinctnta), !is.na(trstep),
!is.na(wrkprty)) %>%
# Keep only trstep, hinctnta, wrkprty columns
select(trstep, hinctnta,wrkprty) %>%
# Recode wrkprty to "Yes" or "No"
mutate(
wrkprty=ifelse(wrkprty==1, "Yes", "No")
)
# Create a boxplot for trust in European Parliament against whether work in political party or not
boxplot(trstep~wrkprty, germany_data,
xlab="Worked in political party or action group last 12 months",
ylab="Trust in European Parliament",
col=c("orange", "cyan"))
# Convert the categorical variable 'wrkprty' to a numeric variable
germany_data$wrkprty_numeric <- ifelse(germany_data$wrkprty == "Yes", 1, 0)
# Make sure all other variables are in the correct format
# Assuming 'hinctnta' and 'trstep' are already numeric
# Load modelsummary package
library(modelsummary)
# Use datasummary_skim to create a table of descriptive statistics
# Note: Make sure all necessary variables are included and are numeric
descriptive_table <- datasummary_skim(germany_data[, c("trstep", "hinctnta", "wrkprty_numeric")])
# You can use datasummary to save the table as an image or view it in R
# datasummary(descriptive_table, output = "descriptive_table.png")
# OR just view it in the R console
descriptive_table
Unique (#)
Missing (%)
Mean
SD
Min
Median
Max
trstep
11
0
3.4
2.7
0.0
3.0
10.0
hinctnta
10
0
4.9
2.4
1.0
5.0
10.0
wrkprty_numeric
2
0
0.0
0.2
0.0
0.0
1.0
# Load the necessary libraries
library(infer)
library(ggplot2)
# Generate the null distribution for the correlation test
null_distribution <- germany_data %>%
specify(formula = trstep ~ hinctnta) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "correlation")
# Get the observed correlation
obs_cor_value <- germany_data %>%
specify(formula = trstep ~ hinctnta) %>%
calculate(stat = "correlation") %>%
pull(stat) # Extract the numeric value of the correlation
# Plot the null distribution
ggplot(null_distribution, aes(x = stat)) +
geom_histogram(binwidth = 0.01, fill = "blue", color = "black") +
geom_vline(aes(xintercept = obs_cor_value), color = "red", linetype = "dashed") +
labs(title = "Null Distribution for Correlation for Trust in Parliament and Household's Income",
x = "Correlation Coefficient",
y = "Frequency") +
theme_minimal()
# Load the necessary library
library(dplyr)
# Convert wrkprty to a factor
germany_data$wrkprty <- as.factor(germany_data$wrkprty)
# Model 1: Single Predictor (Household Income)
model1 <- lm(trstep ~ hinctnta, data = germany_data)
# Model 2: Two Predictor Model (Household Income and Political Participation)
model2 <- lm(trstep ~ hinctnta + wrkprty, data = germany_data)
# Model 3: Interaction Model
model3 <- lm(trstep ~ hinctnta * wrkprty, data = germany_data) # '*' includes both main effects and interaction
# Summarize the models
summary(model1)
##
## Call:
## lm(formula = trstep ~ hinctnta, data = germany_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.2455 -2.5609 -0.0475 1.9525 7.2948
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.5340 0.1108 22.874 <2e-16 ***
## hinctnta 0.1711 0.0201 8.515 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.672 on 2959 degrees of freedom
## Multiple R-squared: 0.02392, Adjusted R-squared: 0.02359
## F-statistic: 72.5 on 1 and 2959 DF, p-value: < 2.2e-16
summary(model2)
##
## Call:
## lm(formula = trstep ~ hinctnta + wrkprty, data = germany_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.7588 -2.5380 -0.0324 1.9676 7.3046
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.52682 0.11077 22.811 <2e-16 ***
## hinctnta 0.16854 0.02013 8.374 <2e-16 ***
## wrkprtyYes 0.54660 0.26114 2.093 0.0364 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.671 on 2958 degrees of freedom
## Multiple R-squared: 0.02536, Adjusted R-squared: 0.0247
## F-statistic: 38.48 on 2 and 2958 DF, p-value: < 2.2e-16
library(ggplot2)
# Create a scatter plot
ggplot(germany_data, aes(x = hinctnta, y = trstep, color = wrkprty)) +
geom_point(alpha = 0.6) + # Use alpha for better visibility if points overlap
geom_smooth(method = "lm", se = FALSE) + # Add linear regression line
labs(title = "Trust in European Parliament vs. Household Income",
subtitle = "Colored by Political Participation",
x = "Household's Total Net Income",
y = "Trust in European Parliament",
color = "Political Participation") +
theme_minimal() +
scale_color_brewer(palette = "Set1")