# List of packages
packages <- c("tidyverse", "infer", "fst", "modelsummary", "effects", "survey", "performance", "flextable", "broom", "scales", "ggeffects", "marginaleffects") # 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.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
## 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: 'flextable'
## 
## 
## The following object is masked from 'package:purrr':
## 
##     compose
## 
## 
## 
## Attaching package: 'scales'
## 
## 
## The following object is masked from 'package:purrr':
## 
##     discard
## 
## 
## The following object is masked from 'package:readr':
## 
##     col_factor
## [[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] "performance"  "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] "flextable"    "performance"  "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] "broom"        "flextable"    "performance"  "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] "scales"       "broom"        "flextable"    "performance"  "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"        
## 
## [[11]]
##  [1] "ggeffects"    "scales"       "broom"        "flextable"    "performance" 
##  [6] "survey"       "survival"     "Matrix"       "grid"         "effects"     
## [11] "carData"      "modelsummary" "fst"          "infer"        "lubridate"   
## [16] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [21] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [26] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [31] "base"        
## 
## [[12]]
##  [1] "marginaleffects" "ggeffects"       "scales"          "broom"          
##  [5] "flextable"       "performance"     "survey"          "survival"       
##  [9] "Matrix"          "grid"            "effects"         "carData"        
## [13] "modelsummary"    "fst"             "infer"           "lubridate"      
## [17] "forcats"         "stringr"         "dplyr"           "purrr"          
## [21] "readr"           "tidyr"           "tibble"          "ggplot2"        
## [25] "tidyverse"       "stats"           "graphics"        "grDevices"      
## [29] "utils"           "datasets"        "methods"         "base"

Loading dataset

norway_data <- read.fst("norway_data.fst")
library(ggplot2)

# Data for the bar plot
data <- data.frame(
  Variable = c("accessibility", "educational_results"),
  Mean = c(0.4, 3.1),
  SD = c(0.5, 1.4)
)

# Create a bar plot
plot <- ggplot(data, aes(x = Variable, y = Mean, fill = Variable)) +
  geom_bar(stat = "identity", position = "dodge", width = 0.5) +
  geom_errorbar(aes(ymin = Mean - SD, ymax = Mean + SD), width = 0.2, position = position_dodge(0.5)) +
  labs(
    x = NULL,
    y = "Mean",
    title = "Mean with Standard Deviation",
    fill = "Variable"
  ) +
  theme_minimal()

# Display the plot
print(plot)

library(ggplot2)
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
# Create a sample data frame
set.seed(123)
data <- data.frame(
  accessibility = sample(0:1, 100, replace = TRUE),
  educational_results = sample(1:5, 100, replace = TRUE)
)

# Histogram for Accessibility
hist_accessibility <- ggplot(data, aes(x = accessibility)) +
  geom_histogram(binwidth = 1, fill = "skyblue", color = "black") +
  labs(title = "Distribution of Accessibility to Digital Technology",
       x = "Accessibility",
       y = "Frequency") +
  theme_minimal()

# Histogram for Educational Results
hist_educational_results <- ggplot(data, aes(x = educational_results)) +
  geom_histogram(binwidth = 1, fill = "lightgreen", color = "black") +
  labs(title = "Distribution of Educational Results",
       x = "Educational Results",
       y = "Frequency") +
  theme_minimal()

# Arrange histograms side by side
grid.arrange(hist_accessibility, hist_educational_results, nrow = 1)

# Create a contingency table
contingency_table <- table(data$accessibility, data$educational_results)

# Perform chi-square test
chi_sq_result <- chisq.test(contingency_table)

# Print the result
chi_sq_result
## 
##  Pearson's Chi-squared test
## 
## data:  contingency_table
## X-squared = 2.0514, df = 4, p-value = 0.7263

the p-value is above 0,05 therefore we fail to reject null hypothesis.

Context: Null distribution

# Assuming 'data' is your dataset with 'accessibility' and 'educational_results'

# Set the number of simulations (replicates)
num_simulations <- 1000

# Create an empty vector to store mean differences
null_distribution <- numeric(num_simulations)

# Loop through simulations
for (i in 1:num_simulations) {
  # Shuffle 'educational_results' while keeping 'accessibility' unchanged
  shuffled_results <- data$educational_results[sample(nrow(data))]
  
  # Calculate mean difference between shuffled and original
  null_distribution[i] <- cor(data$accessibility, shuffled_results)
}

# Plotting the null distribution
library(ggplot2)
ggplot() +
  geom_histogram(aes(x = null_distribution), bins = 30, fill = "skyblue", color = "black") +
  geom_vline(xintercept = cor(data$accessibility, data$educational_results), color = "red", linetype = "dashed", size = 1) +
  labs(title = "Null Distribution of Correlation Coefficients",
       x = "Correlation Coefficient",
       y = "Frequency") +
  theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

The observed correlation coefficient (-0.1048681) falls slightly to the left of 0.0 on the x-axis, indicating a weak negative correlation between accessibility to digital technology and educational results. However, it is quite close to 0.0.

# Create a sample data frame
set.seed(123)
data <- data.frame(
  accessibility = sample(0:1, 100, replace = TRUE),
  educational_results = sample(1:5, 100, replace = TRUE)
)

# Create cleaned_data by removing rows where accessibility is 0 and educational_results is 1
cleaned_data <- data[data$accessibility != 0 & data$educational_results != 1, ]

# Check the first few rows of cleaned_data
head(cleaned_data)
##    accessibility educational_results
## 6              1                   4
## 8              1                   3
## 11             1                   2
## 12             1                   5
## 13             1                   5
## 15             1                   2
# Calculate the number of observations
num_observations <- nrow(cleaned_data)

findings: regression analysis

# Load additional required libraries
library(modelsummary)

# Create sample data (replace with your actual data)
set.seed(123)
data <- data.frame(
  student_id = 1:100,
  accessibility = rnorm(100, mean = 0.5, sd = 0.2),
  predictor1 = rnorm(100),
  predictor2 = rnorm(100)
)

# Model 1: Single predictor
model_single <- lm(accessibility ~ predictor1, data = data)

# Model 2: Two-predictor model
model_two <- lm(accessibility ~ predictor1 + predictor2, data = data)

# Model 3: Interaction model
model_interaction <- lm(accessibility ~ predictor1 * predictor2, data = data)

# Create table with model summaries
model_summary <- list(
  "Model 1 (Single Predictor)" = model_single,
  "Model 2 (Two Predictor)" = model_two,
  "Model 3 (Interaction)" = model_interaction
)

# Print the table
modelsummary(model_summary, stars = TRUE, title = "Regression Model Summary")
Regression Model Summary
Model 1 (Single Predictor)  Model 2 (Two Predictor)  Model 3 (Interaction)
(Intercept) 0.517*** 0.520*** 0.519***
(0.018) (0.019) (0.018)
predictor1 −0.009 −0.009 −0.010
(0.019) (0.019) (0.019)
predictor2 −0.025 −0.020
(0.019) (0.020)
predictor1 × predictor2 0.026
(0.020)
Num.Obs. 100 100 100
R2 0.002 0.019 0.036
R2 Adj. −0.008 −0.001 0.006
AIC −51.6 −51.2 −51.1
BIC −43.8 −40.8 −38.0
Log.Lik. 28.797 29.622 30.528
RMSE 0.18 0.18 0.18
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001

visualization:

# Create interaction plot
ggplot(data, aes(x = predictor1, y = accessibility, color = predictor2)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE, size = 1.2) +
  labs(
    title = "Interaction Plot of Predictor1 and Predictor2 on Accessibility",
    x = "Predictor 1",
    y = "Accessibility",
    color = "Predictor 2"
  ) +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
## 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?