Forever grateful beginner looks for solution with his error code in tetrachoric correlation

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 found

Restarting 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 TRUE

Do 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 status

The 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!

:slightly_smiling_face:

With kind regards

For macOS users who do not have the XCode tools installed (or if the term is unrecognized) should take the no option. For those with XCode, it’s fine to try yes but if it doesn’t work the time, retry with no unless you have intermediate to advanced skills in troubleshooting makefiles.

Thank you very much and please excuse my very unskilled questioned. It worked. Do you also know why I get this error message after running the rest of the code:

Exportieren

write.table(cormat, "~/STATWORX/PolyCorMat.csv", row.names=F, sep=";", dec=",")

Error in file(file, ifelse(append, "a", "w")) :
cannot open the connection
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
cannot open file '/Users/-/STATWORX/PolyCorMat.csv': No such file or directory

I do not understand why it cannot open or find the file. Do you have any advice? Thank you in advance!!!

Open a terminal tab and

ls ~/STATWORX/PolyCorMat.csv

to confirm location of file

Thank you for your answer. It also says 'No such file or directory'. This is the point I do not understand. I want R to export this file because it is not existing yet.

What do I have to do additionally in order to make it work or set up the file?

Why not simply save

write.csv(your_file,"your_file.csv")

Thanks a lot. Another problem appeared after this code

for (i in 1:n){

  • for(j in 1:m){
  • cormat[i,j] <- polychor(data[,i], data[,j])
    
  • }
  • }

These were the warnings I got

There were 17 warnings (use warnings() to see them)

Diagonalelemente auf 1 setzen

diag(cormat) <- 1

Exportieren

write.table(cormat,"~/Documents/Promotion/Datenansicht_Faktorenanalyse_V1_reduziert1.sav", row.names=F, sep=";", dec=",")
warnings()
Warning messages:
1: In log(P) : NaNs produced
2: In optimise(f, interval = c(-maxcor, maxcor)) :
NA/Inf replaced by maximum positive value
3: In optimise(f, interval = c(-maxcor, maxcor)) :
NA/Inf replaced by maximum positive value
4: In optimise(f, interval = c(-maxcor, maxcor)) :
NA/Inf replaced by maximum positive value
5: In optimise(f, interval = c(-maxcor, maxcor)) :
NA/Inf replaced by maximum positive value
6: In optimise(f, interval = c(-maxcor, maxcor)) :
NA/Inf replaced by maximum positive value
7: In optimise(f, interval = c(-maxcor, maxcor)) :
NA/Inf replaced by maximum positive value
8: In optimise(f, interval = c(-maxcor, maxcor)) :
NA/Inf replaced by maximum positive value
9: In polychor(data[, i], data[, j]) : 4 rows with zero marginals removed
10: In polychor(data[, i], data[, j]) : 4 rows with zero marginals removed
11: In polychor(data[, i], data[, j]) : 3 rows with zero marginals removed
12: In polychor(data[, i], data[, j]) : 2 rows with zero marginals removed
13: In polychor(data[, i], data[, j]) : 4 rows with zero marginals removed
14: In polychor(data[, i], data[, j]) : 2 rows with zero marginals removed
15: In polychor(data[, i], data[, j]) : 2 rows with zero marginals removed
16: In polychor(data[, i], data[, j]) : 2 rows with zero marginals removed
17: In polychor(data[, i], data[, j]) : 3 rows with zero marginals removed

Do you have an idea how to deal with those?

Thanks

These are all warning messages, meaning that the code ran as intended but that the data it was given has led to some results that may not be what you expected. For example

> log(0/0)
[1] NaN

Dealing with those depends on the specific data and the purpose for which the script is being run.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.