---
title: "Information Matrix Test"
author: "Daphna Harel"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Information Matrix Test}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
#' @importFrom lme4 VerbAgg
#' @importFrom reshape2 dcast
---

This vignette reproduces the example shows the use of the information matrix test when collapsing categories of a Partial Credit Model.

```{r}
library(IMTest)
library(lme4)
library(reshape2)
```

A commonly analyzed dataset in the illustration of IRT methods consists of 316 responses to 24 items referring to verbally aggressive reactions to situations that may be frustrating. Responses to the items are collected on a three point ordinal scale, with categories labelled as yes, perhaps, or no. The dataset is publicly available, for example through the lme4 package in R. This vignette analyzes item responses to the first 12 items, and investigates the potential model misspecification when collapsing categories of those items.

```{r}

data(VerbAgg)

x = dcast(VerbAgg[,c( "id","item", "resp")], id~item)

for(i in 2:25){
  x[,i] = factor(x[,i], levels = c("no", "perhaps", "yes"))
}

my_data = x[,2:13]

J = dim(my_data)[[2]]
n = dim(my_data)[[1]]
```

First, a set of index vectors is created to test both item-level parameters of each item separately. 
```{r}

# Create index vectors
ind = list()
for(i in 1:J){
  ind[[i]] = rep(0, J*2)
  ind[[i]][(2*i-1):(2*i)] = 1
}
```

A collapsing function is generated, showing that no collapsing has occurred, and the gpcm_IMT function is run. A matrix is initialized to store the p-values.

```{r}
col = split(rep(c(1:3), J), rep(1:J, each = 3))
temp_data = collapse_data(my_data, col, "rasch")
mod = gpcm_IMT(temp_data$data, constraint = "rasch")

## Creates a matrix to store results.
pval = matrix(c(0), nrow = J, ncol = 3)

```

The following set of code runs the IMT for the uncollapsed data, and for two forms of collapsing: collapsing the middle category up, and collapsing the middle category down. 
```{r, results = "hide"}
# No Collapsing
for(i in 1:J){
  test_fit = IMT(mod, "rasch", R = 5000, ind[[i]])
  pval[i,1] = pchisq(test_fit$Tstat, test_fit$df, lower.tail = F)
}

# Collapsing the middle categoy up.
for(i in 1:J){
  col = split(rep(c(1:3), J), rep(1:J, each = 3))
  col[[i]] = c(1,2,2)
  temp_data = collapse_data(my_data, col, "rasch")
  mod = gpcm_IMT(temp_data$data, constraint = "rasch")
  test_fit = IMT(mod, "rasch", R = 5000, temp_data$ind)
  pval[i,2] = pchisq(test_fit$Tstat, test_fit$df, lower.tail = F)
}

# Collapsing the middle category down.
for(i in 1:J){
  col = split(rep(c(1:3), J), rep(1:J, each = 3))
  col[[i]] = c(1,1,2)
  temp_data = collapse_data(my_data, col, "rasch")
  mod = gpcm_IMT(temp_data$data, constraint = "rasch")
  test_fit = IMT(mod, "rasch", R = 5000, temp_data$ind)
  pval[i,3] = pchisq(test_fit$Tstat, test_fit$df, lower.tail = F)
}
```

Originally, all but one item fails to reject goodness-of-fit. After collapsing in one direction, four items now reject goodness-of-fit. After collapsing in the other direction, no items reject goodness-of-fit.
```{r}
# Original fit: all but question 8 fail to reject goodness-of-fit.
p.adjust(pval[,1], method = "fdr")

# After collapsing: question 8 now fails to reject goodness-of-fit, but 4 other questions reject.
p.adjust(pval[,2], method = "fdr")

# Had it been collapsed the other direction, all would fail to reject.
p.adjust(pval[,3], method = "fdr")

```
