# See
# van der Ark (2007).  Mokken scale analysis is R.  Journal of Statistical Software, 20.
 # van der Ark (2012).  New developments in Mokken scale analysis in R.  
#                                         Journal of Statistical Software, 48.
# set working directory
setwd= "C:\\Users\\cja\\Documents\\edps584\\ Factor Analysis"

# Install package & change directory to desktop (or where ever are working and date live
library(mokken)
# Read in data
bfv_csv <- read.csv("espelage_for_R.csv", header=TRUE, colClasses=c(rep("numeric",18)))
 bfv <- bfv_csv[complete.cases(bfv_csv),]
# So if Can figure out what's what
b1 <-bfv$q36
b2 <-bfv$q37
f1 <- bfv$q38
v1 <- bfv$q39
v2 <- bfv$q40
v3 <- bfv$q41
v4 <- bfv$q42
b3 <- bfv$q43
b4 <- bfv$q44
f2 <- bfv$q45
f3 <- bfv$q46
f4 <- bfv$q47
f5 <- bfv$q48
b5 <- bfv$q49
b6<- bfv$q50
b7 <- bfv$q51
b8<- bfv$q52
b9 <- bfv$q53
X <- cbind(b1, b2, b3, b4, b5, b6,  b7, b8, b9, f1, f2, f3, f4, f5, v1, v2, v3, v4)
head(X)
#
# Mokken....looks like 2 distinct scales and some bad items
#
coefH(X)
check.iio(X, item.selection=TRUE, alpha=.05, verbose=FALSE)
summary(check.iio(X))
scale.normal <- aisp(X)
scale.ga <- aisp(X, search="ga")
two <- cbind(scale.normal, scale.ga)
two

# So if looks like bully/fight are one scale and distinct from victimization
# I'll do each by themselves

#
# Mokken just for victimization
#
victim <- X[, 15:18]
coefH(victim)
check.iio(victim, item.selection=TRUE, alpha=.05, verbose=FALSE)
summary(check.iio(victim))
scale.normal <- aisp(victim)
scale.normal
# looking at monoronicity of response functions
mono_check <- check.monotonicity(X)
summary(mono_check)
plot(mono_check)
# Looking at nonintersection of response functions
pmatrix <- check.pmatrix(X)
summary(pmatrix)
plot(pmatrix)
# Restscore
Xrest <- check.restscore(X)
summary(Xrest)
plot(Xrest)
# 
# Mokken just for bully
#
bully <- X[, 1:9]
coefH(bully)
check.iio(bully, item.selection=TRUE, alpha=.05, verbose=FALSE)
summary(check.iio(bully))
bscale.normal <- aisp(bully)
bscale.normal
#
# Mokken just for fight
#
fight <- X[,10:14]
coefH(fight)
check.iio(fight, item.selection=TRUE, alpha=.05, verbose=FALSE)
summary(check.iio(fight))
fscale.normal <- aisp(fight)
fscale.normal