Tutz Coding: Coding a polytomous variable for a continuation ratio model
Analyzing polytomous variables in Item Response Modeling relies in the recoding of those variables into a set of dichotomous variables in order to run a multinomial logistic regression model. This dichotomization can be of four types: (1) baseline-category, (2) adjacent-category, (3) cumulative and, (4) continuation-ratio.
Producing the recoding for the first three kinds is fairly straightforward, but I had a hard time finding a function to create the last one; for that reason I worked on a function (with the help of Ronli Diakow) to make this conversion easier.
The function:
This function takes a dataframe in long format, the name of the column that contains the item names and finally, the name of the column that contains the responses.
From this information, the function will expand the data in order to make the polytomous variables into sets of dichotomous answers.
A couple of caveats about this function:
The function requires the reshape package.
Notice that the function requires three parameters:
The name of the data frame.
The name of the column that contains the item names.
The name of the column that contains the responses.
IMPORTANT: The name of the columns (second and third parameters) must be in quotes.
The code:
TutzCoding <- function( dataMat, itemColumn, respColumn){ require(reshape) cats <- length( unique( dataMat[,respColumn])) TutzMat <- matrix(rep(0, cats*cats), ncol = cats) diag(TutzMat) <- 1 TutzMat[lower.tri(TutzMat)] <- NA TutzMat <- as.data.frame(TutzMat) names(TutzMat) <- rev(paste(rep('C',cats),letters[1:cats],sep='')) TutzMat <- TutzMat[,-1] TutzVars <- names(TutzMat) TutzMat <- t(apply(TutzMat,1,rev)) print('Tutz Coding Matrix') print(TutzMat) TempMat <- with(dataMat,TutzMat[dataMat[,respColumn],]) TempMat2 <- cbind(dataMat,TempMat) Output <- melt(TempMat2, measure.vars = TutzVars) Output$newitem <- interaction(Output[,itemColumn], Output[,"variable"]) colnames(Output)[which(colnames(Output) == 'variable')] <- 'category' colnames(Output)[which(colnames(Output) == 'value')] <- 'tutz' return(Output) }
An Example:
This example uses the verbal aggression dataset available in the lme4 package. You can use this coding function to run polytomous models using the lmer function provided in this package. This first example creates dichotomous variables from a 3 category polytomous variable:
library(lme4) NewData <- TutzCoding(VerbAgg, "item", "resp") str(NewData) head(NewData)
And here is another example illustrating that it also works for 5 categories:
sample <- cbind(c('a','b','c','d','e'),c('i1','i2','i3','i4','i5'),c(1,2,3,4,5)) sample[,3] <- as.numeric(sample[,3]) sample <- as.data.frame(sample) sample[,3] <- as.numeric(as.character(sample[,3])) names(sample) <- c('idvar','item','respvar') FiveCat <- TutzCoding(sample, "item", "respvar") str(FiveCat) head(FiveCat)









