## Test construction scenario

source("~/Dissertation/Material/Statistics/Libraries/InspectionRealm.R")
source("~/Dissertation/Material/Statistics/Libraries/RaschMeasurement.R")


## Setting up the inspectors
inspectors<-data.frame(ID=c(1:30),Ability=rnorm(30,-1.1,1))

## setting up the project
project1<-data.frame(ID=c(1:30),Difficulty=rnorm(30,0,1))


## Computing individual detection probabilities (inspector x defect)
lm1<-LambdaMatrix(inspectors[c(1:30),],project1)

## Computes a response matrix (random process)
rm1<-ResponseMatrix(lm1)

## Plotting the response marix with structural incomplete design
X11()
par(mfrow=c(2,1))
PlotIncompleteResponseMatrix(rm1,title="Response Matrix project1")

## From now on it's latent

## Estimating the item parameters
ltmfit1<-EstimateItemPar(rm1)

## Plotting the ICCs
PlotItemCurves(ltmfit1)

## Testing for unidimensionality with Anderson LR-test
lrres<-ModelTest(ltmfit1)

## Estimating the person.parameters, this will return a person parameter for each possible score (sum of correct responses)
personpar<-person.parameter(ltmfit1)
#personpar<-personpar[["pred.list"]][["1"]]
thetas<-personpar[["thetapar"]][["NAgroup1"]]

## Testing five newly employed inspectors
## Simulating the test data
newinspectors<-data.frame(ID=c(31:35),Ability=rnorm(5,-1.1,1))
lmnew<-LambdaMatrix(newinspectors,project1)
testdata<-ResponseMatrix(lmnew)
## Test scores (sum of corect responses)
testscores<-apply(testdata, 1, sum)
## Looking up the person parameters
newthetas<-data.frame(Inspector=newinspectors$ID,Theta=personpar[["pred.list"]][["1"]][["y"]][c(testscores+1)])
## Printing the distribution of all inspectors with the new ones highlighted
X11()
#pdf(file="/home/martin/Dissertation/Publications/IRMInspection/testresults.pdf", height=5, width=7)
par(mfrow=c(1,2))
PlotIncompleteResponseMatrix(testdata,title=NULL)
boxplot(c(thetas,newthetas$Theta),main=NULL)
for (t in c(1:length(newthetas$Inspector))){
	text(x=newthetas$Theta[t],pos=2,offset=4,labels=paste(newthetas$Inspector[t]),col='Red',font=2)
}
#dev.off()