Recoding Polytomous Items with Missing Categories

[This article was first published on R Snippets for IRT, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This function helps prepare data for analysis with models that require polytomous items to be coded from 0 to N without missing categories, such as the Partial Credit Model (Masters, 1982).

When there are no missing categories, an item that was supposed to be scored from 1 to 5 can be readily converted to 0 to N by subtracting 1 from the vector of responses. However, a problem arises when the respondents have selected only some of its categories. So, instead of having a responses at 1, 2, 3, 4, and 5, respondents only selected 1, 3, and 4. In this case, the response vector needs to be recoded to become 0, 1, and 2.

The function

This function automatically recodes a response vector and can be applied to an entire response data frame using apply to make this conversion quickly (see the example).

The function takes only one parameter, the response vector that needs to be recoded.

The code:

recPoly <- function ( responseVector ){

    flevels <- sort( unique( as.vector( responseVector ) ) )
    flevels <- flevels[ ! flevels ) ]

    ncat <- length(flevels)

    temp01 <- factor( responseVector, levels = flevels, labels = c( seq( 1:ncat ) ) )
    output <- as.numeric( as.character( temp01 ) ) - 1

    return( output )



Just to illustrate that the functions returns the recoded values for the response vector:

 sample1 <- c(1,2,3,4,5,6)


 sample2 <- c(3,2,1,6,5,4)


And to show how it can be applied to an entire matrix:

 sample3 <- matrix( c( 5,3,4,1,2,3,4,4,1,5,1,5,1,2,3,4), ncol = 4)

 apply(sample3, 2, recPoly)

To leave a comment for the author, please follow the link and comment on their blog: R Snippets for IRT. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)