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

5 comments:

  1. Hi Roger,

    Thanks for this post, and for your blog! I am using Mokken scaling and the mokken R package for my dissertation research.

    I am encountering some trouble with Mokken analyses of a set of rating scale data because of the fact that not every rater rated each item. Have you encountered an incomplete data set in a Mokken analysis? Any thoughts about what to do?

    ReplyDelete
  2. Dear Stefanie

    Very glad you found the blog useful. I am afraid that there is not way within the Mokken scaling packages that I know of for taking into account missing data. It likes a complete data set so I usually just delete listwise the ones that are incomplete. Of course, if the missing data are random across the data set you can lose a lot of people. The only way to save the data that I know of is to impute - SPSS has a few very complicated options that I don't understand! The other way is to insert either a neutral value, if your data are polytomous, or to insert the mean value for the group for missing variables. However, these strategies depend on him mush is missing, as an estimate I'd say don't do this for missing data more than ten percent. I'll be very interested to hear more about your work. I am just reviewing a manuscript from a team in USA using Mokken scaling, unusual for the USA, it's usually only used in Europe.

    Roger

    ReplyDelete
  3. Thanks for your reply! I will continue to think about the problem.

    In the meantime, I do have another question about the plotting function for iio in the R package (if you don't mind):

    I am having trouble figuring out how the function determines the order in which the plots are presented. If I use (item.pairs = x), it seems logical for the first few items, but then the ordering seems odd. Any thoughts on this?

    ReplyDelete
  4. Sorry I did not notice this question; I am also sorry I can't really help as I am more or less totally dependent on Andries van der Ark for the syntax and any other advice - without him. R is just a mystery to me! He's very helpful if you email him.

    ReplyDelete
  5. Dear Roger,
    Thank You for Your useful Blog!!!
    Despite it is very clear, I'm having problems with Mokken analysis. I have a database (SPSS) which consists of 412 obs, and 27 items (from the EFA and CFA they were divided into four subscales). My aim is shortening the scale by use of Mokken scaling (if necessary). Nevertheless, after following your instructions I find :

    Error in check.data(X) : Data are not matrix or data.frame

    Is there a problem in my database setting? Have you got any suggestions?

    Thank You in advance for your time.
    Looking forward to receive your kind feedback.

    ReplyDelete