## Call libraries irtoys and ltm
library(irtoys)
## Loading required package: sm
## Package 'sm', version 2.2-6.0: type help(sm) for summary information
## Loading required package: ltm
## Loading required package: MASS
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:sm':
## 
##     muscle
## Loading required package: msm
## Loading required package: polycor
library(ltm)

# 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)
binary <- read.csv("PatternAssess_assessment_dropnonpattern.csv")
binary <- binary[, c(18:48)] #remove X column
Rasch <- est(binary,model="1PL",engine="ltm",rasch=TRUE) 
Rasch #first column is item discrimination second column is item difficulty third column is guessing; only one column for SE which is SE of difficulty
## $est
##                [,1]         [,2] [,3]
## IDPattern_ABB     1 -1.278972963    0
## Extend_AB         1 -0.627713326    0
## Extend_ABCD       1 -0.887825080    0
## Abstract_AAB      1 -0.529338604    0
## Abstract_ABCD     1 -0.492205800    0
## Missing_AB        1 -2.509013237    0
## Missing_ABB       1 -1.441333648    0
## Missing_ABC       1 -1.763423159    0
## Extend_ABC        1 -0.657925944    0
## Extend_AAB        1 -1.061425680    0
## Abstract_AB       1 -0.848924125    0
## Abstract_AABC     1 -0.714545596    0
## IDPattern_ABC     1 -1.233093798    0
## IDPattern_AABB    1 -0.977579581    0
## Missing_ABCC      1 -1.768241083    0
## Extend_AABB       1 -1.170600266    0
## IDPattern_ABC2    1 -0.905543065    0
## IDPattern_ABCD    1 -0.748489804    0
## Extend_AABB2      1 -0.442352458    0
## Extend_AABC       1 -0.442447160    0
## Abstract_ABCD2    1  0.009565874    0
## Abstract_ABB      1 -0.050220522    0
## Abstract_ABB2     1 -0.142431910    0
## Extend_ABCC       1 -0.795718598    0
## Abstract_ABBB     1 -0.555690512    0
## Abstract_ABC      1 -0.674251454    0
## Missing_ABCD      1 -2.395689152    0
## IDUnit_AB         1  1.352468501    0
## IDUnit_AABB       1  1.513377572    0
## IDUnit_ABC        1  0.080430241    0
## IDUnit_ABCD       1  1.683121496    0
## 
## $se
##       [,1]      [,2] [,3]
##  [1,]    0 0.1716806    0
##  [2,]    0 0.1597510    0
##  [3,]    0 0.1635062    0
##  [4,]    0 0.1590167    0
##  [5,]    0 0.1586527    0
##  [6,]    0 0.2701275    0
##  [7,]    0 0.2059900    0
##  [8,]    0 0.2203134    0
##  [9,]    0 0.1747336    0
## [10,]    0 0.1813494    0
## [11,]    0 0.1781259    0
## [12,]    0 0.1762225    0
## [13,]    0 0.2207886    0
## [14,]    0 0.1943535    0
## [15,]    0 0.2498994    0
## [16,]    0 0.2443258    0
## [17,]    0 0.2966271    0
## [18,]    0 0.2929394    0
## [19,]    0 0.2888558    0
## [20,]    0 0.2888565    0
## [21,]    0 0.2899881    0
## [22,]    0 0.2910655    0
## [23,]    0 0.2886713    0
## [24,]    0 0.3698660    0
## [25,]    0 0.3576655    0
## [26,]    0 0.3632885    0
## [27,]    0 0.5562797    0
## [28,]    0 0.4136878    0
## [29,]    0 0.4235953    0
## [30,]    0 0.3875701    0
## [31,]    0 0.4358878    0
## [32,]    0        NA    0
## 
## $vcm
## $vcm$Dffclt.IDPattern_ABB
## [1] 0.02947422
## 
## $vcm$Dffclt.Extend_AB
## [1] 0.02552038
## 
## $vcm$Dffclt.Extend_ABCD
## [1] 0.02673427
## 
## $vcm$Dffclt.Abstract_AAB
## [1] 0.02528632
## 
## $vcm$Dffclt.Abstract_ABCD
## [1] 0.02517069
## 
## $vcm$Dffclt.Missing_AB
## [1] 0.07296887
## 
## $vcm$Dffclt.Missing_ABB
## [1] 0.04243189
## 
## $vcm$Dffclt.Missing_ABC
## [1] 0.048538
## 
## $vcm$Dffclt.Extend_ABC
## [1] 0.03053182
## 
## $vcm$Dffclt.Extend_AAB
## [1] 0.03288762
## 
## $vcm$Dffclt.Abstract_AB
## [1] 0.03172884
## 
## $vcm$Dffclt.Abstract_AABC
## [1] 0.03105438
## 
## $vcm$Dffclt.IDPattern_ABC
## [1] 0.04874758
## 
## $vcm$Dffclt.IDPattern_AABB
## [1] 0.0377733
## 
## $vcm$Dffclt.Missing_ABCC
## [1] 0.06244972
## 
## $vcm$Dffclt.Extend_AABB
## [1] 0.05969508
## 
## $vcm$Dffclt.IDPattern_ABC2
## [1] 0.08798765
## 
## $vcm$Dffclt.IDPattern_ABCD
## [1] 0.08581352
## 
## $vcm$Dffclt.Extend_AABB2
## [1] 0.08343768
## 
## $vcm$Dffclt.Extend_AABC
## [1] 0.08343805
## 
## $vcm$Dffclt.Abstract_ABCD2
## [1] 0.08409308
## 
## $vcm$Dffclt.Abstract_ABB
## [1] 0.08471913
## 
## $vcm$Dffclt.Abstract_ABB2
## [1] 0.08333113
## 
## $vcm$Dffclt.Extend_ABCC
## [1] 0.1368009
## 
## $vcm$Dffclt.Abstract_ABBB
## [1] 0.1279246
## 
## $vcm$Dffclt.Abstract_ABC
## [1] 0.1319785
## 
## $vcm$Dffclt.Missing_ABCD
## [1] 0.3094471
## 
## $vcm$Dffclt.IDUnit_AB
## [1] 0.1711376
## 
## $vcm$Dffclt.IDUnit_AABB
## [1] 0.179433
## 
## $vcm$Dffclt.IDUnit_ABC
## [1] 0.1502106
## 
## $vcm$Dffclt.IDUnit_ABCD
## [1] 0.1899982
## 
## $vcm$Dscrmn
## [1] NA
Rasch.th <- eap(resp=binary,ip=Rasch$est,qu=normal.qu()) #can change to 1PL instead of Rasch if don't want rasch model for whatever reason
Rasch.th # first column is estimated person score, second column is standard error, third column is number of items
##                est       sem  n
##   [1,] -0.35501861 0.5171037 14
##   [2,] -1.36236283 0.4989719 14
##   [3,] -1.11491384 0.4966435 14
##   [4,]  0.92811328 0.6367405 14
##   [5,]  1.36834053 0.6915928 14
##   [6,] -0.35501861 0.5171037 14
##   [7,] -0.07893497 0.5347886 14
##   [8,] -1.11491384 0.4966435 14
##   [9,]  0.55104737 0.5929662 14
##  [10,]  0.92811328 0.6367405 14
##  [11,]  0.21979685 0.5595470 14
##  [12,]  0.21979685 0.5595470 14
##  [13,]  0.92811328 0.6367405 14
##  [14,]  0.21979685 0.5595470 14
##  [15,]  1.36834053 0.6915928 14
##  [16,]  1.36834053 0.6915928 14
##  [17,]  0.55104737 0.5929662 14
##  [18,]  0.92811328 0.6367405 14
##  [19,]  1.36834053 0.6915928 14
##  [20,] -0.07893497 0.5347886 14
##  [21,] -0.61592116 0.5053657 14
##  [22,]  0.55104737 0.5929662 14
##  [23,]  1.36834053 0.6915928 14
##  [24,] -0.61592116 0.5053657 14
##  [25,] -0.35501861 0.5171037 14
##  [26,]  0.55104737 0.5929662 14
##  [27,] -1.11491384 0.4966435 14
##  [28,]  0.55104737 0.5929662 14
##  [29,] -0.35501861 0.5171037 14
##  [30,]  0.92811328 0.6367405 14
##  [31,]  1.36834053 0.6915928 14
##  [32,]  1.36834053 0.6915928 14
##  [33,]  1.36834053 0.6915928 14
##  [34,]  0.92811328 0.6367405 14
##  [35,] -0.07893497 0.5347886 14
##  [36,]  0.92811328 0.6367405 14
##  [37,]  0.21979685 0.5595470 14
##  [38,] -0.07893497 0.5347886 14
##  [39,] -1.11491384 0.4966435 14
##  [40,]  0.21979685 0.5595470 14
##  [41,] -0.07893497 0.5347886 14
##  [42,]  1.36834053 0.6915928 14
##  [43,] -0.07893497 0.5347886 14
##  [44,]  0.92811328 0.6367405 14
##  [45,]  0.55104737 0.5929662 14
##  [46,]  0.92811328 0.6367405 14
##  [47,]  0.55104737 0.5929662 14
##  [48,] -0.58307413 0.5019941 14
##  [49,] -0.05272375 0.5317449 14
##  [50,] -0.32552897 0.5138880 14
##  [51,] -1.07519869 0.4931508 14
##  [52,] -0.83131804 0.4952797 14
##  [53,] -1.31920156 0.4955385 14
##  [54,] -0.83131804 0.4952797 14
##  [55,]  0.24278711 0.5566815 14
##  [56,]  0.24278711 0.5566815 14
##  [57,] -0.58307413 0.5019941 14
##  [58,] -0.32552897 0.5138880 14
##  [59,] -1.82577469 0.5141388 14
##  [60,] -0.58307413 0.5019941 14
##  [61,] -1.07519869 0.4931508 14
##  [62,] -0.32552897 0.5138880 14
##  [63,] -1.19850688 0.5598235 10
##  [64,] -0.86283545 0.5266929 12
##  [65,]  1.38171377 0.6892173 14
##  [66,] -1.07519869 0.4931508 14
##  [67,] -1.07519869 0.4931508 14
##  [68,] -0.32552897 0.5138880 14
##  [69,]  0.24278711 0.5566815 14
##  [70,]  0.57085188 0.5902786 14
##  [71,] -0.58307413 0.5019941 14
##  [72,] -0.32552897 0.5138880 14
##  [73,]  0.57085188 0.5902786 14
##  [74,]  0.24278711 0.5566815 14
##  [75,]  0.94472737 0.6342160 14
##  [76,] -0.05272375 0.5317449 14
##  [77,]  0.94472737 0.6342160 14
##  [78,] -0.05272375 0.5317449 14
##  [79,] -1.56782518 0.5024547 14
##  [80,]  0.24278711 0.5566815 14
##  [81,] -0.32552897 0.5138880 14
##  [82,] -0.83131804 0.4952797 14
##  [83,]  0.57085188 0.5902786 14
##  [84,] -0.83131804 0.4952797 14
##  [85,]  1.38171377 0.6892173 14
##  [86,] -0.83131804 0.4952797 14
##  [87,]  1.38171377 0.6892173 14
##  [88,]  1.38171377 0.6892173 14
##  [89,]  0.24278711 0.5566815 14
##  [90,] -1.07519869 0.4931508 14
##  [91,] -1.56782518 0.5024547 14
##  [92,]  0.94472737 0.6342160 14
##  [93,] -1.07519869 0.4931508 14
##  [94,] -0.05272375 0.5317449 14
##  [95,]  0.57085188 0.5902786 14
##  [96,] -1.31920156 0.4955385 14
##  [97,] -0.64692648 0.5119078 13
##  [98,]  1.38171377 0.6892173 14
##  [99,]  1.38171377 0.6892173 14
## [100,]  0.57085188 0.5902786 14
## [101,] -0.32552897 0.5138880 14
## [102,]  1.38171377 0.6892173 14
## [103,] -1.31920156 0.4955385 14
## [104,]  0.57085188 0.5902786 14
## [105,] -0.83131804 0.4952797 14
## [106,]  1.38171377 0.6892173 14
## [107,]  0.94472737 0.6342160 14
## [108,]  0.24278711 0.5566815 14
## [109,] -0.05272375 0.5317449 14
## [110,] -0.32552897 0.5138880 14
## [111,] -0.58307413 0.5019941 14
## [112,] -0.32552897 0.5138880 14
## [113,] -0.05272375 0.5317449 14
## [114,] -0.32552897 0.5138880 14
## [115,] -1.56782518 0.5024547 14
## [116,] -0.58307413 0.5019941 14
## [117,] -0.05272375 0.5317449 14
## [118,] -0.32552897 0.5138880 14
## [119,]  0.94472737 0.6342160 14
## [120,] -0.83131804 0.4952797 14
## [121,] -0.32552897 0.5138880 14
## [122,] -1.21591141 0.5090121 13
## [123,]  1.38171377 0.6892173 14
## [124,] -0.05272375 0.5317449 14
## [125,] -0.05272375 0.5317449 14
## [126,] -0.83131804 0.4952797 14
## [127,]  0.57085188 0.5902786 14
## [128,] -0.83131804 0.4952797 14
## [129,] -0.32552897 0.5138880 14
## [130,] -1.07519869 0.4931508 14
## [131,] -0.58307413 0.5019941 14
## [132,] -2.09844386 0.5310438 14
## [133,] -0.58307413 0.5019941 14
## [134,]  0.94472737 0.6342160 14
## [135,]  0.57085188 0.5902786 14
## [136,] -1.07519869 0.4931508 14
## [137,] -0.32552897 0.5138880 14
## [138,] -0.32552897 0.5138880 14
## [139,]  1.38171377 0.6892173 14
## [140,] -0.05272375 0.5317449 14
## [141,]  0.13705700 0.5659053 13
## [142,] -0.58307413 0.5019941 14
## [143,]  0.57085188 0.5902786 14
## [144,]  0.32277830 0.4698341 19
## [145,] -0.09241602 0.4441181 19
## [146,]  0.51602017 0.4934843 18
## [147,]  0.06408174 0.4602266 18
## [148,]  0.32277830 0.4698341 19
## [149,] -1.22234616 0.4402408 19
## [150,] -0.65957242 0.4297028 19
## [151,] -0.09241602 0.4441181 19
## [152,] -0.84455336 0.4308817 19
## [153,] -1.22234616 0.4402408 19
## [154,]  0.80384916 0.5144593 19
## [155,]  0.55242868 0.4893445 19
## [156,] -0.28615365 0.4366046 19
## [157,] -0.65957242 0.4297028 19
## [158,] -0.47438378 0.4315517 19
## [159,] -1.85089020 0.4806093 19
## [160,]  0.32277830 0.4698341 19
## [161,]  0.10933571 0.4548527 19
## [162,] -0.65957242 0.4297028 19
## [163,] -1.03146652 0.4341398 19
## [164,]  1.08479330 0.5467884 19
## [165,] -1.03146652 0.4341398 19
## [166,] -0.84455336 0.4308817 19
## [167,] -0.28615365 0.4366046 19
## [168,]  1.78106928 0.6382052 19
## [169,]  0.55242868 0.4893445 19
## [170,]  0.55242868 0.4893445 19
## [171,]  1.30944709 0.5976515 17
## [172,]  0.80384916 0.5144593 19
## [173,]  0.32277830 0.4698341 19
## [174,] -1.62851741 0.4632690 19
## [175,] -1.42023604 0.4500365 19
## [176,]  0.32277830 0.4698341 19
## [177,] -0.47438378 0.4315517 19
## [178,] -1.42023604 0.4500365 19
## [179,] -1.22234616 0.4402408 19
## [180,] -0.65957242 0.4297028 19
## [181,] -0.84455336 0.4308817 19
## [182,]  0.80384916 0.5144593 19
## [183,] -0.47438378 0.4315517 19
## [184,] -0.65957242 0.4297028 19
## [185,] -1.03146652 0.4341398 19
## [186,] -0.09241602 0.4441181 19
## [187,] -0.47438378 0.4315517 19
## [188,]  0.55242868 0.4893445 19
## [189,]  1.78106928 0.6382052 19
## [190,]  1.40589135 0.5878521 19
## [191,]  1.78106928 0.6382052 19
## [192,] -1.03146652 0.4341398 19
## [193,] -0.47438378 0.4315517 19
## [194,] -0.84455336 0.4308817 19
## [195,] -0.65957242 0.4297028 19
## [196,]  0.10933571 0.4548527 19
## [197,] -0.28615365 0.4366046 19
## [198,] -1.22234616 0.4402408 19
## [199,] -1.22234616 0.4402408 19
## [200,]  0.32277830 0.4698341 19
## [201,]  0.80384916 0.5144593 19
## [202,] -0.28615365 0.4366046 19
## [203,] -0.65957242 0.4297028 19
## [204,] -0.09241602 0.4441181 19
## [205,]  0.32277830 0.4698341 19
## [206,] -1.22234616 0.4402408 19
## [207,] -0.47438378 0.4315517 19
## [208,]  0.16733656 0.4970377 18
## [209,] -1.42169435 0.4792776 18
## [210,]  0.16733656 0.4970377 18
## [211,]  0.42122441 0.5111999 18
## [212,] -0.53137768 0.4722454 18
## [213,] -0.53137768 0.4722454 18
## [214,] -0.75295012 0.4696632 18
## [215,]  0.69126733 0.5286431 18
## [216,]  0.98173089 0.5498651 18
## [217,]  0.16733656 0.4970377 18
## [218,] -0.97339796 0.4698187 18
## [219,]  0.93623551 0.5566970 17
## [220,]  0.42122441 0.5111999 18
## [221,] -1.19531502 0.4728726 18
## [222,]  0.42122441 0.5111999 18
## [223,] -0.97339796 0.4698187 18
## [224,] -0.30598116 0.4777437 18
## [225,]  1.64702147 0.6067973 18
## [226,] -0.07400377 0.4859717 18
## [227,] -0.53137768 0.4722454 18
## [228,]  1.29799134 0.5756077 18
## [229,]  1.29799134 0.5756077 18
## [230,]  0.16733656 0.4970377 18
## [231,]  1.64702147 0.6067973 18
## [232,]  0.98173089 0.5498651 18
## [233,]  2.03719693 0.6427193 18
## [234,]  0.63202523 0.5366309 17
## [235,]  0.98173089 0.5498651 18
## [236,]  0.42122441 0.5111999 18
## [237,]  0.69126733 0.5286431 18
## [238,]  0.98173089 0.5498651 18
## [239,]  1.64702147 0.6067973 18
## [240,]  0.69126733 0.5286431 18
## [241,] -0.97339796 0.4698187 18
## [242,]  1.29799134 0.5756077 18
## [243,]  0.16733656 0.4970377 18
write.table(Rasch.th,file="PersonScore_SE.txt",sep="\t")
write.table(Rasch$est,file="Itemdifficulty.txt",sep="\t")
write.table(Rasch$se,file="SE.txt",sep="\t")


# Ensure all item columns are numeric
binary[ , -ncol(binary)] <- lapply(binary[ , -ncol(binary)], as.numeric)

# Calculate total score (sum of each row across all items)
binary$total_score <- rowSums(binary[ , -ncol(binary)], na.rm = TRUE)

# Calculate item-total correlation for each item
item_total_corr <- sapply(binary[ , -ncol(binary)], function(item) {
  cor(as.numeric(item), binary$total_score, use = "complete.obs")
})

item_total_corr
##  IDPattern_ABB      Extend_AB    Extend_ABCD   Abstract_AAB  Abstract_ABCD 
##      0.4120961      0.5358291      0.4036957      0.6260556      0.2362513 
##     Missing_AB    Missing_ABB    Missing_ABC     Extend_ABC     Extend_AAB 
##      0.4050505      0.4663565      0.4677777      0.3078924      0.5417050 
##    Abstract_AB  Abstract_AABC  IDPattern_ABC IDPattern_AABB   Missing_ABCC 
##      0.5300589      0.4799122      0.3990346      0.4370772      0.4073031 
##    Extend_AABB IDPattern_ABC2 IDPattern_ABCD   Extend_AABB2    Extend_AABC 
##      0.5075864      0.2460544      0.2755847      0.3988827      0.5011225 
## Abstract_ABCD2   Abstract_ABB  Abstract_ABB2    Extend_ABCC  Abstract_ABBB 
##      0.5684632      0.2126765      0.6431051      0.5968118      0.6677662 
##   Abstract_ABC   Missing_ABCD      IDUnit_AB    IDUnit_AABB     IDUnit_ABC 
##      0.2910908      0.3780582      0.2370780      0.5397728      0.6206625 
##    IDUnit_ABCD 
##      0.5004790
write.table(item_total_corr,file="item_total_corr.txt",sep="\t")

proportion_correct <- colMeans(binary, na.rm = TRUE)

# Calculate the standard deviation of proportion for each item
item_sd <- apply(binary, 2, sd, na.rm = TRUE)

# Combine results into a data frame for easy viewing
proportions_SD <- data.frame(
  Item = names(binary),
  Proportion_Correct = proportion_correct,
  SD = item_sd
)
write.table(proportions_SD,file="proportions_SD.txt",sep="\t")