---
title: "Item Analysis Report"
author: "(Type in the name of the test here.)"
date: "(The date could be typed in here.)"

#original scripting by Margaret Wu (edmeasurement.com.au)
#last modified 27-Dec-21 / 0930 hrs by Larry Nelson (lertap5.com)
#note: this version centers IRT item difficulties at zero

output:
  word_document: default
  html_document: default
---

```{r echo=FALSE, results='hide', warning=FALSE, message=FALSE}
rm(list=ls())
library(TAM)
library(CTT)
library(WrightMap)
library(knitr)

resp <- read.csv("Omega-Iscores.csv")
#resp <- s$scored
head(resp)

IA <- itemAnalysis(resp)
#mod1 <- tam.jml(resp)
mod1 <- tam.jml(resp, constraint="items")

tamDistr <- tam.ctt(resp,mod1$WLE)

ItemStats <- data.frame(IA$itemReport$itemName, IA$itemReport[,c(2,3,5)], mod1$item1$xsi.item)
colnames(ItemStats) <- c("Item", "Item Mean", "pBis", "AlphaIfDeleted", "IRT difficulty")

lowdisc <- "none"
d <- which(IA$itemReport$pBis<0.25)
if(length(d)>0) lowdisc <- paste("Item", d)

greateralpha <- "none"
d <- which (IA$itemReport$alphaIfDeleted>IA$alpha)
if(length(d)>0) greateralpha <- paste("Item", d)

target <- "on target"
if(mean(mod1$item1$xsi.item)-mean(mod1$theta)> 0.2) target <- "difficult"
if(mean(mod1$item1$xsi.item)-mean(mod1$theta)< -0.2) target <- "easy"
```  

# Introduction
The item analysis results in this report were generated in the open source statistical software program R ([R Core Team, 2017)](https://www.r-project.org/). The objective of the analysis is to identify test items of poor quality, and to see if the difficulty of the test appears to target the ability level of the students who were tested. Statistical measures from both classical test theory (CTT) and item response theory (IRT) are included.

# The data

A total of `r nrow(resp)` students took this `r ncol(resp)` item test. The CTT test reliability index, coefficient alpha, was computed to be `r round(IA$alpha,3)`.

# Analysis results 
In the following table of item statistics, Item Mean is CTT item difficulty; pBis is CTT item discrimination; AlphaIfDeleted indicates what reliability (alpha) would be if the item were deleted from the test - if AlphaIfDeleted is greater than alpha (in this case `r round(IA$alpha,3)`), the item has not performed as might be desired, and its inclusion in the test may have served to lower reliability. 

( Items with undesirable AlphaIfDeleted values: `r greateralpha` )

```{r echo=FALSE, comment=NA}
kable(ItemStats, align="ccccc", caption="Table 1: Summary of Item Statistics",row.names = FALSE, digits = 3)
```  
# Item discrimination index
CTT item discrimination indexes for all items ranged from `r round(min(IA$itemReport$pBis),2)` to `r round(max(IA$itemReport$pBis),2)`.   

( Weak items with CTT discrimination lower than 0.25: `r lowdisc` )  

# Test targeting
The mean IRT student ability score was `r round(mean(mod1$theta),3)`; the mean IRT item difficulty was `r round(mean(mod1$item1$xsi.item),3)`.  

( The test was `r target` for these students. )  

# Wright Map 
A [Wright Map](http://www.wrightmap.org/wrightmap/) displays the distribution of both observed student ability scores, and item difficulty values, over the logits scale.

```{r echo=FALSE, fig.align='center'}
p <- wrightMap(mod1$WLE,mod1$item1$xsi.item,item.side=itemClassic)
```  
  
# Mean IRT ability scores by groups

The tables in this section give summary results for students getting an item wrong (Group 0), and for those getting it correct (Group 1). Mean ability levels are expected to be higher in Group 1 - if they are not, the item has probably not worked as desired. 

```{r, echo=FALSE, comment=NA, message=FALSE, warning=FALSE,results="asis"}

ctt_raw <- cbind(tamDistr[,3:7],round(tamDistr[,8:10],2))
colnames(ctt_raw) <- c("Seq","Item", "Total", "Group", "Count", "Percent", "Pbs", "MeanAbility")
ctt_raw <- cbind(tamDistr[,3:4], tamDistr[,6:7], round(tamDistr[,8:10],2))
colnames(ctt_raw) <- c("Seq","Item", "Group", "Count", "Percent", "Pbs", "MeanAbility")

for (i in 1:ncol(resp)){
  itemi <- ctt_raw[ctt_raw$Item==colnames(resp)[i],]
    print(kable(itemi,row.names=FALSE,align='ccccrrrr',caption="================================================="))
}
```

# Item characteristic curves

Item characteristic curves are shown below. Blue lines plot expected scores over the ability scale as would be predicted by the IRT model. Black lines connect the dots of actual average observed IRT scores at a few specific ability levels.
```{r echo=FALSE, results='hide', comment=NA, message=FALSE, warning=FALSE, fig.height = 3.5, fig.width = 6, fig.align='center'}
plot(mod1)
```  

***
# Save Rasch item difficulty results to a csv file

Finally, IRT item difficulty values will be saved to a csv file which may be used with Excel, or simply viewed with a text editor such as Notepad (Windows), or TextEdit (Mac).

The name of the file is seen within quotation marks below. ('TAM' is the name of the program used to compute the IRT statistics used in this report. 'Mod1' in TAM specifies the Rasch Model. Read about TAM [here.](https://cran.r-project.org/web/packages/TAM/index.html))

Note: if the file already exists, an error message may be displayed.

```{r}
write.csv(mod1$item1$xsi.item,"TAM_Mod1_item_diffs.csv") # Will be saved to the working folder.

```
