# List of packages
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer", 
              "fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr", "infer","dplyr","stargazer","reshape2","MASS","Hmisc") # 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.2     ✔ 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: viridisLite
## 
## 
## Attaching package: 'kableExtra'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     group_rows
## 
## 
## 
## 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 
## 
## 
## 
## Attaching package: 'reshape2'
## 
## 
## The following object is masked from 'package:tidyr':
## 
##     smiths
## 
## 
## 
## Attaching package: 'MASS'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     select
## 
## 
## 
## Attaching package: 'Hmisc'
## 
## 
## The following objects are masked from 'package:questionr':
## 
##     describe, wtd.mean, wtd.table, wtd.var
## 
## 
## The following object is masked from 'package:modelsummary':
## 
##     Mean
## 
## 
## The following objects are masked from 'package:dplyr':
## 
##     src, summarize
## 
## 
## The following objects are masked from 'package:base':
## 
##     format.pval, units
## [[1]]
##  [1] "lubridate" "forcats"   "stringr"   "dplyr"     "purrr"     "readr"    
##  [7] "tidyr"     "tibble"    "ggplot2"   "tidyverse" "stats"     "graphics" 
## [13] "grDevices" "utils"     "datasets"  "methods"   "base"     
## 
## [[2]]
##  [1] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
##  [6] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [11] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [16] "datasets"     "methods"      "base"        
## 
## [[3]]
##  [1] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
##  [6] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [11] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [16] "datasets"     "methods"      "base"        
## 
## [[4]]
##  [1] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
##  [6] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [11] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [16] "utils"        "datasets"     "methods"      "base"        
## 
## [[5]]
##  [1] "fst"          "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
##  [6] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [11] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [16] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[6]]
##  [1] "viridis"      "viridisLite"  "fst"          "RColorBrewer" "modelsummary"
##  [6] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [11] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [16] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [21] "methods"      "base"        
## 
## [[7]]
##  [1] "knitr"        "viridis"      "viridisLite"  "fst"          "RColorBrewer"
##  [6] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
## [11] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [16] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [21] "datasets"     "methods"      "base"        
## 
## [[8]]
##  [1] "kableExtra"   "knitr"        "viridis"      "viridisLite"  "fst"         
##  [6] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
## [11] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [16] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [21] "utils"        "datasets"     "methods"      "base"        
## 
## [[9]]
##  [1] "rmarkdown"    "kableExtra"   "knitr"        "viridis"      "viridisLite" 
##  [6] "fst"          "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
## [11] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [16] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [21] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[10]]
##  [1] "ggridges"     "rmarkdown"    "kableExtra"   "knitr"        "viridis"     
##  [6] "viridisLite"  "fst"          "RColorBrewer" "modelsummary" "lubridate"   
## [11] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [16] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [21] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [26] "base"        
## 
## [[11]]
##  [1] "ggridges"     "rmarkdown"    "kableExtra"   "knitr"        "viridis"     
##  [6] "viridisLite"  "fst"          "RColorBrewer" "modelsummary" "lubridate"   
## [11] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [16] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [21] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [26] "base"        
## 
## [[12]]
##  [1] "questionr"    "ggridges"     "rmarkdown"    "kableExtra"   "knitr"       
##  [6] "viridis"      "viridisLite"  "fst"          "RColorBrewer" "modelsummary"
## [11] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [16] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [21] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [26] "methods"      "base"        
## 
## [[13]]
##  [1] "infer"        "questionr"    "ggridges"     "rmarkdown"    "kableExtra"  
##  [6] "knitr"        "viridis"      "viridisLite"  "fst"          "RColorBrewer"
## [11] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
## [16] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [21] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [26] "datasets"     "methods"      "base"        
## 
## [[14]]
##  [1] "infer"        "questionr"    "ggridges"     "rmarkdown"    "kableExtra"  
##  [6] "knitr"        "viridis"      "viridisLite"  "fst"          "RColorBrewer"
## [11] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
## [16] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [21] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [26] "datasets"     "methods"      "base"        
## 
## [[15]]
##  [1] "stargazer"    "infer"        "questionr"    "ggridges"     "rmarkdown"   
##  [6] "kableExtra"   "knitr"        "viridis"      "viridisLite"  "fst"         
## [11] "RColorBrewer" "modelsummary" "lubridate"    "forcats"      "stringr"     
## [16] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [21] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [26] "utils"        "datasets"     "methods"      "base"        
## 
## [[16]]
##  [1] "reshape2"     "stargazer"    "infer"        "questionr"    "ggridges"    
##  [6] "rmarkdown"    "kableExtra"   "knitr"        "viridis"      "viridisLite" 
## [11] "fst"          "RColorBrewer" "modelsummary" "lubridate"    "forcats"     
## [16] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [21] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [26] "grDevices"    "utils"        "datasets"     "methods"      "base"        
## 
## [[17]]
##  [1] "MASS"         "reshape2"     "stargazer"    "infer"        "questionr"   
##  [6] "ggridges"     "rmarkdown"    "kableExtra"   "knitr"        "viridis"     
## [11] "viridisLite"  "fst"          "RColorBrewer" "modelsummary" "lubridate"   
## [16] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [21] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [26] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [31] "base"        
## 
## [[18]]
##  [1] "Hmisc"        "MASS"         "reshape2"     "stargazer"    "infer"       
##  [6] "questionr"    "ggridges"     "rmarkdown"    "kableExtra"   "knitr"       
## [11] "viridis"      "viridisLite"  "fst"          "RColorBrewer" "modelsummary"
## [16] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [21] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [26] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [31] "methods"      "base"
ess <- read_fst("All-ESS-Data.fst")
france_data <- ess %>%
  filter(cntry == "FR") %>%
  dplyr::select(agea, gndr,essround,cntry,imwbcnt,hinctnta,edlvdfr)

france_data_2 <-france_data %>%
  mutate(
       hinctnta = ifelse(hinctnta %in% c(77, 88,99), NA, hinctnta),
       imwbcnt  = ifelse(imwbcnt%in% c(77, 88,99), NA, imwbcnt),
       year = case_when(essround == 1 ~ 2002,
                         essround == 2 ~ 2004,
                         essround == 3 ~ 2006,
                         essround == 4 ~ 2008,
                         essround == 5 ~ 2010,
                         essround == 6 ~ 2012,
                         essround == 7 ~ 2014,
                         essround == 8 ~ 2016,
                         essround == 9 ~ 2018,
                         essround == 10 ~ 2020),
       agea = ifelse(agea < 999, agea, NA),
       gndr = ifelse(gndr %in% c(1:2), gndr, NA),
       )

  
  
france_clean <- france_data_2%>%
     mutate(
      
       edlvdfr = ifelse(edlvdfr %in% c(5555,7777,8888,9999), NA,edlvdfr),
       
       year = case_when( essround == 1 ~ 2002,
                         essround == 2 ~ 2004,
                         essround == 3 ~ 2006,
                         essround == 4 ~ 2008,
                         essround == 5 ~ 2010,
                         essround == 6 ~ 2012,
                         essround == 7 ~ 2014,
                         essround == 8 ~ 2016,
                         essround == 9 ~ 2018,
                         essround == 10 ~ 2020),
       agea = ifelse(agea < 999, agea, NA),
       gndr = ifelse(gndr %in% c(1:2), gndr, NA),
       
       hinctnta = case_when(hinctnta == 1 ~ 'Low',
                            hinctnta == 2 ~ 'Low',
                            hinctnta == 3 ~ 'LowMed',
                            hinctnta == 4 ~ 'LowMed',
                            hinctnta == 5 ~ 'Medium',
                            hinctnta == 6 ~ 'Medium',
                            hinctnta == 7 ~ 'MedHigh',
                            hinctnta == 8 ~ 'MedHigh',
                            hinctnta == 9 ~ 'High',
                            hinctnta == 10 ~ 'High',
                            hinctnta > 10 ~ NA_character_),
      
        imwbcnt = case_when( imwbcnt == 0 ~ 'Low',
                             imwbcnt == 1 ~ 'Low',
                             imwbcnt == 2 ~ 'LowMed',
                             imwbcnt== 3 ~ 'LowMed',
                             imwbcnt == 4 ~ 'Medium',
                             imwbcnt== 5 ~ 'Medium',
                             imwbcnt == 6 ~ 'MedHigh',
                             imwbcnt == 7 ~ 'MedHigh',
                             imwbcnt == 8 ~ 'MedHigh',
                             imwbcnt == 9 ~ 'High',
                             imwbcnt == 10 ~ 'High',
                             imwbcnt > 10 ~ NA_character_))
datasummary_skim(france_data_2)
Unique (#) Missing (%) Mean SD Min Median Max
agea 87 0 49.9 18.6 14.0 50.0 101.0
gndr 2 0 1.5 0.5 1.0 2.0 2.0
essround 10 0 5.7 2.8 1.0 6.0 10.0
imwbcnt 12 1 4.7 2.2 0.0 5.0 10.0
hinctnta 11 34 5.2 2.9 1.0 5.0 10.0
edlvdfr 30 39 28.2 351.0 1.0 9.0 8888.0
year 10 0 2011.3 5.7 2002.0 2012.0 2020.0
#Compare the immigrant altitude to the ess baseline
ess$year <- NA
replace <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for (i in 1:10){
ess$year[ess$essround == i] <- replace[i]
  }


france_data_2$imwbcnt[france_data_2$imwbcnt %in% c(77, 88, 99)] <- NA

avg_imwbcnt_france <- france_data_2%>%
  group_by(year) %>%
  dplyr::summarize(mean_imwbcnt_france = mean(imwbcnt, na.rm = TRUE))

avg_imwbcnt_all_countries <- ess %>%
  group_by(year) %>%
  dplyr::summarize(mean_imwbcnt_all = mean(imwbcnt, na.rm = TRUE))

combined_data <- left_join(avg_imwbcnt_france, avg_imwbcnt_all_countries, by = "year")


combined_data
## # A tibble: 10 × 3
##     year mean_imwbcnt_france mean_imwbcnt_all
##    <dbl>               <dbl>            <dbl>
##  1  2002                4.52             9.09
##  2  2004                4.43             9.75
##  3  2006                4.46             9.67
##  4  2008                4.71             9.86
##  5  2010                4.59             9.62
##  6  2012                4.54             9.65
##  7  2014                4.85             8.17
##  8  2016                4.82             7.81
##  9  2018                5.03             8.57
## 10  2020                5.20             7.76
library(reshape2)
long <- melt(combined_data, id="year") 

long
##    year            variable    value
## 1  2002 mean_imwbcnt_france 4.518919
## 2  2004 mean_imwbcnt_france 4.432022
## 3  2006 mean_imwbcnt_france 4.455099
## 4  2008 mean_imwbcnt_france 4.712047
## 5  2010 mean_imwbcnt_france 4.592420
## 6  2012 mean_imwbcnt_france 4.535769
## 7  2014 mean_imwbcnt_france 4.847780
## 8  2016 mean_imwbcnt_france 4.822320
## 9  2018 mean_imwbcnt_france 5.028645
## 10 2020 mean_imwbcnt_france 5.199793
## 11 2002    mean_imwbcnt_all 9.094195
## 12 2004    mean_imwbcnt_all 9.754507
## 13 2006    mean_imwbcnt_all 9.665953
## 14 2008    mean_imwbcnt_all 9.858648
## 15 2010    mean_imwbcnt_all 9.618304
## 16 2012    mean_imwbcnt_all 9.654729
## 17 2014    mean_imwbcnt_all 8.173174
## 18 2016    mean_imwbcnt_all 7.807579
## 19 2018    mean_imwbcnt_all 8.573901
## 20 2020    mean_imwbcnt_all 7.762922
ggplot(data = long, aes(x=year, y=value, group=variable, color = variable))+
  geom_line(linewidth = 1 ) +
  labs(title = "comparsion between ess and france in immigrants altitude") +
  ylim(4,10)

table5 <- table(france_data_2$imwbcnt, france_data_2$hinctnta) %>%
  as.data.frame()
table5
##     Var1 Var2 Freq
## 1      0    1  123
## 2      1    1   61
## 3      2    1  103
## 4      3    1  139
## 5      4    1  136
## 6      5    1  504
## 7      6    1  129
## 8      7    1  115
## 9      8    1  101
## 10     9    1   36
## 11    10    1   55
## 12     0    2  125
## 13     1    2   63
## 14     2    2  157
## 15     3    2  136
## 16     4    2  153
## 17     5    2  492
## 18     6    2  147
## 19     7    2  126
## 20     8    2   86
## 21     9    2   22
## 22    10    2   51
## 23     0    3   84
## 24     1    3   50
## 25     2    3   89
## 26     3    3  117
## 27     4    3  132
## 28     5    3  402
## 29     6    3   96
## 30     7    3   71
## 31     8    3   78
## 32     9    3   21
## 33    10    3   32
## 34     0    4   90
## 35     1    4   38
## 36     2    4   92
## 37     3    4  123
## 38     4    4  150
## 39     5    4  418
## 40     6    4  100
## 41     7    4  100
## 42     8    4   75
## 43     9    4   29
## 44    10    4   32
## 45     0    5   51
## 46     1    5   42
## 47     2    5   81
## 48     3    5  117
## 49     4    5  128
## 50     5    5  444
## 51     6    5  119
## 52     7    5  103
## 53     8    5   79
## 54     9    5   28
## 55    10    5   39
## 56     0    6   64
## 57     1    6   26
## 58     2    6   81
## 59     3    6  108
## 60     4    6  113
## 61     5    6  428
## 62     6    6  128
## 63     7    6  110
## 64     8    6   70
## 65     9    6   14
## 66    10    6   21
## 67     0    7   49
## 68     1    7   25
## 69     2    7   85
## 70     3    7   81
## 71     4    7  141
## 72     5    7  462
## 73     6    7  134
## 74     7    7  120
## 75     8    7   64
## 76     9    7   22
## 77    10    7   24
## 78     0    8   40
## 79     1    8   27
## 80     2    8   59
## 81     3    8   97
## 82     4    8   98
## 83     5    8  428
## 84     6    8  116
## 85     7    8  121
## 86     8    8   80
## 87     9    8   11
## 88    10    8   28
## 89     0    9   18
## 90     1    9   13
## 91     2    9   47
## 92     3    9   79
## 93     4    9   76
## 94     5    9  416
## 95     6    9  116
## 96     7    9  133
## 97     8    9   78
## 98     9    9   21
## 99    10    9   20
## 100    0   10   20
## 101    1   10   23
## 102    2   10   43
## 103    3   10   55
## 104    4   10   79
## 105    5   10  461
## 106    6   10  106
## 107    7   10  146
## 108    8   10  121
## 109    9   10   31
## 110   10   10   37
ggplot(table5, aes(x = Var2, y = Freq,color = Var1) )+
  geom_point()+
  labs(title = "Visualization for income and attitude towards immigrants",
              x = "level of income",
              color = "level of attitude towards immigrants") +
  ylim(0,600)

indep_statistic <- france_clean%>%
  specify(imwbcnt ~ hinctnta) %>%
  hypothesize(null = "independence") %>%
  calculate(stat = "Chisq")
## Warning: Removed 6714 rows containing missing values.
indep_statistic
## Response: imwbcnt (factor)
## Explanatory: hinctnta (factor)
## Null Hypothesis: independence
## # A tibble: 1 × 1
##    stat
##   <dbl>
## 1  297.
null_sim<- france_clean %>%
  specify(imwbcnt ~ hinctnta) %>%
  hypothesize(null = "independence") %>%
  generate(reps = 1000, type = "permute") %>%
  calculate(stat = "Chisq")
## Warning: Removed 6714 rows containing missing values.
null_sim
## Response: imwbcnt (factor)
## Explanatory: hinctnta (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
##    replicate  stat
##        <int> <dbl>
##  1         1 20.2 
##  2         2 18.7 
##  3         3 17.2 
##  4         4 19.8 
##  5         5  9.79
##  6         6 13.0 
##  7         7 13.0 
##  8         8 23.8 
##  9         9 23.5 
## 10        10 15.5 
## # ℹ 990 more rows
# visualize the null distribution and test statistic!
null_sim %>%
  visualize() + 
  shade_p_value(indep_statistic,
                direction = "greater")
## Warning in min(diff(unique_loc)): no non-missing arguments to min; returning
## Inf

p_value_independence <- null_sim%>%
  get_p_value(obs_stat = indep_statistic,
              direction = "greater")
## 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_value_independence
## # A tibble: 1 × 1
##   p_value
##     <dbl>
## 1       0
model1 <- polr(as.factor(imwbcnt) ~ hinctnta + edlvdfr, data = france_clean, Hess = TRUE, method = "logistic")
model2 <- polr(as.factor(imwbcnt) ~ hinctnta + edlvdfr + gndr + agea ,data = france_clean, Hess = TRUE, method = "logistic")
model3 <- polr(as.factor(imwbcnt) ~ hinctnta + edlvdfr + gndr + agea + year, data = france_clean, Hess = TRUE, method = "logistic")


model2
## Call:
## polr(formula = as.factor(imwbcnt) ~ hinctnta + edlvdfr + gndr + 
##     agea, data = france_clean, Hess = TRUE, method = "logistic")
## 
## Coefficients:
##     hinctntaLow  hinctntaLowMed hinctntaMedHigh  hinctntaMedium         edlvdfr 
##    -0.328305835    -0.209932537    -0.029919070    -0.115282020     0.014158319 
##            gndr            agea 
##     0.043265550    -0.002525106 
## 
## Intercepts:
##       High|Low     Low|LowMed LowMed|MedHigh MedHigh|Medium 
##     -3.0626699     -1.9578211     -0.9959258      0.1269501 
## 
## Residual Deviance: 28110.80 
## AIC: 28132.80 
## (8587 observations deleted due to missingness)
modelsummary(list(model1, model2,model3))
 (1)   (2)   (3)
High|Low −2.968 −3.063 0.808
(0.079) (0.111) (0.002)
Low|LowMed −1.863 −1.958 1.912
(0.070) (0.106) (0.037)
LowMed|MedHigh −0.902 −0.996 2.874
(0.068) (0.104) (0.042)
MedHigh|Medium 0.219 0.127 3.997
(0.068) (0.103) (0.044)
hinctntaLow −0.312 −0.328 −0.325
(0.062) (0.062) (0.032)
hinctntaLowMed −0.199 −0.210 −0.207
(0.064) (0.064) (0.035)
hinctntaMedHigh −0.022 −0.030 −0.028
(0.062) (0.062) (0.037)
hinctntaMedium −0.105 −0.115 −0.113
(0.063) (0.063) (0.035)
edlvdfr 0.016 0.014 0.014
(0.003) (0.003) (0.003)
gndr 0.043 0.043
(0.036) (0.038)
agea −0.003 −0.003
(0.001) (0.001)
year 0.002
(0.000)
Num.Obs. 10457 10451 10451
AIC 28150.6 28132.8 28134.7
BIC 28215.9 28212.6 28221.7
RMSE 3.97 3.97 3.97