---
title: "One theta to rule them all: Test Individual Differences"
author: "Timo Bechger and Ivailo Partchev"
date: "`r format(Sys.time(), '%d %B, %Y')`"
bibliography: dexter.bib
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{One theta to rule them all: Test Individual Differences}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

<style>
img{ border:none;}
</style>

```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(echo = TRUE,dev='CairoPNG')

par_hook = function(before, options, envir)
{
  if(before)
  {
    do.call(par, options$par)
  }
}
knit_hooks$set(par = par_hook)

library(dexter)
library(dplyr)
```

Educational and psychological testing is all about individual differences. Using a measuring instrument, we try our best to place an individual with respect to others depending on their level of extroversion, depression, or mastery of English.

What if there are no individual differences at all? Classical test theory defines reliability as the ratio of the variance of the true scores to the variance of the observed scores [e.g., @BechgerMarisVerstralen2003]. The observed scores will have some chance variance, so reliability is 0 in that case. We have provided an IRT analogue with function `individual_differences`, to check whether the response data are consistent with the hypothesis of no individual differences in true ability.

First, a simple function to simulate a matrix of response data from the Rasch model, in the long shape expected by dexter. We use it to generate responses to 20 items with uniformly distributed difficulties from 2000 persons having all the same true ability of 0.5: 

```{r}
sim_Rasch = function(theta, delta) {
  n = length(theta)
  m = length(delta)
  data.frame(
    person_id = rep(paste0('p',1:n), m),
    item_id = rep(paste0('i',1:m), each=n),
    item_score = as.integer(rlogis(n*m, outer(theta, delta, "-")) > 0)
  )
}

simulated = sim_Rasch(rep(0.5, 2000), runif(20, -2, 2))
```

Computing the sum scores and examining their distribution, we find nothing conspicuous:

```{r, fig.align='center', fig.width=7,par=list(mfrow=c(1,2))}
ss= simulated %>% 
  group_by(person_id) %>% 
  summarise(sumscore=sum(item_score)) 

hist(ss$sumscore, main='', xlab='sumScore')
plot(ecdf(ss$sumscore), bty='l', main='ecdf', xlab='sumScore' )
mm = fit_inter(simulated)
```

We can also examine the various item-total regressions produced by function `fit_inter`. For example, here are the plots for the first two items:

```{r, fig.align='center', fig.width=7,par=list(mfrow=c(1,2))}
mm = fit_inter(simulated)

plot(mm, show.observed = TRUE, 
     items = c('i1','i2'))
```

The curtains that eliminate the 5% smallest and 5% largest sum scores are drawn somewhat narrow but, apart from that, all regressions look nice. It appears that, by just looking at the response data, we are not in a very good position to judge whether there are any true individual differences in ability. To help with that, dexter offers a function, `individual_differences`:  

```{r, fig.align='center', fig.height=4, fig.width=4}
dd = individual_differences(simulated,degree=10)
plot(dd)
```

The gray line shows the predicted frequency of each sum score under the hypothesis of no true individual differences. The green dots show the observed frequencies and it will be clear that our observed data is compatible with the null hypothesis. 

The print function for the test shows a chi-squared test for the null hypothesis. Note that this uses R's option to simulate the p-value, which explains why the degrees of freedom are missing:

```{r}
print(dd)
```

Thus, we find a p-value of `r round(dd$est$test$p.value,digits=2)` for the hypothesis that there are no individual differences.

What about real data?  **Dexter** comes with a well-known example preinstalled, the verbal aggression data [@VanData] analysed in great detail in [@boeck] and many others. 243 females and 73 males have assessed on a 3-point scale ('yes', 'perhaps', or 'no') how likely they are to become verbally aggressive in four different frustrating situations

```{r, fig.align='center', fig.height=4, fig.width=4}
db2 = start_new_project(verbAggrRules, "verbAggression.db")
add_booklet(db2, verbAggrData, "data")
dd = individual_differences(db2, booklet_id=="data")
plot(dd)
```

This is quite different now, and the chi-squared test is highly significant.


```{r, show=FALSE}
close_project(db2)
```

#References
