Hello dear community,
I have a question about tetrachoric correlation. I would like to use this to transform my data with nominal scaled data level to be able to use it for a factor analysis in SPSS. I have used a tutorial from another blog to do this. However, I keep getting to a point when I run the R script that has many error messages. Can you give me a tip in this regard? The first problem occurs because 'mvtnorm' cannot be found even though I try to install it. It asks if it should be installed and I tried to hit 'yes' by typing in 'y' + enter.
I am using a Mac and running R 4.1.1.
I just adapted the code to my data.
Install the required packages
install.packages("foreign")
install.packages("polycor")
install.packages("mvtnorm")Load packages
library(foreign)
library(polycor)Import SPSS data (adjust file path)
data <- read.spss(file="~/Documents/Promotion/DataView_FactorAnalysis_V1_reduced2.sav",
use.value.labels=FALSE, to.data.frame=TRUE)number of rows/columns correlation matrix
n <- ncol(data); m <- n
Create empty correlation matrix
cormat <- matrix(nrow=n, ncol=m, data=NA)
Calculate polychoric correlations for each pair of items
for (i in 1:n){
for(j in 1:m){
cormat[i,j] <- polychor(data[,i], data[,j])
}
}set diagonal elements to 1
diag(cormat) <- 1
Export
write.table(cormat, "~/STATWORX/PolyCorMat.csv", row.names=F, sep=";", dec=",")
The output looks like this:
install.packages(foreign)
Error in install.packages : object 'foreign' not found
install.packages(polycor)
Error in install.packages : object 'polycor' not found
install.packages(mvtnorm)
Error in install.packages : object 'mvtnorm' not foundRestarting R session...
install.packages("foreign", dependencies = FALSE)
Warning in install.packages :
lzma decoder corrupt data
trying URL
Content type 'application/x-gzip' length 331923 bytes (324 KB)downloaded 324 KB
The downloaded binary packages are in
/var/folders/82/rl6q03vd6ls9wkyzc19rxt580000gn/T//RtmpeFBy11/downloaded_packages
install.packages("polycor", dependencies = FALSE)
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.1/polycor_0.8-1.tgz'
Content type 'application/x-gzip' length 72636 bytes (70 KB)downloaded 70 KB
The downloaded binary packages are in
/var/folders/82/rl6q03vd6ls9wkyzc19rxt580000gn/T//RtmpeFBy11/downloaded_packages
install.packages("mvtnorm", dependencies = FALSE)There is a binary version available but the source version is later:
binary source needs_compilation
mvtnorm 1.1-3 1.2-2 TRUEDo you want to install from sources the package which needs compilation? (Yes/no/cancel) yes
installing the source package ‘mvtnorm’trying URL
Content type 'application/x-gzip' length 662749 bytes (647 KB)downloaded 647 KB
- installing source package ‘mvtnorm’ ...
** package ‘mvtnorm’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/include -fPIC -Wall -g -O2 -c C_FORTRAN_interface.c -o C_FORTRAN_interface.o
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/include -fPIC -Wall -g -O2 -c lpmvnorm.c -o lpmvnorm.o
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/include -fPIC -Wall -g -O2 -c ltMatrices.c -o ltMatrices.o
clang -mmacosx-version-min=10.13 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/usr/local/include -fPIC -Wall -g -O2 -c miwa.c -o miwa.o
gfortran -mmacosx-version-min=10.13 -fno-optimize-sibling-calls -fPIC -Wall -g -O2 -c mvt.f -o mvt.o
make: gfortran: No such file or directory
make: *** [mvt.o] Error 1
ERROR: compilation failed for package ‘mvtnorm’- removing ‘/Library/Frameworks/R.framework/Versions/4.1/Resources/library/mvtnorm’
Warning in install.packages :
installation of package ‘mvtnorm’ had non-zero exit statusThe downloaded source packages are in
‘/private/var/folders/82/rl6q03vd6ls9wkyzc19rxt580000gn/T/RtmpeFBy11/downloaded_packages’Load packages
library(foreign)
Warning message:
package ‘foreign’ was built under R version 4.1.2
library(polycor)
Warning message:
package ‘polycor’ was built under R version 4.1.2
library(mvtnorm)
Error in library(mvtnorm) : there is no package called ‘mvtnorm’
I also think that there are problems with the export. Unfortunately, I am still a beginner in R and need a tip on how to solve this.
I would be very grateful if you could help me.
Many thanks in advance!
With kind regards