# Load necessary libraries
library(TAM)
## Loading required package: CDM
## Loading required package: mvtnorm
## **********************************
## ** CDM 8.2-6 (2022-08-25 15:43:23)       
## ** Cognitive Diagnostic Models  **
## **********************************
## * TAM 4.2-21 (2024-02-19 18:52:08)
# install.packages("WrightMap")
library("WrightMap") 

# Get the directory where the current R script is located
script_dir <- dirname(rstudioapi::getActiveDocumentContext()$path)

# Set the working directory to the script's directory
setwd(script_dir)

# Specify the file name (replace 'your_file_name.extension' with the actual file name)
file_name <- "patternTR.csv"

# Read the file (adjust the function based on the file type, e.g., read.csv, read.table, etc.)
data <- read.csv(file_name, sep=",", stringsAsFactors=FALSE)
# For other file types, use the appropriate function, e.g., read.table, readRDS, etc.

# Display the first few rows of the data
head(data)
##   code IDPattern_ABC IDPattern_AABB IDPattern_ABB IDPattern_ABCD IDPattern_ABC2
## 1 1001             0              1             1              1              1
## 2 1002             1              1             1              1              1
## 3 1003             0              0             1              1              1
## 4 1004             0              1             1              1              0
## 5 1005             0              0             1              0              1
## 6 1006             0              1             1              1              1
##   Extend_AABB Extend_AB Extend_ABC Extend_ABCD Extend_AAB Extend_AABC
## 1           1         1          1           1          1           1
## 2           1         0          1           0          0           0
## 3           0         1          1           0          1           0
## 4           0         0          0           0          0           1
## 5           1         0          1           1          0           0
## 6           1         1          1           1          1           1
##   Extend_AABB2 Abstract_ABB Abstract_AB Abstract_AAB Abstract_AABC
## 1            1            1           1            1             1
## 2            0            0           1            0             0
## 3            0            0           0            0             0
## 4            1            0           1            0             1
## 5            0            1           1            0             0
## 6            0            1           1            0             1
##   Abstract.ABCD Abstract_ABB2 Abstract_ABCC
## 1             1             1             1
## 2             1             1             0
## 3             0             1             0
## 4             0             0             0
## 5             1             1             0
## 6             0             1             1
summary(data)
##       code      IDPattern_ABC    IDPattern_AABB   IDPattern_ABB   
##  Min.   :1001   Min.   :0.0000   Min.   :0.0000   Min.   :0.0000  
##  1st Qu.:1030   1st Qu.:0.0000   1st Qu.:1.0000   1st Qu.:1.0000  
##  Median :1060   Median :1.0000   Median :1.0000   Median :1.0000  
##  Mean   :1059   Mean   :0.6636   Mean   :0.7664   Mean   :0.7944  
##  3rd Qu.:1088   3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:1.0000  
##  Max.   :1115   Max.   :1.0000   Max.   :1.0000   Max.   :1.0000  
##  IDPattern_ABCD   IDPattern_ABC2    Extend_AABB       Extend_AB     
##  Min.   :0.0000   Min.   :0.0000   Min.   :0.0000   Min.   :0.0000  
##  1st Qu.:0.0000   1st Qu.:0.5000   1st Qu.:1.0000   1st Qu.:1.0000  
##  Median :1.0000   Median :1.0000   Median :1.0000   Median :1.0000  
##  Mean   :0.5981   Mean   :0.7477   Mean   :0.8411   Mean   :0.7664  
##  3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:1.0000  
##  Max.   :1.0000   Max.   :1.0000   Max.   :1.0000   Max.   :1.0000  
##    Extend_ABC      Extend_ABCD       Extend_AAB     Extend_AABC    
##  Min.   :0.0000   Min.   :0.0000   Min.   :0.000   Min.   :0.0000  
##  1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:1.000   1st Qu.:0.0000  
##  Median :1.0000   Median :1.0000   Median :1.000   Median :1.0000  
##  Mean   :0.7383   Mean   :0.6075   Mean   :0.785   Mean   :0.7383  
##  3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:1.000   3rd Qu.:1.0000  
##  Max.   :1.0000   Max.   :1.0000   Max.   :1.000   Max.   :1.0000  
##   Extend_AABB2     Abstract_ABB     Abstract_AB      Abstract_AAB   
##  Min.   :0.0000   Min.   :0.0000   Min.   :0.0000   Min.   :0.0000  
##  1st Qu.:1.0000   1st Qu.:0.0000   1st Qu.:1.0000   1st Qu.:0.0000  
##  Median :1.0000   Median :1.0000   Median :1.0000   Median :1.0000  
##  Mean   :0.7944   Mean   :0.6636   Mean   :0.7664   Mean   :0.6542  
##  3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:1.0000  
##  Max.   :1.0000   Max.   :1.0000   Max.   :1.0000   Max.   :1.0000  
##  Abstract_AABC   Abstract.ABCD    Abstract_ABB2   Abstract_ABCC   
##  Min.   :0.000   Min.   :0.0000   Min.   :0.000   Min.   :0.0000  
##  1st Qu.:1.000   1st Qu.:0.0000   1st Qu.:1.000   1st Qu.:0.0000  
##  Median :1.000   Median :1.0000   Median :1.000   Median :1.0000  
##  Mean   :0.757   Mean   :0.7103   Mean   :0.757   Mean   :0.5794  
##  3rd Qu.:1.000   3rd Qu.:1.0000   3rd Qu.:1.000   3rd Qu.:1.0000  
##  Max.   :1.000   Max.   :1.0000   Max.   :1.000   Max.   :1.0000
# Remove the Student ID and Grade level variables:
data.responses <- subset(data, select = -c(code))

# Compute total score for each participant
TotalScore <- rowSums(data.responses)

# Compute the maximum possible score (all items correct)
MaxScore <- ncol(data.responses)

# Identify participants who did NOT score full marks
filtered_data <- data.responses[TotalScore < MaxScore, ]

# Check the number of participants removed
cat("Number of participants removed due to full scores:", nrow(data.responses) - nrow(filtered_data), "\n")
## Number of participants removed due to full scores: 16
# Update the dataset for analysis
data.responses <- filtered_data

# Running the Dichotomous Rasch Model
dichot.data <- tam(data.responses)
## ....................................................
## Processing Data      2025-02-13 01:26:58.590369 
##     * Response Data: 91 Persons and  19 Items 
##     * Numerical integration with 21 nodes
##     * Created Design Matrices   ( 2025-02-13 01:26:58.592417 )
##     * Calculated Sufficient Statistics   ( 2025-02-13 01:26:58.60266 )
## ....................................................
## Iteration 1     2025-02-13 01:26:58.604263
## E Step
## M Step Intercepts   |---
##   Deviance = 2000.527
##   Maximum item intercept parameter change: 0.13592
##   Maximum item slope parameter change: 0
##   Maximum regression parameter change: 0
##   Maximum variance parameter change: 0.097888
## ....................................................
## Iteration 2     2025-02-13 01:26:58.606238
## E Step
## M Step Intercepts   |--
##   Deviance = 1998.2883 | Absolute change: 2.2387 | Relative change: 0.0011203
##   Maximum item intercept parameter change: 0.026118
##   Maximum item slope parameter change: 0
##   Maximum regression parameter change: 0
##   Maximum variance parameter change: 0.031401
## ....................................................
## Iteration 3     2025-02-13 01:26:58.606696
## E Step
## M Step Intercepts   |--
##   Deviance = 1997.9378 | Absolute change: 0.3505 | Relative change: 0.00017545
##   Maximum item intercept parameter change: 0.018739
##   Maximum item slope parameter change: 0
##   Maximum regression parameter change: 0
##   Maximum variance parameter change: 0.0139
## ....................................................
## Iteration 4     2025-02-13 01:26:58.607102
## E Step
## M Step Intercepts   |--
##   Deviance = 1997.7648 | Absolute change: 0.173 | Relative change: 8.66e-05
##   Maximum item intercept parameter change: 0.013441
##   Maximum item slope parameter change: 0
##   Maximum regression parameter change: 0
##   Maximum variance parameter change: 0.005693
## ....................................................
## Iteration 5     2025-02-13 01:26:58.6075
## E Step
## M Step Intercepts   |--
##   Deviance = 1997.6736 | Absolute change: 0.0912 | Relative change: 4.564e-05
##   Maximum item intercept parameter change: 0.00974
##   Maximum item slope parameter change: 0
##   Maximum regression parameter change: 0
##   Maximum variance parameter change: 0.001829
## ....................................................
## Iteration 6     2025-02-13 01:26:58.607891
## E Step
## M Step Intercepts   |--
##   Deviance = 1997.6237 | Absolute change: 0.0499 | Relative change: 2.498e-05
##   Maximum item intercept parameter change: 0.007128
##   Maximum item slope parameter change: 0
##   Maximum regression parameter change: 0
##   Maximum variance parameter change: 8.6e-05
## ....................................................
## Iteration 7     2025-02-13 01:26:58.608278
## E Step
## M Step Intercepts   |--
##   Deviance = 1997.5958 | Absolute change: 0.0279 | Relative change: 1.397e-05
##   Maximum item intercept parameter change: 0.005391
##   Maximum item slope parameter change: 0
##   Maximum regression parameter change: 0
##   Maximum variance parameter change: 0.000618
## ....................................................
## Iteration 8     2025-02-13 01:26:58.608662
## E Step
## M Step Intercepts   |--
##   Deviance = 1997.58 | Absolute change: 0.0158 | Relative change: 7.91e-06
##   Maximum item intercept parameter change: 0.004098
##   Maximum item slope parameter change: 0
##   Maximum regression parameter change: 0
##   Maximum variance parameter change: 0.000829
## ....................................................
## Iteration 9     2025-02-13 01:26:58.609047
## E Step
## M Step Intercepts   |--
##   Deviance = 1997.571 | Absolute change: 0.009 | Relative change: 4.5e-06
##   Maximum item intercept parameter change: 0.003111
##   Maximum item slope parameter change: 0
##   Maximum regression parameter change: 0
##   Maximum variance parameter change: 0.000817
## ....................................................
## Iteration 10     2025-02-13 01:26:58.609428
## E Step
## M Step Intercepts   |--
##   Deviance = 1997.5659 | Absolute change: 0.0051 | Relative change: 2.58e-06
##   Maximum item intercept parameter change: 0.002362
##   Maximum item slope parameter change: 0
##   Maximum regression parameter change: 0
##   Maximum variance parameter change: 0.000719
## ....................................................
## Iteration 11     2025-02-13 01:26:58.609808
## E Step
## M Step Intercepts   |--
##   Deviance = 1997.5629 | Absolute change: 0.003 | Relative change: 1.48e-06
##   Maximum item intercept parameter change: 0.001792
##   Maximum item slope parameter change: 0
##   Maximum regression parameter change: 0
##   Maximum variance parameter change: 0.000597
## ....................................................
## Iteration 12     2025-02-13 01:26:58.610187
## E Step
## M Step Intercepts   |--
##   Deviance = 1997.5612 | Absolute change: 0.0017 | Relative change: 8.5e-07
##   Maximum item intercept parameter change: 0.00136
##   Maximum item slope parameter change: 0
##   Maximum regression parameter change: 0
##   Maximum variance parameter change: 0.000479
## ....................................................
## Iteration 13     2025-02-13 01:26:58.610582
## E Step
## M Step Intercepts   |--
##   Deviance = 1997.5602 | Absolute change: 0.001 | Relative change: 4.9e-07
##   Maximum item intercept parameter change: 0.001032
##   Maximum item slope parameter change: 0
##   Maximum regression parameter change: 0
##   Maximum variance parameter change: 0.000378
## ....................................................
## Iteration 14     2025-02-13 01:26:58.610957
## E Step
## M Step Intercepts   |--
##   Deviance = 1997.5597 | Absolute change: 6e-04 | Relative change: 2.8e-07
##   Maximum item intercept parameter change: 0.000783
##   Maximum item slope parameter change: 0
##   Maximum regression parameter change: 0
##   Maximum variance parameter change: 0.000294
## ....................................................
## Iteration 15     2025-02-13 01:26:58.611347
## E Step
## M Step Intercepts   |--
##   Deviance = 1997.5594 | Absolute change: 3e-04 | Relative change: 1.6e-07
##   Maximum item intercept parameter change: 0.000594
##   Maximum item slope parameter change: 0
##   Maximum regression parameter change: 0
##   Maximum variance parameter change: 0.000227
## ....................................................
## Iteration 16     2025-02-13 01:26:58.611732
## E Step
## M Step Intercepts   |--
##   Deviance = 1997.5592 | Absolute change: 2e-04 | Relative change: 9e-08
##   Maximum item intercept parameter change: 0.000451
##   Maximum item slope parameter change: 0
##   Maximum regression parameter change: 0
##   Maximum variance parameter change: 0.000174
## ....................................................
## Iteration 17     2025-02-13 01:26:58.612111
## E Step
## M Step Intercepts   |--
##   Deviance = 1997.5591 | Absolute change: 1e-04 | Relative change: 5e-08
##   Maximum item intercept parameter change: 0.000342
##   Maximum item slope parameter change: 0
##   Maximum regression parameter change: 0
##   Maximum variance parameter change: 0.000133
## ....................................................
## Iteration 18     2025-02-13 01:26:58.612494
## E Step
## M Step Intercepts   |--
##   Deviance = 1997.559 | Absolute change: 1e-04 | Relative change: 3e-08
##   Maximum item intercept parameter change: 0.00026
##   Maximum item slope parameter change: 0
##   Maximum regression parameter change: 0
##   Maximum variance parameter change: 0.000101
## ....................................................
## Iteration 19     2025-02-13 01:26:58.612875
## E Step
## M Step Intercepts   |--
##   Deviance = 1997.559 | Absolute change: 0 | Relative change: 2e-08
##   Maximum item intercept parameter change: 0.000197
##   Maximum item slope parameter change: 0
##   Maximum regression parameter change: 0
##   Maximum variance parameter change: 7.7e-05
## ....................................................
## Iteration 20     2025-02-13 01:26:58.61327
## E Step
## M Step Intercepts   |--
##   Deviance = 1997.5589 | Absolute change: 0 | Relative change: 1e-08
##   Maximum item intercept parameter change: 0.00015
##   Maximum item slope parameter change: 0
##   Maximum regression parameter change: 0
##   Maximum variance parameter change: 5.9e-05
## ....................................................
## Iteration 21     2025-02-13 01:26:58.613652
## E Step
## M Step Intercepts   |--
##   Deviance = 1997.5589 | Absolute change: 0 | Relative change: 1e-08
##   Maximum item intercept parameter change: 0.000114
##   Maximum item slope parameter change: 0
##   Maximum regression parameter change: 0
##   Maximum variance parameter change: 4.5e-05
## ....................................................
## Iteration 22     2025-02-13 01:26:58.61404
## E Step
## M Step Intercepts   |-
##   Deviance = 1997.5589 | Absolute change: 0 | Relative change: 0
##   Maximum item intercept parameter change: 8.6e-05
##   Maximum item slope parameter change: 0
##   Maximum regression parameter change: 0
##   Maximum variance parameter change: 3.4e-05
## ....................................................
## Item Parameters
##    xsi.index      xsi.label     est
## 1          1  IDPattern_ABC -0.5038
## 2          2 IDPattern_AABB -1.1443
## 3          3  IDPattern_ABB -1.3427
## 4          4 IDPattern_ABCD -0.1315
## 5          5 IDPattern_ABC2 -1.0194
## 6          6    Extend_AABB -1.7148
## 7          7      Extend_AB -1.1443
## 8          8     Extend_ABC -0.9587
## 9          9    Extend_ABCD -0.1840
## 10        10     Extend_AAB -1.2749
## 11        11    Extend_AABC -0.9587
## 12        12   Extend_AABB2 -1.3427
## 13        13   Abstract_ABB -0.5038
## 14        14    Abstract_AB -1.1443
## 15        15   Abstract_AAB -0.4497
## 16        16  Abstract_AABC -1.0812
## 17        17  Abstract.ABCD -0.7826
## 18        18  Abstract_ABB2 -1.0812
## 19        19  Abstract_ABCC -0.0267
## ...................................
## Regression Coefficients
##      [,1]
## [1,]    0
## 
## Variance:
##        [,1]
## [1,] 0.8548
## 
## 
## EAP Reliability:
## [1] 0.735
## 
## -----------------------------
## Start:  2025-02-13 01:26:58.589836
## End:  2025-02-13 01:26:58.621954 
## Time difference of 0.03211808 secs
# Request a summary of the model results
summary(dichot.data)
## ------------------------------------------------------------
## TAM 4.2-21 (2024-02-19 18:52:08) 
## R version 4.4.2 (2024-10-31) aarch64, darwin20 | nodename=unknown1a5b05895e03.home | login=root 
## 
## Date of Analysis: 2025-02-13 01:26:58.621954 
## Time difference of 0.03211808 secs
## Computation time: 0.03211808 
## 
## Multidimensional Item Response Model in TAM 
## 
## IRT Model: 1PL
## Call:
## tam.mml(resp = resp)
## 
## ------------------------------------------------------------
## Number of iterations = 22 
## Numeric integration with 21 integration points
## 
## Deviance = 1997.56 
## Log likelihood = -998.78 
## Number of persons = 91 
## Number of persons used = 91 
## Number of items = 19 
## Number of estimated parameters = 20 
##     Item threshold parameters = 19 
##     Item slope parameters = 0 
##     Regression parameters = 0 
##     Variance/covariance parameters = 1 
## 
## AIC = 2038  | penalty=40    | AIC=-2*LL + 2*p 
## AIC3 = 2058  | penalty=60    | AIC3=-2*LL + 3*p 
## BIC = 2088  | penalty=90.22    | BIC=-2*LL + log(n)*p 
## aBIC = 2024  | penalty=26.21    | aBIC=-2*LL + log((n-2)/24)*p  (adjusted BIC) 
## CAIC = 2108  | penalty=110.22    | CAIC=-2*LL + [log(n)+1]*p  (consistent AIC) 
## AICc = 2050  | penalty=52    | AICc=-2*LL + 2*p + 2*p*(p+1)/(n-p-1)  (bias corrected AIC) 
## GHP = 0.58923     | GHP=( -LL + p ) / (#Persons * #Items)  (Gilula-Haberman log penalty) 
## 
## ------------------------------------------------------------
## EAP Reliability
## [1] 0.735
## ------------------------------------------------------------
## Covariances and Variances
##       [,1]
## [1,] 0.855
## ------------------------------------------------------------
## Correlations and Standard Deviations (in the diagonal)
##       [,1]
## [1,] 0.925
## ------------------------------------------------------------
## Regression Coefficients
##      [,1]
## [1,]    0
## ------------------------------------------------------------
## Item Parameters -A*Xsi
##              item  N     M xsi.item AXsi_.Cat1 B.Cat1.Dim1
## 1   IDPattern_ABC 91 0.604   -0.504     -0.504           1
## 2  IDPattern_AABB 91 0.725   -1.144     -1.144           1
## 3   IDPattern_ABB 91 0.758   -1.343     -1.343           1
## 4  IDPattern_ABCD 91 0.527   -0.131     -0.131           1
## 5  IDPattern_ABC2 91 0.703   -1.019     -1.019           1
## 6     Extend_AABB 91 0.813   -1.715     -1.715           1
## 7       Extend_AB 91 0.725   -1.144     -1.144           1
## 8      Extend_ABC 91 0.692   -0.959     -0.959           1
## 9     Extend_ABCD 91 0.538   -0.184     -0.184           1
## 10     Extend_AAB 91 0.747   -1.275     -1.275           1
## 11    Extend_AABC 91 0.692   -0.959     -0.959           1
## 12   Extend_AABB2 91 0.758   -1.343     -1.343           1
## 13   Abstract_ABB 91 0.604   -0.504     -0.504           1
## 14    Abstract_AB 91 0.725   -1.144     -1.144           1
## 15   Abstract_AAB 91 0.593   -0.450     -0.450           1
## 16  Abstract_AABC 91 0.714   -1.081     -1.081           1
## 17  Abstract.ABCD 91 0.659   -0.783     -0.783           1
## 18  Abstract_ABB2 91 0.714   -1.081     -1.081           1
## 19  Abstract_ABCC 91 0.505   -0.027     -0.027           1
## 
## Item Parameters in IRT parameterization
##              item alpha   beta
## 1   IDPattern_ABC     1 -0.504
## 2  IDPattern_AABB     1 -1.144
## 3   IDPattern_ABB     1 -1.343
## 4  IDPattern_ABCD     1 -0.131
## 5  IDPattern_ABC2     1 -1.019
## 6     Extend_AABB     1 -1.715
## 7       Extend_AB     1 -1.144
## 8      Extend_ABC     1 -0.959
## 9     Extend_ABCD     1 -0.184
## 10     Extend_AAB     1 -1.275
## 11    Extend_AABC     1 -0.959
## 12   Extend_AABB2     1 -1.343
## 13   Abstract_ABB     1 -0.504
## 14    Abstract_AB     1 -1.144
## 15   Abstract_AAB     1 -0.450
## 16  Abstract_AABC     1 -1.081
## 17  Abstract.ABCD     1 -0.783
## 18  Abstract_ABB2     1 -1.081
## 19  Abstract_ABCC     1 -0.027
# Plot the Wright Map 
IRT.WrightMap(dichot.data, show.thr.lab=FALSE, main.title = "Patterning Wright Map")

difficulty <- dichot.data$xsi

summary(difficulty)
##       xsi               se.xsi      
##  Min.   :-1.71483   Min.   :0.2287  
##  1st Qu.:-1.14431   1st Qu.:0.2331  
##  Median :-1.01935   Median :0.2475  
##  Mean   :-0.88364   Mean   :0.2468  
##  3rd Qu.:-0.50383   3rd Qu.:0.2526  
##  Max.   :-0.02674   Max.   :0.2852
sd(difficulty$xsi)
## [1] 0.4626988
sd(difficulty$se.xsi)
## [1] 0.01452339
hist(difficulty$xsi, main = "Histogram of Item Difficulty Estimates for the Patterning TR Data",
     xlab = "Item Difficulty Estimates in Logits") 

item.fit <- tam.fit(dichot.data)
## Item fit calculation based on 100 simulations
## |**********|
## |----------|
item.fit <- as.data.frame(item.fit$itemfit)

summary(item.fit)
##   parameter             Outfit          Outfit_t          Outfit_p     
##  Length:19          Min.   :0.7719   Min.   :-2.3035   Min.   :0.0000  
##  Class :character   1st Qu.:0.8434   1st Qu.:-1.3608   1st Qu.:0.0631  
##  Mode  :character   Median :0.8920   Median :-0.9137   Median :0.1651  
##                     Mean   :0.9866   Mean   :-0.0990   Mean   :0.2000  
##                     3rd Qu.:1.1331   3rd Qu.: 1.4228   3rd Qu.:0.2925  
##                     Max.   :1.5250   Max.   : 5.4786   Max.   :0.6026  
##   Outfit_pholm           Infit           Infit_t            Infit_p         
##  Min.   :0.0000008   Min.   :0.8620   Min.   :-1.46123   Min.   :0.0000092  
##  1st Qu.:0.9044535   1st Qu.:0.8970   1st Qu.:-0.92772   1st Qu.:0.1717641  
##  Median :1.0000000   Median :0.9452   Median :-0.44586   Median :0.3655766  
##  Mean   :0.8659853   Mean   :0.9984   Mean   : 0.03083   Mean   :0.3719531  
##  3rd Qu.:1.0000000   3rd Qu.:1.0837   3rd Qu.: 0.83085   3rd Qu.:0.5061732  
##  Max.   :1.0000000   Max.   :1.4119   Max.   : 4.43436   Max.   :0.8766593  
##   Infit_pholm       
##  Min.   :0.0001755  
##  1st Qu.:1.0000000  
##  Median :1.0000000  
##  Mean   :0.9473777  
##  3rd Qu.:1.0000000  
##  Max.   :1.0000000
achievement <- as.data.frame(tam.wle(dichot.data))
## Iteration in WLE/MLE estimation  1   | Maximal change  0.9945 
## Iteration in WLE/MLE estimation  2   | Maximal change  0.2444 
## Iteration in WLE/MLE estimation  3   | Maximal change  0.0074 
## Iteration in WLE/MLE estimation  4   | Maximal change  4e-04 
## Iteration in WLE/MLE estimation  5   | Maximal change  0 
## ----
##  WLE Reliability= 0.671
summary(achievement)
##       pid          N.items    PersonScores    PersonMax      theta        
##  Min.   : 1.0   Min.   :19   Min.   : 3.0   Min.   :19   Min.   :-2.5039  
##  1st Qu.:23.5   1st Qu.:19   1st Qu.:10.0   1st Qu.:19   1st Qu.:-0.7815  
##  Median :46.0   Median :19   Median :13.0   Median :19   Median :-0.1167  
##  Mean   :46.0   Mean   :19   Mean   :12.8   Mean   :19   Mean   :-0.0194  
##  3rd Qu.:68.5   3rd Qu.:19   3rd Qu.:16.0   3rd Qu.:19   3rd Qu.: 0.7393  
##  Max.   :91.0   Max.   :19   Max.   :18.0   Max.   :19   Max.   : 1.7266  
##      error           WLE.rel      
##  Min.   :0.4705   Min.   :0.6706  
##  1st Qu.:0.4844   1st Qu.:0.6706  
##  Median :0.5013   Median :0.6706  
##  Mean   :0.5705   Mean   :0.6706  
##  3rd Qu.:0.6148   3rd Qu.:0.6706  
##  Max.   :0.8815   Max.   :0.6706
hist(achievement$theta, main = "Histogram of Person Achievement Estimates \nfor the Patterning TR Data",
     xlab = "Person Achievement Estimates in Logits") 

person.fit <- tam.personfit(dichot.data) 
summary(person.fit)
##   outfitPerson    outfitPerson_t      infitPerson     infitPerson_t     
##  Min.   :0.3765   Min.   :-1.30060   Min.   :0.6451   Min.   :-1.16583  
##  1st Qu.:0.8044   1st Qu.:-0.50064   1st Qu.:0.8460   1st Qu.:-0.28291  
##  Median :0.9597   Median :-0.05992   Median :0.9532   Median :-0.09401  
##  Mean   :0.9100   Mean   :-0.04553   Mean   :0.9398   Mean   : 0.00029  
##  3rd Qu.:1.0273   3rd Qu.: 0.24468   3rd Qu.:1.0126   3rd Qu.: 0.15923  
##  Max.   :1.3254   Max.   : 2.54319   Max.   :1.2666   Max.   : 2.49697
summary.table.statistics <- c("Logit Scale Location Mean",
                              "Logit Scale Location SD",
                              "Standard Error Mean",
                              "Standard Error SD",
                              "Outfit MSE Mean",
                              "Outfit MSE SD",
                              "Infit MSE Mean",
                              "Infit MSE SD",
                              "Std. Outfit Mean",
                              "Std. Outfit SD",
                              "Std. Infit Mean",
                              "Std. Infit SD",
                              "Reliability of Separation")

item.summary.results <- rbind(mean(difficulty$xsi),
                              sd(difficulty$xsi),
                              mean(difficulty$se.xsi),
                              sd(difficulty$se.xsi),
                              mean(item.fit$Outfit),
                              sd(item.fit$Outfit),
                              mean(item.fit$Infit),
                              sd(item.fit$Infit),
                              mean(item.fit$Outfit_t),
                              sd(item.fit$Outfit_t),
                              mean(item.fit$Infit_t),
                              sd(item.fit$Infit_t),
                              dichot.data$EAP.rel)  

person.summary.results <- rbind(mean(achievement$theta),
                                sd(achievement$theta),
                                mean(achievement$error),
                                sd(achievement$error),
                                mean(person.fit$outfitPerson),
                                sd(person.fit$outfitPerson),
                                mean(person.fit$infitPerson),
                                sd(person.fit$infitPerson),
                                mean(person.fit$outfitPerson_t),
                                sd(person.fit$outfitPerson_t),
                                mean(person.fit$infitPerson_t),
                                sd(person.fit$infitPerson_t),
                                mean(achievement$WLE.rel))

# Round the values for presentation in a table:
item.summary.results_rounded <- round(item.summary.results, digits = 2)

person.summary.results_rounded <- round(person.summary.results, digits = 2)

Table1 <- cbind.data.frame(summary.table.statistics,
                           item.summary.results_rounded, 
                           person.summary.results_rounded)

# add descriptive column labels:
names(Table1) <- c("Statistic", "Items", "Persons")  

# Calculate the proportion correct for each task:
TaskCorrect <- apply(data.responses, 2, sum)
PropCorrect <- (TaskCorrect/nrow(data.responses))
SD_PropCorrect <- sqrt((PropCorrect * (1 - PropCorrect)) / nrow(data.responses))


# Compute Item-Trait Correlation (correlation of each item with person ability estimates)
ItemTraitCorrelation <- cor(data.responses, achievement$theta, use = "complete.obs")

# Compute Item-Total Correlation (correlation of each item with total test scores)
TotalScore <- rowSums(data.responses)  # Compute total score for each student
ItemTotalCorrelation <- sapply(1:ncol(data.responses), function(i) {
  cor(data.responses[, i], TotalScore - data.responses[, i], use = "complete.obs")
})

# Combine item statistics into Table2
Table2 <- cbind.data.frame(item.fit$parameter, 
                           PropCorrect,
                           SD_PropCorrect,
                           difficulty$xsi,
                           difficulty$se.xsi,
                           item.fit$Outfit,
                           item.fit$Outfit_t,
                           item.fit$Infit,
                           item.fit$Infit_t,
                           ItemTraitCorrelation,  # 🔹 Added Item-Trait Correlation
                           ItemTotalCorrelation)  # 🔹 Added Item-Total Correlation

# Rename columns to include new statistics
names(Table2) <- c("Task ID", "Proportion Correct", "Proportion Correct SD", 
                   "Item Location", "Item SE", "Outfit MSE", "Std. Outfit", 
                   "Infit MSE", "Std. Infit", "Item-Trait Correlation", "Item-Total Correlation")

# Sort Table2 by Item difficulty
Table2 <- Table2[order(-Table2$`Item Location`),]

# Round the numeric values (all columns except the first one) to 2 digits
Table2[, -1] <- round(Table2[, -1], digits = 2)

# Display Table2
print(Table2)
##                       Task ID Proportion Correct Proportion Correct SD
## Abstract_ABCC   Abstract_ABCC               0.51                  0.05
## IDPattern_ABCD IDPattern_ABCD               0.53                  0.05
## Extend_ABCD       Extend_ABCD               0.54                  0.05
## Abstract_AAB     Abstract_AAB               0.59                  0.05
## IDPattern_ABC   IDPattern_ABC               0.60                  0.05
## Abstract_ABB     Abstract_ABB               0.60                  0.05
## Abstract.ABCD   Abstract.ABCD               0.66                  0.05
## Extend_ABC         Extend_ABC               0.69                  0.05
## Extend_AABC       Extend_AABC               0.69                  0.05
## IDPattern_ABC2 IDPattern_ABC2               0.70                  0.05
## Abstract_AABC   Abstract_AABC               0.71                  0.05
## Abstract_ABB2   Abstract_ABB2               0.71                  0.05
## IDPattern_AABB IDPattern_AABB               0.73                  0.05
## Extend_AB           Extend_AB               0.73                  0.05
## Abstract_AB       Abstract_AB               0.73                  0.05
## Extend_AAB         Extend_AAB               0.75                  0.05
## IDPattern_ABB   IDPattern_ABB               0.76                  0.04
## Extend_AABB2     Extend_AABB2               0.76                  0.04
## Extend_AABB       Extend_AABB               0.81                  0.04
##                Item Location Item SE Outfit MSE Std. Outfit Infit MSE
## Abstract_ABCC          -0.03    0.23       0.87       -1.65      0.88
## IDPattern_ABCD         -0.13    0.23       1.52        5.48      1.41
## Extend_ABCD            -0.18    0.23       1.16        1.80      1.11
## Abstract_AAB           -0.45    0.23       0.83       -2.00      0.88
## IDPattern_ABC          -0.50    0.23       1.11        1.19      1.08
## Abstract_ABB           -0.50    0.23       0.84       -1.93      0.88
## Abstract.ABCD          -0.78    0.24       1.23        2.15      1.16
## Extend_ABC             -0.96    0.25       1.19        1.66      1.14
## Extend_AABC            -0.96    0.25       0.77       -2.30      0.86
## IDPattern_ABC2         -1.02    0.25       1.20        1.66      1.08
## Abstract_AABC          -1.08    0.25       1.06        0.52      1.09
## Abstract_ABB2          -1.08    0.25       0.85       -1.39      0.91
## IDPattern_AABB         -1.14    0.25       0.89       -0.95      0.95
## Extend_AB              -1.14    0.25       0.87       -1.17      0.91
## Abstract_AB            -1.14    0.25       0.90       -0.91      0.95
## Extend_AAB             -1.27    0.26       0.89       -0.89      0.87
## IDPattern_ABB          -1.34    0.26       0.92       -0.58      0.98
## Extend_AABB2           -1.34    0.26       0.83       -1.33      0.91
## Extend_AABB            -1.71    0.29       0.81       -1.23      0.93
##                Std. Infit Item-Trait Correlation Item-Total Correlation
## Abstract_ABCC       -1.46                   0.60                   0.50
## IDPattern_ABCD       4.43                  -0.03                  -0.17
## Extend_ABCD          1.25                   0.34                   0.20
## Abstract_AAB        -1.40                   0.60                   0.51
## IDPattern_ABC        0.90                   0.35                   0.22
## Abstract_ABB        -1.44                   0.61                   0.52
## Abstract.ABCD        1.58                   0.25                   0.12
## Extend_ABC           1.26                   0.26                   0.14
## Extend_AABC         -1.33                   0.60                   0.53
## IDPattern_ABC2       0.69                   0.30                   0.20
## Abstract_AABC        0.76                   0.34                   0.21
## Abstract_ABB2       -0.79                   0.53                   0.45
## IDPattern_AABB      -0.45                   0.48                   0.40
## Extend_AB           -0.73                   0.51                   0.44
## Abstract_AB         -0.45                   0.49                   0.40
## Extend_AAB          -1.07                   0.53                   0.49
## IDPattern_ABB       -0.16                   0.43                   0.34
## Extend_AABB2        -0.64                   0.51                   0.44
## Extend_AABB         -0.38                   0.46                   0.40
# Calculate proportion correct for persons:
PersonPropCorrect <- achievement$PersonScores / achievement$PersonMax

# Combine person calibration results in a table:
Table3 <- cbind.data.frame(achievement$pid,
                           PersonPropCorrect,
                           achievement$theta,
                           achievement$error,
                           person.fit$outfitPerson,
                           person.fit$outfitPerson_t,
                           person.fit$infitPerson,
                           person.fit$infitPerson_t)

names(Table3) <- c("Person ID", "Proportion Correct", "Person Location","Person SE","Outfit MSE","Std. Outfit", "Infit MSE","Std. Infit")

# Round the numeric values (all columns except the first one) to 2 digits:
Table3[, -1] <- round(Table3[,-1], digits = 2)

# Print Table 1:
knitr::kable(
  Table1, booktabs = TRUE,
  caption = 'Model Summary Table'
)
Model Summary Table
Statistic Items Persons
Logit Scale Location Mean -0.88 -0.02
Logit Scale Location SD 0.46 1.02
Standard Error Mean 0.25 0.57
Standard Error SD 0.01 0.12
Outfit MSE Mean 0.99 0.91
Outfit MSE SD 0.20 0.20
Infit MSE Mean 1.00 0.94
Infit MSE SD 0.14 0.13
Std. Outfit Mean -0.10 -0.05
Std. Outfit SD 1.97 0.63
Std. Infit Mean 0.03 0.00
Std. Infit SD 1.46 0.57
Reliability of Separation 0.74 0.67
# Print Table 2:
knitr::kable(
  Table2, booktabs = TRUE,
  caption = 'Item Calibration'
)
Item Calibration
Task ID Proportion Correct Proportion Correct SD Item Location Item SE Outfit MSE Std. Outfit Infit MSE Std. Infit Item-Trait Correlation Item-Total Correlation
Abstract_ABCC Abstract_ABCC 0.51 0.05 -0.03 0.23 0.87 -1.65 0.88 -1.46 0.60 0.50
IDPattern_ABCD IDPattern_ABCD 0.53 0.05 -0.13 0.23 1.52 5.48 1.41 4.43 -0.03 -0.17
Extend_ABCD Extend_ABCD 0.54 0.05 -0.18 0.23 1.16 1.80 1.11 1.25 0.34 0.20
Abstract_AAB Abstract_AAB 0.59 0.05 -0.45 0.23 0.83 -2.00 0.88 -1.40 0.60 0.51
IDPattern_ABC IDPattern_ABC 0.60 0.05 -0.50 0.23 1.11 1.19 1.08 0.90 0.35 0.22
Abstract_ABB Abstract_ABB 0.60 0.05 -0.50 0.23 0.84 -1.93 0.88 -1.44 0.61 0.52
Abstract.ABCD Abstract.ABCD 0.66 0.05 -0.78 0.24 1.23 2.15 1.16 1.58 0.25 0.12
Extend_ABC Extend_ABC 0.69 0.05 -0.96 0.25 1.19 1.66 1.14 1.26 0.26 0.14
Extend_AABC Extend_AABC 0.69 0.05 -0.96 0.25 0.77 -2.30 0.86 -1.33 0.60 0.53
IDPattern_ABC2 IDPattern_ABC2 0.70 0.05 -1.02 0.25 1.20 1.66 1.08 0.69 0.30 0.20
Abstract_AABC Abstract_AABC 0.71 0.05 -1.08 0.25 1.06 0.52 1.09 0.76 0.34 0.21
Abstract_ABB2 Abstract_ABB2 0.71 0.05 -1.08 0.25 0.85 -1.39 0.91 -0.79 0.53 0.45
IDPattern_AABB IDPattern_AABB 0.73 0.05 -1.14 0.25 0.89 -0.95 0.95 -0.45 0.48 0.40
Extend_AB Extend_AB 0.73 0.05 -1.14 0.25 0.87 -1.17 0.91 -0.73 0.51 0.44
Abstract_AB Abstract_AB 0.73 0.05 -1.14 0.25 0.90 -0.91 0.95 -0.45 0.49 0.40
Extend_AAB Extend_AAB 0.75 0.05 -1.27 0.26 0.89 -0.89 0.87 -1.07 0.53 0.49
IDPattern_ABB IDPattern_ABB 0.76 0.04 -1.34 0.26 0.92 -0.58 0.98 -0.16 0.43 0.34
Extend_AABB2 Extend_AABB2 0.76 0.04 -1.34 0.26 0.83 -1.33 0.91 -0.64 0.51 0.44
Extend_AABB Extend_AABB 0.81 0.04 -1.71 0.29 0.81 -1.23 0.93 -0.38 0.46 0.40
# Print Table 3 in a neat way:
knitr::kable(
  head(Table3,10), booktabs = TRUE,
  caption = 'Person Calibration'
)
Person Calibration
Person ID Proportion Correct Person Location Person SE Outfit MSE Std. Outfit Infit MSE Std. Infit
1 0.95 1.73 0.88 0.57 -0.35 0.72 -0.18
2 0.53 -0.78 0.47 0.99 -0.05 0.99 -0.02
3 0.37 -1.42 0.48 1.00 0.07 1.00 0.07
4 0.37 -1.42 0.48 0.98 -0.05 0.98 -0.09
5 0.47 -0.99 0.47 1.01 0.14 1.02 0.21
6 0.79 0.41 0.56 0.97 0.03 0.99 0.07
7 0.63 -0.35 0.49 1.10 0.65 1.11 0.78
8 0.79 0.41 0.56 0.92 -0.13 0.94 -0.11
9 0.53 -0.78 0.47 0.92 -0.76 0.92 -0.74
10 0.58 -0.57 0.48 0.96 -0.23 0.97 -0.22
# Plot the variable-Map
IRT.WrightMap(dichot.data,show.thr.lab=FALSE)