Scaling of NEPS example data ex3

Author

Timo Gnambs

Published

2024-04-09 14:05:00

Modified

2024-04-10 13:38:42

1 R setup

Code
# Clear work space
rm(list = ls())

# Load packages
library(scaling)
library(rlang)

# Turn of warnings
knitr::opts_chunk$set(warning = FALSE, message = FALSE) 

# Load example data
load("../../../../data/ex3.rda")
resp <- ex3$resp
vars <- ex3$vars

# Set missing codes
mvs <- c(OM = -97, NV = -95, NR = -94, ND = -55, NAd = -54)
labels_mvs <- c(
  ALL = 'total missing responses',
  OM = 'omitted items',
  NV = 'invalid responses',
  NR = 'not reached items',
  ND = 'not determinable items',
  NAd = 'not administered items'
)

2 Sample descriptives

Code
# Sample size with only valid cases
scaling::n_valid(resp, valid = "valid")

# Sample size by test version
scaling::sample_by_group(
  resp = resp,
  grouping_variable = "booklet",
  labels = c("missing" = -56, "easy" = 1, "medium" = 2, "difficult" = 3),
  save = TRUE,
  overwrite = TRUE,
  path = "tables"
)
Code
# Descriptives for nominal variables
scaling::desc_nom(resp, desc = c("sex", "mig", "rotation"), valid = "valid")
      sex mig rotation
0    1071 861     1045
1    1029 813     1055
<NA>    0 426        0
     sex   mig      rotation
[1,] "51%" "41%"    "49.76%"
[2,] "49%" "38.71%" "50.24%"
[3,] "0%"  "20.29%" "0%"    
     sex   mig      rotation
[1,] "51%" "51.43%" "49.76%"
[2,] "49%" "48.57%" "50.24%"
Code
scaling::show_attributes(resp, desc = c("sex", "mig", "rotation"))
  male female 
     0      1 
 no yes 
  0   1 
 first second 
     0      1 
Code
# Item type by test version
scaling::props_by_group(
  vars = vars,
  select = "mixed",
  grouping = c("booklet1", "booklet2", "booklet3"),
  properties = c("texttype", "type"),
  save = TRUE,
  overwrite = TRUE,
  path = "tables"
)
$texttype
                      booklet1 booklet2 booklet3
literary                     3        3        4
advertising                  2        4        2
information                  2        3        2
commenting                   1       NA        2
instruction                  2        1       NA
Total number of items       10       11       10

$type
                      booklet1 booklet2 booklet3
MC                           7        8        7
CMC                          3        3        3
Total number of items       10       11       10

3 Missing responses

3.1 By person

Code
scaling::mv_person(
  resp = resp,
  vars = vars,
  select = "dich",
  valid = "valid",
  grouping = c("booklet1", "booklet2", "booklet3"),
  mvs = mvs,
  labels_mvs = labels_mvs,
  name_grouping = "Booklet",
  labels_legend = c("1", "2", "3"),
  show_all = FALSE,
  overwrite = TRUE,
  plots = TRUE,
  save = TRUE,
  print = FALSE,
  return = FALSE,
  path_results = paste0(getwd(), "/results"),
  path_table = "tables",
  path_plots = "plots/missing/by_person",
  digits = 2,
  warn = FALSE,
  verbose = FALSE
)

3.2 By item

Code
scaling::mv_item(
  resp = resp,
  vars = vars,
  select = "mixed",
  valid = "valid",
  grouping = c("booklet1", "booklet2", "booklet3"),
  position = c(booklet1 = "pos1", booklet2 = "pos2", booklet3 = "pos3"),
  mvs = mvs,
  labels_mvs = labels_mvs,
  name_grouping = "Booklet",
  labels_legend = c("1", "2", "3"),
  show_all = FALSE,
  overwrite = TRUE,
  plots = TRUE,
  save = TRUE,
  print = FALSE,
  return = FALSE,
  path_results = paste0(getwd(), "/results"),
  path_table = "tables",
  path_plots = "plots/missing/by_item",
  digits = 2,
  warn = FALSE,
  verbose = FALSE
)

4 IRT analyses

4.1 Dichotomous

Code
# by booklet
vars$dich1 <- vars$dich & vars$booklet1
vars$dich2 <- vars$dich & vars$booklet2
vars$dich3 <- vars$dich & vars$booklet3
scaling::grouped_irt_analysis(
  resp = resp,
  vars = vars,
  select = c(booklet1 = "dich1", booklet2 = "dich2", booklet3 = "dich3"),
  valid = "valid",
  mvs = mvs,
  scoring = "scoring",
  plots = TRUE,
  save = TRUE,
  print = FALSE,
  return = FALSE,
  path_results = paste0(getwd(), "/results"),
  path_table = "tables",
  path_plots = paste0(getwd(), "/plots"),
  overwrite = TRUE,
  digits = 2,
  verbose = FALSE
)

# combined
scaling::irt_analysis(
  resp = resp,
  vars = vars,
  select = "dich",
  valid = "valid",
  mvs = mvs,
  scoring = "scoring",
  plots = TRUE,
  save = TRUE,
  print = FALSE,
  return = FALSE,
  path_results = paste0(getwd(), "/results"),
  path_table = "tables",
  path_plots = paste0(getwd(), "/plots"),
  overwrite = TRUE,
  digits = 2,
  verbose = FALSE,
  name_group = "all"
)

4.2 Polytomous

Code
# by booklet
vars$mixed1 <- vars$mixed & vars$booklet1
vars$mixed2 <- vars$mixed & vars$booklet2
vars$mixed3 <- vars$mixed & vars$booklet3
scaling::grouped_irt_analysis(
  resp = resp,
  vars = vars,
  select = c(booklet1 = "mixed1", booklet2 = "mixed2", booklet3 = "mixed3"),
  valid = "valid",
  mvs = mvs,
  scoring = "scoring",
  plots = TRUE,
  save = TRUE,
  print = FALSE,
  return = FALSE,
  path_results = paste0(getwd(), "/results"),
  path_table = "tables",
  path_plots = paste0(getwd(), "/plots"),
  overwrite = TRUE,
  digits = 2,
  verbose = FALSE
)

# combined
scaling::irt_analysis(
  resp = resp,
  vars = vars,
  select = "mixed",
  valid = "valid",
  mvs = mvs,
  scoring = "scoring",
  plots = TRUE,
  save = TRUE,
  print = FALSE,
  return = FALSE,
  path_results = paste0(getwd(), "/results"),
  path_table = "tables",
  path_plots = paste0(getwd(), "/plots"),
  overwrite = TRUE,
  digits = 2,
  verbose = FALSE,
  name_group = "all"
)

5 DIF analysis

Code
scaling::dif_analysis(
  resp = resp,
  vars = vars,
  select = "dich",
  valid = "valid",
  dif_vars = c("sex", "mig"),
  scoring = "scoring",
  mvs = mvs,
  overwrite = TRUE,
  verbose = FALSE,
  save = TRUE,
  print = FALSE,
  return = FALSE,
  path_results = paste0(getwd(), "/results"),
  path_table = "tables",
  warn = FALSE
)
Code
vars$mixed_common <- vars$mixed & vars$booklet1 & vars$booklet2 & vars$booklet3
scaling::dif_analysis(
  resp = resp,
  vars = vars,
  select = c("mixed", "mixed", "mixed_common"),
  valid = "valid",
  dif_vars = c("sex", "mig", "booklet"),
  scoring = "scoring",
  mvs = mvs,
  overwrite = TRUE,
  verbose = FALSE,
  save = TRUE,
  print = FALSE,
  return = FALSE,
  path_results = paste0(getwd(), "/results"),
  path_table = "tables",
  warn = FALSE
)

6 Distractor analysis

Code
scaling::dis_analysis(
  resp = resp,
  vars = vars,
  valid = "valid",
  select_raw = "raw",
  correct = "correct",
  mvs = mvs,
  print = FALSE,
  save = TRUE,
  overwrite = TRUE,
  path_results = paste0(getwd(), "/results"),
  path_table = "tables"
)

7 Create SUF

Code
scores <- scaling::create_scores(
  resp = resp,
  vars = vars,
  score_name = "reg7",
  select = "mixed",
  valid = "valid",
  scoring = "scoring",
  #facet = "rotation", # does not work in current version of scaling
  num_cat = "num_cat",
  mvs = mvs,
  sum_select = "dich",
  wle = TRUE,
  sum_score = TRUE,
  metap = FALSE,
  return = TRUE,
  save = TRUE,
  path_results = paste0(getwd(), "/results")
)

scaling::create_suf(
  resp = resp,
  vars = vars,
  select = "mixed",
  competence = 'Reading competence',
  scores = scores,
  score_name = 'reg7',
  return = FALSE,
  path = paste0(getwd(), "/results")
)
rm(scores)

8 Hash table

Code
# Create hash for results and table files
hashvec <- c()
dirvec <- c(base::dir("results", full.names = TRUE),
            base::dir("tables", full.names = TRUE))
for (i in dirvec)
  hashvec <- c(hashvec, rlang::hash(i))
names(hashvec) <- base::basename(dirvec)

# Plots for missing by person
filelist <- base::dir("plots/missing/by_person", full.names = TRUE)
hashvec <- c(hashvec, mv_person.plot = rlang::hash(rlang::hash_file(filelist)))

# Plots for missing by item
filelist <- base::dir("plots/missing/by_item", full.names = TRUE)
hashvec <- c(hashvec, mv_item.plot = rlang::hash(rlang::hash_file(filelist)))

# Plots for ICCs
filelist <- base::dir("plots/ICCs", full.names = TRUE, recursive = TRUE)
hashvec <- c(hashvec, iccs.plot = rlang::hash(rlang::hash_file(filelist)))

# Plots for Wright maps
filelist <- base::dir("plots/Wright_Maps", full.names = TRUE, recursive = TRUE)
hashvec <- c(hashvec, wmaps.plot = rlang::hash(rlang::hash_file(filelist)))

# Save hash vector
saveRDS(hashvec, "ex3hash.rds")

9 Software

Code
R version 4.3.3 (2024-02-29 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default


locale:
[1] LC_COLLATE=German_Germany.utf8  LC_CTYPE=German_Germany.utf8   
[3] LC_MONETARY=German_Germany.utf8 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.utf8    

time zone: Europe/Berlin
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] scaling_1.1.1    tibble_3.2.1     colorspace_2.1-0 sfsmisc_1.1-17  
 [5] haven_2.5.4      Hmisc_5.1-1      psych_2.4.1      rlang_1.1.3     
 [9] magrittr_2.0.3   openxlsx_4.2.5.2 tidyselect_1.2.0 tidyr_1.3.1     
[13] ggplot2_3.5.0    here_1.0.1       dplyr_1.1.4      TAM_4.2-21      
[17] CDM_8.2-6        mvtnorm_1.2-4   

loaded via a namespace (and not attached):
 [1] gtable_0.3.4       xfun_0.42          htmlwidgets_1.6.4  lattice_0.22-5    
 [5] vctrs_0.6.5        tools_4.3.3        generics_0.1.3     parallel_4.3.3    
 [9] fansi_1.0.6        cluster_2.1.6      pkgconfig_2.0.3    data.table_1.15.2 
[13] checkmate_2.3.1    RColorBrewer_1.1-3 lifecycle_1.0.4    compiler_4.3.3    
[17] farver_2.1.1       stringr_1.5.1      textshaping_0.3.7  munsell_0.5.0     
[21] mnormt_2.1.1       htmltools_0.5.7    WrightMap_1.3      yaml_2.3.8        
[25] htmlTable_2.4.2    Formula_1.2-5      pillar_1.9.0       MASS_7.3-60.0.1   
[29] admisc_0.34        rpart_4.1.23       nlme_3.1-164       polycor_0.8-1     
[33] zip_2.3.1          digest_0.6.34      stringi_1.8.3      purrr_1.0.2       
[37] forcats_1.0.0      rprojroot_2.0.4    fastmap_1.1.1      grid_4.3.3        
[41] cli_3.6.2          base64enc_0.1-3    utf8_1.2.4         foreign_0.8-86    
[45] withr_3.0.0        scales_1.3.0       backports_1.4.1    rmarkdown_2.26    
[49] nnet_7.3-19        gridExtra_2.3      ragg_1.2.7         hms_1.1.3         
[53] evaluate_0.23      knitr_1.45         Rcpp_1.0.12        glue_1.7.0        
[57] rstudioapi_0.15.0  jsonlite_1.8.8     R6_2.5.1           systemfonts_1.0.5