R/itemDistribution.r

Defines functions itemDist

Documented in itemDist

#' Item difficulty distribution
#'
#' This function requires a fitted mirt-model of class `SingleGroupClass` to visualize item difficulty distribution. Currently only works for unidimensional models.
#' 
#' @param model an object of class `SingleGroupClass` returned by the function `mirt()`. 
#' @param ... any argument passed to `geom_point()`
#'
#' @return a ggplot object.
#' @import ggplot2
#' @import dplyr
#' @import tidyr
#' @import mirt
#' @export
#'
#' @examples
#' # Loading packages
#' library(mirt)
#' library(ggmirt)
#' 
#' # Getting data
#' data <- expand.table(LSAT7)
#' 
#' # Fitting a model 
#' (mod <- mirt(data, 1))
#' 
#' # Simple plot
#' itemDist(mod)
#' 
#' # Customized plot
#' itemDist(mod, size = 3, shape = 17, color = "blue")
#' 
itemDist <- function(model,
                     theta_range = c(-4, 4),
                     ...) {
  
  item.params <- mirt::coef(model, IRTpars = TRUE, simplify = TRUE) %>%
    as.data.frame %>%
    tibble::rownames_to_column("items") 
  
  p <- item.params %>%
    mutate(items = forcats::fct_reorder(items, items.b)) %>%
    ggplot(aes(y = items, x = items.b)) + 
    geom_point(...)
  
  p + xlim(theta_range) + theme_minimal() + labs(x = expression(theta), y = "")
}
masurp/ggmirt documentation built on Oct. 14, 2023, 1:16 p.m.