Scaling of NEPS example data ex2

Author

Timo Gnambs

Published

2024-04-10 13:18:00

Modified

2024-04-10 13:48:34

1 R setup

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

# Load packages
library(scaling)
library(rlang)

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

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

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

2 Sample descriptives

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

# Descriptives for nominal variables
scaling::desc_nom(resp, desc = c("sex", "mig", "school"), valid = "valid")
$sex
x
   0    1 <NA> 
 762  738    0 

$mig
x
   0    1 <NA> 
 586  610  304 

$school
x
   0    1    2 <NA> 
 507  516  477    0 

$sex
[1] "50.8%" "49.2%" "0%"   

$mig
[1] "39.07%" "40.67%" "20.27%"

$school
[1] "33.8%" "34.4%" "31.8%" "0%"   

$sex
[1] "50.8%" "49.2%"

$mig
[1] "49%" "51%"

$school
[1] "33.8%" "34.4%" "31.8%"
Code
scaling::show_attributes(resp, desc = c("sex", "mig", "school"))
  male female 
     0      1 
 no yes 
  0   1 
 lower medium  upper 
     0      1      2 

3 Missing responses

3.1 By person

Code
scaling::mv_person(
  resp = resp,
  vars = vars,
  select = "mixed",
  valid = "valid",
  mvs = mvs,
  labels_mvs = labels_mvs,
  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 = TRUE,
  verbose = FALSE
)

3.2 By item

Code
scaling::mv_item(
  resp = resp,
  vars = vars,
  select = "mixed",
  valid = "valid",
  position = "pos",
  mvs = mvs,
  labels_mvs = labels_mvs,
  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 = TRUE,
  verbose = FALSE
)

4 IRT analyses

4.1 Dichotomous

Code
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
)

4.2 Polytomous

Code
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
)

5 DIF analysis

Code
scaling::dif_analysis(
  resp = resp,
  vars = vars,
  select = "mixed",
  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"
)

6 Dimensionality

Code
vars$content <- as.numeric(factor(vars$content))
scaling::dim_analysis(
  resp = resp,
  vars = vars,
  select = "mixed",
  valid = "valid",
  scoring = "scoring",
  mvs = mvs,
  dim = "content",
  snodes = 5000,
  return = FALSE,
  save = TRUE,
  print = FALSE,
  path_results = paste0(getwd(), "/results"), 
  path_table = "tables",
  overwrite = TRUE,
  verbose = FALSE
)

7 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"
)

8 Create SUF

Code
scores <- scaling::create_scores(
  resp = resp,
  vars = vars,
  score_name = "mag12",
  select = "mixed",
  valid = "valid",
  mvs = mvs,
  sum_select = "dich",
  scoring = "scoring",
  #facet = "rotation", # does not work in current version of scaling
  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 = 'Mathematical competences',
  scores = scores,
  score_name = 'mag12',
  return = FALSE,
  path = paste0(getwd(), "/results")
)
rm(scores)

9 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)
hashvec <- c(hashvec, wmaps.plot = rlang::hash(rlang::hash_file(filelist)))

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

10 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