#########################################################################################
# Little's test
#########################################################################################

#Data set b is the MCAR dataset. There are a few cases that are missing every value, which causes the routine
#to crash. Thus Little's test on Data Set b below is run on a subset that excludes completely missing cases.
#The remaining data sets are MNAR.

b<- read.csv("class13_practice_2014_b.csv")
c<- read.csv("class13_practice_2014_c.csv")
d<- read.csv("class13_practice_2014_d.csv")


#install.packages("BaylorEdPsych")
#Note, this package has been removed from CRAN
#You can download it from this index page
#Download package to a location on your computer.
#In RStudio, Tools->Install Packages->browse to find tar.gz file->Install
library(BaylorEdPsych)

#install.packages("mvnmle")
#Note, this package has been removed from CRAN. Follow instructions above
library(mvnmle)

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
#Create MCAR dataset and run Little's test
b_subset<-select(b,Executive,Memory,IQ,PositiveAffect,Extraversion,WellBeing,Hearing,Vision,Olfactory)
#LittleMCAR(b_subset)

#The above bombs because there is at least one row that is COMPLETELY missing. That violates an
#assumption of Little's test, which requires at least one value per case. So, below, we drop
#any rows that are *completely* missing. The -9 represents the number of variables in the data
#subset (9)

LittleMCAR(b_subset[ !apply(b_subset, 1, function(x) all(is.na(x))), -9])
## this could take a while
## $chi.square
## [1] 31.52806
##
## $df
## [1] 41
##
## $p.value
## [1] 0.8564034
##
## $missing.patterns
## [1] 9
##
## $amount.missing
## Executive Memory IQ PositiveAffect Extraversion
## Number Missing 2.000000000 4.0000000 3.0000000 4.0000000 3.0000000
## Percent Missing 0.008368201 0.0167364 0.0125523 0.0167364 0.0125523
## WellBeing Hearing Vision
## Number Missing 4.0000000 3.0000000 2.000000000
## Percent Missing 0.0167364 0.0125523 0.008368201
##
## <additional material deleted for length>

#Create MNAR dataset and run Little's test

c_subset<-select(c,Executive,Memory,IQ,PositiveAffect,Extraversion,WellBeing,Hearing,Vision,Olfactory)
LittleMCAR(c_subset)
## Warning in nlm(lf, startvals, ...): NA/Inf replaced by maximum positive value

## Warning in nlm(lf, startvals, ...): NA/Inf replaced by maximum positive value
## this could take a while
## $chi.square
## [1] 72.29784
##
## $df
## [1] 43
##
## $p.value
## [1] 0.003406427
##
## $missing.patterns
## [1] 12
##
## $amount.missing
## Executive Memory IQ PositiveAffect Extraversion
## Number Missing 10.00000000 8.00000000 4.00000000 10.00000000 4.00000000
## Percent Missing 0.04166667 0.03333333 0.01666667 0.04166667 0.01666667
## WellBeing Hearing Vision Olfactory
## Number Missing 3.0000 9.0000 6.000 8.00000000
## Percent Missing 0.0125 0.0375 0.025 0.03333333
##
## <additional material deleted for length>

#Create MNAR dataset and run Little's test

d_subset<-select(d,Executive,Memory,IQ,PositiveAffect,Extraversion,WellBeing,Hearing,Vision,Olfactory)
LittleMCAR(d_subset)
## this could take a while
## $chi.square
## [1] 103.2427
##
## $df
## [1] 71
##
## $p.value
## [1] 0.007482612
##
## $missing.patterns
## [1] 10
##
## $amount.missing
## Executive Memory IQ PositiveAffect Extraversion
## Number Missing 2.000000000 2.000000000 2.000000000 3.0000 2.000000000
## Percent Missing 0.008333333 0.008333333 0.008333333 0.0125 0.008333333
## WellBeing Hearing Vision Olfactory
## Number Missing 2.000000000 2.000000000 2.000000000 0
## Percent Missing 0.008333333 0.008333333 0.008333333 0
##
## <additional material deleted for length>