R/IRTplot.R

Defines functions IRTplot

Documented in IRTplot

#' IRT graphing function
#'
#' Generates ggplot ICCs for 2-parameter models generated by the ltm package.
#'
#' @param ltm An object of class ltm
#'
#' @return NULL
#' @export
#'
#' @examples
#' \dontrun{m1 <- ltm::ltm(ltm::WIRS~z1)
#' IRTplot(m1)}
IRTplot <- function(ltm){
  if(class(ltm)!="ltm")
    stop("Object is not a ltm object")

  irt2 <-  function(x, diff, disc) {
    1 / (1 + exp(-disc*(x-diff)))
  }

  df <-  ltm %>%
    coef() %>%
    as.data.frame()

  df <-  df %>%
    dplyr::transmute(item = row.names(df),
                     diff = Dffclt,
                     disc = Dscrmn) %>%
    tidyr::crossing(x = seq(-5, 5, .1)) %>%
    dplyr::mutate(y = irt2(x, diff, disc))

  ggplot2::ggplot(df, ggplot2::aes(x=x, y=y, colour = item)) +
    ggplot2::geom_line(size=.7) +
    ggplot2::theme_classic() + ggplot2::xlab("Difficulty") + ggplot2::ylab("Probability of endorsement") +
    ggplot2::labs(colour= "Item")

}
Lingtax/Qualtrics documentation built on July 4, 2022, 3:24 a.m.