R/conditionalTotalScoreProbs.R

Defines functions conditionalTotalScoreProbs

# Thissen et. al. 1995
# compute P(totalscore|theta=thetas) for each theta in thetas
# cats is vector with item max scores
# probs is list. Each element is each items P(x|theta) prob matrices
# where (rows are thetas, cols are response categories)
# Resulting matrix: rows are scores, cols are thetas
conditionalTotalScoreProbs <- function(probs, thetas) {
    # get the number of response categories for each item
    noItems <- length(probs)
    cats <- vector(mode = "numeric", length = length(probs))
    for (item in 1:noItems) {
        cats[item] <- nrow(probs[[item]])
    }

    max <- sum(cats) - noItems
    # Columns are theta values, rows are total scores
    nsprobs <- matrix(0, nrow = max + 1, ncol = ncol(probs[[1]]))
    nsprobs[1:(cats[1]), ] <- probs[[1]]
    if (noItems > 1) {
        for (i in 2:noItems) {
            maxsc <- sum(cats[1:i]) - length(cats[1:i])
            sprobs <- nsprobs
            nsprobs <- matrix(0, nrow = max + 1, ncol = length(thetas))
            for (j in 1:(maxsc - cats[i] + 2))
                nsprobs[j:(cats[i] + j - 1), ] <- t(sprobs[j, ] * t(probs[[i]])) +
                nsprobs[j:(cats[i] + j - 1), ]
        }
    }
    nsprobs
}
joakimwallmark/PolyOptimalIRT documentation built on Dec. 21, 2021, 1:16 a.m.