R/BRSM.DIF.transform.R

Defines functions BRSM.DIF.transform

Documented in BRSM.DIF.transform

#' BRSM.DIF.transform
#'
#' Transform BRSM parameter to allow andrich threshold to follow sum-to-zero constraint
#'
#' @param mcmc MCMC sample from a BRSM DIF model
#' @param item item included in the model
#' @param p number of item included
#' @param N number of subject in each group
#' @param K number of category
#' @param G number of group

BRSM.DIF.transform = function(mcmc, item, p, N, K, G) {
  mcmc.new = list()

  #Other group
  for(i in 1:G){
    group = matrix(NA, nrow(mcmc), p + N[i] + K)
    colnames(group) = c(paste0("beta[", 1:p, "]"), paste0("theta[", i,",", 1:N[i], "]"), paste0("thres[", 1:K, "]"))
    for(j in 1:p) {
      group[, paste0("beta[", j, "]")] = mcmc[, paste0("beta[", j, "]")] + mcmc[, paste0("d[", i, ",", j, "]")]
    }
    group[, paste0("theta[", i ,",", 1:N[i], "]")] = mcmc[, paste0("theta[", i ,",", 1:N[i], "]")]
    group[, paste0("thres[", 1:K, "]")] = mcmc[, paste0("thres[", 1:K, "]")]
    mcmc.new[[i]] = group
  }

  #transform beta and thresh in each group
  for(l in 1:G){
    mcmc.temp = mcmc.new[[l]]
    for(i in 1:nrow(mcmc.temp)){
      ata = matrix(NA, length(item), K)
      for(j in 1:length(item)){
        for(k in 2:K){
          ata[j,k] = mcmc.temp[i, paste0("beta[",j,"]")] + mcmc.temp[i,paste0("thres[",k,"]")]
        }
      }
      #transform beta
      for(j in 1:length(item)){
        mcmc.temp[i,paste0("beta[",j,"]")] = mean(ata[j,], na.rm = TRUE)
      }

      #transform thres
      for(k in 2:K){
        mcmc.temp[i,paste0("thres[",k,"]")]  = ata[1, k] - mcmc.temp[i,paste0("beta[",1,"]")]
      }
    }
    mcmc.new[[l]] = mcmc.temp
  }
  mcmc.new
}
changxiulee/BayesianRasch documentation built on Nov. 18, 2019, 6:54 a.m.