R/lanchor.R

Defines functions lanchor

#' lanchor
#'
#' Performs longitudinal anchoring
#'
#'
#' @param data
#'
#' @param timepoints the number of timepoints
#'
#' @param method the anchoring method
#'
#'
#'
#' @author Tara Valladares <tls8vx at virginia.edu>
#'
#' @examples
#'
#' \donttest{
#' #examples here
#' }
#'
#' @export
#'
#'
lanchor <- function(data, timepoints, model, method) {
  available_methods <-  c("random", "mean", "racked", "stacked", "single", "floated")
  if(!(method %in% available_methods)){
    warning("'method' is not an available method for longitudinal anchoring. Check ?lanchor for a list of available methods.")
  }

  #WHAT KIND OF DATA IS ACCEPTED? LONG OR WIDE?

  nItems <- 1 #NEED TO CHANGE

  # Set the mean of the item locations to be zero
  eqfun <- function(p, optim_args) sum(p[1:(timepoints*nItems)]) #could use browser() here, if it helps
  solnp_args <- list(eqfun=eqfun, eqB=0, LB = c(rep(-15, timepoints*nItems +1), 1e-4))


  if(method == "random"){

  }

  if(method == "mean"){

  }

  if(method == "racked"){
      wideFrame <-
        data.frame(matrix(NA, ncol = (nItems * nWaves), nrow = nParticipants))
      for (k in 1:nWaves) {
        wideFrame[, ((k - 1) * nItems + 1):(k * nItems)] <-
          parList [[i]][[k]]
      }
      colnames(wideFrame) <- itemNames


      # Set up model
      awModel <- mirt.model(paste0("F1 = 1-",nItems*nWaves,"
                                   \n"," MEAN = F1",
                                   "\n", "COV = F1*F1"))

      # Run the All Year Model, extract estimated values

      vals <- tryCatch({
        mod2values(mirt(wideFrame,
                        awModel, 'Rasch',  optimizer = 'solnp', solnp_args=solnp_args))
      },
      error = function(e)
        e)

      if (inherits(vals, "error")) {
        errorList <- rbind(errorList, c("AllWave", i, "All Wave Model", paste(vals)))
      }

      if (inherits(vals, "error"))
        next

      # Create empty one wave df
      awModelInv <- mirt.model(paste0("F1 = 1-",nItems,"
                                      \n"," MEAN = F1",
                                      "\n", "COV = F1*F1"))


      yearvals <- mirt(parList[[i]][[1]], awModelInv, 'Rasch', pars = "values")
      yearvals[, "est"] <- FALSE
      yearvals[c(nrow(yearvals) - 1, nrow(yearvals)), "est"]  <- TRUE


      for (j in 1:nWaves) {
        # extract the appropriate estimated values + estimated mean/cov
        yearvals$value <-
          vals[c(((4 * (j - 1)) * nItems + 1):(4 * j * nItems), (nrow(vals) - 1):nrow(vals)), "value"]

        simList$AllWave[[i]][[j]] <- tryCatch({
          mirt(parList[[i]][[j]],
               awModelInv, 'Rasch', pars = yearvals)
        },
        error = function(e)
          e)

        if (inherits(simList$AllWave[[i]][[j]], "error")) {
          errorList <- rbind(errorList, c("AllWave", i, j, paste(simList$AllWave[[i]][[j]])))
        }
        if (inherits(simList$AllWave[[i]][[j]], "error"))
          next
      }
  }

  if(method == "stacked"){

  }

  if(method == "single"){

  }

  if(method == "floated"){

  }


}
tvall/lanchor documentation built on Nov. 5, 2019, 11:01 a.m.