# List of packages
packages <- c("tidyverse", "infer", "fst") # 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.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ 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
## [[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"
ess <- read_fst("All-ESS-Data.fst")
#Observations in the cleaned and subsetted data
# Load necessary packages
library(dplyr) # for data manipulation
library(haven) # for reading ESS data files
# Data cleaning and subset for variables of interest
cleaned_subset <- ess %>%
select(trstplt, imdfetn, netusoft, cntry) %>%
filter(!is.na(trstplt) & !is.na(imdfetn) & !is.na(netusoft)) # Remove rows with NA values in any selected variable
# Subset the cleaned data for France
subset_data <- cleaned_subset %>%
filter(cntry == "FR") # Filter data for France
cat("\n\nNumber of observations in the cleaned and subsetted data for France:\n")
##
##
## Number of observations in the cleaned and subsetted data for France:
n_obs <- nrow(subset_data)
print(n_obs)
## [1] 6057
#Number of observations in the cleaned and subsetted data for France:6057
##Descriptive statistics
# Assuming you've loaded the ESS dataset and the necessary libraries
# Replace 'ess_data' with the name of your dataset
library(modelsummary)
# Subset the data to include only the variables of interest
ess_subset <- ess %>%
select(imdfetn, trstplt, netusoft)
# Generate summary statistics using datasummary_skim
summary_table <- datasummary_skim(ess_subset)
# View the summary table
summary_table
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| imdfetn | 8 | 0 | 2.7 | 1.4 | 1.0 | 2.0 | 9.0 | |
| trstplt | 14 | 0 | 5.2 | 12.3 | 0.0 | 4.0 | 99.0 | |
| netusoft | 9 | 69 | 4.0 | 1.5 | 1.0 | 5.0 | 9.0 |
#imdfetn: Range of unique values: 1.0 to 9.0, Mean: 2.7, Standard deviation: 1.4, No missing values, Represents a scale with 8 unique values denoting perceptions or attitudes, with a mean score around 2.7 and moderate variability.
#trstplt:Range of unique values: 0.0 to 99.0, Mean: 5.2, Standard deviation: 12.3, No missing values, Indicates trust in something (possibly a political entity or institution), but there appears to be a wide range of values, with an average of 5.2 and notably high variability (SD = 12.3). The maximum value of 99.0 might indicate outliers or coding issues.
#netusoft:Range of unique values: 1.0 to 9.0, Mean: 4.0, Standard deviation: 1.5, 69% missing values, Measures something related to internet use or access, displaying moderate variability with a mean score of 4.0. However, caution is necessary due to a high percentage of missing data (69%).
##Calculate the Test Statistic
# Check for missing values in the relevant columns
sum(is.na(ess$trstplt) | is.na(ess$netusoft) | is.na(ess$imdfetn))
## [1] 338625
# Calculate the correlation matrix
correlation_matrix <- cor(ess[c("trstplt", "netusoft", "imdfetn")], use = "pairwise.complete.obs")
# Extract the correlation coefficient of interest (between trust, media, and cultural integration)
correlation_coefficient <- correlation_matrix[2, 3] # Assuming trust is the second and media is the third variable
print(correlation_coefficient)
## [1] -0.1841749
#Correlation Coefficent
# Load necessary libraries if not loaded already
library(infer)
library(dplyr)
# Step 1: Calculate the correlation matrix
correlation_matrix <- ess %>%
select(trstplt, netusoft, imdfetn) %>%
cor(use = "pairwise.complete.obs")
# Step 2: Extract the correlation coefficient of interest (between trstplt, netusoft, and imdfetn)
correlation_coefficient <- correlation_matrix[1:2, 3] # Assuming trstplt is the first variable, netusoft is the second, and imdfetn is the third
print(correlation_coefficient)
## trstplt netusoft
## 0.07928832 -0.18417489
##Regression modelling output
#Model 1
# Model 1: Single predictor (trstplt)
model_1 <- lm(imdfetn ~ trstplt, data = ess)
summary(model_1)
##
## Call:
## lm(formula = imdfetn ~ trstplt, data = ess)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.5179 -0.6860 -0.6417 0.3494 6.3583
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.6417121 0.0021197 1246.27 <2e-16 ***
## trstplt 0.0088506 0.0001591 55.61 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.364 on 488892 degrees of freedom
## (1661 observations deleted due to missingness)
## Multiple R-squared: 0.006287, Adjusted R-squared: 0.006285
## F-statistic: 3093 on 1 and 488892 DF, p-value: < 2.2e-16
#Model 2
# Model 2: Two predictors (trstplt and netusoft)
model_2 <- lm(imdfetn ~ trstplt + netusoft, data = ess)
summary(model_2)
##
## Call:
## lm(formula = imdfetn ~ trstplt + netusoft, data = ess)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.5628 -0.5853 -0.4023 0.5868 7.2258
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.1749017 0.0093010 341.35 <2e-16 ***
## trstplt 0.0054898 0.0003084 17.80 <2e-16 ***
## netusoft -0.1556282 0.0021340 -72.93 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.278 on 151927 degrees of freedom
## (338625 observations deleted due to missingness)
## Multiple R-squared: 0.03593, Adjusted R-squared: 0.03592
## F-statistic: 2831 on 2 and 151927 DF, p-value: < 2.2e-16
#Model 3
# Model 3: Interaction model (trstplt and netusoft with interaction term)
model_3 <- lm(imdfetn ~ trstplt * netusoft, data = ess)
summary(model_3)
##
## Call:
## lm(formula = imdfetn ~ trstplt * netusoft, data = ess)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.4856 -0.5855 -0.4006 0.5876 7.2342
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.1809661 0.0098697 322.296 < 2e-16 ***
## trstplt 0.0043602 0.0006881 6.337 2.35e-10 ***
## netusoft -0.1572424 0.0023079 -68.133 < 2e-16 ***
## trstplt:netusoft 0.0003053 0.0001662 1.837 0.0663 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.278 on 151926 degrees of freedom
## (338625 observations deleted due to missingness)
## Multiple R-squared: 0.03595, Adjusted R-squared: 0.03593
## F-statistic: 1889 on 3 and 151926 DF, p-value: < 2.2e-16
#Model 1: Single Predictor (trstplt): Coefficient Interpretation:The intercept (2.64) represents the predicted value of imdfetn when trstplt is zero. It’s statistically significant. The coefficient for trstplt (0.00885) indicates that for a one-unit increase in trstplt, imdfetn is expected to increase by approximately 0.00885 units. This coefficient is highly statistically significant (p < 0.001). Model Fit: The model explains a small proportion of the variance in imdfetn (Adjusted R-squared: 0.0063).The F-statistic is significant (p < 0.001), suggesting that the model is statistically significant.
#Model 2: Two Predictors (trstplt and netusoft): Coefficient Interpretation: The intercept (3.17) represents the predicted value of imdfetn when both trstplt and netusoft are zero. It’s statistically significant. The coefficient for trstplt (0.00549) suggests that a one-unit increase in trstplt leads to an approximately 0.00549 increase in imdfetn, holding other variables constant. It’s statistically significant. The coefficient for netusoft (-0.1556) indicates that a one-unit increase in netusoft leads to a decrease of approximately 0.1556 units in imdfetn, holding other variables constant. It’s statistically significant. Model Fit: The model explains a small proportion of the variance in imdfetn (Adjusted R-squared: 0.0359). The F-statistic is significant (p < 0.001), indicating that the model is statistically significant.
#Model 3: Interaction Model (trstplt and netusoft with interaction term) Coefficient Interpretation: All coefficients are statistically significant except for the interaction term (trstplt:netusoft). The interaction term (0.000305) suggests that the effect of the interaction between trstplt and netusoft on imdfetn is not statistically significant (p = 0.0663). Model Fit: Similar to Model 2, this model explains a small proportion of the variance in imdfetn (Adjusted R-squared: 0.0359). The F-statistic is significant (p < 0.001), indicating that the overall model is statistically significant. General Assessment: All models show statistically significant relationships between predictors and the outcome variable, though the effect sizes are relatively small. The interaction term in Model 3 doesn’t seem to significantly impact imdfetn, as indicated by its non-significant p-value. The models have low Adjusted R-squared values, suggesting that these predictors might not fully explain the variability in imdfetn.
##Third visual
library(ggplot2)
# Create an interaction plot
ggplot(ess, aes(x = trstplt, y = imdfetn, color = netusoft)) +
geom_point() +
stat_smooth(method = "lm", formula = y ~ x, se = FALSE) +
labs(
x = "Trust in Governmental Institutions",
y = "Perceptions of Cultural Integration",
color = "Media Exposure"
) +
ggtitle("Interaction Effect: Trust in Institutions, Media Exposure, and Cultural Integration")
## Warning: Removed 1661 rows containing non-finite values (`stat_smooth()`).
## Warning: The following aesthetics were dropped during statistical transformation: colour
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?
## Warning: Removed 1661 rows containing missing values (`geom_point()`).
#Trust in Governmental Institutions and Cultural Integration: As trust in governmental institutions increases (on the x-axis), there’s a slight positive trend in perceptions of cultural integration (on the y-axis). For instance: For individuals with low media exposure (indicated by a specific color line), higher trust in institutions appears to correspond to slightly more positive perceptions of cultural integration compared to those with lower trust. However, the impact of trust on perceptions varies; while the relationship is positive, it’s not necessarily substantial.
#Influence of Media Exposure: Different colors (representing varied levels of media exposure) showcase how this factor influences the relationship between trust in institutions and cultural integration.Higher media exposure seems to amplify the positive relationship between trust in institutions and cultural integration, suggesting that media exposure might magnify the impact of trust in shaping perceptions of cultural integration.
#Interaction Effect:The interaction effect between trust in institutions and media exposure is evident in the varying slopes across different colored lines.For instance, a steeper slope (indicating a stronger relationship) for a specific media exposure level might imply that, at that level, trust in institutions has a more pronounced impact on perceptions of cultural integration compared to other media exposure levels.
#Overall Insight:This visualization highlights that trust in governmental institutions might have a slight positive association with favorable attitudes toward cultural integration.The interaction plot illustrates that media exposure could potentially moderate or strengthen the impact of trust in institutions on cultural integration perceptions, but the relationship isn’t uniformly strong across all levels of media exposure.