R/gdina_proc_item_response_patterns.R

Defines functions gdina_proc_item_response_patterns

## File Name: gdina_proc_item_response_patterns.R
## File Version: 0.29

gdina_proc_item_response_patterns <- function( dat.items, J, G, weights, group,
            freq_weights=TRUE)
{
    # string with item response patterns
    if (freq_weights){
        item.patt.subj <- dat.items[,1]
        for (jj in 2:J){
            item.patt.subj <- paste( item.patt.subj, dat.items[,jj], sep="")
        }
    } else {
        N <- nrow(dat.items)
        item.patt.subj <- paste0( "P", 1E6 + 1:N )
    }
    # calculate frequency of each item response pattern
    item.patt <- table( item.patt.subj )

    # sort item response pattern according to their absolute frequencies
    six <- sort( item.patt, index.return=FALSE, decreasing=TRUE)
    # define data frame 'item.patt' with item response pattern and its frequency (weight)
    item.patt <- cbind( "pattern"=rownames(six), "freq"=as.numeric(as.vector(six) ) )

    # calculate weighted frequency for each item response pattern
    if (G==1){
        h1 <- rowsum( weights, item.patt.subj )
        item.patt[,2] <- h1[ match( item.patt[,1], rownames(h1) ), 1]
        item.patt.freq <- as.numeric(item.patt[,2])
    }

    if (G > 1){
        item.patt.freq <- matrix( 0, nrow(item.patt), G )
        for (gg in 1:G){
            h1 <- rowsum( weights * (group==gg ), item.patt.subj )
            item.patt[,2] <- h1[ match( item.patt[,1], rownames(h1) ), 1]
            item.patt.freq[,gg] <- as.numeric(item.patt[,2])
        }
    }
    #---- OUTPUT
    res <- list(item.patt.subj=item.patt.subj, item.patt=item.patt, six=six,
                    item.patt.freq=item.patt.freq)
    return(res)
}

Try the CDM package in your browser

Any scripts or data that you put into this service are public.

CDM documentation built on Aug. 25, 2022, 5:08 p.m.