General communalities
Correlation
basecom<-elsi %>% select(less_education,
hearing_loss,
high_cholesterol,
depression,
physical_inactivity,
smoking,
diabetes,
hypertension,
obesity,
exc_alcohol,
social_isolation,
air_pollution,
vision_loss)
a<-tetrachoric(basecom)
library(knitr)
rho_matrix <- a$rho
#loop for estimate the p values of the correlation
n <- nrow(basecom) # sample size
result <- data.frame() #output dataframe
#p-value loop
for (i in 1:ncol(rho_matrix)) {
for (j in 1:i) {
if (i != j) {
rho <- rho_matrix[i, j]
z <- rho * sqrt(n - 2) / sqrt(1 - rho^2) #z value for the correlation
p_value <- 2 * (1 - pnorm(abs(z))) #p from z
result <- rbind(result, data.frame(
Var1 = colnames(rho_matrix)[i],
Var2 = colnames(rho_matrix)[j],
Rho = rho,
P_Value = p_value,
Significance = case_when(
p_value < 0.01 ~ "***",
p_value < 0.05 ~ "**",
TRUE ~ "ns"
)
))
}
}
}
result %>%
arrange(desc(abs(Rho)))%>% #arrange by bigger correlation (absolute value)
kable("html", digits = 4, caption = " Tetrachoric correlation") %>%
kable_styling(full_width = TRUE, bootstrap_options = c("striped", "hover", "condensed"))
Tetrachoric correlation
|
Var1
|
Var2
|
Rho
|
P_Value
|
Significance
|
|
vision_loss
|
hearing_loss
|
0.4879
|
0.0000
|
***
|
|
exc_alcohol
|
smoking
|
0.3724
|
0.0000
|
***
|
|
air_pollution
|
less_education
|
-0.3533
|
0.0000
|
***
|
|
diabetes
|
high_cholesterol
|
0.3482
|
0.0000
|
***
|
|
hypertension
|
diabetes
|
0.3256
|
0.0000
|
***
|
|
hypertension
|
high_cholesterol
|
0.2894
|
0.0000
|
***
|
|
obesity
|
hypertension
|
0.2490
|
0.0000
|
***
|
|
obesity
|
smoking
|
-0.2233
|
0.0000
|
***
|
|
hearing_loss
|
less_education
|
0.2168
|
0.0000
|
***
|
|
vision_loss
|
less_education
|
0.2139
|
0.0000
|
***
|
|
social_isolation
|
hearing_loss
|
0.1897
|
0.0000
|
***
|
|
air_pollution
|
social_isolation
|
-0.1812
|
0.0000
|
***
|
|
exc_alcohol
|
less_education
|
-0.1800
|
0.0000
|
***
|
|
air_pollution
|
exc_alcohol
|
0.1770
|
0.0000
|
***
|
|
physical_inactivity
|
less_education
|
0.1719
|
0.0000
|
***
|
|
vision_loss
|
air_pollution
|
-0.1687
|
0.0000
|
***
|
|
air_pollution
|
hearing_loss
|
-0.1674
|
0.0000
|
***
|
|
obesity
|
diabetes
|
0.1627
|
0.0000
|
***
|
|
hypertension
|
less_education
|
0.1624
|
0.0000
|
***
|
|
air_pollution
|
physical_inactivity
|
0.1575
|
0.0000
|
***
|
|
vision_loss
|
social_isolation
|
0.1552
|
0.0000
|
***
|
|
depression
|
high_cholesterol
|
0.1550
|
0.0000
|
***
|
|
vision_loss
|
physical_inactivity
|
0.1485
|
0.0000
|
***
|
|
social_isolation
|
exc_alcohol
|
0.1474
|
0.0000
|
***
|
|
physical_inactivity
|
hearing_loss
|
0.1467
|
0.0000
|
***
|
|
air_pollution
|
diabetes
|
0.1377
|
0.0000
|
***
|
|
social_isolation
|
depression
|
-0.1233
|
0.0000
|
***
|
|
diabetes
|
depression
|
0.1222
|
0.0000
|
***
|
|
social_isolation
|
less_education
|
0.1207
|
0.0000
|
***
|
|
obesity
|
high_cholesterol
|
0.1199
|
0.0000
|
***
|
|
vision_loss
|
diabetes
|
0.1173
|
0.0000
|
***
|
|
exc_alcohol
|
depression
|
-0.1153
|
0.0000
|
***
|
|
diabetes
|
physical_inactivity
|
0.1100
|
0.0000
|
***
|
|
exc_alcohol
|
diabetes
|
-0.1039
|
0.0000
|
***
|
|
hypertension
|
smoking
|
-0.1020
|
0.0000
|
***
|
|
depression
|
hearing_loss
|
0.0984
|
0.0000
|
***
|
|
obesity
|
depression
|
0.0976
|
0.0000
|
***
|
|
hypertension
|
hearing_loss
|
0.0901
|
0.0000
|
***
|
|
smoking
|
high_cholesterol
|
-0.0899
|
0.0000
|
***
|
|
vision_loss
|
high_cholesterol
|
0.0892
|
0.0000
|
***
|
|
air_pollution
|
high_cholesterol
|
0.0891
|
0.0000
|
***
|
|
air_pollution
|
depression
|
0.0883
|
0.0000
|
***
|
|
exc_alcohol
|
hearing_loss
|
-0.0876
|
0.0000
|
***
|
|
air_pollution
|
obesity
|
0.0830
|
0.0000
|
***
|
|
vision_loss
|
depression
|
0.0804
|
0.0000
|
***
|
|
vision_loss
|
hypertension
|
0.0790
|
0.0000
|
***
|
|
physical_inactivity
|
depression
|
0.0764
|
0.0000
|
***
|
|
hypertension
|
depression
|
0.0763
|
0.0000
|
***
|
|
exc_alcohol
|
physical_inactivity
|
-0.0756
|
0.0000
|
***
|
|
vision_loss
|
exc_alcohol
|
-0.0743
|
0.0000
|
***
|
|
hypertension
|
physical_inactivity
|
0.0718
|
0.0000
|
***
|
|
exc_alcohol
|
high_cholesterol
|
-0.0717
|
0.0000
|
***
|
|
high_cholesterol
|
hearing_loss
|
0.0703
|
0.0000
|
***
|
|
diabetes
|
less_education
|
0.0702
|
0.0000
|
***
|
|
obesity
|
hearing_loss
|
-0.0700
|
0.0000
|
***
|
|
diabetes
|
hearing_loss
|
0.0672
|
0.0000
|
***
|
|
exc_alcohol
|
obesity
|
-0.0648
|
0.0000
|
***
|
|
smoking
|
less_education
|
0.0593
|
0.0000
|
***
|
|
obesity
|
less_education
|
-0.0569
|
0.0000
|
***
|
|
social_isolation
|
smoking
|
0.0567
|
0.0000
|
***
|
|
exc_alcohol
|
hypertension
|
-0.0561
|
0.0000
|
***
|
|
smoking
|
hearing_loss
|
-0.0546
|
0.0000
|
***
|
|
social_isolation
|
physical_inactivity
|
0.0537
|
0.0000
|
***
|
|
social_isolation
|
hypertension
|
0.0522
|
0.0000
|
***
|
|
vision_loss
|
obesity
|
-0.0437
|
0.0000
|
***
|
|
diabetes
|
smoking
|
-0.0415
|
0.0000
|
***
|
|
vision_loss
|
smoking
|
0.0389
|
0.0001
|
***
|
|
obesity
|
physical_inactivity
|
0.0322
|
0.0013
|
***
|
|
social_isolation
|
obesity
|
-0.0306
|
0.0023
|
***
|
|
depression
|
less_education
|
0.0296
|
0.0031
|
***
|
|
smoking
|
physical_inactivity
|
0.0287
|
0.0041
|
***
|
|
air_pollution
|
hypertension
|
0.0178
|
0.0760
|
ns
|
|
air_pollution
|
smoking
|
-0.0140
|
0.1632
|
ns
|
|
physical_inactivity
|
high_cholesterol
|
0.0075
|
0.4561
|
ns
|
|
social_isolation
|
high_cholesterol
|
-0.0051
|
0.6136
|
ns
|
|
high_cholesterol
|
less_education
|
-0.0040
|
0.6900
|
ns
|
|
social_isolation
|
diabetes
|
-0.0038
|
0.7066
|
ns
|
|
smoking
|
depression
|
0.0021
|
0.8352
|
ns
|
corplot
corrplot(a$rho, method = "shade", diag = FALSE, order = 'AOE', addrect = 4)

communalities
b<-as.data.frame(a$rho)
# cuantos componentes principales tienen eigenvalues>1?
eigenvalues <-eigen(b)$values
eigenvalues
## [1] 2.1754554 1.8764926 1.4013326 1.1725945 1.0192945 0.9985141 0.8764012
## [8] 0.7463694 0.6548234 0.6195857 0.5251874 0.4829863 0.4509629
##5!
eigenVectors<-eigen(b)$vectors
# I use the paper criteria for caculation:
#"Communality was calculated as the sum of the square of all factor loadings"
comunalities<-as.data.frame(eigenVectors)
comunalities$RF<-names(b)
comunalities$comunality<-comunalities$V1^2+comunalities$V2^2+comunalities$V3^2+
comunalities$V4^2+comunalities$V5^2
# limpio todo y nos vamos
comunalities<-comunalities %>% select(RF, comunality)
comunalities %>%
kable("html", digits = 4, caption = "Comunalities") %>%
kable_styling(full_width = TRUE, bootstrap_options = c("striped", "hover", "condensed"))
Comunalities
|
RF
|
comunality
|
|
less_education
|
0.2656
|
|
hearing_loss
|
0.3030
|
|
high_cholesterol
|
0.3216
|
|
depression
|
0.4144
|
|
physical_inactivity
|
0.4988
|
|
smoking
|
0.5242
|
|
diabetes
|
0.2937
|
|
hypertension
|
0.3358
|
|
obesity
|
0.3172
|
|
exc_alcohol
|
0.4750
|
|
social_isolation
|
0.4647
|
|
air_pollution
|
0.4969
|
|
vision_loss
|
0.2893
|
##by sex ##males
Correlation
basecom<-elsi %>% filter(man==1) %>% select(less_education,
hearing_loss,
high_cholesterol,
depression,
physical_inactivity,
smoking,
diabetes,
hypertension,
obesity,
exc_alcohol,
social_isolation,
air_pollution,
vision_loss)
a<-tetrachoric(basecom)
rho_matrix <- a$rho
n <- nrow(basecom) # sample size
result <- data.frame() #output dataframe
#p-value loop
for (i in 1:ncol(rho_matrix)) {
for (j in 1:i) {
if (i != j) {
rho <- rho_matrix[i, j]
z <- rho * sqrt(n - 2) / sqrt(1 - rho^2) #z value for the correlation
p_value <- 2 * (1 - pnorm(abs(z))) #p from z
result <- rbind(result, data.frame(
Var1 = colnames(rho_matrix)[i],
Var2 = colnames(rho_matrix)[j],
Rho = rho,
P_Value = p_value,
Significance = case_when(
p_value < 0.01 ~ "***",
p_value < 0.05 ~ "**",
TRUE ~ "ns"
)
))
}
}
}
result %>%
arrange(desc(abs(Rho)))%>% #arrange by bigger correlation (absolute value)
kable("html", digits = 4, caption = " Tetrachoric correlation") %>%
kable_styling(full_width = TRUE, bootstrap_options = c("striped", "hover", "condensed"))
Tetrachoric correlation
|
Var1
|
Var2
|
Rho
|
P_Value
|
Significance
|
|
vision_loss
|
hearing_loss
|
0.4942
|
0.0000
|
***
|
|
air_pollution
|
less_education
|
-0.4627
|
0.0000
|
***
|
|
diabetes
|
high_cholesterol
|
0.4003
|
0.0000
|
***
|
|
exc_alcohol
|
smoking
|
0.3366
|
0.0000
|
***
|
|
hypertension
|
high_cholesterol
|
0.2976
|
0.0000
|
***
|
|
hypertension
|
diabetes
|
0.2690
|
0.0000
|
***
|
|
vision_loss
|
less_education
|
0.2597
|
0.0000
|
***
|
|
obesity
|
smoking
|
-0.2495
|
0.0000
|
***
|
|
air_pollution
|
exc_alcohol
|
0.2473
|
0.0000
|
***
|
|
obesity
|
hypertension
|
0.2335
|
0.0000
|
***
|
|
vision_loss
|
air_pollution
|
-0.2305
|
0.0000
|
***
|
|
hearing_loss
|
less_education
|
0.2082
|
0.0000
|
***
|
|
exc_alcohol
|
less_education
|
-0.2051
|
0.0000
|
***
|
|
air_pollution
|
physical_inactivity
|
0.1803
|
0.0000
|
***
|
|
air_pollution
|
hearing_loss
|
-0.1698
|
0.0000
|
***
|
|
air_pollution
|
social_isolation
|
-0.1666
|
0.0000
|
***
|
|
social_isolation
|
hearing_loss
|
0.1646
|
0.0000
|
***
|
|
obesity
|
diabetes
|
0.1593
|
0.0000
|
***
|
|
social_isolation
|
exc_alcohol
|
0.1572
|
0.0000
|
***
|
|
obesity
|
high_cholesterol
|
0.1508
|
0.0000
|
***
|
|
physical_inactivity
|
hearing_loss
|
0.1466
|
0.0000
|
***
|
|
air_pollution
|
diabetes
|
0.1371
|
0.0000
|
***
|
|
obesity
|
less_education
|
-0.1359
|
0.0000
|
***
|
|
physical_inactivity
|
less_education
|
0.1344
|
0.0000
|
***
|
|
vision_loss
|
social_isolation
|
0.1313
|
0.0000
|
***
|
|
vision_loss
|
physical_inactivity
|
0.1306
|
0.0000
|
***
|
|
physical_inactivity
|
depression
|
0.1118
|
0.0000
|
***
|
|
depression
|
high_cholesterol
|
0.1065
|
0.0000
|
***
|
|
air_pollution
|
high_cholesterol
|
0.1062
|
0.0000
|
***
|
|
exc_alcohol
|
hearing_loss
|
-0.1025
|
0.0000
|
***
|
|
smoking
|
less_education
|
0.1024
|
0.0000
|
***
|
|
social_isolation
|
diabetes
|
-0.1016
|
0.0000
|
***
|
|
hypertension
|
smoking
|
-0.0990
|
0.0000
|
***
|
|
social_isolation
|
smoking
|
0.0953
|
0.0000
|
***
|
|
vision_loss
|
hypertension
|
0.0865
|
0.0000
|
***
|
|
hypertension
|
hearing_loss
|
0.0857
|
0.0000
|
***
|
|
high_cholesterol
|
hearing_loss
|
0.0852
|
0.0000
|
***
|
|
social_isolation
|
hypertension
|
0.0823
|
0.0000
|
***
|
|
air_pollution
|
obesity
|
0.0800
|
0.0000
|
***
|
|
social_isolation
|
less_education
|
0.0778
|
0.0000
|
***
|
|
smoking
|
high_cholesterol
|
-0.0753
|
0.0000
|
***
|
|
depression
|
hearing_loss
|
0.0749
|
0.0000
|
***
|
|
hypertension
|
less_education
|
0.0744
|
0.0000
|
***
|
|
smoking
|
physical_inactivity
|
0.0705
|
0.0000
|
***
|
|
exc_alcohol
|
physical_inactivity
|
-0.0676
|
0.0000
|
***
|
|
vision_loss
|
exc_alcohol
|
-0.0665
|
0.0000
|
***
|
|
hypertension
|
physical_inactivity
|
0.0663
|
0.0000
|
***
|
|
exc_alcohol
|
depression
|
-0.0658
|
0.0000
|
***
|
|
vision_loss
|
smoking
|
0.0656
|
0.0000
|
***
|
|
vision_loss
|
high_cholesterol
|
0.0625
|
0.0001
|
***
|
|
exc_alcohol
|
diabetes
|
-0.0608
|
0.0001
|
***
|
|
vision_loss
|
diabetes
|
0.0561
|
0.0004
|
***
|
|
high_cholesterol
|
less_education
|
-0.0559
|
0.0004
|
***
|
|
diabetes
|
hearing_loss
|
0.0555
|
0.0004
|
***
|
|
diabetes
|
depression
|
0.0539
|
0.0006
|
***
|
|
vision_loss
|
obesity
|
-0.0521
|
0.0009
|
***
|
|
obesity
|
depression
|
0.0508
|
0.0012
|
***
|
|
air_pollution
|
hypertension
|
0.0505
|
0.0013
|
***
|
|
diabetes
|
physical_inactivity
|
0.0483
|
0.0021
|
***
|
|
air_pollution
|
depression
|
0.0481
|
0.0022
|
***
|
|
diabetes
|
smoking
|
-0.0420
|
0.0075
|
***
|
|
smoking
|
depression
|
-0.0405
|
0.0099
|
***
|
|
obesity
|
hearing_loss
|
-0.0398
|
0.0114
|
**
|
|
air_pollution
|
smoking
|
-0.0394
|
0.0122
|
**
|
|
obesity
|
physical_inactivity
|
0.0309
|
0.0490
|
**
|
|
hypertension
|
depression
|
0.0308
|
0.0498
|
**
|
|
social_isolation
|
physical_inactivity
|
0.0257
|
0.1021
|
ns
|
|
exc_alcohol
|
high_cholesterol
|
0.0249
|
0.1137
|
ns
|
|
depression
|
less_education
|
-0.0231
|
0.1419
|
ns
|
|
physical_inactivity
|
high_cholesterol
|
-0.0173
|
0.2722
|
ns
|
|
smoking
|
hearing_loss
|
-0.0152
|
0.3332
|
ns
|
|
social_isolation
|
depression
|
0.0125
|
0.4264
|
ns
|
|
exc_alcohol
|
obesity
|
-0.0114
|
0.4676
|
ns
|
|
exc_alcohol
|
hypertension
|
-0.0113
|
0.4711
|
ns
|
|
social_isolation
|
high_cholesterol
|
-0.0101
|
0.5194
|
ns
|
|
social_isolation
|
obesity
|
0.0100
|
0.5244
|
ns
|
|
vision_loss
|
depression
|
0.0050
|
0.7484
|
ns
|
|
diabetes
|
less_education
|
-0.0048
|
0.7598
|
ns
|
communalities
b<-as.data.frame(a$rho)
# cuantos componentes principales tienen eigenvalues>1?
eigenvalues <-eigen(b)$values
eigenvalues
## [1] 2.1002875 1.9398842 1.4519769 1.1807915 1.0790750 0.9488925 0.9386915
## [8] 0.7262624 0.6696871 0.5766141 0.5136771 0.4871799 0.3869802
##5!
eigenVectors<-eigen(b)$vectors
# I use the paper criteria for caculation:
#"Communality was calculated as the sum of the square of all factor loadings"
comunalities<-as.data.frame(eigenVectors)
comunalities$RF<-names(b)
comunalities$comunality<-comunalities$V1^2+comunalities$V2^2+comunalities$V3^2+
comunalities$V4^2+comunalities$V5^2
# limpio todo y nos vamos
comunalities<-comunalities %>% select(RF, comunality)
comunalities %>%
kable("html", digits = 4, caption = "Comunalities") %>%
kable_styling(full_width = TRUE, bootstrap_options = c("striped", "hover", "condensed"))
Comunalities
|
RF
|
comunality
|
|
less_education
|
0.3103
|
|
hearing_loss
|
0.2935
|
|
high_cholesterol
|
0.3528
|
|
depression
|
0.2418
|
|
physical_inactivity
|
0.5072
|
|
smoking
|
0.4858
|
|
diabetes
|
0.4234
|
|
hypertension
|
0.2779
|
|
obesity
|
0.3833
|
|
exc_alcohol
|
0.4861
|
|
social_isolation
|
0.5414
|
|
air_pollution
|
0.4215
|
|
vision_loss
|
0.2750
|
##females
Correlation
basecom<-elsi %>% filter(man==0) %>% select(less_education,
hearing_loss,
high_cholesterol,
depression,
physical_inactivity,
smoking,
diabetes,
hypertension,
obesity,
exc_alcohol,
social_isolation,
air_pollution,
vision_loss)
a<-tetrachoric(basecom)
## For i = 11 j = 10 A cell entry of 0 was replaced with correct = 0.5. Check your data!
rho_matrix <- a$rho
n <- nrow(basecom) # sample size
result <- data.frame() #output dataframe
#p-value loop
for (i in 1:ncol(rho_matrix)) {
for (j in 1:i) {
if (i != j) {
rho <- rho_matrix[i, j]
z <- rho * sqrt(n - 2) / sqrt(1 - rho^2) #z value for the correlation
p_value <- 2 * (1 - pnorm(abs(z))) #p from z
result <- rbind(result, data.frame(
Var1 = colnames(rho_matrix)[i],
Var2 = colnames(rho_matrix)[j],
Rho = rho,
P_Value = p_value,
Significance = case_when(
p_value < 0.01 ~ "***",
p_value < 0.05 ~ "**",
TRUE ~ "ns"
)
))
}
}
}
result %>%
arrange(desc(abs(Rho)))%>% #arrange by bigger correlation (absolute value)
kable("html", digits = 4, caption = " Tetrachoric correlation") %>%
kable_styling(full_width = TRUE, bootstrap_options = c("striped", "hover", "condensed"))
Tetrachoric correlation
|
Var1
|
Var2
|
Rho
|
P_Value
|
Significance
|
|
vision_loss
|
hearing_loss
|
0.4842
|
0.0000
|
***
|
|
exc_alcohol
|
smoking
|
0.4056
|
0.0000
|
***
|
|
hypertension
|
diabetes
|
0.3582
|
0.0000
|
***
|
|
diabetes
|
high_cholesterol
|
0.3135
|
0.0000
|
***
|
|
air_pollution
|
less_education
|
-0.2802
|
0.0000
|
***
|
|
hypertension
|
high_cholesterol
|
0.2711
|
0.0000
|
***
|
|
obesity
|
hypertension
|
0.2425
|
0.0000
|
***
|
|
hypertension
|
less_education
|
0.2251
|
0.0000
|
***
|
|
hearing_loss
|
less_education
|
0.2227
|
0.0000
|
***
|
|
social_isolation
|
hearing_loss
|
0.2096
|
0.0000
|
***
|
|
physical_inactivity
|
less_education
|
0.2001
|
0.0000
|
***
|
|
social_isolation
|
depression
|
-0.2000
|
0.0000
|
***
|
|
air_pollution
|
social_isolation
|
-0.1915
|
0.0000
|
***
|
|
exc_alcohol
|
diabetes
|
-0.1878
|
0.0000
|
***
|
|
exc_alcohol
|
less_education
|
-0.1871
|
0.0000
|
***
|
|
vision_loss
|
social_isolation
|
0.1839
|
0.0000
|
***
|
|
vision_loss
|
less_education
|
0.1828
|
0.0000
|
***
|
|
obesity
|
smoking
|
-0.1744
|
0.0000
|
***
|
|
social_isolation
|
less_education
|
0.1716
|
0.0000
|
***
|
|
air_pollution
|
hearing_loss
|
-0.1637
|
0.0000
|
***
|
|
exc_alcohol
|
hearing_loss
|
-0.1637
|
0.0000
|
***
|
|
vision_loss
|
physical_inactivity
|
0.1620
|
0.0000
|
***
|
|
vision_loss
|
diabetes
|
0.1546
|
0.0000
|
***
|
|
obesity
|
diabetes
|
0.1527
|
0.0000
|
***
|
|
physical_inactivity
|
hearing_loss
|
0.1515
|
0.0000
|
***
|
|
diabetes
|
physical_inactivity
|
0.1472
|
0.0000
|
***
|
|
air_pollution
|
physical_inactivity
|
0.1363
|
0.0000
|
***
|
|
air_pollution
|
diabetes
|
0.1348
|
0.0000
|
***
|
|
diabetes
|
depression
|
0.1314
|
0.0000
|
***
|
|
depression
|
high_cholesterol
|
0.1277
|
0.0000
|
***
|
|
vision_loss
|
exc_alcohol
|
-0.1273
|
0.0000
|
***
|
|
depression
|
hearing_loss
|
0.1251
|
0.0000
|
***
|
|
vision_loss
|
air_pollution
|
-0.1231
|
0.0000
|
***
|
|
diabetes
|
less_education
|
0.1180
|
0.0000
|
***
|
|
vision_loss
|
depression
|
0.1114
|
0.0000
|
***
|
|
smoking
|
hearing_loss
|
-0.1076
|
0.0000
|
***
|
|
vision_loss
|
high_cholesterol
|
0.1045
|
0.0000
|
***
|
|
social_isolation
|
physical_inactivity
|
0.1006
|
0.0000
|
***
|
|
hypertension
|
hearing_loss
|
0.1006
|
0.0000
|
***
|
|
air_pollution
|
depression
|
0.0960
|
0.0000
|
***
|
|
hypertension
|
smoking
|
-0.0843
|
0.0000
|
***
|
|
social_isolation
|
diabetes
|
0.0831
|
0.0000
|
***
|
|
diabetes
|
hearing_loss
|
0.0789
|
0.0000
|
***
|
|
exc_alcohol
|
hypertension
|
-0.0779
|
0.0000
|
***
|
|
exc_alcohol
|
high_cholesterol
|
-0.0777
|
0.0000
|
***
|
|
obesity
|
hearing_loss
|
-0.0765
|
0.0000
|
***
|
|
air_pollution
|
obesity
|
0.0761
|
0.0000
|
***
|
|
high_cholesterol
|
hearing_loss
|
0.0755
|
0.0000
|
***
|
|
vision_loss
|
hypertension
|
0.0737
|
0.0000
|
***
|
|
social_isolation
|
exc_alcohol
|
0.0733
|
0.0000
|
***
|
|
air_pollution
|
high_cholesterol
|
0.0720
|
0.0000
|
***
|
|
smoking
|
depression
|
0.0704
|
0.0000
|
***
|
|
hypertension
|
depression
|
0.0701
|
0.0000
|
***
|
|
obesity
|
high_cholesterol
|
0.0687
|
0.0000
|
***
|
|
hypertension
|
physical_inactivity
|
0.0684
|
0.0000
|
***
|
|
obesity
|
depression
|
0.0651
|
0.0000
|
***
|
|
smoking
|
high_cholesterol
|
-0.0624
|
0.0000
|
***
|
|
depression
|
less_education
|
0.0545
|
0.0000
|
***
|
|
air_pollution
|
exc_alcohol
|
0.0538
|
0.0000
|
***
|
|
exc_alcohol
|
depression
|
0.0520
|
0.0001
|
***
|
|
physical_inactivity
|
depression
|
0.0482
|
0.0002
|
***
|
|
vision_loss
|
obesity
|
-0.0403
|
0.0020
|
***
|
|
social_isolation
|
smoking
|
-0.0365
|
0.0051
|
***
|
|
exc_alcohol
|
obesity
|
0.0364
|
0.0051
|
***
|
|
social_isolation
|
hypertension
|
0.0355
|
0.0064
|
***
|
|
social_isolation
|
obesity
|
-0.0347
|
0.0077
|
***
|
|
exc_alcohol
|
physical_inactivity
|
-0.0297
|
0.0224
|
**
|
|
social_isolation
|
high_cholesterol
|
0.0266
|
0.0412
|
**
|
|
diabetes
|
smoking
|
-0.0263
|
0.0436
|
**
|
|
high_cholesterol
|
less_education
|
0.0259
|
0.0468
|
**
|
|
air_pollution
|
smoking
|
0.0228
|
0.0797
|
ns
|
|
obesity
|
physical_inactivity
|
0.0188
|
0.1479
|
ns
|
|
smoking
|
less_education
|
0.0186
|
0.1529
|
ns
|
|
vision_loss
|
smoking
|
0.0151
|
0.2475
|
ns
|
|
air_pollution
|
hypertension
|
-0.0136
|
0.2972
|
ns
|
|
obesity
|
less_education
|
-0.0102
|
0.4319
|
ns
|
|
physical_inactivity
|
high_cholesterol
|
0.0062
|
0.6346
|
ns
|
|
smoking
|
physical_inactivity
|
0.0028
|
0.8308
|
ns
|
communalities
b<-as.data.frame(a$rho)
# cuantos componentes principales tienen eigenvalues>1?
eigenvalues <-eigen(b)$values
eigenvalues
## [1] 2.2861943 1.7095427 1.3940665 1.1904649 1.0725138 1.0010667 0.9492799
## [8] 0.7085198 0.6594236 0.6149411 0.5454142 0.4654434 0.4031292
##5!
eigenVectors<-eigen(b)$vectors
# I use the paper criteria for caculation:
#"Communality was calculated as the sum of the square of all factor loadings"
comunalities<-as.data.frame(eigenVectors)
comunalities$RF<-names(b)
comunalities$comunality<-comunalities$V1^2+comunalities$V2^2+comunalities$V3^2+
comunalities$V4^2+comunalities$V5^2
# limpio todo y nos vamos
comunalities<-comunalities %>% select(RF, comunality)
comunalities %>%
kable("html", digits = 4, caption = "Comunalities") %>%
kable_styling(full_width = TRUE, bootstrap_options = c("striped", "hover", "condensed"))
Comunalities
|
RF
|
comunality
|
|
less_education
|
0.2276
|
|
hearing_loss
|
0.3296
|
|
high_cholesterol
|
0.2181
|
|
depression
|
0.5075
|
|
physical_inactivity
|
0.5375
|
|
smoking
|
0.4965
|
|
diabetes
|
0.2871
|
|
hypertension
|
0.3709
|
|
obesity
|
0.2493
|
|
exc_alcohol
|
0.4828
|
|
social_isolation
|
0.4438
|
|
air_pollution
|
0.5370
|
|
vision_loss
|
0.3123
|
##by rich or poor ##poor
Correlation
basecom<-elsi %>% filter(poor_regions==1) %>% select(less_education,
hearing_loss,
high_cholesterol,
depression,
physical_inactivity,
smoking,
diabetes,
hypertension,
obesity,
exc_alcohol,
social_isolation,
air_pollution,
vision_loss)
a<-tetrachoric(basecom)
rho_matrix <- a$rho
n <- nrow(basecom) # sample size
result <- data.frame() #output dataframe
#p-value loop
for (i in 1:ncol(rho_matrix)) {
for (j in 1:i) {
if (i != j) {
rho <- rho_matrix[i, j]
z <- rho * sqrt(n - 2) / sqrt(1 - rho^2) #z value for the correlation
p_value <- 2 * (1 - pnorm(abs(z))) #p from z
result <- rbind(result, data.frame(
Var1 = colnames(rho_matrix)[i],
Var2 = colnames(rho_matrix)[j],
Rho = rho,
P_Value = p_value,
Significance = case_when(
p_value < 0.01 ~ "***",
p_value < 0.05 ~ "**",
TRUE ~ "ns"
)
))
}
}
}
result %>%
arrange(desc(abs(Rho)))%>% #arrange by bigger correlation (absolute value)
kable("html", digits = 4, caption = " Tetrachoric correlation") %>%
kable_styling(full_width = TRUE, bootstrap_options = c("striped", "hover", "condensed"))
Tetrachoric correlation
|
Var1
|
Var2
|
Rho
|
P_Value
|
Significance
|
|
vision_loss
|
hearing_loss
|
0.4598
|
0.0000
|
***
|
|
diabetes
|
high_cholesterol
|
0.3421
|
0.0000
|
***
|
|
exc_alcohol
|
smoking
|
0.3301
|
0.0000
|
***
|
|
hypertension
|
high_cholesterol
|
0.2979
|
0.0000
|
***
|
|
air_pollution
|
less_education
|
-0.2684
|
0.0000
|
***
|
|
hypertension
|
diabetes
|
0.2658
|
0.0000
|
***
|
|
obesity
|
hypertension
|
0.2563
|
0.0000
|
***
|
|
hearing_loss
|
less_education
|
0.2290
|
0.0000
|
***
|
|
air_pollution
|
exc_alcohol
|
0.2081
|
0.0000
|
***
|
|
physical_inactivity
|
less_education
|
0.2016
|
0.0000
|
***
|
|
obesity
|
smoking
|
-0.1986
|
0.0000
|
***
|
|
diabetes
|
depression
|
0.1892
|
0.0000
|
***
|
|
exc_alcohol
|
less_education
|
-0.1852
|
0.0000
|
***
|
|
exc_alcohol
|
hearing_loss
|
-0.1843
|
0.0000
|
***
|
|
depression
|
high_cholesterol
|
0.1830
|
0.0000
|
***
|
|
physical_inactivity
|
hearing_loss
|
0.1793
|
0.0000
|
***
|
|
social_isolation
|
hearing_loss
|
0.1791
|
0.0000
|
***
|
|
air_pollution
|
depression
|
0.1766
|
0.0000
|
***
|
|
social_isolation
|
depression
|
-0.1751
|
0.0000
|
***
|
|
air_pollution
|
hearing_loss
|
-0.1725
|
0.0000
|
***
|
|
vision_loss
|
physical_inactivity
|
0.1724
|
0.0000
|
***
|
|
air_pollution
|
diabetes
|
0.1655
|
0.0000
|
***
|
|
vision_loss
|
less_education
|
0.1628
|
0.0000
|
***
|
|
obesity
|
less_education
|
-0.1610
|
0.0000
|
***
|
|
air_pollution
|
obesity
|
0.1542
|
0.0000
|
***
|
|
depression
|
hearing_loss
|
0.1530
|
0.0000
|
***
|
|
social_isolation
|
exc_alcohol
|
0.1491
|
0.0000
|
***
|
|
obesity
|
high_cholesterol
|
0.1491
|
0.0000
|
***
|
|
air_pollution
|
social_isolation
|
-0.1459
|
0.0000
|
***
|
|
obesity
|
diabetes
|
0.1452
|
0.0000
|
***
|
|
exc_alcohol
|
physical_inactivity
|
-0.1436
|
0.0000
|
***
|
|
air_pollution
|
physical_inactivity
|
0.1382
|
0.0000
|
***
|
|
vision_loss
|
social_isolation
|
0.1357
|
0.0000
|
***
|
|
vision_loss
|
depression
|
0.1302
|
0.0000
|
***
|
|
exc_alcohol
|
depression
|
-0.1297
|
0.0000
|
***
|
|
physical_inactivity
|
depression
|
0.1273
|
0.0000
|
***
|
|
hypertension
|
smoking
|
-0.1261
|
0.0000
|
***
|
|
smoking
|
high_cholesterol
|
-0.1252
|
0.0000
|
***
|
|
diabetes
|
physical_inactivity
|
0.1240
|
0.0000
|
***
|
|
exc_alcohol
|
hypertension
|
-0.1237
|
0.0000
|
***
|
|
exc_alcohol
|
diabetes
|
-0.1206
|
0.0000
|
***
|
|
social_isolation
|
obesity
|
-0.1172
|
0.0000
|
***
|
|
vision_loss
|
exc_alcohol
|
-0.1168
|
0.0000
|
***
|
|
smoking
|
less_education
|
0.1146
|
0.0000
|
***
|
|
social_isolation
|
less_education
|
0.1125
|
0.0000
|
***
|
|
hypertension
|
less_education
|
0.1091
|
0.0000
|
***
|
|
hypertension
|
physical_inactivity
|
0.1034
|
0.0000
|
***
|
|
depression
|
less_education
|
-0.0973
|
0.0000
|
***
|
|
high_cholesterol
|
less_education
|
-0.0932
|
0.0000
|
***
|
|
vision_loss
|
air_pollution
|
-0.0932
|
0.0000
|
***
|
|
vision_loss
|
diabetes
|
0.0911
|
0.0000
|
***
|
|
hypertension
|
hearing_loss
|
0.0865
|
0.0000
|
***
|
|
smoking
|
physical_inactivity
|
0.0851
|
0.0000
|
***
|
|
physical_inactivity
|
high_cholesterol
|
0.0837
|
0.0000
|
***
|
|
social_isolation
|
physical_inactivity
|
0.0792
|
0.0000
|
***
|
|
hypertension
|
depression
|
0.0789
|
0.0000
|
***
|
|
obesity
|
depression
|
0.0774
|
0.0000
|
***
|
|
obesity
|
hearing_loss
|
-0.0752
|
0.0000
|
***
|
|
vision_loss
|
hypertension
|
0.0715
|
0.0000
|
***
|
|
smoking
|
hearing_loss
|
-0.0700
|
0.0000
|
***
|
|
diabetes
|
smoking
|
-0.0655
|
0.0000
|
***
|
|
social_isolation
|
diabetes
|
-0.0584
|
0.0001
|
***
|
|
social_isolation
|
smoking
|
0.0574
|
0.0001
|
***
|
|
air_pollution
|
hypertension
|
0.0568
|
0.0001
|
***
|
|
exc_alcohol
|
high_cholesterol
|
-0.0557
|
0.0002
|
***
|
|
diabetes
|
hearing_loss
|
0.0550
|
0.0002
|
***
|
|
exc_alcohol
|
obesity
|
-0.0545
|
0.0002
|
***
|
|
air_pollution
|
high_cholesterol
|
0.0536
|
0.0003
|
***
|
|
vision_loss
|
obesity
|
-0.0475
|
0.0014
|
***
|
|
air_pollution
|
smoking
|
-0.0425
|
0.0043
|
***
|
|
vision_loss
|
high_cholesterol
|
0.0346
|
0.0204
|
**
|
|
social_isolation
|
hypertension
|
0.0260
|
0.0806
|
ns
|
|
smoking
|
depression
|
-0.0246
|
0.0994
|
ns
|
|
high_cholesterol
|
hearing_loss
|
0.0162
|
0.2785
|
ns
|
|
obesity
|
physical_inactivity
|
0.0148
|
0.3209
|
ns
|
|
social_isolation
|
high_cholesterol
|
-0.0141
|
0.3455
|
ns
|
|
vision_loss
|
smoking
|
-0.0119
|
0.4231
|
ns
|
|
diabetes
|
less_education
|
-0.0099
|
0.5051
|
ns
|
communalities
b<-as.data.frame(a$rho)
# cuantos componentes principales tienen eigenvalues>1?
eigenvalues <-eigen(b)$values
eigenvalues
## [1] 2.1715620 1.9959325 1.3460947 1.1796671 1.0491090 0.9469046 0.8201708
## [8] 0.7196142 0.6700958 0.6265794 0.5440637 0.5106334 0.4195729
##5!
eigenVectors<-eigen(b)$vectors
# I use the paper criteria for caculation:
#"Communality was calculated as the sum of the square of all factor loadings"
comunalities<-as.data.frame(eigenVectors)
comunalities$RF<-names(b)
comunalities$comunality<-comunalities$V1^2+comunalities$V2^2+comunalities$V3^2+
comunalities$V4^2+comunalities$V5^2
# limpio todo y nos vamos
comunalities<-comunalities %>% select(RF, comunality)
comunalities %>%
kable("html", digits = 4, caption = "Comunalities") %>%
kable_styling(full_width = TRUE, bootstrap_options = c("striped", "hover", "condensed"))
Comunalities
|
RF
|
comunality
|
|
less_education
|
0.4795
|
|
hearing_loss
|
0.4011
|
|
high_cholesterol
|
0.2656
|
|
depression
|
0.4112
|
|
physical_inactivity
|
0.3679
|
|
smoking
|
0.4682
|
|
diabetes
|
0.2475
|
|
hypertension
|
0.3779
|
|
obesity
|
0.2165
|
|
exc_alcohol
|
0.5000
|
|
social_isolation
|
0.5130
|
|
air_pollution
|
0.3518
|
|
vision_loss
|
0.3999
|
##rich
Correlation
basecom<-elsi %>% filter(poor_regions==0) %>% select(less_education,
hearing_loss,
high_cholesterol,
depression,
physical_inactivity,
smoking,
diabetes,
hypertension,
obesity,
exc_alcohol,
social_isolation,
air_pollution,
vision_loss)
a<-tetrachoric(basecom)
rho_matrix <- a$rho
n <- nrow(basecom) # sample size
result <- data.frame() #output dataframe
#p-value loop
for (i in 1:ncol(rho_matrix)) {
for (j in 1:i) {
if (i != j) {
rho <- rho_matrix[i, j]
z <- rho * sqrt(n - 2) / sqrt(1 - rho^2) #z value for the correlation
p_value <- 2 * (1 - pnorm(abs(z))) #p from z
result <- rbind(result, data.frame(
Var1 = colnames(rho_matrix)[i],
Var2 = colnames(rho_matrix)[j],
Rho = rho,
P_Value = p_value,
Significance = case_when(
p_value < 0.01 ~ "***",
p_value < 0.05 ~ "**",
TRUE ~ "ns"
)
))
}
}
}
result %>%
arrange(desc(abs(Rho)))%>% #arrange by bigger correlation (absolute value)
kable("html", digits = 4, caption = " Tetrachoric correlation") %>%
kable_styling(full_width = TRUE, bootstrap_options = c("striped", "hover", "condensed"))
Tetrachoric correlation
|
Var1
|
Var2
|
Rho
|
P_Value
|
Significance
|
|
air_pollution
|
less_education
|
-0.5197
|
0.0000
|
***
|
|
vision_loss
|
hearing_loss
|
0.4932
|
0.0000
|
***
|
|
exc_alcohol
|
smoking
|
0.4104
|
0.0000
|
***
|
|
hypertension
|
diabetes
|
0.3766
|
0.0000
|
***
|
|
diabetes
|
high_cholesterol
|
0.3527
|
0.0000
|
***
|
|
hypertension
|
high_cholesterol
|
0.2816
|
0.0000
|
***
|
|
vision_loss
|
less_education
|
0.2462
|
0.0000
|
***
|
|
obesity
|
hypertension
|
0.2451
|
0.0000
|
***
|
|
obesity
|
smoking
|
-0.2447
|
0.0000
|
***
|
|
social_isolation
|
obesity
|
0.2155
|
0.0000
|
***
|
|
hypertension
|
less_education
|
0.2018
|
0.0000
|
***
|
|
hearing_loss
|
less_education
|
0.1980
|
0.0000
|
***
|
|
vision_loss
|
air_pollution
|
-0.1895
|
0.0000
|
***
|
|
air_pollution
|
high_cholesterol
|
0.1822
|
0.0000
|
***
|
|
obesity
|
diabetes
|
0.1820
|
0.0000
|
***
|
|
exc_alcohol
|
less_education
|
-0.1798
|
0.0000
|
***
|
|
air_pollution
|
exc_alcohol
|
0.1696
|
0.0000
|
***
|
|
vision_loss
|
physical_inactivity
|
0.1589
|
0.0000
|
***
|
|
physical_inactivity
|
less_education
|
0.1555
|
0.0000
|
***
|
|
depression
|
high_cholesterol
|
0.1486
|
0.0000
|
***
|
|
physical_inactivity
|
hearing_loss
|
0.1394
|
0.0000
|
***
|
|
air_pollution
|
physical_inactivity
|
0.1390
|
0.0000
|
***
|
|
vision_loss
|
diabetes
|
0.1360
|
0.0000
|
***
|
|
diabetes
|
less_education
|
0.1356
|
0.0000
|
***
|
|
social_isolation
|
hearing_loss
|
0.1346
|
0.0000
|
***
|
|
vision_loss
|
high_cholesterol
|
0.1273
|
0.0000
|
***
|
|
air_pollution
|
diabetes
|
0.1215
|
0.0000
|
***
|
|
social_isolation
|
exc_alcohol
|
0.1190
|
0.0000
|
***
|
|
depression
|
less_education
|
0.1169
|
0.0000
|
***
|
|
social_isolation
|
less_education
|
0.1154
|
0.0000
|
***
|
|
high_cholesterol
|
hearing_loss
|
0.1139
|
0.0000
|
***
|
|
social_isolation
|
hypertension
|
0.1114
|
0.0000
|
***
|
|
vision_loss
|
social_isolation
|
0.1088
|
0.0000
|
***
|
|
diabetes
|
physical_inactivity
|
0.1007
|
0.0000
|
***
|
|
social_isolation
|
diabetes
|
0.0997
|
0.0000
|
***
|
|
exc_alcohol
|
depression
|
-0.0987
|
0.0000
|
***
|
|
depression
|
hearing_loss
|
0.0962
|
0.0000
|
***
|
|
obesity
|
high_cholesterol
|
0.0954
|
0.0000
|
***
|
|
obesity
|
depression
|
0.0951
|
0.0000
|
***
|
|
exc_alcohol
|
high_cholesterol
|
-0.0909
|
0.0000
|
***
|
|
air_pollution
|
social_isolation
|
-0.0905
|
0.0000
|
***
|
|
vision_loss
|
depression
|
0.0902
|
0.0000
|
***
|
|
exc_alcohol
|
diabetes
|
-0.0895
|
0.0000
|
***
|
|
hypertension
|
hearing_loss
|
0.0895
|
0.0000
|
***
|
|
air_pollution
|
hearing_loss
|
-0.0859
|
0.0000
|
***
|
|
diabetes
|
depression
|
0.0855
|
0.0000
|
***
|
|
hypertension
|
smoking
|
-0.0821
|
0.0000
|
***
|
|
vision_loss
|
hypertension
|
0.0808
|
0.0000
|
***
|
|
hypertension
|
depression
|
0.0806
|
0.0000
|
***
|
|
vision_loss
|
smoking
|
0.0783
|
0.0000
|
***
|
|
diabetes
|
hearing_loss
|
0.0740
|
0.0000
|
***
|
|
exc_alcohol
|
obesity
|
-0.0722
|
0.0000
|
***
|
|
high_cholesterol
|
less_education
|
0.0682
|
0.0000
|
***
|
|
social_isolation
|
physical_inactivity
|
0.0612
|
0.0000
|
***
|
|
smoking
|
high_cholesterol
|
-0.0604
|
0.0000
|
***
|
|
air_pollution
|
depression
|
-0.0601
|
0.0000
|
***
|
|
air_pollution
|
obesity
|
-0.0595
|
0.0000
|
***
|
|
physical_inactivity
|
high_cholesterol
|
-0.0585
|
0.0000
|
***
|
|
social_isolation
|
smoking
|
0.0500
|
0.0002
|
***
|
|
vision_loss
|
exc_alcohol
|
-0.0470
|
0.0005
|
***
|
|
obesity
|
hearing_loss
|
-0.0460
|
0.0007
|
***
|
|
hypertension
|
physical_inactivity
|
0.0458
|
0.0007
|
***
|
|
smoking
|
hearing_loss
|
-0.0442
|
0.0011
|
***
|
|
obesity
|
physical_inactivity
|
0.0412
|
0.0023
|
***
|
|
obesity
|
less_education
|
0.0376
|
0.0055
|
***
|
|
air_pollution
|
smoking
|
0.0369
|
0.0064
|
***
|
|
air_pollution
|
hypertension
|
-0.0245
|
0.0701
|
ns
|
|
physical_inactivity
|
depression
|
0.0235
|
0.0821
|
ns
|
|
vision_loss
|
obesity
|
-0.0230
|
0.0899
|
ns
|
|
diabetes
|
smoking
|
-0.0215
|
0.1131
|
ns
|
|
smoking
|
physical_inactivity
|
-0.0213
|
0.1164
|
ns
|
|
smoking
|
depression
|
0.0210
|
0.1205
|
ns
|
|
smoking
|
less_education
|
0.0159
|
0.2412
|
ns
|
|
exc_alcohol
|
physical_inactivity
|
0.0114
|
0.3985
|
ns
|
|
exc_alcohol
|
hypertension
|
0.0081
|
0.5479
|
ns
|
|
social_isolation
|
high_cholesterol
|
-0.0078
|
0.5642
|
ns
|
|
social_isolation
|
depression
|
0.0046
|
0.7337
|
ns
|
|
exc_alcohol
|
hearing_loss
|
-0.0022
|
0.8739
|
ns
|
communalities
b<-as.data.frame(a$rho)
# cuantos componentes principales tienen eigenvalues>1?
eigenvalues <-eigen(b)$values
eigenvalues
## [1] 2.2949579 1.6842555 1.5405150 1.2418292 1.1304905 0.9568796 0.9461203
## [8] 0.7250216 0.6326415 0.5968507 0.4932391 0.4322442 0.3249548
##5!
eigenVectors<-eigen(b)$vectors
# I use the paper criteria for caculation:
#"Communality was calculated as the sum of the square of all factor loadings"
comunalities<-as.data.frame(eigenVectors)
comunalities$RF<-names(b)
comunalities$comunality<-comunalities$V1^2+comunalities$V2^2+comunalities$V3^2+
comunalities$V4^2+comunalities$V5^2
# limpio todo y nos vamos
comunalities<-comunalities %>% select(RF, comunality)
comunalities %>%
kable("html", digits = 4, caption = "Comunalities") %>%
kable_styling(full_width = TRUE, bootstrap_options = c("striped", "hover", "condensed"))
Comunalities
|
RF
|
comunality
|
|
less_education
|
0.3455
|
|
hearing_loss
|
0.3700
|
|
high_cholesterol
|
0.4023
|
|
depression
|
0.1446
|
|
physical_inactivity
|
0.4293
|
|
smoking
|
0.5168
|
|
diabetes
|
0.2959
|
|
hypertension
|
0.2949
|
|
obesity
|
0.4249
|
|
exc_alcohol
|
0.4627
|
|
social_isolation
|
0.4300
|
|
air_pollution
|
0.5217
|
|
vision_loss
|
0.3614
|
##by race ##white
Correlation
basecom<-elsi %>% filter(race_2==1) %>% select(less_education,
hearing_loss,
high_cholesterol,
depression,
physical_inactivity,
smoking,
diabetes,
hypertension,
obesity,
exc_alcohol,
social_isolation,
air_pollution,
vision_loss)
a<-tetrachoric(basecom)
rho_matrix <- a$rho
n <- nrow(basecom) # sample size
result <- data.frame() #output dataframe
#p-value loop
for (i in 1:ncol(rho_matrix)) {
for (j in 1:i) {
if (i != j) {
rho <- rho_matrix[i, j]
z <- rho * sqrt(n - 2) / sqrt(1 - rho^2) #z value for the correlation
p_value <- 2 * (1 - pnorm(abs(z))) #p from z
result <- rbind(result, data.frame(
Var1 = colnames(rho_matrix)[i],
Var2 = colnames(rho_matrix)[j],
Rho = rho,
P_Value = p_value,
Significance = case_when(
p_value < 0.01 ~ "***",
p_value < 0.05 ~ "**",
TRUE ~ "ns"
)
))
}
}
}
result %>%
arrange(desc(abs(Rho)))%>% #arrange by bigger correlation (absolute value)
kable("html", digits = 4, caption = " Tetrachoric correlation") %>%
kable_styling(full_width = TRUE, bootstrap_options = c("striped", "hover", "condensed"))
Tetrachoric correlation
|
Var1
|
Var2
|
Rho
|
P_Value
|
Significance
|
|
vision_loss
|
hearing_loss
|
0.5198
|
0.0000
|
***
|
|
air_pollution
|
less_education
|
-0.4895
|
0.0000
|
***
|
|
diabetes
|
high_cholesterol
|
0.3425
|
0.0000
|
***
|
|
hypertension
|
diabetes
|
0.3362
|
0.0000
|
***
|
|
air_pollution
|
exc_alcohol
|
0.3309
|
0.0000
|
***
|
|
exc_alcohol
|
smoking
|
0.3282
|
0.0000
|
***
|
|
hypertension
|
high_cholesterol
|
0.2493
|
0.0000
|
***
|
|
obesity
|
hypertension
|
0.2492
|
0.0000
|
***
|
|
vision_loss
|
less_education
|
0.2346
|
0.0000
|
***
|
|
social_isolation
|
hearing_loss
|
0.2270
|
0.0000
|
***
|
|
obesity
|
smoking
|
-0.2269
|
0.0000
|
***
|
|
hearing_loss
|
less_education
|
0.2180
|
0.0000
|
***
|
|
social_isolation
|
exc_alcohol
|
0.2081
|
0.0000
|
***
|
|
air_pollution
|
social_isolation
|
-0.1962
|
0.0000
|
***
|
|
air_pollution
|
physical_inactivity
|
0.1938
|
0.0000
|
***
|
|
vision_loss
|
air_pollution
|
-0.1830
|
0.0000
|
***
|
|
exc_alcohol
|
less_education
|
-0.1745
|
0.0000
|
***
|
|
hypertension
|
less_education
|
0.1733
|
0.0000
|
***
|
|
physical_inactivity
|
hearing_loss
|
0.1629
|
0.0000
|
***
|
|
air_pollution
|
high_cholesterol
|
0.1588
|
0.0000
|
***
|
|
physical_inactivity
|
less_education
|
0.1551
|
0.0000
|
***
|
|
depression
|
high_cholesterol
|
0.1429
|
0.0000
|
***
|
|
air_pollution
|
hearing_loss
|
-0.1424
|
0.0000
|
***
|
|
air_pollution
|
diabetes
|
0.1421
|
0.0000
|
***
|
|
exc_alcohol
|
depression
|
-0.1398
|
0.0000
|
***
|
|
obesity
|
diabetes
|
0.1393
|
0.0000
|
***
|
|
diabetes
|
depression
|
0.1340
|
0.0000
|
***
|
|
vision_loss
|
high_cholesterol
|
0.1338
|
0.0000
|
***
|
|
vision_loss
|
diabetes
|
0.1302
|
0.0000
|
***
|
|
obesity
|
depression
|
0.1272
|
0.0000
|
***
|
|
vision_loss
|
hypertension
|
0.1255
|
0.0000
|
***
|
|
high_cholesterol
|
hearing_loss
|
0.1250
|
0.0000
|
***
|
|
diabetes
|
physical_inactivity
|
0.1231
|
0.0000
|
***
|
|
depression
|
hearing_loss
|
0.1211
|
0.0000
|
***
|
|
vision_loss
|
physical_inactivity
|
0.1151
|
0.0000
|
***
|
|
hypertension
|
depression
|
0.1110
|
0.0000
|
***
|
|
hypertension
|
hearing_loss
|
0.1101
|
0.0000
|
***
|
|
exc_alcohol
|
physical_inactivity
|
-0.0947
|
0.0000
|
***
|
|
depression
|
less_education
|
0.0906
|
0.0000
|
***
|
|
obesity
|
high_cholesterol
|
0.0902
|
0.0000
|
***
|
|
diabetes
|
less_education
|
0.0855
|
0.0000
|
***
|
|
hypertension
|
physical_inactivity
|
0.0844
|
0.0000
|
***
|
|
vision_loss
|
depression
|
0.0841
|
0.0000
|
***
|
|
hypertension
|
smoking
|
-0.0820
|
0.0000
|
***
|
|
obesity
|
hearing_loss
|
-0.0797
|
0.0000
|
***
|
|
vision_loss
|
exc_alcohol
|
-0.0792
|
0.0000
|
***
|
|
exc_alcohol
|
hearing_loss
|
-0.0782
|
0.0000
|
***
|
|
smoking
|
high_cholesterol
|
-0.0723
|
0.0000
|
***
|
|
social_isolation
|
diabetes
|
-0.0686
|
0.0000
|
***
|
|
social_isolation
|
less_education
|
0.0671
|
0.0000
|
***
|
|
exc_alcohol
|
high_cholesterol
|
-0.0671
|
0.0000
|
***
|
|
social_isolation
|
obesity
|
0.0666
|
0.0000
|
***
|
|
vision_loss
|
obesity
|
-0.0610
|
0.0000
|
***
|
|
diabetes
|
hearing_loss
|
0.0609
|
0.0000
|
***
|
|
smoking
|
depression
|
-0.0558
|
0.0002
|
***
|
|
exc_alcohol
|
diabetes
|
-0.0530
|
0.0003
|
***
|
|
social_isolation
|
smoking
|
-0.0522
|
0.0004
|
***
|
|
diabetes
|
smoking
|
-0.0509
|
0.0006
|
***
|
|
social_isolation
|
depression
|
-0.0508
|
0.0006
|
***
|
|
physical_inactivity
|
depression
|
0.0475
|
0.0013
|
***
|
|
smoking
|
hearing_loss
|
-0.0448
|
0.0024
|
***
|
|
obesity
|
physical_inactivity
|
0.0442
|
0.0027
|
***
|
|
vision_loss
|
smoking
|
0.0430
|
0.0036
|
***
|
|
vision_loss
|
social_isolation
|
0.0421
|
0.0043
|
***
|
|
high_cholesterol
|
less_education
|
0.0351
|
0.0174
|
**
|
|
exc_alcohol
|
hypertension
|
-0.0250
|
0.0903
|
ns
|
|
air_pollution
|
smoking
|
0.0250
|
0.0904
|
ns
|
|
air_pollution
|
hypertension
|
-0.0239
|
0.1055
|
ns
|
|
social_isolation
|
hypertension
|
0.0235
|
0.1117
|
ns
|
|
social_isolation
|
physical_inactivity
|
0.0214
|
0.1471
|
ns
|
|
physical_inactivity
|
high_cholesterol
|
-0.0178
|
0.2279
|
ns
|
|
smoking
|
physical_inactivity
|
0.0164
|
0.2655
|
ns
|
|
exc_alcohol
|
obesity
|
0.0152
|
0.3036
|
ns
|
|
air_pollution
|
obesity
|
0.0071
|
0.6327
|
ns
|
|
smoking
|
less_education
|
0.0069
|
0.6424
|
ns
|
|
obesity
|
less_education
|
0.0035
|
0.8146
|
ns
|
|
air_pollution
|
depression
|
0.0026
|
0.8617
|
ns
|
|
social_isolation
|
high_cholesterol
|
-0.0018
|
0.9049
|
ns
|
communalities
b<-as.data.frame(a$rho)
# cuantos componentes principales tienen eigenvalues>1?
eigenvalues <-eigen(b)$values
eigenvalues
## [1] 2.2711787 1.7884856 1.4596122 1.2350918 1.0721664 1.0286244 0.9150664
## [8] 0.7642354 0.6315420 0.6075716 0.5422593 0.4245568 0.2596095
##5!
eigenVectors<-eigen(b)$vectors
# I use the paper criteria for caculation:
#"Communality was calculated as the sum of the square of all factor loadings"
comunalities<-as.data.frame(eigenVectors)
comunalities$RF<-names(b)
comunalities$comunality<-comunalities$V1^2+comunalities$V2^2+comunalities$V3^2+
comunalities$V4^2+comunalities$V5^2
# limpio todo y nos vamos
comunalities<-comunalities %>% select(RF, comunality)
comunalities %>%
kable("html", digits = 4, caption = "Comunalities") %>%
kable_styling(full_width = TRUE, bootstrap_options = c("striped", "hover", "condensed"))
Comunalities
|
RF
|
comunality
|
|
less_education
|
0.3079
|
|
hearing_loss
|
0.3970
|
|
high_cholesterol
|
0.2610
|
|
depression
|
0.1103
|
|
physical_inactivity
|
0.4196
|
|
smoking
|
0.5209
|
|
diabetes
|
0.3209
|
|
hypertension
|
0.3195
|
|
obesity
|
0.4103
|
|
exc_alcohol
|
0.5203
|
|
social_isolation
|
0.5734
|
|
air_pollution
|
0.5180
|
|
vision_loss
|
0.3209
|
##black
Correlation
basecom<-elsi %>% filter(race_2==2) %>% select(less_education,
hearing_loss,
high_cholesterol,
depression,
physical_inactivity,
smoking,
diabetes,
hypertension,
obesity,
exc_alcohol,
social_isolation,
air_pollution,
vision_loss)
a<-tetrachoric(basecom)
## For i = 10 j = 4 A cell entry of 0 was replaced with correct = 0.5. Check your data!
## For i = 11 j = 1 A cell entry of 0 was replaced with correct = 0.5. Check your data!
## For i = 11 j = 4 A cell entry of 0 was replaced with correct = 0.5. Check your data!
## For i = 13 j = 11 A cell entry of 0 was replaced with correct = 0.5. Check your data!
rho_matrix <- a$rho
n <- nrow(basecom) # sample size
result <- data.frame() #output dataframe
#p-value loop
for (i in 1:ncol(rho_matrix)) {
for (j in 1:i) {
if (i != j) {
rho <- rho_matrix[i, j]
z <- rho * sqrt(n - 2) / sqrt(1 - rho^2) #z value for the correlation
p_value <- 2 * (1 - pnorm(abs(z))) #p from z
result <- rbind(result, data.frame(
Var1 = colnames(rho_matrix)[i],
Var2 = colnames(rho_matrix)[j],
Rho = rho,
P_Value = p_value,
Significance = case_when(
p_value < 0.01 ~ "***",
p_value < 0.05 ~ "**",
TRUE ~ "ns"
)
))
}
}
}
result %>%
arrange(desc(abs(Rho)))%>% #arrange by bigger correlation (absolute value)
kable("html", digits = 4, caption = " Tetrachoric correlation") %>%
kable_styling(full_width = TRUE, bootstrap_options = c("striped", "hover", "condensed"))
Tetrachoric correlation
|
Var1
|
Var2
|
Rho
|
P_Value
|
Significance
|
|
vision_loss
|
social_isolation
|
0.5086
|
0.0000
|
***
|
|
diabetes
|
high_cholesterol
|
0.4358
|
0.0000
|
***
|
|
vision_loss
|
hearing_loss
|
0.4270
|
0.0000
|
***
|
|
exc_alcohol
|
smoking
|
0.3907
|
0.0000
|
***
|
|
exc_alcohol
|
less_education
|
-0.3652
|
0.0000
|
***
|
|
hypertension
|
diabetes
|
0.3327
|
0.0000
|
***
|
|
exc_alcohol
|
depression
|
-0.3228
|
0.0000
|
***
|
|
air_pollution
|
physical_inactivity
|
0.3178
|
0.0000
|
***
|
|
air_pollution
|
less_education
|
-0.2918
|
0.0000
|
***
|
|
obesity
|
hypertension
|
0.2763
|
0.0000
|
***
|
|
social_isolation
|
less_education
|
0.2661
|
0.0000
|
***
|
|
hypertension
|
high_cholesterol
|
0.2599
|
0.0000
|
***
|
|
obesity
|
smoking
|
-0.2508
|
0.0000
|
***
|
|
depression
|
high_cholesterol
|
0.2440
|
0.0000
|
***
|
|
social_isolation
|
exc_alcohol
|
0.2354
|
0.0000
|
***
|
|
obesity
|
high_cholesterol
|
0.2249
|
0.0000
|
***
|
|
obesity
|
diabetes
|
0.2164
|
0.0000
|
***
|
|
social_isolation
|
hearing_loss
|
0.1864
|
0.0000
|
***
|
|
social_isolation
|
smoking
|
0.1763
|
0.0000
|
***
|
|
vision_loss
|
less_education
|
0.1709
|
0.0000
|
***
|
|
social_isolation
|
diabetes
|
-0.1656
|
0.0000
|
***
|
|
obesity
|
less_education
|
-0.1643
|
0.0000
|
***
|
|
hypertension
|
less_education
|
0.1612
|
0.0000
|
***
|
|
diabetes
|
physical_inactivity
|
0.1551
|
0.0000
|
***
|
|
vision_loss
|
high_cholesterol
|
0.1452
|
0.0000
|
***
|
|
air_pollution
|
diabetes
|
0.1447
|
0.0000
|
***
|
|
depression
|
hearing_loss
|
0.1421
|
0.0000
|
***
|
|
air_pollution
|
social_isolation
|
-0.1413
|
0.0000
|
***
|
|
air_pollution
|
high_cholesterol
|
0.1396
|
0.0000
|
***
|
|
social_isolation
|
obesity
|
-0.1314
|
0.0000
|
***
|
|
smoking
|
high_cholesterol
|
-0.1293
|
0.0000
|
***
|
|
diabetes
|
depression
|
0.1292
|
0.0000
|
***
|
|
social_isolation
|
depression
|
-0.1266
|
0.0000
|
***
|
|
physical_inactivity
|
less_education
|
0.1247
|
0.0000
|
***
|
|
exc_alcohol
|
hypertension
|
-0.1247
|
0.0000
|
***
|
|
hypertension
|
physical_inactivity
|
0.1190
|
0.0001
|
***
|
|
air_pollution
|
depression
|
0.1186
|
0.0001
|
***
|
|
hypertension
|
smoking
|
-0.1140
|
0.0002
|
***
|
|
diabetes
|
less_education
|
0.1138
|
0.0002
|
***
|
|
vision_loss
|
physical_inactivity
|
0.1066
|
0.0005
|
***
|
|
obesity
|
hearing_loss
|
-0.1031
|
0.0008
|
***
|
|
air_pollution
|
exc_alcohol
|
0.1027
|
0.0008
|
***
|
|
exc_alcohol
|
hearing_loss
|
-0.1009
|
0.0010
|
***
|
|
physical_inactivity
|
depression
|
0.0987
|
0.0013
|
***
|
|
air_pollution
|
smoking
|
-0.0971
|
0.0016
|
***
|
|
depression
|
less_education
|
0.0928
|
0.0025
|
***
|
|
physical_inactivity
|
hearing_loss
|
0.0906
|
0.0032
|
***
|
|
social_isolation
|
physical_inactivity
|
-0.0888
|
0.0038
|
***
|
|
vision_loss
|
diabetes
|
0.0877
|
0.0043
|
***
|
|
air_pollution
|
obesity
|
0.0874
|
0.0045
|
***
|
|
hearing_loss
|
less_education
|
0.0856
|
0.0054
|
***
|
|
physical_inactivity
|
high_cholesterol
|
0.0851
|
0.0057
|
***
|
|
smoking
|
hearing_loss
|
-0.0851
|
0.0057
|
***
|
|
hypertension
|
depression
|
0.0836
|
0.0065
|
***
|
|
vision_loss
|
air_pollution
|
-0.0770
|
0.0124
|
**
|
|
exc_alcohol
|
diabetes
|
-0.0757
|
0.0139
|
**
|
|
vision_loss
|
obesity
|
0.0747
|
0.0152
|
**
|
|
vision_loss
|
depression
|
0.0723
|
0.0188
|
**
|
|
smoking
|
depression
|
0.0706
|
0.0218
|
**
|
|
air_pollution
|
hearing_loss
|
-0.0655
|
0.0333
|
**
|
|
exc_alcohol
|
obesity
|
-0.0655
|
0.0335
|
**
|
|
vision_loss
|
hypertension
|
0.0592
|
0.0548
|
ns
|
|
exc_alcohol
|
high_cholesterol
|
-0.0586
|
0.0570
|
ns
|
|
obesity
|
physical_inactivity
|
0.0538
|
0.0809
|
ns
|
|
diabetes
|
smoking
|
-0.0533
|
0.0839
|
ns
|
|
hypertension
|
hearing_loss
|
0.0532
|
0.0843
|
ns
|
|
smoking
|
physical_inactivity
|
-0.0529
|
0.0860
|
ns
|
|
exc_alcohol
|
physical_inactivity
|
-0.0503
|
0.1029
|
ns
|
|
high_cholesterol
|
less_education
|
-0.0420
|
0.1733
|
ns
|
|
smoking
|
less_education
|
0.0400
|
0.1945
|
ns
|
|
social_isolation
|
high_cholesterol
|
-0.0384
|
0.2129
|
ns
|
|
air_pollution
|
hypertension
|
0.0357
|
0.2467
|
ns
|
|
high_cholesterol
|
hearing_loss
|
0.0258
|
0.4032
|
ns
|
|
vision_loss
|
smoking
|
-0.0249
|
0.4192
|
ns
|
|
obesity
|
depression
|
0.0217
|
0.4817
|
ns
|
|
diabetes
|
hearing_loss
|
0.0188
|
0.5422
|
ns
|
|
social_isolation
|
hypertension
|
0.0175
|
0.5709
|
ns
|
|
vision_loss
|
exc_alcohol
|
-0.0037
|
0.9053
|
ns
|
communalities
b<-as.data.frame(a$rho)
# cuantos componentes principales tienen eigenvalues>1?
eigenvalues <-eigen(b)$values
eigenvalues
## [1] 2.3211050 1.9973514 1.5202020 1.2515780 1.1881592 1.0272203 0.7680002
## [8] 0.7316772 0.6655096 0.5387383 0.3824100 0.3458471 0.2622019
##5!
eigenVectors<-eigen(b)$vectors
# I use the paper criteria for caculation:
#"Communality was calculated as the sum of the square of all factor loadings"
comunalities<-as.data.frame(eigenVectors)
comunalities$RF<-names(b)
comunalities$comunality<-comunalities$V1^2+comunalities$V2^2+comunalities$V3^2+
comunalities$V4^2+comunalities$V5^2
# limpio todo y nos vamos
comunalities<-comunalities %>% select(RF, comunality)
comunalities %>%
kable("html", digits = 4, caption = "Comunalities") %>%
kable_styling(full_width = TRUE, bootstrap_options = c("striped", "hover", "condensed"))
Comunalities
|
RF
|
comunality
|
|
less_education
|
0.4244
|
|
hearing_loss
|
0.3628
|
|
high_cholesterol
|
0.2660
|
|
depression
|
0.3380
|
|
physical_inactivity
|
0.3105
|
|
smoking
|
0.5608
|
|
diabetes
|
0.3340
|
|
hypertension
|
0.2847
|
|
obesity
|
0.3675
|
|
exc_alcohol
|
0.4981
|
|
social_isolation
|
0.3857
|
|
air_pollution
|
0.4406
|
|
vision_loss
|
0.4268
|