I am trying to calculate some polychoric correlations (reproducible code here)
library(polycor)
Var1 <- (c(1,2,3,1,2,2,3,2,2,1,2))
Var2 <- (c(2,2,3,1,2,1,3,2,2,1,2))
df <- as.data.frame(cbind(Var1, Var2))
polychor(df$Var1,df$Var2)
However, I also need the confidence intervals but I am not able to calculate them. I have calculated the polychoric correlations with the polycor package. I have found some packages such as ci.rpc but they do not work in my R version.
Does anybody now how to calculate CI for polychoric correlations? Maybe using boostrapping?
Thank you so much in advance.
You can use the boot library.Bootstrapping involves resampling your data multiple times to create a distribution of the polychoric correlation estimates, from which you can derive confidence intervals.
library(polycor)
library(boot)
Var1 <- (c(1,2,3,1,2,2,3,2,2,1,2))
Var2 <- (c(2,2,3,1,2,1,3,2,2,1,2))
df <- as.data.frame(cbind(Var1, Var2))
polychor(df$Var1,df$Var2)
# Function to calculate polychoric correlation
polychoric_correlation <- function(data, indices) {
sample_data <- data[indices, ] # resample the data
return(polychor(sample_data$Var1, sample_data$Var2))
}
# Bootstrapping
set.seed(123) # for reproducibility
results <- boot(data = df, statistic = polychoric_correlation, R = 1000)
# Confidence intervals
boot.ci(results, type = "perc")
Output:
BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 997 bootstrap replicates
CALL :
boot.ci(boot.out = results, type = "perc")
Intervals :
Level Percentile
95% ( 0.4239, 0.9998 )
Calculations and Intervals on Original Scale
Also, if you have any problems installing the libraries, I recommend that you update your version of R and RStudio https://posit.co/download/rstudio-desktop/