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

#original scripting done by Margaret Wu
#last modified 27-February-2024 by Larry Nelson (lertap5.com)
#time was 1600 hrs

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)

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

IA <- itemAnalysis(resp)
mod1 <- tam.jml(resp)
#tamDistr <- tam.ctt(resp_raw,mod1$WLE)

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

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

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

target <- "on target"
if(mean(mod1$xsi)-mean(mod1$theta)> 0.2) target <- "difficult"
if(mean(mod1$xsi)-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). The objective of the analysis is to identify poor quality test items, 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 well, and its inclusion in the test will very likely have served to lower reliability. 
```{r echo=FALSE, comment=NA}
kable(ItemStats, align="ccccc", caption="Table 1: Summary f 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)`.   
Weakly-performing items with CTT discrimination below 0.2: `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$xsi),3)`.  
This 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 -3 to +3 logits.

```{r echo=FALSE, fig.align='center'}
p <- wrightMap(mod1$WLE,mod1$xsi,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 not, the item has not worked as expected. 

```{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 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 statistics (IRT item difficulty and corresponding standard error) will be copied 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))

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

```
