#################################################################################### # L6 - IRT MODELS FOR BINARY DATA # # NMST570 # # Last update: 16/11/2020 # #################################################################################### # Slides available at http://www.cs.cas.cz/martinkova/NMST570.html # HW assignment available at http://www.cs.cas.cz/hladka/documents/NMST570_HW6.pdf #################################################################################### # Loading data ##################################################################### #################################################################################### data <- read.csv("epi_escore.csv") summary(data) #################################################################################### # Loading packages ################################################################# #################################################################################### library(mirt) #################################################################################### # Ex. 3.2. ######################################################################### #################################################################################### #----------------------------------------------------------------------------------- # 1. reversing some items ### this is function to reverse some items of dataset reverse.items <- function(data, which.items){ rev.data <- sapply(data, function(x) as.numeric(x == 0)) new.data <- data new.data[, which.items] <- rev.data[, which.items] return(new.data) } ### specify which items do you want to reverse (their colnames) ### replace ??? colnames(data) which.items <- c(???) ### now create new data with reverse.items() function ### replace ??? revdata <- reverse.items(data = ???, which.items = ???) #----------------------------------------------------------------------------------- # 2. fitting 2PL model with the mirt package on data with the reversed items ?mirt ### In our case model = 1 means that we assumed that latent trait is unidimensional ### itemtype = "2PL" means that we assumed 2PL model for all items ### model for original data, replace ??? fit2PL.mirt <- mirt(data = ???, model = ???, itemtype = ???) ### model for reversed items, replace ??? fit2PL.mirt.rev <- mirt(data = ???, model = ???, itemtype = ???) #----------------------------------------------------------------------------------- # 3. print estimated parameters ### use coef() function on model ### use simplify = TRUE argument #----------------------------------------------------------------------------------- # 4. extract factor scores ### open help for fscores() function ?fscores ### use fscores() to extract factor scores ###### 2PL IRT model on original data, replace ??? fs2PL <- as.vector(fscores(???)) ###### 2PL IRT model on data with reversed items, replace ??? fs2PL.rev <- as.vector(fscores(???)) ### compare factor scores, you can for example print summary on both, plot them using ### function plot() or calculate their correlation with cor() function