Using R within SPSS to obtain polychoric correlations

Thanks to Jarlath Quinn of SPSS for the syntax below. You need SPSS version 16 or above to use this syntax and follow these guidelines for using R code in SPSS. Details and examples of using R within SPSS are in the programming and data management guide which may downloaded for free from here. You can alternatively run the R code in R version 2.5 to produce polychoric correlations from a SPSS data file without needing SPSS (see later example syntax further down this page). Further details of the R example are in the Graduate Statistics Talk on Factor Analysis located here.

The example SPSS data file for use in SPSS is given here.

GET
  FILE='C:\SE demos\Assess\demo dataset v4.sav'.
DATASET NAME $DataSet WINDOW=FRONT.

begin program r.
library(polycor)
casedata <- spssdata.GetDataFromSPSS(variables=c(9,10,11,12))
satcat<-as.factor(casedata$satcat)
sat <- casedata$sat
perf_2 <- casedata$perf_2
perf_3 <- casedata$perf_3
res <- hetcor(satcat, sat, perf_2, perf_3, ML = FALSE,std.err = FALSE, bins=4)
spsspivottable.Display(res$correlations,
                                          title="Correlation Matrix",
                                          rowdim=" ",
                                          coldim="col",
                                          format=formatSpec.Correlation)
end program.

The below R syntax can be run in R version 2.5 to produce polychoric correlations from a SPSS data file without the need to use SPSS. Note : This example may not work in later versions of R. You will need to change the directory location and filename in the 'read.spss' command, the variable names replacing PTCI1, PTCI2 etc. and amend the list of variable names inputted into 'hetcor'.

library(foreign)
library(mvtnorm)
library(sfsmisc)
library(polycor)
x <- read.spss("C:\\Documents and Settings\\peterw\\Desktop\\POLYCHORIC DEMO IN SPSS\\example33.sav")
p1 <- as.factor(x$PTCI1)
p2 <- as.factor(x$PTCI2)
p3 <- as.factor(x$PTCI3)
p4 <- as.factor(x$PTCI4)
p5 <- as.factor(x$PTCI5)
p6 <- as.factor(x$PTCI6)
res <- hetcor(p1, p2, p3, p4, p5, p6, ML=FALSE, std.err=FALSE)
res$correlation
res$type