R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Description of Data

Results

## ── 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() ──
## ✖ purrr::compose() masks flextable::compose()
## ✖ 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: viridisLite
## 
## 
## Attaching package: 'kableExtra'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     group_rows
## 
## 
## The following objects are masked from 'package:flextable':
## 
##     as_image, footnote
## [[1]]
## [1] "flextable" "stats"     "graphics"  "grDevices" "utils"     "datasets" 
## [7] "methods"   "base"     
## 
## [[2]]
## [1] "broom"     "flextable" "stats"     "graphics"  "grDevices" "utils"    
## [7] "datasets"  "methods"   "base"     
## 
## [[3]]
##  [1] "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"     "readr"    
##  [7] "tidyr"     "tibble"    "ggplot2"   "tidyverse" "broom"     "flextable"
## [13] "stats"     "graphics"  "grDevices" "utils"     "datasets"  "methods"  
## [19] "base"     
## 
## [[4]]
##  [1] "infer"     "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"    
##  [7] "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse" "broom"    
## [13] "flextable" "stats"     "graphics"  "grDevices" "utils"     "datasets" 
## [19] "methods"   "base"     
## 
## [[5]]
##  [1] "fst"       "infer"     "lubridate" "forcats"   "stringr"   "dplyr"    
##  [7] "purrr"     "readr"     "tidyr"     "tibble"    "ggplot2"   "tidyverse"
## [13] "broom"     "flextable" "stats"     "graphics"  "grDevices" "utils"    
## [19] "datasets"  "methods"   "base"     
## 
## [[6]]
##  [1] "modelsummary" "fst"          "infer"        "lubridate"    "forcats"     
##  [6] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [11] "tibble"       "ggplot2"      "tidyverse"    "broom"        "flextable"   
## [16] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [21] "methods"      "base"        
## 
## [[7]]
##  [1] "effects"      "carData"      "modelsummary" "fst"          "infer"       
##  [6] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [11] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [16] "broom"        "flextable"    "stats"        "graphics"     "grDevices"   
## [21] "utils"        "datasets"     "methods"      "base"        
## 
## [[8]]
##  [1] "effects"      "carData"      "modelsummary" "fst"          "infer"       
##  [6] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [11] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [16] "broom"        "flextable"    "stats"        "graphics"     "grDevices"   
## [21] "utils"        "datasets"     "methods"      "base"        
## 
## [[9]]
##  [1] "effects"      "carData"      "modelsummary" "fst"          "infer"       
##  [6] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [11] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [16] "broom"        "flextable"    "stats"        "graphics"     "grDevices"   
## [21] "utils"        "datasets"     "methods"      "base"        
## 
## [[10]]
##  [1] "effects"      "carData"      "modelsummary" "fst"          "infer"       
##  [6] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [11] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [16] "broom"        "flextable"    "stats"        "graphics"     "grDevices"   
## [21] "utils"        "datasets"     "methods"      "base"        
## 
## [[11]]
##  [1] "effects"      "carData"      "modelsummary" "fst"          "infer"       
##  [6] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [11] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [16] "broom"        "flextable"    "stats"        "graphics"     "grDevices"   
## [21] "utils"        "datasets"     "methods"      "base"        
## 
## [[12]]
##  [1] "RColorBrewer" "effects"      "carData"      "modelsummary" "fst"         
##  [6] "infer"        "lubridate"    "forcats"      "stringr"      "dplyr"       
## [11] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [16] "tidyverse"    "broom"        "flextable"    "stats"        "graphics"    
## [21] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[13]]
##  [1] "RColorBrewer" "effects"      "carData"      "modelsummary" "fst"         
##  [6] "infer"        "lubridate"    "forcats"      "stringr"      "dplyr"       
## [11] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [16] "tidyverse"    "broom"        "flextable"    "stats"        "graphics"    
## [21] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[14]]
##  [1] "RColorBrewer" "effects"      "carData"      "modelsummary" "fst"         
##  [6] "infer"        "lubridate"    "forcats"      "stringr"      "dplyr"       
## [11] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [16] "tidyverse"    "broom"        "flextable"    "stats"        "graphics"    
## [21] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[15]]
##  [1] "viridis"      "viridisLite"  "RColorBrewer" "effects"      "carData"     
##  [6] "modelsummary" "fst"          "infer"        "lubridate"    "forcats"     
## [11] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [16] "tibble"       "ggplot2"      "tidyverse"    "broom"        "flextable"   
## [21] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [26] "methods"      "base"        
## 
## [[16]]
##  [1] "knitr"        "viridis"      "viridisLite"  "RColorBrewer" "effects"     
##  [6] "carData"      "modelsummary" "fst"          "infer"        "lubridate"   
## [11] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [16] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "broom"       
## [21] "flextable"    "stats"        "graphics"     "grDevices"    "utils"       
## [26] "datasets"     "methods"      "base"        
## 
## [[17]]
##  [1] "kableExtra"   "knitr"        "viridis"      "viridisLite"  "RColorBrewer"
##  [6] "effects"      "carData"      "modelsummary" "fst"          "infer"       
## [11] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [16] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [21] "broom"        "flextable"    "stats"        "graphics"     "grDevices"   
## [26] "utils"        "datasets"     "methods"      "base"        
## 
## [[18]]
##  [1] "rmarkdown"    "kableExtra"   "knitr"        "viridis"      "viridisLite" 
##  [6] "RColorBrewer" "effects"      "carData"      "modelsummary" "fst"         
## [11] "infer"        "lubridate"    "forcats"      "stringr"      "dplyr"       
## [16] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [21] "tidyverse"    "broom"        "flextable"    "stats"        "graphics"    
## [26] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[19]]
##  [1] "ggridges"     "rmarkdown"    "kableExtra"   "knitr"        "viridis"     
##  [6] "viridisLite"  "RColorBrewer" "effects"      "carData"      "modelsummary"
## [11] "fst"          "infer"        "lubridate"    "forcats"      "stringr"     
## [16] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [21] "ggplot2"      "tidyverse"    "broom"        "flextable"    "stats"       
## [26] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [31] "base"        
## 
## [[20]]
##  [1] "ggridges"     "rmarkdown"    "kableExtra"   "knitr"        "viridis"     
##  [6] "viridisLite"  "RColorBrewer" "effects"      "carData"      "modelsummary"
## [11] "fst"          "infer"        "lubridate"    "forcats"      "stringr"     
## [16] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [21] "ggplot2"      "tidyverse"    "broom"        "flextable"    "stats"       
## [26] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [31] "base"        
## 
## [[21]]
##  [1] "questionr"    "ggridges"     "rmarkdown"    "kableExtra"   "knitr"       
##  [6] "viridis"      "viridisLite"  "RColorBrewer" "effects"      "carData"     
## [11] "modelsummary" "fst"          "infer"        "lubridate"    "forcats"     
## [16] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [21] "tibble"       "ggplot2"      "tidyverse"    "broom"        "flextable"   
## [26] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [31] "methods"      "base"
france_data <- ess 
france_data <- ess %>% filter(cntry == "FR")
france_data <- ess %>% filter(cntry == "FR") %>% mutate(trstplc = ifelse(trstplc %in% c(77, 88, 99), NA, trstplc))
france_data <- france_data %>% filter(cntry == "FR") %>%
  mutate(
    # Recoding age variable, setting 999 to NA
    age = ifelse(agea == 999, NA, agea),
    # Recoding cohort variable, setting years before 1900 and after 2000 to NA
    cohort = ifelse(yrbrn < 1930 | yrbrn > 2000, NA, yrbrn),
    # Recoding generational cohorts based on year of birth
    gen = case_when(
      yrbrn %in% 1900:1964 ~ "1",
      yrbrn %in% 1965:1996 ~ "2",
      TRUE ~ as.character(cohort)  # Keeping other values as character if they do not fit the ranges
    ),
    # Converting cohort to a factor with labels
    gen = factor(gen,
                 levels = c("1", "2"),
                 labels = c("youth", "adult"))
  )
france_data <- france_data %>% filter(cntry == "FR") %>%
# Recoding question of belonging to ethnic minority
    mutate(minority = case_when(
      blgetmg %in% c(7, 8, 9) ~ NA_character_,
      blgetmg == 1 ~ "Ethnic Minority",
      blgetmg == 2 ~ "Not Ethnic Minority",
      TRUE ~ as.character(blgetmg)  # Convert other values to character if necessary
    ))
# here's how to make sure it's really cleaned
france_data <- france_data %>% filter(!is.na(trstplc))
france_data <- france_data %>% filter(!is.na(minority))
france_data <- france_data %>% filter(!is.na(gen))

# double-checking + quick distribution
#unique(france_data$trstplc)
#table(france_data$trstplc)
#table(france_data$gen)
#Plotting outcome and explanatory. 
ggplot(france_data, mapping = aes(x = gen, fill = trstplc)) + geom_bar()
## Warning: The following aesthetics were dropped during statistical transformation: fill
## ℹ 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?

france_data_shuffled <- france_data %>% 
  group_by(gen, trstplc) %>% 
  tally()

ggplot(france_data_shuffled, mapping = aes(x = gen, fill = trstplc)) + geom_bar()
## Warning: The following aesthetics were dropped during statistical transformation: fill
## ℹ 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?

# data summary skim
datasummary_skim(france_data %>% select(trstplc, yrbrn, blgetmg))
Unique (#) Missing (%) Mean SD Min Median Max
trstplc 11 0 6.0 2.2 0.0 6.0 10.0
yrbrn 89 0 1960.1 18.3 1905.0 1960.0 1996.0
blgetmg 2 0 2.0 0.2 1.0 2.0 2.0
#Cleaning average trust in police by summarizing averages, grouping by al countries, filtering answers with refusal, don't know, and No answer.
country_averages <- ess %>%
  filter(!(trstplc %in% c(77, 88, 99))) %>%
  group_by(cntry) %>%
  summarize(avg_trstplc = mean(trstplc, na.rm = TRUE))


# Reorder the factor levels of 'cntry' based on 'avg_trstplc'
country_averages$cntry <- reorder(country_averages$cntry, country_averages$avg_trstplc)

# Plotting the factor levels of 'cntry' based on 'avg_trstplc.'
Main_Trend_of_Country_Feeling_Close_to_Trust_in_Police <- ggplot(country_averages, aes(x = cntry, y = avg_trstplc, label = cntry)) +
  geom_point(aes(color = avg_trstplc), size = 3) + 
  geom_text(nudge_y = 0.2, size = 3) + 
  labs(
    x = "Country",
    y = "Average Trust in Police",
    title = "Main Trend of Average Trust in Police by Country"
  )  +
  scale_color_gradient(low = "green", high = "darkgreen") +
  geom_line(aes(group = 1), color = "darkgreen", linetype = "dashed")
  theme_minimal() +
  theme(
    axis.text.x = element_blank(),
    axis.title.x = element_text(face="bold", size=14))
## List of 97
##  $ line                      :List of 6
##   ..$ colour       : chr "black"
##   ..$ linewidth    : num 0.5
##   ..$ linetype     : num 1
##   ..$ lineend      : chr "butt"
##   ..$ arrow        : logi FALSE
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_line" "element"
##  $ rect                      :List of 5
##   ..$ fill         : chr "white"
##   ..$ colour       : chr "black"
##   ..$ linewidth    : num 0.5
##   ..$ linetype     : num 1
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_rect" "element"
##  $ text                      :List of 11
##   ..$ family       : chr ""
##   ..$ face         : chr "plain"
##   ..$ colour       : chr "black"
##   ..$ size         : num 11
##   ..$ hjust        : num 0.5
##   ..$ vjust        : num 0.5
##   ..$ angle        : num 0
##   ..$ lineheight   : num 0.9
##   ..$ margin       : 'margin' num [1:4] 0points 0points 0points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : logi FALSE
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ title                     : NULL
##  $ aspect.ratio              : NULL
##  $ axis.title                : NULL
##  $ axis.title.x              :List of 11
##   ..$ family       : NULL
##   ..$ face         : chr "bold"
##   ..$ colour       : NULL
##   ..$ size         : num 14
##   ..$ hjust        : NULL
##   ..$ vjust        : num 1
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 2.75points 0points 0points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi FALSE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.title.x.top          :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : NULL
##   ..$ vjust        : num 0
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 0points 2.75points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.title.x.bottom       : NULL
##  $ axis.title.y              :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : NULL
##   ..$ vjust        : num 1
##   ..$ angle        : num 90
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 2.75points 0points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.title.y.left         : NULL
##  $ axis.title.y.right        :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : NULL
##   ..$ vjust        : num 0
##   ..$ angle        : num -90
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 0points 0points 2.75points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.text                 :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : chr "grey30"
##   ..$ size         : 'rel' num 0.8
##   ..$ hjust        : NULL
##   ..$ vjust        : NULL
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : NULL
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.text.x               : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ axis.text.x.top           :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : NULL
##   ..$ vjust        : num 0
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 0points 2.2points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.text.x.bottom        : NULL
##  $ axis.text.y               :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : num 1
##   ..$ vjust        : NULL
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 2.2points 0points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.text.y.left          : NULL
##  $ axis.text.y.right         :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : num 0
##   ..$ vjust        : NULL
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 0points 0points 2.2points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ axis.ticks                : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ axis.ticks.x              : NULL
##  $ axis.ticks.x.top          : NULL
##  $ axis.ticks.x.bottom       : NULL
##  $ axis.ticks.y              : NULL
##  $ axis.ticks.y.left         : NULL
##  $ axis.ticks.y.right        : NULL
##  $ axis.ticks.length         : 'simpleUnit' num 2.75points
##   ..- attr(*, "unit")= int 8
##  $ axis.ticks.length.x       : NULL
##  $ axis.ticks.length.x.top   : NULL
##  $ axis.ticks.length.x.bottom: NULL
##  $ axis.ticks.length.y       : NULL
##  $ axis.ticks.length.y.left  : NULL
##  $ axis.ticks.length.y.right : NULL
##  $ axis.line                 : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ axis.line.x               : NULL
##  $ axis.line.x.top           : NULL
##  $ axis.line.x.bottom        : NULL
##  $ axis.line.y               : NULL
##  $ axis.line.y.left          : NULL
##  $ axis.line.y.right         : NULL
##  $ legend.background         : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ legend.margin             : 'margin' num [1:4] 5.5points 5.5points 5.5points 5.5points
##   ..- attr(*, "unit")= int 8
##  $ legend.spacing            : 'simpleUnit' num 11points
##   ..- attr(*, "unit")= int 8
##  $ legend.spacing.x          : NULL
##  $ legend.spacing.y          : NULL
##  $ legend.key                : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ legend.key.size           : 'simpleUnit' num 1.2lines
##   ..- attr(*, "unit")= int 3
##  $ legend.key.height         : NULL
##  $ legend.key.width          : NULL
##  $ legend.text               :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : 'rel' num 0.8
##   ..$ hjust        : NULL
##   ..$ vjust        : NULL
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : NULL
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ legend.text.align         : NULL
##  $ legend.title              :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : num 0
##   ..$ vjust        : NULL
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : NULL
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ legend.title.align        : NULL
##  $ legend.position           : chr "right"
##  $ legend.direction          : NULL
##  $ legend.justification      : chr "center"
##  $ legend.box                : NULL
##  $ legend.box.just           : NULL
##  $ legend.box.margin         : 'margin' num [1:4] 0cm 0cm 0cm 0cm
##   ..- attr(*, "unit")= int 1
##  $ legend.box.background     : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ legend.box.spacing        : 'simpleUnit' num 11points
##   ..- attr(*, "unit")= int 8
##  $ panel.background          : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ panel.border              : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ panel.spacing             : 'simpleUnit' num 5.5points
##   ..- attr(*, "unit")= int 8
##  $ panel.spacing.x           : NULL
##  $ panel.spacing.y           : NULL
##  $ panel.grid                :List of 6
##   ..$ colour       : chr "grey92"
##   ..$ linewidth    : NULL
##   ..$ linetype     : NULL
##   ..$ lineend      : NULL
##   ..$ arrow        : logi FALSE
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_line" "element"
##  $ panel.grid.major          : NULL
##  $ panel.grid.minor          :List of 6
##   ..$ colour       : NULL
##   ..$ linewidth    : 'rel' num 0.5
##   ..$ linetype     : NULL
##   ..$ lineend      : NULL
##   ..$ arrow        : logi FALSE
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_line" "element"
##  $ panel.grid.major.x        : NULL
##  $ panel.grid.major.y        : NULL
##  $ panel.grid.minor.x        : NULL
##  $ panel.grid.minor.y        : NULL
##  $ panel.ontop               : logi FALSE
##  $ plot.background           : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ plot.title                :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : 'rel' num 1.2
##   ..$ hjust        : num 0
##   ..$ vjust        : num 1
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 0points 5.5points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ plot.title.position       : chr "panel"
##  $ plot.subtitle             :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : num 0
##   ..$ vjust        : num 1
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 0points 0points 5.5points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ plot.caption              :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : 'rel' num 0.8
##   ..$ hjust        : num 1
##   ..$ vjust        : num 1
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 5.5points 0points 0points 0points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ plot.caption.position     : chr "panel"
##  $ plot.tag                  :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : 'rel' num 1.2
##   ..$ hjust        : num 0.5
##   ..$ vjust        : num 0.5
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : NULL
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ plot.tag.position         : chr "topleft"
##  $ plot.margin               : 'margin' num [1:4] 5.5points 5.5points 5.5points 5.5points
##   ..- attr(*, "unit")= int 8
##  $ strip.background          : list()
##   ..- attr(*, "class")= chr [1:2] "element_blank" "element"
##  $ strip.background.x        : NULL
##  $ strip.background.y        : NULL
##  $ strip.clip                : chr "inherit"
##  $ strip.placement           : chr "inside"
##  $ strip.text                :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : chr "grey10"
##   ..$ size         : 'rel' num 0.8
##   ..$ hjust        : NULL
##   ..$ vjust        : NULL
##   ..$ angle        : NULL
##   ..$ lineheight   : NULL
##   ..$ margin       : 'margin' num [1:4] 4.4points 4.4points 4.4points 4.4points
##   .. ..- attr(*, "unit")= int 8
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ strip.text.x              : NULL
##  $ strip.text.x.bottom       : NULL
##  $ strip.text.x.top          : NULL
##  $ strip.text.y              :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : NULL
##   ..$ vjust        : NULL
##   ..$ angle        : num -90
##   ..$ lineheight   : NULL
##   ..$ margin       : NULL
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ strip.text.y.left         :List of 11
##   ..$ family       : NULL
##   ..$ face         : NULL
##   ..$ colour       : NULL
##   ..$ size         : NULL
##   ..$ hjust        : NULL
##   ..$ vjust        : NULL
##   ..$ angle        : num 90
##   ..$ lineheight   : NULL
##   ..$ margin       : NULL
##   ..$ debug        : NULL
##   ..$ inherit.blank: logi TRUE
##   ..- attr(*, "class")= chr [1:2] "element_text" "element"
##  $ strip.text.y.right        : NULL
##  $ strip.switch.pad.grid     : 'simpleUnit' num 2.75points
##   ..- attr(*, "unit")= int 8
##  $ strip.switch.pad.wrap     : 'simpleUnit' num 2.75points
##   ..- attr(*, "unit")= int 8
##  - attr(*, "class")= chr [1:2] "theme" "gg"
##  - attr(*, "complete")= logi TRUE
##  - attr(*, "validate")= logi TRUE
  print(Main_Trend_of_Country_Feeling_Close_to_Trust_in_Police)

#print("Bivariate statistics and visualizations")
table(france_data$trstplc, france_data$gen) %>%
 cprop()
##        
##         youth adult All  
##   0       2.4   3.5   2.9
##   1       1.6   1.8   1.7
##   2       3.6   4.0   3.8
##   3       5.5   6.1   5.8
##   4       6.9   7.4   7.1
##   5      18.1  17.1  17.7
##   6      14.1  14.3  14.2
##   7      18.7  20.6  19.5
##   8      19.9  16.6  18.5
##   9       5.3   5.5   5.4
##   10      4.0   3.1   3.6
##   Total 100.0 100.0 100.0
filtered_data_cleaned <- france_data %>% filter(!is.na(trstplc), !is.na(gen))

# Compute percentages
percentages <- filtered_data_cleaned %>%
  group_by(gen, trstplc) %>%
  summarise(n = n()) %>%
  mutate(percentage = n/sum(n)*100)
## `summarise()` has grouped output by 'gen'. You can override using the `.groups`
## argument.
## `summarise()` has grouped output by 'wrkorg_recode'. You can override using the
#Check#unique(filtered_data_cleaned$gen)

## `.groups` argument.
Percentage_of_Attitudes_Towards_Police_by_Generations <- ggplot(percentages, aes(x = as.factor(gen), y = percentage, fill = factor(trstplc))) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.9), width = 0.7) +
  geom_text(aes(label = paste0(round(percentage, 1), "%"), group = trstplc),
            position = position_dodge(width = 0.9), vjust = -0.5, size = 3) + # Add rounded percentage labels
  labs(title = "Percentage of Attitudes Towards Police by Generations",
       x = "Attitude towards Police",
       y = "Percentage (%)") +
  scale_x_discrete(breaks = c("youth", "adult"), labels = c("youth", "adult")) +
  scale_fill_manual(values = c("10" = "lightgreen", "9" = "lightgreen", "8" = "lightgreen", "7" = "lightgreen", "6" = "lightgreen", "5" = "lightgreen", "4" = "lightgreen", "3" = "lightgreen", "2" = "lightgreen", "1" = "lightgreen", "0" = "lightgreen"),
                    name = "Trust in Police Levels") +
  theme_minimal()
  
  print(Percentage_of_Attitudes_Towards_Police_by_Generations)

# Step 1: Calculate the test statistic of your sample


test_stat <- france_data %>%
  specify(explanatory = gen, # change variable name for explanatory variable
          response = trstplc) %>% # change variable name for outcome of interest
  hypothesize(null = "independence") %>%
  calculate(stat = "t") # replace in between quotation marks appropriate test statistic
## Warning: The statistic is based on a difference or ratio; by default, for
## difference-based statistics, the explanatory variable is subtracted in the
## order "youth" - "adult", or divided in the order "youth" / "adult" for
## ratio-based statistics. To specify this order yourself, supply `order =
## c("youth", "adult")` to the calculate() function.
print(test_stat$stat)
##        t 
## 5.407069
# Step 2: Simulate the null distribution


null_distribution_gen<- france_data %>%
  specify(explanatory = gen,
          response = trstplc) %>%
  hypothesize(null = "independence") %>%
  generate(reps = 1000, type = "permute") %>% # only line we are adding, use reps = 1000 as standard
  calculate( stat = "t",  order = c("youth", "adult"))

null_distribution_gen
## Response: trstplc (numeric)
## Explanatory: gen (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
##    replicate    stat
##        <int>   <dbl>
##  1         1  1.57  
##  2         2  0.756 
##  3         3  0.371 
##  4         4  0.0688
##  5         5  0.775 
##  6         6 -0.485 
##  7         7  0.223 
##  8         8  0.329 
##  9         9  0.678 
## 10        10 -2.05  
## # ℹ 990 more rows
# Step 3: Calculate the p-value of your sample


p_val_gen <- null_distribution_gen %>% # replace name here if you assigned something other than null_distribution above
  get_pvalue(obs_stat = test_stat, direction = "two-sided") # would only replace test_stat if assigned another name in Step 1
## 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_gen
## # A tibble: 1 × 1
##   p_value
##     <dbl>
## 1       0
# Step 4: Visualize
null_distribution_gen %>%
  visualize() +
  shade_p_value(obs_stat = test_stat, direction = "two-sided")

null_distribution_gen
## Response: trstplc (numeric)
## Explanatory: gen (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
##    replicate    stat
##        <int>   <dbl>
##  1         1  1.57  
##  2         2  0.756 
##  3         3  0.371 
##  4         4  0.0688
##  5         5  0.775 
##  6         6 -0.485 
##  7         7  0.223 
##  8         8  0.329 
##  9         9  0.678 
## 10        10 -2.05  
## # ℹ 990 more rows
#We can also visualize alongside confidence intervals

conf_int <- null_distribution_gen %>%
  get_confidence_interval(level = 0.95, type = "percentile")


null_distribution_gen %>%
  visualize() +
  shade_p_value(obs_stat = test_stat, direction = "two-sided") +
  shade_confidence_interval(endpoints = conf_int)

null_distribution_gen
## Response: trstplc (numeric)
## Explanatory: gen (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
##    replicate    stat
##        <int>   <dbl>
##  1         1  1.57  
##  2         2  0.756 
##  3         3  0.371 
##  4         4  0.0688
##  5         5  0.775 
##  6         6 -0.485 
##  7         7  0.223 
##  8         8  0.329 
##  9         9  0.678 
## 10        10 -2.05  
## # ℹ 990 more rows
## Three models
# List of packages
packages <- c("tidyverse", "infer", "fst", "modelsummary", "effects", "survey", "MASS") # 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)
## 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
## [[1]]
##  [1] "fstcore"      "questionr"    "ggridges"     "rmarkdown"    "kableExtra"  
##  [6] "knitr"        "viridis"      "viridisLite"  "RColorBrewer" "effects"     
## [11] "carData"      "modelsummary" "fst"          "infer"        "lubridate"   
## [16] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [21] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "broom"       
## [26] "flextable"    "stats"        "graphics"     "grDevices"    "utils"       
## [31] "datasets"     "methods"      "base"        
## 
## [[2]]
##  [1] "fstcore"      "questionr"    "ggridges"     "rmarkdown"    "kableExtra"  
##  [6] "knitr"        "viridis"      "viridisLite"  "RColorBrewer" "effects"     
## [11] "carData"      "modelsummary" "fst"          "infer"        "lubridate"   
## [16] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [21] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "broom"       
## [26] "flextable"    "stats"        "graphics"     "grDevices"    "utils"       
## [31] "datasets"     "methods"      "base"        
## 
## [[3]]
##  [1] "fstcore"      "questionr"    "ggridges"     "rmarkdown"    "kableExtra"  
##  [6] "knitr"        "viridis"      "viridisLite"  "RColorBrewer" "effects"     
## [11] "carData"      "modelsummary" "fst"          "infer"        "lubridate"   
## [16] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [21] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "broom"       
## [26] "flextable"    "stats"        "graphics"     "grDevices"    "utils"       
## [31] "datasets"     "methods"      "base"        
## 
## [[4]]
##  [1] "fstcore"      "questionr"    "ggridges"     "rmarkdown"    "kableExtra"  
##  [6] "knitr"        "viridis"      "viridisLite"  "RColorBrewer" "effects"     
## [11] "carData"      "modelsummary" "fst"          "infer"        "lubridate"   
## [16] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [21] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "broom"       
## [26] "flextable"    "stats"        "graphics"     "grDevices"    "utils"       
## [31] "datasets"     "methods"      "base"        
## 
## [[5]]
##  [1] "fstcore"      "questionr"    "ggridges"     "rmarkdown"    "kableExtra"  
##  [6] "knitr"        "viridis"      "viridisLite"  "RColorBrewer" "effects"     
## [11] "carData"      "modelsummary" "fst"          "infer"        "lubridate"   
## [16] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [21] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "broom"       
## [26] "flextable"    "stats"        "graphics"     "grDevices"    "utils"       
## [31] "datasets"     "methods"      "base"        
## 
## [[6]]
##  [1] "survey"       "survival"     "Matrix"       "grid"         "fstcore"     
##  [6] "questionr"    "ggridges"     "rmarkdown"    "kableExtra"   "knitr"       
## [11] "viridis"      "viridisLite"  "RColorBrewer" "effects"      "carData"     
## [16] "modelsummary" "fst"          "infer"        "lubridate"    "forcats"     
## [21] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [26] "tibble"       "ggplot2"      "tidyverse"    "broom"        "flextable"   
## [31] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [36] "methods"      "base"        
## 
## [[7]]
##  [1] "MASS"         "survey"       "survival"     "Matrix"       "grid"        
##  [6] "fstcore"      "questionr"    "ggridges"     "rmarkdown"    "kableExtra"  
## [11] "knitr"        "viridis"      "viridisLite"  "RColorBrewer" "effects"     
## [16] "carData"      "modelsummary" "fst"          "infer"        "lubridate"   
## [21] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [26] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "broom"       
## [31] "flextable"    "stats"        "graphics"     "grDevices"    "utils"       
## [36] "datasets"     "methods"      "base"
model1 <- lm(trstplc ~ gen, data = france_data)
model2 <- lm(trstplc ~ gen + minority, data = france_data)
model3 <- lm(trstplc ~ gen + minority + gen*minority, data = france_data)
modelsummary(
  list(model1, model2, model3))
 (1)   (2)   (3)
(Intercept) 6.055 5.652 5.570
(0.023) (0.089) (0.139)
genadult −0.192 −0.178 −0.044
(0.035) (0.035) (0.177)
minorityNot Ethnic Minority 0.413 0.498
(0.088) (0.141)
genadult × minorityNot Ethnic Minority −0.140
(0.181)
Num.Obs. 16511 16511 16511
R2 0.002 0.003 0.003
R2 Adj. 0.002 0.003 0.003
AIC 73493.6 73473.8 73475.2
BIC 73516.8 73504.6 73513.7
Log.Lik. −36743.817 −36732.881 −36732.584
RMSE 2.24 2.24 2.24
model4 <- lm(trstplc ~ minority, data = france_data)
model5 <- lm(trstplc ~ minority + gen, data = france_data)
model6 <- lm(trstplc ~ minority + gen + minority*gen, data = france_data)
modelsummary(
  list(model4, model5, model6))
 (1)   (2)   (3)
(Intercept) 5.542 5.652 5.570
(0.086) (0.089) (0.139)
minorityNot Ethnic Minority 0.449 0.413 0.498
(0.088) (0.088) (0.141)
genadult −0.178 −0.044
(0.035) (0.177)
minorityNot Ethnic Minority × genadult −0.140
(0.181)
Num.Obs. 16511 16511 16511
R2 0.002 0.003 0.003
R2 Adj. 0.002 0.003 0.003
AIC 73497.3 73473.8 73475.2
BIC 73520.4 73504.6 73513.7
Log.Lik. −36745.632 −36732.881 −36732.584
RMSE 2.24 2.24 2.24
modelsummary(
  list(model1, model2, model3),
  fmt = 1,
  estimate  = c( "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}"),
  statistic = NULL,
  coef_omit = "Intercept")
 (1)   (2)   (3)
genadult −0.2 (0.0)*** −0.2 (0.0)*** 0.0 (0.2)
minorityNot Ethnic Minority 0.4 (0.1)*** 0.5 (0.1)***
genadult × minorityNot Ethnic Minority −0.1 (0.2)
Num.Obs. 16511 16511 16511
R2 0.002 0.003 0.003
R2 Adj. 0.002 0.003 0.003
AIC 73493.6 73473.8 73475.2
BIC 73516.8 73504.6 73513.7
Log.Lik. −36743.817 −36732.881 −36732.584
RMSE 2.24 2.24 2.24
### Seperate here

# We use the "effect" function to call the interaction term from model 1. We assign the results to "effect_plot"

interaction_plot_1 <- effect("gen", model1, na.rm=TRUE)

#We plot here the interaction term

plot(interaction_plot_1,
     main="Interaction effect the youth and adult x minority status",
     xlab="the youth and adult",
     ylab="Trust in Police")

# We use the "effect" function to call the interaction term from model 2. We assign the results to "effect_plot"

interaction_plot_2 <- effect("gen*minority", model2, na.rm=TRUE)
## NOTE: gen:minority does not appear in the model
#We plot here the interaction term

plot(interaction_plot_2,
     main="Interaction effect the youth and adult x minority status",
     xlab="the youth and adult",
     ylab="Trust in Police")

     # We use the "effect" function to call the interaction term from model 3. We assign the results to "effect_plot"

interaction_plot_3 <- effect("gen*minority", model3, na.rm=TRUE)

#We plot here the interaction term

plot(interaction_plot_3,
     main="Interaction effect the youth and adult x minority status",
     xlab="the youth and adult",
     ylab="Trust in Police")

# Now, let's extract just the coefficients

coefficients1 <- coef(model1)
print(coefficients1)
## (Intercept)    genadult 
##   6.0546223  -0.1915794
coefficients2 <- coef(model2)
print(coefficients2)
##                 (Intercept)                    genadult 
##                   5.6524164                  -0.1784969 
## minorityNot Ethnic Minority 
##                   0.4134766
coefficients3 <- coef(model3)
print(coefficients3)
##                          (Intercept)                             genadult 
##                           5.56976744                          -0.04446624 
##          minorityNot Ethnic Minority genadult:minorityNot Ethnic Minority 
##                           0.49844153                          -0.13956237
#For example, for my own interpretation
#(intercept of education level) 
#if a person has a BA= 8.257593
#If a person has no BA= 8.257593
#if a person is a male= 8.1828554
#If a person is a female= 8.1828554 -0.1502479
#If a person has paid work=  8.0712298
#If a person has no paid work= 8.0712298 + 0.0721098

# A first crack at a table output
tidy(model1)
## # A tibble: 2 × 5
##   term        estimate std.error statistic      p.value
##   <chr>          <dbl>     <dbl>     <dbl>        <dbl>
## 1 (Intercept)    6.05     0.0230    263.   0           
## 2 genadult      -0.192    0.0352     -5.44 0.0000000555
tidy(model2)
## # A tibble: 3 × 5
##   term                        estimate std.error statistic     p.value
##   <chr>                          <dbl>     <dbl>     <dbl>       <dbl>
## 1 (Intercept)                    5.65     0.0890     63.5  0          
## 2 genadult                      -0.178    0.0353     -5.05 0.000000443
## 3 minorityNot Ethnic Minority    0.413    0.0884      4.68 0.00000292
tidy(model3)
## # A tibble: 4 × 5
##   term                                 estimate std.error statistic  p.value
##   <chr>                                   <dbl>     <dbl>     <dbl>    <dbl>
## 1 (Intercept)                            5.57       0.139    40.0   0       
## 2 genadult                              -0.0445     0.177    -0.251 0.802   
## 3 minorityNot Ethnic Minority            0.498      0.141     3.53  0.000421
## 4 genadult:minorityNot Ethnic Minority  -0.140      0.181    -0.771 0.441
# Striving to improve table presentation
tidy(model1) |>
  knitr::kable (digits = 3)
term estimate std.error statistic p.value
(Intercept) 6.055 0.023 262.950 0
genadult -0.192 0.035 -5.435 0
tidy(model2) |>
  knitr::kable(digits = 3)
term estimate std.error statistic p.value
(Intercept) 5.652 0.089 63.507 0
genadult -0.178 0.035 -5.051 0
minorityNot Ethnic Minority 0.413 0.088 4.678 0
tidy(model3) |>
  knitr::kable(digits = 3)
term estimate std.error statistic p.value
(Intercept) 5.570 0.139 39.961 0.000
genadult -0.044 0.177 -0.251 0.802
minorityNot Ethnic Minority 0.498 0.141 3.527 0.000
genadult:minorityNot Ethnic Minority -0.140 0.181 -0.771 0.441
# The full summary output
summary(model1)
## 
## Call:
## lm(formula = trstplc ~ gen, data = france_data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -6.055 -1.055  0.137  1.945  4.137 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  6.05462    0.02303 262.950  < 2e-16 ***
## genadult    -0.19158    0.03525  -5.435 5.55e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.24 on 16509 degrees of freedom
## Multiple R-squared:  0.001786,   Adjusted R-squared:  0.001726 
## F-statistic: 29.54 on 1 and 16509 DF,  p-value: 5.55e-08
summary(model2)
## 
## Call:
## lm(formula = trstplc ~ gen + minority, data = france_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.0659 -1.0659  0.1126  1.9341  4.5261 
## 
## Coefficients:
##                             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                  5.65242    0.08900  63.507  < 2e-16 ***
## genadult                    -0.17850    0.03534  -5.051 4.43e-07 ***
## minorityNot Ethnic Minority  0.41348    0.08839   4.678 2.92e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.239 on 16508 degrees of freedom
## Multiple R-squared:  0.003108,   Adjusted R-squared:  0.002987 
## F-statistic: 25.73 on 2 and 16508 DF,  p-value: 6.956e-12
summary(model3)
## 
## Call:
## lm(formula = trstplc ~ gen + minority + gen * minority, data = france_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.0682 -1.0682  0.1158  1.9318  4.4747 
## 
## Coefficients:
##                                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                           5.56977    0.13938  39.961  < 2e-16 ***
## genadult                             -0.04447    0.17749  -0.251 0.802184    
## minorityNot Ethnic Minority           0.49844    0.14132   3.527 0.000421 ***
## genadult:minorityNot Ethnic Minority -0.13956    0.18112  -0.771 0.440978    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.239 on 16507 degrees of freedom
## Multiple R-squared:  0.003144,   Adjusted R-squared:  0.002962 
## F-statistic: 17.35 on 3 and 16507 DF,  p-value: 3.032e-11
# Using modelsummary for prettier table, focusing here on just R squared and adj.
modelsummary::modelsummary(
  list(model1),
  gof_map = c("nobs", "r.squared", "adj.r.squared"))
 (1)
(Intercept) 6.055
(0.023)
genadult −0.192
(0.035)
Num.Obs. 16511
R2 0.002
R2 Adj. 0.002
modelsummary::modelsummary(
  list(model2),
  gof_map = c("nobs", "r.squared", "adj.r.squared"))
 (1)
(Intercept) 5.652
(0.089)
genadult −0.178
(0.035)
minorityNot Ethnic Minority 0.413
(0.088)
Num.Obs. 16511
R2 0.003
R2 Adj. 0.003
modelsummary::modelsummary(
  list(model3),
  gof_map = c("nobs", "r.squared", "adj.r.squared"))
 (1)
(Intercept) 5.570
(0.139)
genadult −0.044
(0.177)
minorityNot Ethnic Minority 0.498
(0.141)
genadult × minorityNot Ethnic Minority −0.140
(0.181)
Num.Obs. 16511
R2 0.003
R2 Adj. 0.003
## Finally, Getting equation output with equatiomatic 

remotes::install_github("datalorax/equatiomatic", force = TRUE)  
## Downloading GitHub repo datalorax/equatiomatic@HEAD
## xfun     (0.40   -> 0.41  ) [CRAN]
## rlang    (1.1.1  -> 1.1.2 ) [CRAN]
## cli      (3.6.1  -> 3.6.2 ) [CRAN]
## sass     (0.4.7  -> 0.4.8 ) [CRAN]
## jsonlite (1.8.7  -> 1.8.8 ) [CRAN]
## later    (1.3.1  -> 1.3.2 ) [CRAN]
## vctrs    (0.6.3  -> 0.6.5 ) [CRAN]
## utf8     (1.2.3  -> 1.2.4 ) [CRAN]
## fansi    (1.0.4  -> 1.0.6 ) [CRAN]
## stringi  (1.7.12 -> 1.8.3 ) [CRAN]
## stringr  (1.5.0  -> 1.5.1 ) [CRAN]
## dplyr    (1.1.3  -> 1.1.4 ) [CRAN]
## yaml     (2.3.7  -> 2.3.8 ) [CRAN]
## httpuv   (1.6.11 -> 1.6.13) [CRAN]
## knitr    (1.44   -> 1.45  ) [CRAN]
## Installing 15 packages: xfun, rlang, cli, sass, jsonlite, later, vctrs, utf8, fansi, stringi, stringr, dplyr, yaml, httpuv, knitr
## Warning: packages 'stringr', 'dplyr', 'knitr' are in use and will not be
## installed
## Installing packages into 'C:/Users/Owner/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'xfun' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'xfun'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\Owner\AppData\Local\R\win-library\4.3\00LOCK\xfun\libs\x64\xfun.dll to
## C:\Users\Owner\AppData\Local\R\win-library\4.3\xfun\libs\x64\xfun.dll:
## Permission denied
## Warning: restored 'xfun'
## package 'rlang' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'rlang'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\Owner\AppData\Local\R\win-library\4.3\00LOCK\rlang\libs\x64\rlang.dll
## to C:\Users\Owner\AppData\Local\R\win-library\4.3\rlang\libs\x64\rlang.dll:
## Permission denied
## Warning: restored 'rlang'
## package 'cli' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'cli'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\Owner\AppData\Local\R\win-library\4.3\00LOCK\cli\libs\x64\cli.dll to
## C:\Users\Owner\AppData\Local\R\win-library\4.3\cli\libs\x64\cli.dll: Permission
## denied
## Warning: restored 'cli'
## package 'sass' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'sass'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\Owner\AppData\Local\R\win-library\4.3\00LOCK\sass\libs\x64\sass.dll to
## C:\Users\Owner\AppData\Local\R\win-library\4.3\sass\libs\x64\sass.dll:
## Permission denied
## Warning: restored 'sass'
## package 'jsonlite' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'jsonlite'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\Owner\AppData\Local\R\win-library\4.3\00LOCK\jsonlite\libs\x64\jsonlite.dll
## to
## C:\Users\Owner\AppData\Local\R\win-library\4.3\jsonlite\libs\x64\jsonlite.dll:
## Permission denied
## Warning: restored 'jsonlite'
## package 'later' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'later'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\Owner\AppData\Local\R\win-library\4.3\00LOCK\later\libs\x64\later.dll
## to C:\Users\Owner\AppData\Local\R\win-library\4.3\later\libs\x64\later.dll:
## Permission denied
## Warning: restored 'later'
## package 'vctrs' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'vctrs'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\Owner\AppData\Local\R\win-library\4.3\00LOCK\vctrs\libs\x64\vctrs.dll
## to C:\Users\Owner\AppData\Local\R\win-library\4.3\vctrs\libs\x64\vctrs.dll:
## Permission denied
## Warning: restored 'vctrs'
## package 'utf8' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'utf8'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\Owner\AppData\Local\R\win-library\4.3\00LOCK\utf8\libs\x64\utf8.dll to
## C:\Users\Owner\AppData\Local\R\win-library\4.3\utf8\libs\x64\utf8.dll:
## Permission denied
## Warning: restored 'utf8'
## package 'fansi' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'fansi'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\Owner\AppData\Local\R\win-library\4.3\00LOCK\fansi\libs\x64\fansi.dll
## to C:\Users\Owner\AppData\Local\R\win-library\4.3\fansi\libs\x64\fansi.dll:
## Permission denied
## Warning: restored 'fansi'
## package 'stringi' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'stringi'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\Owner\AppData\Local\R\win-library\4.3\00LOCK\stringi\libs\icudt69l.dat
## to C:\Users\Owner\AppData\Local\R\win-library\4.3\stringi\libs\icudt69l.dat:
## Invalid argument
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\Owner\AppData\Local\R\win-library\4.3\00LOCK\stringi\libs\x64\stringi.dll
## to C:\Users\Owner\AppData\Local\R\win-library\4.3\stringi\libs\x64\stringi.dll:
## Permission denied
## Warning: restored 'stringi'
## package 'yaml' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'yaml'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\Owner\AppData\Local\R\win-library\4.3\00LOCK\yaml\libs\x64\yaml.dll to
## C:\Users\Owner\AppData\Local\R\win-library\4.3\yaml\libs\x64\yaml.dll:
## Permission denied
## Warning: restored 'yaml'
## package 'httpuv' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'httpuv'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\Owner\AppData\Local\R\win-library\4.3\00LOCK\httpuv\libs\x64\httpuv.dll
## to C:\Users\Owner\AppData\Local\R\win-library\4.3\httpuv\libs\x64\httpuv.dll:
## Permission denied
## Warning: restored 'httpuv'
## 
## The downloaded binary packages are in
##  C:\Users\Owner\AppData\Local\Temp\Rtmpc7Jr6S\downloaded_packages
## ── R CMD build ─────────────────────────────────────────────────────────────────
##   
  
  
   checking for file 'C:\Users\Owner\AppData\Local\Temp\Rtmpc7Jr6S\remotes16b07a91468c\datalorax-equatiomatic-39e8c3e/DESCRIPTION' ...
  
   checking for file 'C:\Users\Owner\AppData\Local\Temp\Rtmpc7Jr6S\remotes16b07a91468c\datalorax-equatiomatic-39e8c3e/DESCRIPTION' ... 
  
✔  checking for file 'C:\Users\Owner\AppData\Local\Temp\Rtmpc7Jr6S\remotes16b07a91468c\datalorax-equatiomatic-39e8c3e/DESCRIPTION' (1s)
## 
  
  
  
─  preparing 'equatiomatic': (458ms)
##    checking DESCRIPTION meta-information ...
  
   checking DESCRIPTION meta-information ... 
  
✔  checking DESCRIPTION meta-information
## 
  
  
  
─  checking for LF line-endings in source and make files and shell scripts (654ms)
## 
  
  
  
─  checking for empty or unneeded directories
## 
  
  
  
─  building 'equatiomatic_0.3.1.tar.gz'
## 
  
   
## 
## Installing package into 'C:/Users/Owner/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
equatiomatic::extract_eq(model1, use_coefs = TRUE)

\[ \operatorname{\widehat{trstplc}} = 6.05 - 0.19(\operatorname{gen}_{\operatorname{adult}}) \]

equatiomatic::extract_eq(model2, use_coefs = TRUE)

\[ \operatorname{\widehat{trstplc}} = 5.65 - 0.18(\operatorname{gen}_{\operatorname{adult}}) + 0.41(\operatorname{minority}_{\operatorname{Not\ Ethnic\ Minority}}) \]

equatiomatic::extract_eq(model3, use_coefs = TRUE)

\[ \operatorname{\widehat{trstplc}} = 5.57 - 0.04(\operatorname{gen}_{\operatorname{adult}}) + 0.5(\operatorname{minority}_{\operatorname{Not\ Ethnic\ Minority}}) - 0.14(\operatorname{gen}_{\operatorname{adult}} \times \operatorname{minority}_{\operatorname{Not\ Ethnic\ Minority}}) \]