#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
## [[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] "broom" "modelsummary" "fst" "infer" "lubridate"
## [6] "forcats" "stringr" "dplyr" "purrr" "readr"
## [11] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [16] "graphics" "grDevices" "utils" "datasets" "methods"
## [21] "base"
##
## [[6]]
## [1] "stargazer" "broom" "modelsummary" "fst" "infer"
## [6] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [11] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [16] "stats" "graphics" "grDevices" "utils" "datasets"
## [21] "methods" "base"
##
## [[7]]
## [1] "remotes" "stargazer" "broom" "modelsummary" "fst"
## [6] "infer" "lubridate" "forcats" "stringr" "dplyr"
## [11] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [16] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [21] "datasets" "methods" "base"
remotes::install_github("datalorax/equatiomatic", force = TRUE)
## Downloading GitHub repo datalorax/equatiomatic@HEAD
## stringi (1.8.2 -> 1.8.3) [CRAN]
## yaml (2.3.7 -> 2.3.8) [CRAN]
## Installing 2 packages: stringi, yaml
##
## The downloaded binary packages are in
## /var/folders/fs/2z898qp57xqdt_rxc_r7cqqw0000gn/T//Rtmp4cnawd/downloaded_packages
## Running `R CMD build`...
## * checking for file ‘/private/var/folders/fs/2z898qp57xqdt_rxc_r7cqqw0000gn/T/Rtmp4cnawd/remotes416f4504c281/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"))

# Create a bar plot of trust in European Parliament frequencies
barplot(table(germany_data$trstep), xlab="Trust in European Parliament", # Label for x-axis
ylab="Frequency", col=rainbow(11), # Label for y-axis and color bars with a rainbow pattern
ylim=c(0, 700)) #Set y-axis limits from 0 to 700

# Create a bar plot of household's total net income frequencies
barplot(table(germany_data$hinctnta), xlab="Household's total net income", # Label for x-axis
ylab="Frequency", col=rainbow(10), ylim=c(0, 500))

# Label for y-axis
# Color bars with a rainbow pattern of 10 colors
# Set y-axis limits from 0 to 500
# Fit linear regression model with trstep(trust in the European Parliament) as dependent and (household income)hinctnta, wrkprty(work in political party or not) as independent variables
model1 <- lm(trstep ~ hinctnta + wrkprty , data = germany_data)
# Display summary of the model
summary(model1)
##
## 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
# Fit linear regression model with interaction between (household income)hinctnta and wrkprty(work in political party or not).
model2 <- lm(trstep ~ hinctnta + wrkprty +hinctnta:wrkprty , data = germany_data)
# Display summary of the model with the interaction term
summary(model2)
##
## Call:
## lm(formula = trstep ~ hinctnta + wrkprty + hinctnta:wrkprty,
## data = germany_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.7689 -2.5379 -0.0327 1.9673 7.3042
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.527436 0.113116 22.344 < 2e-16 ***
## hinctnta 0.168410 0.020660 8.152 5.24e-16 ***
## wrkprtyYes 0.532432 0.583152 0.913 0.361
## hinctnta:wrkprtyYes 0.002496 0.091877 0.027 0.978
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.671 on 2957 degrees of freedom
## Multiple R-squared: 0.02536, Adjusted R-squared: 0.02437
## F-statistic: 25.65 on 3 and 2957 DF, p-value: 2.242e-16
# Compare and output model1 and model2 using stargazer
stargazer(model1, model2, header=F, type="text", out='a.doc')
##
## =====================================================================
## Dependent variable:
## -------------------------------------------------
## trstep
## (1) (2)
## ---------------------------------------------------------------------
## hinctnta 0.169*** 0.168***
## (0.020) (0.021)
##
## wrkprtyYes 0.547** 0.532
## (0.261) (0.583)
##
## hinctnta:wrkprtyYes 0.002
## (0.092)
##
## Constant 2.527*** 2.527***
## (0.111) (0.113)
##
## ---------------------------------------------------------------------
## Observations 2,961 2,961
## R2 0.025 0.025
## Adjusted R2 0.025 0.024
## Residual Std. Error 2.671 (df = 2958) 2.671 (df = 2957)
## F Statistic 38.484*** (df = 2; 2958) 25.648*** (df = 3; 2957)
## =====================================================================
## Note: *p<0.1; **p<0.05; ***p<0.01
# Disable the default header
# Output type set as text
# Output the comparison to a file that is named 'a.doc'
# Set up plotting area to display 4 plots in a 2x2 grid
par(mfrow=c(2,2))
# Generate diagnostic plots for the interaction model which is model2
plot(model2)

# Reset plotting area to display only one plot at a time
par(mfrow=c(1,1))