packages <- c("tidyverse", "infer", "fst", "modelsummary", "effects", "survey", "performance", "broom", "MASS", "aod", "interactions", "kableExtra", "flextable", "scales", "dplyr", "viridis", "knitr", "rmarkdown", "ggridges", "questionr") 

new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)

lapply(packages, library, character.only = TRUE)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ 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: 'MASS'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     select
## 
## 
## 
## Attaching package: 'aod'
## 
## 
## The following object is masked from 'package:survival':
## 
##     rats
## 
## 
## 
## Attaching package: 'kableExtra'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     group_rows
## 
## 
## 
## Attaching package: 'flextable'
## 
## 
## The following objects are masked from 'package:kableExtra':
## 
##     as_image, footnote
## 
## 
## 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
## 
## 
## Loading required package: viridisLite
## 
## 
## Attaching package: 'viridis'
## 
## 
## The following object is masked from 'package:scales':
## 
##     viridis_pal
## [[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] "broom"        "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] "MASS"         "broom"        "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] "aod"          "MASS"         "broom"        "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] "interactions" "aod"          "MASS"         "broom"        "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] "kableExtra"   "interactions" "aod"          "MASS"         "broom"       
##  [6] "performance"  "survey"       "survival"     "Matrix"       "grid"        
## [11] "effects"      "carData"      "modelsummary" "fst"          "infer"       
## [16] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [21] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [26] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [31] "methods"      "base"        
## 
## [[13]]
##  [1] "flextable"    "kableExtra"   "interactions" "aod"          "MASS"        
##  [6] "broom"        "performance"  "survey"       "survival"     "Matrix"      
## [11] "grid"         "effects"      "carData"      "modelsummary" "fst"         
## [16] "infer"        "lubridate"    "forcats"      "stringr"      "dplyr"       
## [21] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [26] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [31] "datasets"     "methods"      "base"        
## 
## [[14]]
##  [1] "scales"       "flextable"    "kableExtra"   "interactions" "aod"         
##  [6] "MASS"         "broom"        "performance"  "survey"       "survival"    
## [11] "Matrix"       "grid"         "effects"      "carData"      "modelsummary"
## [16] "fst"          "infer"        "lubridate"    "forcats"      "stringr"     
## [21] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [26] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [31] "utils"        "datasets"     "methods"      "base"        
## 
## [[15]]
##  [1] "scales"       "flextable"    "kableExtra"   "interactions" "aod"         
##  [6] "MASS"         "broom"        "performance"  "survey"       "survival"    
## [11] "Matrix"       "grid"         "effects"      "carData"      "modelsummary"
## [16] "fst"          "infer"        "lubridate"    "forcats"      "stringr"     
## [21] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [26] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [31] "utils"        "datasets"     "methods"      "base"        
## 
## [[16]]
##  [1] "viridis"      "viridisLite"  "scales"       "flextable"    "kableExtra"  
##  [6] "interactions" "aod"          "MASS"         "broom"        "performance" 
## [11] "survey"       "survival"     "Matrix"       "grid"         "effects"     
## [16] "carData"      "modelsummary" "fst"          "infer"        "lubridate"   
## [21] "forcats"      "stringr"      "dplyr"        "purrr"        "readr"       
## [26] "tidyr"        "tibble"       "ggplot2"      "tidyverse"    "stats"       
## [31] "graphics"     "grDevices"    "utils"        "datasets"     "methods"     
## [36] "base"        
## 
## [[17]]
##  [1] "knitr"        "viridis"      "viridisLite"  "scales"       "flextable"   
##  [6] "kableExtra"   "interactions" "aod"          "MASS"         "broom"       
## [11] "performance"  "survey"       "survival"     "Matrix"       "grid"        
## [16] "effects"      "carData"      "modelsummary" "fst"          "infer"       
## [21] "lubridate"    "forcats"      "stringr"      "dplyr"        "purrr"       
## [26] "readr"        "tidyr"        "tibble"       "ggplot2"      "tidyverse"   
## [31] "stats"        "graphics"     "grDevices"    "utils"        "datasets"    
## [36] "methods"      "base"        
## 
## [[18]]
##  [1] "rmarkdown"    "knitr"        "viridis"      "viridisLite"  "scales"      
##  [6] "flextable"    "kableExtra"   "interactions" "aod"          "MASS"        
## [11] "broom"        "performance"  "survey"       "survival"     "Matrix"      
## [16] "grid"         "effects"      "carData"      "modelsummary" "fst"         
## [21] "infer"        "lubridate"    "forcats"      "stringr"      "dplyr"       
## [26] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [31] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [36] "datasets"     "methods"      "base"        
## 
## [[19]]
##  [1] "ggridges"     "rmarkdown"    "knitr"        "viridis"      "viridisLite" 
##  [6] "scales"       "flextable"    "kableExtra"   "interactions" "aod"         
## [11] "MASS"         "broom"        "performance"  "survey"       "survival"    
## [16] "Matrix"       "grid"         "effects"      "carData"      "modelsummary"
## [21] "fst"          "infer"        "lubridate"    "forcats"      "stringr"     
## [26] "dplyr"        "purrr"        "readr"        "tidyr"        "tibble"      
## [31] "ggplot2"      "tidyverse"    "stats"        "graphics"     "grDevices"   
## [36] "utils"        "datasets"     "methods"      "base"        
## 
## [[20]]
##  [1] "questionr"    "ggridges"     "rmarkdown"    "knitr"        "viridis"     
##  [6] "viridisLite"  "scales"       "flextable"    "kableExtra"   "interactions"
## [11] "aod"          "MASS"         "broom"        "performance"  "survey"      
## [16] "survival"     "Matrix"       "grid"         "effects"      "carData"     
## [21] "modelsummary" "fst"          "infer"        "lubridate"    "forcats"     
## [26] "stringr"      "dplyr"        "purrr"        "readr"        "tidyr"       
## [31] "tibble"       "ggplot2"      "tidyverse"    "stats"        "graphics"    
## [36] "grDevices"    "utils"        "datasets"     "methods"      "base"

loading data

norway_data <- read_fst("norway_data.fst")
df <- norway_data
df$weight <- df$dweight * df$pweight
survey_design <- svydesign(ids = ~1, data = df, weights = ~weight)
df$happy <- ifelse(df$happy > 10, NA, df$happy)
df$happy <- scales::rescale(df$happy, na.rm = TRUE, to = c(0, 100))
df$happy <- scales::rescale(df$happy, na.rm = TRUE, to=c(100,0))
df <- df %>%
  mutate(
    educ.ba = case_when(
      essround < 5 & edulvla == 5 ~ "BA or more",
      essround >= 5 & edulvlb > 600 ~ "BA or more",
      TRUE ~ "No BA"
    ),
    
    edulvla = ifelse(edulvla %in% c(77, 88, 99), NA_integer_, edulvla),
    edulvlb = ifelse(edulvlb %in% c(5555, 7777, 8888), NA_integer_, edulvlb),
  
    educ.ba = factor(educ.ba, levels = c("No BA", "BA or more")),

    age = ifelse(agea == 999, NA, agea),

    cohort = ifelse(yrbrn < 1930 | yrbrn > 2000, NA, yrbrn),

    gen = case_when(
      yrbrn %in% 1900:1945 ~ "1",
      yrbrn %in% 1946:1964 ~ "2",
      yrbrn %in% 1965:1979 ~ "3",
      yrbrn %in% 1980:1996 ~ "4",
      TRUE ~ as.character(cohort)  
    ),

    gen = factor(gen,
                 levels = c("1", "2", "3", "4"),
                 labels = c("Interwar", "Baby Boomers", "Gen X", "Millennials"))
  )
table(df$gen)
## 
##     Interwar Baby Boomers        Gen X  Millennials 
##         2752         5177         4324         3240
df <- df %>%
  mutate(
    educ.ba = case_when(
      essround < 5 & edulvla == 5 ~ "BA or more",
      essround >= 5 & edulvlb > 600 ~ "BA or more",
      TRUE ~ "No BA"
    ),
    edulvla = ifelse(edulvla %in% c(77, 88, 99), NA_integer_, edulvla),
    edulvlb = ifelse(edulvlb %in% c(5555, 7777, 8888), NA_integer_, edulvlb),
    educ.ba = factor(educ.ba, levels = c("No BA", "BA or more"))  
  ) 

regression models

model1 <- lm(happy ~ educ.ba, data = df, weights = weight)
model2 <- lm(happy ~ educ.ba + gen, data = df, weights = weight)
model3 <- lm(happy ~ educ.ba + gen + educ.ba*gen, data = df, weights = weight)
modelsummary(
  list(model1, model2, model3), 
  fmt = 1,
  estimate  = c( "{estimate} ({std.error}){stars}", 
                  "{estimate} ({std.error}){stars}",
                 "{estimate} ({std.error}){stars}"),
  statistic = NULL)
 (1)   (2)   (3)
(Intercept) 20.2 (0.2)*** 19.4 (0.3)*** 19.6 (0.3)***
educ.baBA or more 0.1 (0.3) 0.1 (0.3) −0.5 (0.7)
genBaby Boomers 0.5 (0.4) 0.1 (0.4)
genGen X 1.0 (0.4)* 1.0 (0.5)*
genMillennials 0.7 (0.4)+ 0.7 (0.5)
educ.baBA or more × genBaby Boomers 1.5 (0.8)+
educ.baBA or more × genGen X 0.3 (0.8)
educ.baBA or more × genMillennials 0.2 (0.9)
Num.Obs. 16043 15472 15472
R2 0.000 0.000 0.001
R2 Adj. 0.000 0.000 0.000
AIC 133635.4 128684.1 128684.5
BIC 133658.4 128729.9 128753.3
Log.Lik. −66814.692 −64336.031 −64333.260
RMSE 15.39 15.31 15.31
pdf1 <- modelsummary(list(model1, model2, model3), fmt = 1,
  estimate  = c("{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}",
                "{estimate} ({std.error}){stars}"),
  statistic = NULL,
  coef_rename = c("genBaby Boomers" = "Baby Boomers", "genGen X" = "Gen X", "genMillennials" = "Millennials", "educ.baBA or more" = "BA or More"),
  title = 'Table 1. Regression Models for Happiness')
pdf1
Table 1. Regression Models for Happiness
 (1)   (2)   (3)
(Intercept) 20.2 (0.2)*** 19.4 (0.3)*** 19.6 (0.3)***
BA or More 0.1 (0.3) 0.1 (0.3) −0.5 (0.7)
Baby Boomers 0.5 (0.4) 0.1 (0.4)
Gen X 1.0 (0.4)* 1.0 (0.5)*
Millennials 0.7 (0.4)+ 0.7 (0.5)
BA or More:Baby Boomers 1.5 (0.8)+
BA or More:Gen X 0.3 (0.8)
BA or More:Millennials 0.2 (0.9)
Num.Obs. 16043 15472 15472
R2 0.000 0.000 0.001
R2 Adj. 0.000 0.000 0.000
AIC 133635.4 128684.1 128684.5
BIC 133658.4 128729.9 128753.3
Log.Lik. −66814.692 −64336.031 −64333.260
RMSE 15.39 15.31 15.31
flextable::save_as_docx(pdf1, path = "table1final.docx", 
                       width = 7.0, height = 7.0) 

set_flextable_defaults(fonts_ignore=TRUE)
print(pdf1, preview = "pdf")
## <table class="table" style="width: auto !important; margin-left: auto; margin-right: auto;">
## <caption>Table 1. Regression Models for Happiness</caption>
##  <thead>
##   <tr>
##    <th style="text-align:left;">   </th>
##    <th style="text-align:center;">  (1) </th>
##    <th style="text-align:center;">   (2) </th>
##    <th style="text-align:center;">   (3) </th>
##   </tr>
##  </thead>
## <tbody>
##   <tr>
##    <td style="text-align:left;"> (Intercept) </td>
##    <td style="text-align:center;"> 20.2 (0.2)*** </td>
##    <td style="text-align:center;"> 19.4 (0.3)*** </td>
##    <td style="text-align:center;"> 19.6 (0.3)*** </td>
##   </tr>
##   <tr>
##    <td style="text-align:left;"> BA or More </td>
##    <td style="text-align:center;"> 0.1 (0.3) </td>
##    <td style="text-align:center;"> 0.1 (0.3) </td>
##    <td style="text-align:center;"> −0.5 (0.7) </td>
##   </tr>
##   <tr>
##    <td style="text-align:left;"> Baby Boomers </td>
##    <td style="text-align:center;">  </td>
##    <td style="text-align:center;"> 0.5 (0.4) </td>
##    <td style="text-align:center;"> 0.1 (0.4) </td>
##   </tr>
##   <tr>
##    <td style="text-align:left;"> Gen X </td>
##    <td style="text-align:center;">  </td>
##    <td style="text-align:center;"> 1.0 (0.4)* </td>
##    <td style="text-align:center;"> 1.0 (0.5)* </td>
##   </tr>
##   <tr>
##    <td style="text-align:left;"> Millennials </td>
##    <td style="text-align:center;">  </td>
##    <td style="text-align:center;"> 0.7 (0.4)+ </td>
##    <td style="text-align:center;"> 0.7 (0.5) </td>
##   </tr>
##   <tr>
##    <td style="text-align:left;"> BA or More:Baby Boomers </td>
##    <td style="text-align:center;">  </td>
##    <td style="text-align:center;">  </td>
##    <td style="text-align:center;"> 1.5 (0.8)+ </td>
##   </tr>
##   <tr>
##    <td style="text-align:left;"> BA or More:Gen X </td>
##    <td style="text-align:center;">  </td>
##    <td style="text-align:center;">  </td>
##    <td style="text-align:center;"> 0.3 (0.8) </td>
##   </tr>
##   <tr>
##    <td style="text-align:left;box-shadow: 0px 1.5px"> BA or More:Millennials </td>
##    <td style="text-align:center;box-shadow: 0px 1.5px">  </td>
##    <td style="text-align:center;box-shadow: 0px 1.5px">  </td>
##    <td style="text-align:center;box-shadow: 0px 1.5px"> 0.2 (0.9) </td>
##   </tr>
##   <tr>
##    <td style="text-align:left;"> Num.Obs. </td>
##    <td style="text-align:center;"> 16043 </td>
##    <td style="text-align:center;"> 15472 </td>
##    <td style="text-align:center;"> 15472 </td>
##   </tr>
##   <tr>
##    <td style="text-align:left;"> R2 </td>
##    <td style="text-align:center;"> 0.000 </td>
##    <td style="text-align:center;"> 0.000 </td>
##    <td style="text-align:center;"> 0.001 </td>
##   </tr>
##   <tr>
##    <td style="text-align:left;"> R2 Adj. </td>
##    <td style="text-align:center;"> 0.000 </td>
##    <td style="text-align:center;"> 0.000 </td>
##    <td style="text-align:center;"> 0.000 </td>
##   </tr>
##   <tr>
##    <td style="text-align:left;"> AIC </td>
##    <td style="text-align:center;"> 133635.4 </td>
##    <td style="text-align:center;"> 128684.1 </td>
##    <td style="text-align:center;"> 128684.5 </td>
##   </tr>
##   <tr>
##    <td style="text-align:left;"> BIC </td>
##    <td style="text-align:center;"> 133658.4 </td>
##    <td style="text-align:center;"> 128729.9 </td>
##    <td style="text-align:center;"> 128753.3 </td>
##   </tr>
##   <tr>
##    <td style="text-align:left;"> Log.Lik. </td>
##    <td style="text-align:center;"> −66814.692 </td>
##    <td style="text-align:center;"> −64336.031 </td>
##    <td style="text-align:center;"> −64333.260 </td>
##   </tr>
##   <tr>
##    <td style="text-align:left;"> RMSE </td>
##    <td style="text-align:center;"> 15.39 </td>
##    <td style="text-align:center;"> 15.31 </td>
##    <td style="text-align:center;"> 15.31 </td>
##   </tr>
## </tbody>
## </table>

interaction plot

model3 <- lm(happy ~ + educ.ba + cohort + educ.ba*cohort, data = df, weights = weight)
plot3 <- interact_plot(model3, pred = cohort, modx = educ.ba, jnplot = TRUE)
plot3

ggsave(filename = "plot1.pdf", plot = plot3, device = "pdf", width = 10, height = 8)

null distribution

norway_task1 <- norway_data %>%
  mutate(
    ID = case_when(
      happy %in% 0:4 ~ "unhappy",                     
      happy %in% 6:10 ~ "happy",                  
      happy %in% c(77, 88) ~ NA_character_,    
      TRUE ~ NA_character_                           
    ),
    educ.ba = case_when(
      essround < 5 & edulvla == 5 ~ "BA or more",
      essround >= 5 & edulvlb > 600 ~ "BA or more",
      TRUE ~ "No BA"
    ),
    edulvla = ifelse(edulvla %in% c(77, 88, 99), NA_integer_, edulvla),
    edulvlb = ifelse(edulvlb %in% c(5555, 7777, 8888), NA_integer_, edulvlb),
    educ.ba = factor(educ.ba, levels = c("No BA", "BA or more"))  
  ) %>%
  filter(!is.na(ID), !is.na(educ.ba))  
mytab <- table(norway_task1$ID, norway_task1$educ.ba)
mytab
##          
##           No BA BA or more
##   happy    9425       5384
##   unhappy   359        145
rsums <- rowSums(mytab)
csums <- colSums(mytab)
N <- sum(mytab)
expected <- outer(rsums, csums) / N
expected
##             No BA BA or more
## happy   9461.9771  5347.0229
## unhappy  322.0229   181.9771
alpha <- 0.05
df <- (nrow(mytab) - 1) * (ncol(mytab) - 1)
critical_value <- qchisq(1 - alpha, df)

observed <- mytab
chi_squared <- sum((observed - expected)^2 / expected)

p_value <- pchisq(chi_squared, df, lower.tail = FALSE)

cat("Critical Value:", critical_value, "\n")
## Critical Value: 3.841459
cat("Chi-Squared Statistic:", chi_squared, "\n")
## Chi-Squared Statistic: 12.15985
cat("P-Value:", p_value, "\n")
## P-Value: 0.0004882927
edu_stat <- norway_task1 %>%
  specify(explanatory = ID, 
          response = educ.ba, 
          success = "BA or more") %>% 
 hypothesize(null = "independence") %>%
  calculate(stat = "chisq") 
## Warning: The statistic is based on a difference or ratio; by default, for
## difference-based statistics, the explanatory variable is subtracted in the
## order "happy" - "unhappy", or divided in the order "happy" / "unhappy" for
## ratio-based statistics. To specify this order yourself, supply `order =
## c("happy", "unhappy")` to the calculate() function.
print(edu_stat$stat)
## X-squared 
##  11.83323
null_edu <- norway_task1 %>%
  specify(explanatory = ID,
          response = educ.ba,
          success = "BA or more") %>% 
  hypothesize(null = "independence") %>%
  generate(reps = 1000, type = "permute") %>% 
  calculate(stat = "chisq")
## Warning: The statistic is based on a difference or ratio; by default, for
## difference-based statistics, the explanatory variable is subtracted in the
## order "happy" - "unhappy", or divided in the order "happy" / "unhappy" for
## ratio-based statistics. To specify this order yourself, supply `order =
## c("happy", "unhappy")` to the calculate() function.
null_edu
## Response: educ.ba (factor)
## Explanatory: ID (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
##    replicate  stat
##        <int> <dbl>
##  1         1 2.13 
##  2         2 0.108
##  3         3 0.110
##  4         4 0.108
##  5         5 2.14 
##  6         6 0.497
##  7         7 0.267
##  8         8 3.05 
##  9         9 0.646
## 10        10 6.26 
## # ℹ 990 more rows
p_val <- null_edu %>% 
  get_pvalue(obs_stat = edu_stat, direction = "greater") 

p_val
## # A tibble: 1 × 1
##   p_value
##     <dbl>
## 1   0.001
conf_int <- null_edu%>%
  get_confidence_interval(level = 0.95, type = "percentile")

null_edu %>%
  visualize(data, bins = 15, method = "simulation", dens_color = "black") +
  shade_p_value(obs_stat = edu_stat, direction = "greater") +
  shade_confidence_interval(endpoints = conf_int)

edu_stat <- 1000

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

null_edu %>%
  visualize(bins = 10, method = "simulation", dens_color = "black") +
  geom_vline(aes(xintercept = edu_stat), color = "red", linetype = "dashed", size = 1) +
  shade_p_value(obs_stat = edu_stat, direction = "greater") +
  shade_confidence_interval(endpoints = conf_int) +
  coord_cartesian(xlim = c(0, max(null_edu$stat) * 1.1)) + 
  labs(title = "Null Distribution for Norway",
       x = "Chi-Square",
       y = "Count")
## 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.
## Warning in min(diff(unique_loc)): no non-missing arguments to min; returning
## Inf

In the visualization of the null distibution the red line indicates evidence under the hypothesis testing to reject the null.

Critical Value (3.841459):

Chi-Squared Statistic (12.15985):

The Chi squared statistic value is greater than the critical value which indicates a deviation from what would be expected is there was no association between the variables. This also indicates that there is some evidence to reject the null hypothesis.

P-Value (0.0004882927)

This P value would provide evidence to reject the null hypothesis. This value is smaller than the significance level of (α = 0.05) which provides evidence to reject the null and support the alternative hypothesis

descriptive statistics