Sunday 13 January 2013

R syntax for Mokken Scaling Analysis



R syntax for Mokken Scaling Analysis (MSA)

Installing R

For full instructions see: https://cran.r-project.org/web/packages/mokken/mokken.pdf

1. Go to http://cran.r-project.org/

2. Click on Windows

3. Click on base

4. Click on Download R for Windows (the most recent version).

5. Click on Install R for the first time.

6. Click on Download R 3.0.1 for Windows.

7. Save the R-3.0.1-win.exe to your computer.

6. Run the R-3.0.1-win.exe from your computer and choose all the default values in the installation Wizard.



Installing MSA



1. Open R.

2. In the pull down menu choose Packages, Install package(s), choose a location nearby you, and choose the package ‘mokken’. MSA is now installed on your computer and need not be installed again.

3. If that does not work then use the following syntax:



> install.packages("mokken", dependencies=TRUE, repos="http://lib.stat.cmu.edu/R/CRAN")



Ignore any error messages and the Mokken package should load; you will know if no errors are returned after using the first command below.



Using MSA



Open R and type:



> library(mokken)



Converting an SPSS file for use in R



> library(foreign)



> FileR <- data.frame(read.spss("C:/FileSPSS.sav")) [NB: this function no longer appears to be working, instead, follow suggestion below]



Install R Studio

In ‘Session’ in top toolbar scroll down to ‘Set Working Directory’

Select ‘Choose Directory’

Go to the folder containing your SPSS file and click (NOT double click) on it

Click ‘Open’, then



> data <- data.frame(read.spss(“FileSPSS.sav”)) [NB: advisable in SPSS to turn OFF Unicode*)

You may get an error message:

re-encoding from UTF-8 - ignore




You may get some other errors or warnings at this stage which may have to be fixed before proceeding, then:



> fix(FileR) [NB: this function no longer works, use:

> str(data)




This will show you the data as they appear in R, then:



> save(FileR, file = "C:/FileR.Rdata")



Once you have create an R file is can be uploaded again by:



> load("C:/FileR.Rdata")

- I have found recently that this works but I cannot load a file and use it in ‘mokken’ so I have just been converting the file from SPSS to R as above and for ‘FileR’ below I have used ‘data’ – at the time of writing I have yet to run a full MSP. This will be updated in due course when I do if there are any other differences.




Generating scales

To partition items in the FileR database into Mokken scales type:



> aisp(FileR)



Scalability coefficients

To produce scalability coefficients for items and the overall scale(s) type:

> coefH (FileR)



Mean item scores

To produce the mean values for all of the items in the scale type:

> apply(FileR,2,mean)



Monotonicity

To check monotonicity type:

> summary(check.monotonicity(FileR))



Plotting item step response functions

To plot item step response functions type:

> plot(check.monotonicity(FileR))

NB: if this does not work and you get:

*****Error in est - qnorm(1 - alpha.ci/2) * se : non-conformable arrays

In addition: Warning messages:

1: In (x - x^2)/n :

longer object length is not a multiple of shorter object length****

this is a problem in R and you should use:

> plot(check.monotonicity(FileR), plot.ci = FALSE



Invariant item ordering

To check invariant item ordering type:

> summary(check.iio(FileR))



OR



> iio.results <- check.iio(FileR)

> summary(check.iio(FileR, item.selection = FALSE))



Generating pair plots

To generate pair plots:

> plot(check.iio(FileR))

The confidence intervals can be omitted by:

> plot(check.iio(FileR), plot.ci = FALSE)

To select item pairs, eg 1st, 3rd & 7th:

> plot(check.iio(FileR), item.pair = c(1, 3, 7) )



Saving plots

To save plots in a file (eg as pdf) in eg drive C:\

> NameOfFigure = "FileR.pdf"

> setwd("C:")

> pdf(NameOfFigure)

> plot(check.iio(FileR), ask = FALSE)

> dev.off()



Without confidence intervals

> NameOfFigure = "FileR.pdf"

> setwd("C:")

> pdf(NameOfFigure)

> plot(check.iio(FileR), plot.ci = FALSE, ask = FALSE)

> dev.off()



Additional information about plotting item pairs

# The complete command, where everything is set to default values

plot(check.iio(FileR), plot.ci = TRUE, color.ci = c("orange", "yellow"), alpha.ci = .05, ask = TRUE)



# Because default values can be omitted, the above command equals

plot(check.iio(FileR))



# Without colors

plot(check.iio(FileR), plot.ci = TRUE, color.ci = c("white", "white"), alpha.ci = .05, ask = TRUE)



# No more hitting Enters

plot(check.iio(FileR), plot.ci = TRUE, color.ci = c("white", "white"), alpha.ci = .05, ask = FALSE)



# Only the third item pair Pair1 = 1,2; Pair2 = 1,3; Pair3 = 1,4

plot(check.iio(FileR), item.pa



Reliability

To check reliability type:

> check.reliability(FileR)



Selecting items to analyse

To select specific items you need to create a new file as follows, type:

> FileRy <- FileR[ ,c(1,2,3,4)] - this will select items 1, 2, 3 & 4

> FileRy <- FileR[ ,c(1,2,3:10)] - this will select items 1, 2, 3, 4, 5, 6, 7, 8, 9 & 10

In both cases you analyse FileRy



Selecting individual for analysis

To select specific individuals you need to create a new file as follows, type:

> FileRx <- FileR[c(1,2:5)] - this will select individuals 1, 2, 3, 4 & 5



Removing R files from memory

> rm(list = ls())




Person item fit for polytomous data (with thanks to Jorge Tendeiro)



Load PerFit from R packages



NB: Ncat= number of response categories; Blvl=percentage cutoff level



> library(PerFit)

> load("C:/FileR.Rdata")

> x.Gnormedpoly <- Gnormed.poly(FileR, Ncat)

> plot(x.Gnormedpoly)

> Gnormedpoly.out <- Gnormed.poly(FileR, Ncat)

> Gnormedpoly.cut <- cutoff(Gnormedpoly.out, Blvl=.01)

> flagged.resp(Gnormedpoly.out, Gnormedpoly.cut, scores=FALSE)$PFSscores





> library(PerFit)

> load("G:/ItADL1PF.Rdata")

> x.Gnormedpoly <- Gnormed.poly(ItADL1PF, 5)

> plot(x.Gnormedpoly)

> Gnormedpoly.out <- Gnormed.poly(ItADL1PF, Ncat=5)

> Gnormedpoly.cut <- cutoff(Gnormedpoly.out, Blvl=.01)

> flagged.resp(Gnormedpoly.out, Gnormedpoly.cut, scores=FALSE)$PFSscores





When packages won’t load this syntax is useful:



install.packages("missing.package.name", repos=c("http://rstudio.org/_packages", "http://cran.rstudio.com"))



TCD R course: http://www.tcd.ie/Zoology/research/research/theoretical/Rpodcasts.php



* turning off Unicode: https://www.youtube.com/watch?v=2pfhSugLaIQ



RW 11 October 2020