Confiabilidad interna= alfa de Cronbach y Alfa estandarizado

library(psych)
library(dplyr)

# Seleccionar ítems de la escala
items <- datosn %>% select(starts_with("P"))

# Alfa de Cronbach
alpha_global <- psych::alpha(items)
## Warning in response.frequencies(x, max = max): response.frequency has been
## deprecated and replaced with responseFrequecy.  Please fix your call
alpha_global
## 
## Reliability analysis   
## Call: psych::alpha(x = items)
## 
##   raw_alpha std.alpha G6(smc) average_r S/N  ase mean  sd median_r
##       0.85      0.85    0.92      0.22 5.6 0.03  3.1 0.6     0.23
## 
##     95% confidence boundaries 
##          lower alpha upper
## Feldt     0.78  0.85  0.91
## Duhachek  0.80  0.85  0.91
## 
##  Reliability if an item is dropped:
##     raw_alpha std.alpha G6(smc) average_r S/N alpha se var.r med.r
## P1       0.85      0.85    0.91      0.22 5.5    0.031 0.034  0.23
## P2       0.86      0.86    0.92      0.24 6.1    0.028 0.029  0.25
## P3       0.86      0.85    0.92      0.23 5.8    0.030 0.033  0.24
## P4       0.85      0.85    0.91      0.22 5.5    0.031 0.032  0.22
## P5       0.85      0.85    0.92      0.23 5.7    0.030 0.033  0.24
## P6       0.84      0.83    0.91      0.21 5.0    0.033 0.034  0.22
## P7       0.85      0.85    0.92      0.23 5.7    0.030 0.034  0.24
## P8       0.85      0.85    0.92      0.22 5.5    0.031 0.033  0.23
## P9       0.84      0.83    0.91      0.21 5.0    0.033 0.033  0.22
## P10      0.85      0.85    0.91      0.23 5.7    0.030 0.033  0.24
## P11      0.84      0.83    0.91      0.21 5.1    0.033 0.032  0.22
## P12      0.84      0.84    0.91      0.22 5.2    0.032 0.034  0.22
## P13      0.84      0.84    0.91      0.21 5.1    0.033 0.033  0.22
## P14      0.84      0.83    0.91      0.21 5.0    0.034 0.032  0.22
## P15      0.86      0.85    0.91      0.23 5.8    0.029 0.032  0.24
## P16      0.84      0.84    0.91      0.21 5.1    0.032 0.033  0.22
## P17      0.84      0.83    0.91      0.21 5.0    0.033 0.031  0.22
## P18      0.84      0.84    0.91      0.21 5.1    0.032 0.032  0.22
## P19      0.84      0.84    0.91      0.22 5.2    0.032 0.034  0.22
## P20      0.84      0.84    0.91      0.21 5.2    0.032 0.033  0.22
## 
##  Item statistics 
##      n raw.r std.r r.cor r.drop mean   sd
## P1  46  0.45  0.45 0.421  0.371  3.2 1.19
## P2  46  0.13  0.13 0.083  0.028  3.1 1.26
## P3  46  0.28  0.29 0.235  0.204  2.5 1.03
## P4  46  0.44  0.44 0.427  0.366  3.3 0.98
## P5  46  0.33  0.34 0.304  0.256  3.8 1.02
## P6  46  0.69  0.69 0.679  0.626  3.3 1.32
## P7  46  0.34  0.35 0.300  0.259  4.1 1.02
## P8  46  0.44  0.44 0.408  0.356  3.4 1.26
## P9  46  0.71  0.71 0.708  0.659  2.2 1.17
## P10 46  0.31  0.31 0.287  0.239  3.1 0.98
## P11 46  0.68  0.67 0.669  0.619  3.4 1.32
## P12 46  0.58  0.58 0.563  0.514  3.4 1.11
## P13 46  0.65  0.64 0.636  0.583  2.5 1.19
## P14 46  0.73  0.72 0.718  0.666  3.1 1.33
## P15 46  0.25  0.26 0.227  0.159  4.0 1.14
## P16 46  0.64  0.64 0.630  0.569  2.5 1.28
## P17 46  0.69  0.70 0.697  0.638  2.4 1.11
## P18 46  0.64  0.64 0.631  0.576  2.2 1.14
## P19 46  0.59  0.58 0.554  0.512  2.8 1.29
## P20 46  0.59  0.59 0.575  0.520  3.8 1.22
## 
## Non missing response frequency for each item
##        1    2    3    4    5 miss
## P1  0.09 0.20 0.30 0.26 0.15    0
## P2  0.13 0.20 0.28 0.24 0.15    0
## P3  0.20 0.28 0.37 0.13 0.02    0
## P4  0.07 0.09 0.43 0.33 0.09    0
## P5  0.00 0.13 0.26 0.33 0.28    0
## P6  0.13 0.11 0.30 0.22 0.24    0
## P7  0.02 0.07 0.15 0.35 0.41    0
## P8  0.04 0.24 0.28 0.15 0.28    0
## P9  0.37 0.24 0.22 0.15 0.02    0
## P10 0.04 0.20 0.52 0.13 0.11    0
## P11 0.13 0.11 0.26 0.26 0.24    0
## P12 0.00 0.26 0.26 0.26 0.22    0
## P13 0.24 0.28 0.24 0.20 0.04    0
## P14 0.11 0.28 0.17 0.24 0.20    0
## P15 0.04 0.07 0.17 0.26 0.46    0
## P16 0.28 0.26 0.24 0.13 0.09    0
## P17 0.26 0.26 0.35 0.09 0.04    0
## P18 0.33 0.28 0.26 0.09 0.04    0
## P19 0.24 0.13 0.30 0.24 0.09    0
## P20 0.07 0.07 0.26 0.22 0.39    0
# Resultado principal
alpha_global$total$raw_alpha
## [1] 0.8539175
alpha_global$total$std.alpha
## [1] 0.8492842
# Estadísticos por ítem
resultado_items <- data.frame(
  Item = rownames(alpha_global$alpha.drop),
  r_item_total = round(alpha_global$item.stats$r.drop, 3),
  alpha_si_se_elimina = round(alpha_global$alpha.drop$raw_alpha, 3),
  media = round(alpha_global$item.stats$mean, 3),
  desviacion = round(alpha_global$item.stats$sd, 3)
)

resultado_items
##    Item r_item_total alpha_si_se_elimina media desviacion
## 1    P1        0.371               0.850 3.196      1.185
## 2    P2        0.028               0.865 3.087      1.262
## 3    P3        0.204               0.856 2.500      1.027
## 4    P4        0.366               0.850 3.283      0.981
## 5    P5        0.256               0.854 3.761      1.015
## 6    P6        0.626               0.839 3.326      1.317
## 7    P7        0.259               0.854 4.065      1.020
## 8    P8        0.356               0.851 3.391      1.256
## 9    P9        0.659               0.838 2.217      1.172
## 10  P10        0.239               0.854 3.065      0.975
## 11  P11        0.619               0.839 3.370      1.323
## 12  P12        0.514               0.844 3.435      1.109
## 13  P13        0.583               0.841 2.522      1.188
## 14  P14        0.666               0.837 3.130      1.327
## 15  P15        0.159               0.858 4.022      1.145
## 16  P16        0.569               0.842 2.478      1.278
## 17  P17        0.638               0.840 2.391      1.105
## 18  P18        0.576               0.842 2.239      1.139
## 19  P19        0.512               0.844 2.804      1.293
## 20  P20        0.520               0.844 3.804      1.222
for(c in unique(datosn$CURSO)) {
  sub <- datosn %>% filter(CURSO == c) %>% select(starts_with("P"))
  a_sub <- psych::alpha(sub)
  cat("Curso", c, ":", round(a_sub$total$raw_alpha, 3), "\n")
}
## Warning in response.frequencies(x, max = max): response.frequency has been
## deprecated and replaced with responseFrequecy.  Please fix your call
## Curso A : 0.873
## Warning in response.frequencies(x, max = max): response.frequency has been
## deprecated and replaced with responseFrequecy.  Please fix your call
## Warning in log(det(r)): Se han producido NaNs
## Curso B : 0.815
resultado_items %>%
  filter(r_item_total < 0.30 | alpha_si_se_elimina > alpha_global$total$raw_alpha)
##   Item r_item_total alpha_si_se_elimina media desviacion
## 1   P2        0.028               0.865 3.087      1.262
## 2   P3        0.204               0.856 2.500      1.027
## 3   P5        0.256               0.854 3.761      1.015
## 4   P7        0.259               0.854 4.065      1.020
## 5  P10        0.239               0.854 3.065      0.975
## 6  P15        0.159               0.858 4.022      1.145
library(tibble)

codebook <- tribble(
 ~item, ~dimension, ~pregunta,
 "P1","D1_Aprendizaje","En el colegio he aprendido qué es el cuidado del medio ambiente.",
 "P2","D1_Aprendizaje","En mi familia se habla sobre problemas ambientales (contaminación/cambio climático).",
 "P3","D1_Aprendizaje","Con mis amigos conversamos sobre cómo nuestras acciones afectan al medio ambiente.",
 "P4","D1_Aprendizaje","En redes sociales he visto información que me ayuda a aprender sobre el cuidado del ambiente.",
 "P5","D1_Aprendizaje","Sé identificar acciones que dañan o protegen el medio ambiente.",
 "P6","D2_Practicas","En el colegio separo la basura cuando existen tachos para hacerlo.",
 "P7","D2_Practicas","En mi casa ahorro agua y energía (cierro llaves, apago luces).",
 "P8","D2_Practicas","Con mis amigos evito botar basura en la calle o en áreas verdes.",
 "P9","D2_Practicas","En redes sociales comparto o apoyo publicaciones sobre el cuidado del ambiente.",
 "P10","D2_Practicas","Realizo acciones diarias para cuidar el medio ambiente.",
 "P11","D3_Actitudes","Me preocupa el daño ambiental que ocurre en mi colegio o comunidad.",
 "P12","D3_Actitudes","Me siento responsable de cuidar el medio ambiente junto a mi familia.",
 "P13","D3_Actitudes","Animo a mis amigos a cuidar el medio ambiente.",
 "P14","D3_Actitudes","Me interesa el contenido ambiental que aparece en mis redes sociales.",
 "P15","D3_Actitudes","Considero importante cuidar el medio ambiente para el futuro.",
 "P16","D4_Tecnologia","En el colegio se usan videos/plataformas/tecnología para enseñar temas ambientales.",
 "P17","D4_Tecnologia","En mi casa utilizo internet o dispositivos digitales para aprender sobre el ambiente.",
 "P18","D4_Tecnologia","Con mis amigos usamos redes o apps para informarnos sobre temas ambientales.",
 "P19","D4_Tecnologia","Uso redes sociales o IA para aprender sobre el cuidado del medio ambiente.",
 "P20","D4_Tecnologia","Considero que la tecnología ayuda a aprender mejor sobre el cuidado ambiental."
)

codebook
## # A tibble: 20 × 3
##    item  dimension      pregunta                                                
##    <chr> <chr>          <chr>                                                   
##  1 P1    D1_Aprendizaje En el colegio he aprendido qué es el cuidado del medio …
##  2 P2    D1_Aprendizaje En mi familia se habla sobre problemas ambientales (con…
##  3 P3    D1_Aprendizaje Con mis amigos conversamos sobre cómo nuestras acciones…
##  4 P4    D1_Aprendizaje En redes sociales he visto información que me ayuda a a…
##  5 P5    D1_Aprendizaje Sé identificar acciones que dañan o protegen el medio a…
##  6 P6    D2_Practicas   En el colegio separo la basura cuando existen tachos pa…
##  7 P7    D2_Practicas   En mi casa ahorro agua y energía (cierro llaves, apago …
##  8 P8    D2_Practicas   Con mis amigos evito botar basura en la calle o en área…
##  9 P9    D2_Practicas   En redes sociales comparto o apoyo publicaciones sobre …
## 10 P10   D2_Practicas   Realizo acciones diarias para cuidar el medio ambiente. 
## 11 P11   D3_Actitudes   Me preocupa el daño ambiental que ocurre en mi colegio …
## 12 P12   D3_Actitudes   Me siento responsable de cuidar el medio ambiente junto…
## 13 P13   D3_Actitudes   Animo a mis amigos a cuidar el medio ambiente.          
## 14 P14   D3_Actitudes   Me interesa el contenido ambiental que aparece en mis r…
## 15 P15   D3_Actitudes   Considero importante cuidar el medio ambiente para el f…
## 16 P16   D4_Tecnologia  En el colegio se usan videos/plataformas/tecnología par…
## 17 P17   D4_Tecnologia  En mi casa utilizo internet o dispositivos digitales pa…
## 18 P18   D4_Tecnologia  Con mis amigos usamos redes o apps para informarnos sob…
## 19 P19   D4_Tecnologia  Uso redes sociales o IA para aprender sobre el cuidado …
## 20 P20   D4_Tecnologia  Considero que la tecnología ayuda a aprender mejor sobr…

Graficos de barra de cada pregunta por secciones

library(kableExtra)
# Orden de la escala Likert
niveles_likert <- c("Nunca", "Casi nunca", "Aveces", "Casi siempre", "Siempre")

# Función para porcentaje con coma decimal
fmt_pct <- function(x) {
  gsub("\\.", ",", sprintf("%.2f%%", x))
}

# Función para construir minibarras HTML
barra_item_html <- function(freq) {
  maxv <- max(freq, na.rm = TRUE)
  if (is.infinite(maxv) || maxv == 0) maxv <- 1
  
  barras <- purrr::map_chr(freq, function(x) {
    ancho <- round((x / maxv) * 100, 1)
    paste0(
      "<div style='width:120px;height:18px;border:1px solid #B3B3B3;",
      "margin:4px 0;background:#FFFFFF;'>",
      "<div style='height:100%;width:", ancho, "%;background:#D9D9D9;'></div>",
      "</div>"
    )
  })
  
  paste(barras, collapse = "")
}

# Función principal por sección/dimensión
tabla_frecuencias_seccion <- function(data_texto, codebook, seccion,
                                      niveles = niveles_likert) {
  
  cb <- codebook %>% filter(dimension == seccion)
  
  out <- purrr::map_dfr(seq_len(nrow(cb)), function(i) {
    item_i <- cb$item[i]
    preg_i <- cb$pregunta[i]
    
    frec <- table(factor(data_texto[[item_i]], levels = niveles))
    frec <- as.numeric(frec)
    total <- sum(frec)
    porc <- 100 * frec / total
    
    estadisticas_html <- paste0(
      seq_along(niveles), ". ", niveles, " &nbsp;&nbsp;",
      frec, " (", fmt_pct(porc), ")",
      collapse = "<br>"
    )
    
    tibble(
      `N.°` = as.integer(stringr::str_remove(item_i, "P")),
      Variable = preg_i,
      `Estadísticas / Valores` = estadisticas_html,
      Gráfico = barra_item_html(frec)
    )
  })
  
  out
}

# Secciones del cuestionario
secciones <- unique(codebook$dimension)

for (sec in secciones) {
  cat("\n\n## ", gsub("_", " ", sec), "\n\n", sep = "")
  
  tabla_sec <- tabla_frecuencias_seccion(datost, codebook, sec)
  
  print(
    kableExtra::kbl(
      tabla_sec,
      escape = FALSE,
      align = c("c", "l", "l", "c"),
      col.names = c("N.°", "Variable", "Estadísticas / Valores", "Gráfico")
    ) %>%
      kableExtra::kable_styling(
        full_width = TRUE,
        bootstrap_options = c("striped", "condensed")
      ) %>%
      kableExtra::column_spec(1, width = "5em") %>%
      kableExtra::column_spec(2, width = "30em") %>%
      kableExtra::column_spec(3, width = "24em") %>%
      kableExtra::column_spec(4, width = "10em")
  )
}

D1 Aprendizaje

N.° Variable Estadísticas / Valores Gráfico
1 En el colegio he aprendido qué es el cuidado del medio ambiente.
  1. Nunca   4 (8,70%)
    2. Casi nunca   9 (19,57%)
    3. Aveces   14 (30,43%)
    4. Casi siempre   12 (26,09%)
    5. Siempre   7 (15,22%)
2 En mi familia se habla sobre problemas ambientales (contaminación/cambio climático).
  1. Nunca   6 (13,04%)
    2. Casi nunca   9 (19,57%)
    3. Aveces   13 (28,26%)
    4. Casi siempre   11 (23,91%)
    5. Siempre   7 (15,22%)
3 Con mis amigos conversamos sobre cómo nuestras acciones afectan al medio ambiente.
  1. Nunca   9 (19,57%)
    2. Casi nunca   13 (28,26%)
    3. Aveces   17 (36,96%)
    4. Casi siempre   6 (13,04%)
    5. Siempre   1 (2,17%)
4 En redes sociales he visto información que me ayuda a aprender sobre el cuidado del ambiente.
  1. Nunca   3 (6,52%)
    2. Casi nunca   4 (8,70%)
    3. Aveces   20 (43,48%)
    4. Casi siempre   15 (32,61%)
    5. Siempre   4 (8,70%)
5 Sé identificar acciones que dañan o protegen el medio ambiente.
  1. Nunca   0 (0,00%)
    2. Casi nunca   6 (13,04%)
    3. Aveces   12 (26,09%)
    4. Casi siempre   15 (32,61%)
    5. Siempre   13 (28,26%)

D2 Practicas

N.° Variable Estadísticas / Valores Gráfico
6 En el colegio separo la basura cuando existen tachos para hacerlo.
  1. Nunca   6 (13,04%)
    2. Casi nunca   5 (10,87%)
    3. Aveces   14 (30,43%)
    4. Casi siempre   10 (21,74%)
    5. Siempre   11 (23,91%)
7 En mi casa ahorro agua y energía (cierro llaves, apago luces).
  1. Nunca   1 (2,17%)
    2. Casi nunca   3 (6,52%)
    3. Aveces   7 (15,22%)
    4. Casi siempre   16 (34,78%)
    5. Siempre   19 (41,30%)
8 Con mis amigos evito botar basura en la calle o en áreas verdes.
  1. Nunca   2 (4,35%)
    2. Casi nunca   11 (23,91%)
    3. Aveces   13 (28,26%)
    4. Casi siempre   7 (15,22%)
    5. Siempre   13 (28,26%)
9 En redes sociales comparto o apoyo publicaciones sobre el cuidado del ambiente.
  1. Nunca   17 (36,96%)
    2. Casi nunca   11 (23,91%)
    3. Aveces   10 (21,74%)
    4. Casi siempre   7 (15,22%)
    5. Siempre   1 (2,17%)
10 Realizo acciones diarias para cuidar el medio ambiente.
  1. Nunca   2 (4,35%)
    2. Casi nunca   9 (19,57%)
    3. Aveces   24 (52,17%)
    4. Casi siempre   6 (13,04%)
    5. Siempre   5 (10,87%)

D3 Actitudes

N.° Variable Estadísticas / Valores Gráfico
11 Me preocupa el daño ambiental que ocurre en mi colegio o comunidad.
  1. Nunca   6 (13,04%)
    2. Casi nunca   5 (10,87%)
    3. Aveces   12 (26,09%)
    4. Casi siempre   12 (26,09%)
    5. Siempre   11 (23,91%)
12 Me siento responsable de cuidar el medio ambiente junto a mi familia.
  1. Nunca   0 (0,00%)
    2. Casi nunca   12 (26,09%)
    3. Aveces   12 (26,09%)
    4. Casi siempre   12 (26,09%)
    5. Siempre   10 (21,74%)
13 Animo a mis amigos a cuidar el medio ambiente.
  1. Nunca   11 (23,91%)
    2. Casi nunca   13 (28,26%)
    3. Aveces   11 (23,91%)
    4. Casi siempre   9 (19,57%)
    5. Siempre   2 (4,35%)
14 Me interesa el contenido ambiental que aparece en mis redes sociales.
  1. Nunca   5 (10,87%)
    2. Casi nunca   13 (28,26%)
    3. Aveces   8 (17,39%)
    4. Casi siempre   11 (23,91%)
    5. Siempre   9 (19,57%)
15 Considero importante cuidar el medio ambiente para el futuro.
  1. Nunca   2 (4,35%)
    2. Casi nunca   3 (6,52%)
    3. Aveces   8 (17,39%)
    4. Casi siempre   12 (26,09%)
    5. Siempre   21 (45,65%)

D4 Tecnologia

N.° Variable Estadísticas / Valores Gráfico
16 En el colegio se usan videos/plataformas/tecnología para enseñar temas ambientales.
  1. Nunca   13 (28,26%)
    2. Casi nunca   12 (26,09%)
    3. Aveces   11 (23,91%)
    4. Casi siempre   6 (13,04%)
    5. Siempre   4 (8,70%)
17 En mi casa utilizo internet o dispositivos digitales para aprender sobre el ambiente.
  1. Nunca   12 (26,09%)
    2. Casi nunca   12 (26,09%)
    3. Aveces   16 (34,78%)
    4. Casi siempre   4 (8,70%)
    5. Siempre   2 (4,35%)
18 Con mis amigos usamos redes o apps para informarnos sobre temas ambientales.
  1. Nunca   15 (32,61%)
    2. Casi nunca   13 (28,26%)
    3. Aveces   12 (26,09%)
    4. Casi siempre   4 (8,70%)
    5. Siempre   2 (4,35%)
19 Uso redes sociales o IA para aprender sobre el cuidado del medio ambiente.
  1. Nunca   11 (23,91%)
    2. Casi nunca   6 (13,04%)
    3. Aveces   14 (30,43%)
    4. Casi siempre   11 (23,91%)
    5. Siempre   4 (8,70%)
20 Considero que la tecnología ayuda a aprender mejor sobre el cuidado ambiental.
  1. Nunca   3 (6,52%)
    2. Casi nunca   3 (6,52%)
    3. Aveces   12 (26,09%)
    4. Casi siempre   10 (21,74%)
    5. Siempre   18 (39,13%)

Graficos de barra de cada pregunta por secciones diferenciado por curso

## 4. Qué hace exactamente este bloque

#

## 5.  por curso

# tabla separada para **Curso A** y **Curso B**, 

for (g in unique(datost$CURSO)) {
  cat("\n\n# CURSO ", g, "\n\n", sep = "")
  
  dat_g <- datost %>% filter(CURSO == g)
  
  for (sec in unique(codebook$dimension)) {
    cat("\n\n## ", gsub("_", " ", sec), "\n\n", sep = "")
    
    tabla_sec <- tabla_frecuencias_seccion(dat_g, codebook, sec)
    
    print(
      kableExtra::kbl(
        tabla_sec,
        escape = FALSE,
        align = c("c", "l", "l", "c"),
        col.names = c("N.°", "Variable", "Estadísticas / Valores", "Gráfico")
      ) %>%
        kableExtra::kable_styling(
          full_width = TRUE,
          bootstrap_options = c("striped", "condensed")
        ) %>%
        kableExtra::column_spec(1, width = "5em") %>%
        kableExtra::column_spec(2, width = "30em") %>%
        kableExtra::column_spec(3, width = "24em") %>%
        kableExtra::column_spec(4, width = "10em")
    )
  }
}

CURSO A

D1 Aprendizaje

N.° Variable Estadísticas / Valores Gráfico
1 En el colegio he aprendido qué es el cuidado del medio ambiente.
  1. Nunca   3 (11,54%)
    2. Casi nunca   7 (26,92%)
    3. Aveces   7 (26,92%)
    4. Casi siempre   5 (19,23%)
    5. Siempre   4 (15,38%)
2 En mi familia se habla sobre problemas ambientales (contaminación/cambio climático).
  1. Nunca   4 (15,38%)
    2. Casi nunca   6 (23,08%)
    3. Aveces   4 (15,38%)
    4. Casi siempre   8 (30,77%)
    5. Siempre   4 (15,38%)
3 Con mis amigos conversamos sobre cómo nuestras acciones afectan al medio ambiente.
  1. Nunca   3 (11,54%)
    2. Casi nunca   8 (30,77%)
    3. Aveces   9 (34,62%)
    4. Casi siempre   6 (23,08%)
    5. Siempre   0 (0,00%)
4 En redes sociales he visto información que me ayuda a aprender sobre el cuidado del ambiente.
  1. Nunca   2 (7,69%)
    2. Casi nunca   3 (11,54%)
    3. Aveces   13 (50,00%)
    4. Casi siempre   6 (23,08%)
    5. Siempre   2 (7,69%)
5 Sé identificar acciones que dañan o protegen el medio ambiente.
  1. Nunca   0 (0,00%)
    2. Casi nunca   5 (19,23%)
    3. Aveces   7 (26,92%)
    4. Casi siempre   6 (23,08%)
    5. Siempre   8 (30,77%)

D2 Practicas

N.° Variable Estadísticas / Valores Gráfico
6 En el colegio separo la basura cuando existen tachos para hacerlo.
  1. Nunca   5 (19,23%)
    2. Casi nunca   4 (15,38%)
    3. Aveces   6 (23,08%)
    4. Casi siempre   7 (26,92%)
    5. Siempre   4 (15,38%)
7 En mi casa ahorro agua y energía (cierro llaves, apago luces).
  1. Nunca   1 (3,85%)
    2. Casi nunca   3 (11,54%)
    3. Aveces   4 (15,38%)
    4. Casi siempre   8 (30,77%)
    5. Siempre   10 (38,46%)
8 Con mis amigos evito botar basura en la calle o en áreas verdes.
  1. Nunca   1 (3,85%)
    2. Casi nunca   9 (34,62%)
    3. Aveces   6 (23,08%)
    4. Casi siempre   3 (11,54%)
    5. Siempre   7 (26,92%)
9 En redes sociales comparto o apoyo publicaciones sobre el cuidado del ambiente.
  1. Nunca   11 (42,31%)
    2. Casi nunca   6 (23,08%)
    3. Aveces   6 (23,08%)
    4. Casi siempre   2 (7,69%)
    5. Siempre   1 (3,85%)
10 Realizo acciones diarias para cuidar el medio ambiente.
  1. Nunca   1 (3,85%)
    2. Casi nunca   4 (15,38%)
    3. Aveces   13 (50,00%)
    4. Casi siempre   5 (19,23%)
    5. Siempre   3 (11,54%)

D3 Actitudes

N.° Variable Estadísticas / Valores Gráfico
11 Me preocupa el daño ambiental que ocurre en mi colegio o comunidad.
  1. Nunca   5 (19,23%)
    2. Casi nunca   4 (15,38%)
    3. Aveces   4 (15,38%)
    4. Casi siempre   7 (26,92%)
    5. Siempre   6 (23,08%)
12 Me siento responsable de cuidar el medio ambiente junto a mi familia.
  1. Nunca   0 (0,00%)
    2. Casi nunca   8 (30,77%)
    3. Aveces   8 (30,77%)
    4. Casi siempre   5 (19,23%)
    5. Siempre   5 (19,23%)
13 Animo a mis amigos a cuidar el medio ambiente.
  1. Nunca   8 (30,77%)
    2. Casi nunca   5 (19,23%)
    3. Aveces   4 (15,38%)
    4. Casi siempre   7 (26,92%)
    5. Siempre   2 (7,69%)
14 Me interesa el contenido ambiental que aparece en mis redes sociales.
  1. Nunca   3 (11,54%)
    2. Casi nunca   8 (30,77%)
    3. Aveces   4 (15,38%)
    4. Casi siempre   4 (15,38%)
    5. Siempre   7 (26,92%)
15 Considero importante cuidar el medio ambiente para el futuro.
  1. Nunca   1 (3,85%)
    2. Casi nunca   2 (7,69%)
    3. Aveces   3 (11,54%)
    4. Casi siempre   6 (23,08%)
    5. Siempre   14 (53,85%)

D4 Tecnologia

N.° Variable Estadísticas / Valores Gráfico
16 En el colegio se usan videos/plataformas/tecnología para enseñar temas ambientales.
  1. Nunca   7 (26,92%)
    2. Casi nunca   7 (26,92%)
    3. Aveces   6 (23,08%)
    4. Casi siempre   3 (11,54%)
    5. Siempre   3 (11,54%)
17 En mi casa utilizo internet o dispositivos digitales para aprender sobre el ambiente.
  1. Nunca   8 (30,77%)
    2. Casi nunca   8 (30,77%)
    3. Aveces   6 (23,08%)
    4. Casi siempre   4 (15,38%)
    5. Siempre   0 (0,00%)
18 Con mis amigos usamos redes o apps para informarnos sobre temas ambientales.
  1. Nunca   11 (42,31%)
    2. Casi nunca   5 (19,23%)
    3. Aveces   6 (23,08%)
    4. Casi siempre   3 (11,54%)
    5. Siempre   1 (3,85%)
19 Uso redes sociales o IA para aprender sobre el cuidado del medio ambiente.
  1. Nunca   6 (23,08%)
    2. Casi nunca   2 (7,69%)
    3. Aveces   9 (34,62%)
    4. Casi siempre   9 (34,62%)
    5. Siempre   0 (0,00%)
20 Considero que la tecnología ayuda a aprender mejor sobre el cuidado ambiental.
  1. Nunca   3 (11,54%)
    2. Casi nunca   1 (3,85%)
    3. Aveces   6 (23,08%)
    4. Casi siempre   6 (23,08%)
    5. Siempre   10 (38,46%)

CURSO B

D1 Aprendizaje

N.° Variable Estadísticas / Valores Gráfico
1 En el colegio he aprendido qué es el cuidado del medio ambiente.
  1. Nunca   1 (5,00%)
    2. Casi nunca   2 (10,00%)
    3. Aveces   7 (35,00%)
    4. Casi siempre   7 (35,00%)
    5. Siempre   3 (15,00%)
2 En mi familia se habla sobre problemas ambientales (contaminación/cambio climático).
  1. Nunca   2 (10,00%)
    2. Casi nunca   3 (15,00%)
    3. Aveces   9 (45,00%)
    4. Casi siempre   3 (15,00%)
    5. Siempre   3 (15,00%)
3 Con mis amigos conversamos sobre cómo nuestras acciones afectan al medio ambiente.
  1. Nunca   6 (30,00%)
    2. Casi nunca   5 (25,00%)
    3. Aveces   8 (40,00%)
    4. Casi siempre   0 (0,00%)
    5. Siempre   1 (5,00%)
4 En redes sociales he visto información que me ayuda a aprender sobre el cuidado del ambiente.
  1. Nunca   1 (5,00%)
    2. Casi nunca   1 (5,00%)
    3. Aveces   7 (35,00%)
    4. Casi siempre   9 (45,00%)
    5. Siempre   2 (10,00%)
5 Sé identificar acciones que dañan o protegen el medio ambiente.
  1. Nunca   0 (0,00%)
    2. Casi nunca   1 (5,00%)
    3. Aveces   5 (25,00%)
    4. Casi siempre   9 (45,00%)
    5. Siempre   5 (25,00%)

D2 Practicas

N.° Variable Estadísticas / Valores Gráfico
6 En el colegio separo la basura cuando existen tachos para hacerlo.
  1. Nunca   1 (5,00%)
    2. Casi nunca   1 (5,00%)
    3. Aveces   8 (40,00%)
    4. Casi siempre   3 (15,00%)
    5. Siempre   7 (35,00%)
7 En mi casa ahorro agua y energía (cierro llaves, apago luces).
  1. Nunca   0 (0,00%)
    2. Casi nunca   0 (0,00%)
    3. Aveces   3 (15,00%)
    4. Casi siempre   8 (40,00%)
    5. Siempre   9 (45,00%)
8 Con mis amigos evito botar basura en la calle o en áreas verdes.
  1. Nunca   1 (5,00%)
    2. Casi nunca   2 (10,00%)
    3. Aveces   7 (35,00%)
    4. Casi siempre   4 (20,00%)
    5. Siempre   6 (30,00%)
9 En redes sociales comparto o apoyo publicaciones sobre el cuidado del ambiente.
  1. Nunca   6 (30,00%)
    2. Casi nunca   5 (25,00%)
    3. Aveces   4 (20,00%)
    4. Casi siempre   5 (25,00%)
    5. Siempre   0 (0,00%)
10 Realizo acciones diarias para cuidar el medio ambiente.
  1. Nunca   1 (5,00%)
    2. Casi nunca   5 (25,00%)
    3. Aveces   11 (55,00%)
    4. Casi siempre   1 (5,00%)
    5. Siempre   2 (10,00%)

D3 Actitudes

N.° Variable Estadísticas / Valores Gráfico
11 Me preocupa el daño ambiental que ocurre en mi colegio o comunidad.
  1. Nunca   1 (5,00%)
    2. Casi nunca   1 (5,00%)
    3. Aveces   8 (40,00%)
    4. Casi siempre   5 (25,00%)
    5. Siempre   5 (25,00%)
12 Me siento responsable de cuidar el medio ambiente junto a mi familia.
  1. Nunca   0 (0,00%)
    2. Casi nunca   4 (20,00%)
    3. Aveces   4 (20,00%)
    4. Casi siempre   7 (35,00%)
    5. Siempre   5 (25,00%)
13 Animo a mis amigos a cuidar el medio ambiente.
  1. Nunca   3 (15,00%)
    2. Casi nunca   8 (40,00%)
    3. Aveces   7 (35,00%)
    4. Casi siempre   2 (10,00%)
    5. Siempre   0 (0,00%)
14 Me interesa el contenido ambiental que aparece en mis redes sociales.
  1. Nunca   2 (10,00%)
    2. Casi nunca   5 (25,00%)
    3. Aveces   4 (20,00%)
    4. Casi siempre   7 (35,00%)
    5. Siempre   2 (10,00%)
15 Considero importante cuidar el medio ambiente para el futuro.
  1. Nunca   1 (5,00%)
    2. Casi nunca   1 (5,00%)
    3. Aveces   5 (25,00%)
    4. Casi siempre   6 (30,00%)
    5. Siempre   7 (35,00%)

D4 Tecnologia

N.° Variable Estadísticas / Valores Gráfico
16 En el colegio se usan videos/plataformas/tecnología para enseñar temas ambientales.
  1. Nunca   6 (30,00%)
    2. Casi nunca   5 (25,00%)
    3. Aveces   5 (25,00%)
    4. Casi siempre   3 (15,00%)
    5. Siempre   1 (5,00%)
17 En mi casa utilizo internet o dispositivos digitales para aprender sobre el ambiente.
  1. Nunca   4 (20,00%)
    2. Casi nunca   4 (20,00%)
    3. Aveces   10 (50,00%)
    4. Casi siempre   0 (0,00%)
    5. Siempre   2 (10,00%)
18 Con mis amigos usamos redes o apps para informarnos sobre temas ambientales.
  1. Nunca   4 (20,00%)
    2. Casi nunca   8 (40,00%)
    3. Aveces   6 (30,00%)
    4. Casi siempre   1 (5,00%)
    5. Siempre   1 (5,00%)
19 Uso redes sociales o IA para aprender sobre el cuidado del medio ambiente.
  1. Nunca   5 (25,00%)
    2. Casi nunca   4 (20,00%)
    3. Aveces   5 (25,00%)
    4. Casi siempre   2 (10,00%)
    5. Siempre   4 (20,00%)
20 Considero que la tecnología ayuda a aprender mejor sobre el cuidado ambiental.
  1. Nunca   0 (0,00%)
    2. Casi nunca   2 (10,00%)
    3. Aveces   6 (30,00%)
    4. Casi siempre   4 (20,00%)
    5. Siempre   8 (40,00%)

cluster

library(dplyr)
library(tibble)
library(cluster)
library(factoextra)
## Cargando paquete requerido: ggplot2
## 
## Adjuntando el paquete: 'ggplot2'
## The following objects are masked from 'package:psych':
## 
##     %+%, alpha
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library(ggplot2)


# Asegurar que los ítems estén como numéricos
items <- paste0("P", 1:20)

datosn[items] <- lapply(datosn[items], as.numeric)

# Listado de ítems por dimensión
items_d1 <- codebook %>% filter(dimension == "D1_Aprendizaje") %>% pull(item)
items_d2 <- codebook %>% filter(dimension == "D2_Practicas") %>% pull(item)
items_d3 <- codebook %>% filter(dimension == "D3_Actitudes") %>% pull(item)
items_d4 <- codebook %>% filter(dimension == "D4_Tecnologia") %>% pull(item)

# Puntajes promedio por dimensión
datos_cluster <- datosn %>%
  mutate(
    D1_Aprendizaje = rowMeans(select(., all_of(items_d1)), na.rm = TRUE),
    D2_Practicas   = rowMeans(select(., all_of(items_d2)), na.rm = TRUE),
    D3_Actitudes   = rowMeans(select(., all_of(items_d3)), na.rm = TRUE),
    D4_Tecnologia  = rowMeans(select(., all_of(items_d4)), na.rm = TRUE)
  ) %>%
  select(CURSO, D1_Aprendizaje, D2_Practicas, D3_Actitudes, D4_Tecnologia)

datos_cluster
## # A tibble: 46 × 5
##    CURSO D1_Aprendizaje D2_Practicas D3_Actitudes D4_Tecnologia
##    <chr>          <dbl>        <dbl>        <dbl>         <dbl>
##  1 A                2.8          3.6          4.2           3.6
##  2 A                3.4          4.4          4.8           3.6
##  3 A                2.8          2            1.6           1.8
##  4 A                2.4          3            2.8           1.8
##  5 A                2            3.2          3.2           2.6
##  6 A                3            2.2          3             2.2
##  7 A                2.6          2.8          2.6           3.2
##  8 A                3.4          2.8          2.4           2.8
##  9 A                2.2          3            2.4           2.2
## 10 A                3.2          3.4          4.2           2.8
## # ℹ 36 more rows
X <- datos_cluster %>%
  select(D1_Aprendizaje, D2_Practicas, D3_Actitudes, D4_Tecnologia)

X_scaled <- scale(X)

set.seed(123)
modelo_pam <- pam(X_scaled, k = 3)

modelo_pam
## Medoids:
##      ID D1_Aprendizaje D2_Practicas D3_Actitudes D4_Tecnologia
## [1,] 37     0.82217272    1.3759682     1.110949     0.7628126
## [2,] 43    -1.44702399   -1.1335068    -1.100267    -1.3286075
## [3,] 36     0.06577382   -0.2970152    -0.363195    -0.1667074
## Clustering vector:
##  [1] 1 1 2 2 2 3 3 3 2 1 1 2 1 2 2 3 3 1 1 1 3 3 3 3 3 3 3 1 1 1 1 3 3 3 3 3 1 1
## [39] 3 3 3 3 2 2 1 3
## Objective function:
##    build     swap 
## 1.260418 1.260418 
## 
## Available components:
##  [1] "medoids"    "id.med"     "clustering" "objective"  "isolation" 
##  [6] "clusinfo"   "silinfo"    "diss"       "call"       "data"
datos_cluster$cluster <- factor(modelo_pam$clustering)

perfil_cluster <- datos_cluster %>%
  group_by(cluster) %>%
  summarise(
    n = n(),
    Aprendizaje = mean(D1_Aprendizaje, na.rm = TRUE),
    Practicas   = mean(D2_Practicas, na.rm = TRUE),
    Actitudes   = mean(D3_Actitudes, na.rm = TRUE),
    Tecnologia  = mean(D4_Tecnologia, na.rm = TRUE)
  )

perfil_cluster
## # A tibble: 3 × 6
##   cluster     n Aprendizaje Practicas Actitudes Tecnologia
##   <fct>   <int>       <dbl>     <dbl>     <dbl>      <dbl>
## 1 1          15        3.61      3.88      4.11       3.56
## 2 2           9        2.62      2.47      2.42       1.6 
## 3 3          22        3.08      3.06      3.1        2.65
fviz_cluster(modelo_pam, data = X_scaled,
             ellipse.type = "convex",
             geom = "point",
             repel = TRUE) +
  labs(title = "Clúster de estudiantes según dimensiones ambientales")

cluster jerarquico

library(dplyr)
library(ggplot2)
library(tidyr)
## 
## Adjuntando el paquete: 'tidyr'
## The following objects are masked from 'package:Matrix':
## 
##     expand, pack, unpack
library(factoextra)

# Selección de dimensiones
X <- datos_cluster %>%
  select(D1_Aprendizaje, D2_Practicas, D3_Actitudes, D4_Tecnologia)

# Escalamiento
X_scaled <- scale(X)

# Distancias y clúster jerárquico
dist_mat <- dist(X_scaled, method = "euclidean")
hc <- hclust(dist_mat, method = "ward.D2")

# Dendrograma clásico
plot(hc, labels = FALSE, hang = -1,
     main = "Dendrograma de clúster jerárquico",
     xlab = "Casos",
     ylab = "Distancia")
rect.hclust(hc, k = 3, border = c("red", "blue", "darkgreen"))

# Dendrograma mejorado
library(factoextra)

fviz_dend(hc,
          k = 3,
          cex = 0.6,
          rect = TRUE,
          main = "Dendrograma de clúster jerárquico")
## Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
## ℹ Please use tidy evaluation idioms with `aes()`.
## ℹ See also `vignette("ggplot2-in-packages")` for more information.
## ℹ The deprecated feature was likely used in the factoextra package.
##   Please report the issue at <https://github.com/kassambara/factoextra/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## ℹ The deprecated feature was likely used in the factoextra package.
##   Please report the issue at <https://github.com/kassambara/factoextra/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.
## ℹ The deprecated feature was likely used in the factoextra package.
##   Please report the issue at <https://github.com/kassambara/factoextra/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

# Asignación de grupos
datos_cluster$cluster_hc <- factor(cutree(hc, k = 3))

# Tabla resumen
perfil_hc <- datos_cluster %>%
  group_by(cluster_hc) %>%
  summarise(
    n = n(),
    Aprendizaje = mean(D1_Aprendizaje, na.rm = TRUE),
    Practicas   = mean(D2_Practicas, na.rm = TRUE),
    Actitudes   = mean(D3_Actitudes, na.rm = TRUE),
    Tecnologia  = mean(D4_Tecnologia, na.rm = TRUE)
  )

print(perfil_hc)
## # A tibble: 3 × 6
##   cluster_hc     n Aprendizaje Practicas Actitudes Tecnologia
##   <fct>      <int>       <dbl>     <dbl>     <dbl>      <dbl>
## 1 1             26        3.14      3.32      3.38       2.96
## 2 2              7        3.8       4.26      4.46       3.71
## 3 3             13        2.88      2.43      2.49       1.78
# Perfil en formato largo
perfil_long <- perfil_hc %>%
  pivot_longer(cols = c(Aprendizaje, Practicas, Actitudes, Tecnologia),
               names_to = "Dimension",
               values_to = "Media")

# Gráfico de perfil
ggplot(perfil_long, aes(x = Dimension, y = Media, group = cluster_hc, color = cluster_hc)) +
  geom_line(linewidth = 1) +
  geom_point(size = 3) +
  labs(title = "Perfil promedio de los clústeres",
       x = "Dimensión",
       y = "Media",
       color = "Clúster") +
  theme_minimal()

# PCA para visualizar grupos
pca <- prcomp(X_scaled, center = TRUE, scale. = TRUE)

pca_df <- data.frame(
  PC1 = pca$x[,1],
  PC2 = pca$x[,2],
  cluster = datos_cluster$cluster_hc
)

ggplot(pca_df, aes(x = PC1, y = PC2, color = cluster)) +
  geom_point(size = 3) +
  labs(title = "Distribución de clústeres en el espacio PCA",
       x = "CP1",
       y = "CP2",
       color = "Clúster") +
  theme_minimal()

correlacion: solo 10 preguntas con mayor correlacion

library(dplyr)
library(tidyr)
library(ggplot2)
library(scales)
## 
## Adjuntando el paquete: 'scales'
## The following objects are masked from 'package:psych':
## 
##     alpha, rescale
items <- paste0("P", 1:20)

heat_curso <- datosn %>%
  pivot_longer(
    cols = all_of(items),
    names_to = "Item",
    values_to = "Valor"
  ) %>%
  group_by(CURSO, Item) %>%
  summarise(Promedio = mean(Valor, na.rm = TRUE), .groups = "drop") %>%
  mutate(Item = factor(Item, levels = items))

ggplot(heat_curso, aes(x = Item, y = CURSO, fill = Promedio)) +
  geom_tile(color = "white", linewidth = 0.4) +
  geom_text(aes(label = round(Promedio, 2)), size = 3.5) +
  scale_fill_gradient2(
    low = "#d73027",
    mid = "#fee08b",
    high = "#1a9850",
    midpoint = 3,
    limits = c(1, 5),
    name = "Promedio"
  ) +
  labs(
    title = "Heatmap de promedios por curso e ítem",
    x = "Ítems",
    y = "Curso"
  ) +
  theme_minimal(base_size = 12) +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1),
    panel.grid = element_blank(),
    plot.title = element_text(face = "bold"))

heat_est <- datosn %>%
  pivot_longer(
    cols = all_of(items),
    names_to = "Item",
    values_to = "Valor"
  ) %>%
  mutate(
    Item = factor(Item, levels = items),
    ESTUDIANTES = factor(ESTUDIANTES, levels = rev(unique(ESTUDIANTES)))
  )

ggplot(heat_est, aes(x = Item, y = ESTUDIANTES, fill = Valor)) +
  geom_tile(color = "white", linewidth = 0.15) +
  scale_fill_gradient2(
    low = "#d73027",
    mid = "#fee08b",
    high = "#1a9850",
    midpoint = 3,
    limits = c(1, 5),
    name = "Valor"
  ) +
  facet_grid(CURSO ~ ., scales = "free_y", space = "free_y") +
  labs(
    title = "Heatmap por estudiante e ítem",
    x = "Ítems",
    y = "Estudiantes"
  ) +
  theme_minimal(base_size = 11) +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1),
    panel.grid = element_blank(),
    plot.title = element_text(face = "bold")
  )

correlacion de spearma

library(dplyr)
library(tidyr)
library(ggplot2)

# Definir ítems
items <- paste0("P", 1:20)

# Seleccionar solo los ítems
matriz_items <- datosn %>%
  select(all_of(items))

# Calcular matriz de correlación entre ítems
cor_items <- cor(
  matriz_items,
  use = "pairwise.complete.obs",
  method = "spearman"
)

# Convertir a formato largo para ggplot
cor_larga <- as.data.frame(as.table(cor_items)) %>%
  rename(Item1 = Var1, Item2 = Var2, Correlacion = Freq)

# Heatmap
ggplot(cor_larga, aes(x = Item1, y = Item2, fill = Correlacion)) +
  geom_tile(color = "white", linewidth = 0.3) +
  geom_text(aes(label = round(Correlacion, 2)), size = 3) +
  scale_fill_gradient2(
    low = "#b2182b",
    mid = "white",
    high = "#2166ac",
    midpoint = 0,
    limits = c(-1, 1),
    name = "r Spearman"
  ) +
  labs(
    title = "Heatmap de correlación entre ítems",
    x = "Ítems",
    y = "Ítems"
  ) +
  theme_minimal(base_size = 12) +
  theme(
    axis.text.x = element_text(angle = 45, hjust = 1),
    panel.grid = element_blank(),
    plot.title = element_text(face = "bold")
  )

library(dplyr)
library(psych)
library(GPArotation)
## 
## Adjuntando el paquete: 'GPArotation'
## The following objects are masked from 'package:psych':
## 
##     equamax, varimin
# =========================
# 1. Seleccionar ítems
# =========================
items <- paste0("P", 1:20)

base_afe <- datosn %>%
  select(all_of(items))

# Verificar estructura
str(base_afe)
## tibble [46 × 20] (S3: tbl_df/tbl/data.frame)
##  $ P1 : num [1:46] 3 5 2 3 2 2 3 4 1 3 ...
##  $ P2 : num [1:46] 1 4 1 2 2 5 2 3 1 4 ...
##  $ P3 : num [1:46] 2 3 3 2 1 2 1 3 2 2 ...
##  $ P4 : num [1:46] 3 3 3 2 3 3 3 3 4 4 ...
##  $ P5 : num [1:46] 5 2 5 3 2 3 4 4 3 3 ...
##  $ P6 : num [1:46] 4 4 3 2 2 1 3 3 1 3 ...
##  $ P7 : num [1:46] 5 4 2 5 5 5 3 3 5 4 ...
##  $ P8 : num [1:46] 5 4 2 3 3 2 4 2 5 5 ...
##  $ P9 : num [1:46] 1 5 2 2 2 1 1 3 1 3 ...
##  $ P10: num [1:46] 3 5 1 3 4 2 3 3 3 2 ...
##  $ P11: num [1:46] 4 5 1 2 4 2 2 3 3 5 ...
##  $ P12: num [1:46] 3 5 2 2 3 5 3 3 2 4 ...
##  $ P13: num [1:46] 4 5 1 3 3 1 2 2 1 4 ...
##  $ P14: num [1:46] 5 5 1 2 2 2 2 2 3 4 ...
##  $ P15: num [1:46] 5 4 3 5 4 5 4 2 3 4 ...
##  $ P16: num [1:46] 3 4 2 1 3 2 3 2 3 2 ...
##  $ P17: num [1:46] 4 4 1 2 3 2 2 2 2 3 ...
##  $ P18: num [1:46] 2 3 1 1 1 2 3 3 1 3 ...
##  $ P19: num [1:46] 4 4 2 2 3 3 3 3 1 3 ...
##  $ P20: num [1:46] 5 3 3 3 3 2 5 4 4 3 ...
summary(base_afe)
##        P1              P2              P3            P4              P5       
##  Min.   :1.000   Min.   :1.000   Min.   :1.0   Min.   :1.000   Min.   :2.000  
##  1st Qu.:2.000   1st Qu.:2.000   1st Qu.:2.0   1st Qu.:3.000   1st Qu.:3.000  
##  Median :3.000   Median :3.000   Median :3.0   Median :3.000   Median :4.000  
##  Mean   :3.196   Mean   :3.087   Mean   :2.5   Mean   :3.283   Mean   :3.761  
##  3rd Qu.:4.000   3rd Qu.:4.000   3rd Qu.:3.0   3rd Qu.:4.000   3rd Qu.:5.000  
##  Max.   :5.000   Max.   :5.000   Max.   :5.0   Max.   :5.000   Max.   :5.000  
##        P6              P7              P8              P9       
##  Min.   :1.000   Min.   :1.000   Min.   :1.000   Min.   :1.000  
##  1st Qu.:3.000   1st Qu.:4.000   1st Qu.:2.000   1st Qu.:1.000  
##  Median :3.000   Median :4.000   Median :3.000   Median :2.000  
##  Mean   :3.326   Mean   :4.065   Mean   :3.391   Mean   :2.217  
##  3rd Qu.:4.000   3rd Qu.:5.000   3rd Qu.:5.000   3rd Qu.:3.000  
##  Max.   :5.000   Max.   :5.000   Max.   :5.000   Max.   :5.000  
##       P10             P11            P12             P13             P14      
##  Min.   :1.000   Min.   :1.00   Min.   :2.000   Min.   :1.000   Min.   :1.00  
##  1st Qu.:3.000   1st Qu.:3.00   1st Qu.:2.250   1st Qu.:2.000   1st Qu.:2.00  
##  Median :3.000   Median :3.50   Median :3.000   Median :2.000   Median :3.00  
##  Mean   :3.065   Mean   :3.37   Mean   :3.435   Mean   :2.522   Mean   :3.13  
##  3rd Qu.:3.000   3rd Qu.:4.00   3rd Qu.:4.000   3rd Qu.:3.000   3rd Qu.:4.00  
##  Max.   :5.000   Max.   :5.00   Max.   :5.000   Max.   :5.000   Max.   :5.00  
##       P15             P16             P17             P18       
##  Min.   :1.000   Min.   :1.000   Min.   :1.000   Min.   :1.000  
##  1st Qu.:3.000   1st Qu.:1.000   1st Qu.:1.250   1st Qu.:1.000  
##  Median :4.000   Median :2.000   Median :2.000   Median :2.000  
##  Mean   :4.022   Mean   :2.478   Mean   :2.391   Mean   :2.239  
##  3rd Qu.:5.000   3rd Qu.:3.000   3rd Qu.:3.000   3rd Qu.:3.000  
##  Max.   :5.000   Max.   :5.000   Max.   :5.000   Max.   :5.000  
##       P19             P20       
##  Min.   :1.000   Min.   :1.000  
##  1st Qu.:2.000   1st Qu.:3.000  
##  Median :3.000   Median :4.000  
##  Mean   :2.804   Mean   :3.804  
##  3rd Qu.:4.000   3rd Qu.:5.000  
##  Max.   :5.000   Max.   :5.000
# =========================
# 2. Revisar datos faltantes
# =========================
colSums(is.na(base_afe))
##  P1  P2  P3  P4  P5  P6  P7  P8  P9 P10 P11 P12 P13 P14 P15 P16 P17 P18 P19 P20 
##   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
# =========================
# 3. Correlación policórica
# =========================
cor_poly <- psych::polychoric(base_afe)$rho
## Warning in cor.smooth(mat): Matrix was not positive definite, smoothing was
## done
# Ver matriz
round(cor_poly, 2)
##        P1    P2    P3    P4    P5   P6    P7    P8   P9   P10   P11   P12  P13
## P1   1.00  0.09  0.06 -0.01 -0.04 0.40  0.09 -0.02 0.49  0.12  0.37  0.55 0.12
## P2   0.09  1.00  0.25 -0.19 -0.06 0.24  0.00 -0.27 0.15  0.20 -0.04  0.10 0.06
## P3   0.06  0.25  1.00 -0.03  0.24 0.17  0.07 -0.12 0.22 -0.04  0.12  0.10 0.21
## P4  -0.01 -0.19 -0.03  1.00  0.10 0.31  0.14  0.29 0.39 -0.28  0.37  0.05 0.27
## P5  -0.04 -0.06  0.24  0.10  1.00 0.40 -0.04  0.18 0.17 -0.18  0.03  0.14 0.20
## P6   0.40  0.24  0.17  0.31  0.40 1.00  0.04  0.29 0.65  0.39  0.46  0.31 0.42
## P7   0.09  0.00  0.07  0.14 -0.04 0.04  1.00 -0.11 0.16  0.08  0.17  0.25 0.17
## P8  -0.02 -0.27 -0.12  0.29  0.18 0.29 -0.11  1.00 0.30  0.32  0.51  0.20 0.39
## P9   0.49  0.15  0.22  0.39  0.17 0.65  0.16  0.30 1.00  0.23  0.46  0.55 0.46
## P10  0.12  0.20 -0.04 -0.28 -0.18 0.39  0.08  0.32 0.23  1.00  0.41  0.16 0.21
## P11  0.37 -0.04  0.12  0.37  0.03 0.46  0.17  0.51 0.46  0.41  1.00  0.49 0.52
## P12  0.55  0.10  0.10  0.05  0.14 0.31  0.25  0.20 0.55  0.16  0.49  1.00 0.26
## P13  0.12  0.06  0.21  0.27  0.20 0.42  0.17  0.39 0.46  0.21  0.52  0.26 1.00
## P14  0.30 -0.04  0.20  0.42  0.17 0.39  0.25  0.28 0.43  0.21  0.58  0.43 0.75
## P15  0.07  0.21 -0.04  0.31  0.08 0.21  0.06  0.27 0.17  0.09 -0.02 -0.11 0.27
## P16  0.41 -0.01  0.34  0.31  0.26 0.33  0.51  0.11 0.50  0.08  0.45  0.38 0.31
## P17  0.24 -0.23 -0.06  0.55  0.21 0.30  0.35  0.48 0.51  0.05  0.50  0.41 0.58
## P18  0.20 -0.01  0.13  0.42  0.40 0.44  0.12  0.28 0.50  0.02  0.39  0.46 0.60
## P19  0.24 -0.09  0.25  0.27  0.16 0.46  0.30  0.25 0.31  0.25  0.63  0.34 0.41
## P20  0.28 -0.24  0.16  0.37  0.51 0.47  0.17  0.30 0.35  0.16  0.38  0.40 0.24
##       P14   P15   P16   P17   P18   P19   P20
## P1   0.30  0.07  0.41  0.24  0.20  0.24  0.28
## P2  -0.04  0.21 -0.01 -0.23 -0.01 -0.09 -0.24
## P3   0.20 -0.04  0.34 -0.06  0.13  0.25  0.16
## P4   0.42  0.31  0.31  0.55  0.42  0.27  0.37
## P5   0.17  0.08  0.26  0.21  0.40  0.16  0.51
## P6   0.39  0.21  0.33  0.30  0.44  0.46  0.47
## P7   0.25  0.06  0.51  0.35  0.12  0.30  0.17
## P8   0.28  0.27  0.11  0.48  0.28  0.25  0.30
## P9   0.43  0.17  0.50  0.51  0.50  0.31  0.35
## P10  0.21  0.09  0.08  0.05  0.02  0.25  0.16
## P11  0.58 -0.02  0.45  0.50  0.39  0.63  0.38
## P12  0.43 -0.11  0.38  0.41  0.46  0.34  0.40
## P13  0.75  0.27  0.31  0.58  0.60  0.41  0.24
## P14  1.00  0.26  0.45  0.63  0.51  0.51  0.51
## P15  0.26  1.00 -0.16  0.49  0.06 -0.02  0.16
## P16  0.45 -0.16  1.00  0.45  0.58  0.43  0.49
## P17  0.63  0.49  0.45  1.00  0.64  0.33  0.50
## P18  0.51  0.06  0.58  0.64  1.00  0.35  0.44
## P19  0.51 -0.02  0.43  0.33  0.35  1.00  0.28
## P20  0.51  0.16  0.49  0.50  0.44  0.28  1.00
# =========================
# 4. KMO
# =========================
kmo_result <- KMO(cor_poly)
kmo_result
## Kaiser-Meyer-Olkin factor adequacy
## Call: KMO(r = cor_poly)
## Overall MSA =  0.1
## MSA for each item = 
##   P1   P2   P3   P4   P5   P6   P7   P8   P9  P10  P11  P12  P13  P14  P15  P16 
## 0.07 0.02 0.03 0.08 0.05 0.12 0.04 0.08 0.14 0.04 0.14 0.10 0.13 0.15 0.04 0.13 
##  P17  P18  P19  P20 
## 0.16 0.14 0.10 0.11
# KMO global
kmo_result$MSA
## [1] 0.09561506
# KMO por ítem
kmo_result$MSAi
##         P1         P2         P3         P4         P5         P6         P7 
## 0.06800213 0.02494257 0.02826887 0.08424870 0.04927904 0.12203890 0.03951923 
##         P8         P9        P10        P11        P12        P13        P14 
## 0.07639195 0.13588032 0.04368315 0.14298568 0.10266868 0.12847254 0.15122510 
##        P15        P16        P17        P18        P19        P20 
## 0.03820959 0.12547505 0.15669958 0.13571692 0.10334088 0.11432759
# =========================
# 5. Bartlett
# =========================
bart_result <- cortest.bartlett(cor_poly, n = nrow(base_afe))
bart_result
## $chisq
## [1] 1334.438
## 
## $p.value
## [1] 5.510717e-171
## 
## $df
## [1] 190
# =========================
# 6. Determinar número de factores
# =========================
fa.parallel(cor_poly, n.obs = nrow(base_afe), fa = "fa")

## Parallel analysis suggests that the number of factors =  1  and the number of components =  NA
# =========================
# 7. AFE
# Ejemplo con 2 factores
# =========================
afe_2 <- fa(
  r = cor_poly,
  nfactors = 2,
  n.obs = nrow(base_afe),
  fm = "minres",
  rotate = "oblimin"
)

print(afe_2, sort = TRUE)
## Factor Analysis using method =  minres
## Call: fa(r = cor_poly, nfactors = 2, n.obs = nrow(base_afe), rotate = "oblimin", 
##     fm = "minres")
## Standardized loadings (pattern matrix) based upon correlation matrix
##     item   MR1   MR2    h2   u2 com
## P12   12  0.71 -0.08 0.451 0.55 1.0
## P9     9  0.67  0.12 0.550 0.45 1.1
## P1     1  0.67 -0.22 0.343 0.66 1.2
## P16   16  0.64  0.09 0.467 0.53 1.0
## P6     6  0.63  0.08 0.457 0.54 1.0
## P11   11  0.60  0.22 0.539 0.46 1.3
## P19   19  0.54  0.12 0.374 0.63 1.1
## P14   14  0.45  0.44 0.600 0.40 2.0
## P10   10  0.42 -0.17 0.133 0.87 1.3
## P3     3  0.40 -0.20 0.119 0.88 1.5
## P7     7  0.27  0.06 0.093 0.91 1.1
## P17   17  0.10  0.86 0.838 0.16 1.0
## P4     4 -0.09  0.72 0.453 0.55 1.0
## P8     8  0.02  0.54 0.298 0.70 1.0
## P15   15 -0.15  0.44 0.151 0.85 1.2
## P18   18  0.39  0.43 0.515 0.49 2.0
## P13   13  0.36  0.43 0.471 0.53 1.9
## P2     2  0.35 -0.42 0.153 0.85 1.9
## P20   20  0.34  0.39 0.398 0.60 2.0
## P5     5  0.12  0.25 0.111 0.89 1.5
## 
##                        MR1  MR2
## SS loadings           4.32 3.19
## Proportion Var        0.22 0.16
## Cumulative Var        0.22 0.38
## Proportion Explained  0.57 0.43
## Cumulative Proportion 0.57 1.00
## 
##  With factor correlations of 
##      MR1  MR2
## MR1 1.00 0.51
## MR2 0.51 1.00
## 
## Mean item complexity =  1.4
## Test of the hypothesis that 2 factors are sufficient.
## 
## df null model =  190  with the objective function =  35.59 with Chi Square =  1334.44
## df of  the model are 151  and the objective function was  29.07 
## 
## The root mean square of the residuals (RMSR) is  0.08 
## The df corrected root mean square of the residuals is  0.09 
## 
## The harmonic n.obs is  46 with the empirical chi square  100.57  with prob <  1 
## The total n.obs was  46  with Likelihood Chi Square =  1051.49  with prob <  9.5e-135 
## 
## Tucker Lewis Index of factoring reliability =  -0.033
## RMSEA index =  0.359  and the 90 % confidence intervals are  0.343 0.385
## BIC =  473.37
## Fit based upon off diagonal values = 0.95
## Measures of factor score adequacy             
##                                                    MR1  MR2
## Correlation of (regression) scores with factors   0.87 0.87
## Multiple R square of scores with factors          0.76 0.76
## Minimum correlation of possible factor scores     0.53 0.52
fa.parallel(cor_poly, n.obs = nrow(base_afe), fa = "fa")

## Parallel analysis suggests that the number of factors =  1  and the number of components =  NA
afe_1 <- fa(cor_poly, nfactors = 1, n.obs = nrow(base_afe), fm = "minres", rotate = "oblimin")
afe_2 <- fa(cor_poly, nfactors = 2, n.obs = nrow(base_afe), fm = "minres", rotate = "oblimin")
afe_3 <- fa(cor_poly, nfactors = 3, n.obs = nrow(base_afe), fm = "minres", rotate = "oblimin")

print(afe_1, sort = TRUE)
## Factor Analysis using method =  minres
## Call: fa(r = cor_poly, nfactors = 1, n.obs = nrow(base_afe), rotate = "oblimin", 
##     fm = "minres")
## Standardized loadings (pattern matrix) based upon correlation matrix
##      V   MR1      h2   u2 com
## P14 14  0.77 0.60037 0.40   1
## P17 17  0.76 0.57791 0.42   1
## P11 11  0.73 0.52949 0.47   1
## P18 18  0.71 0.50983 0.49   1
## P9   9  0.71 0.50630 0.49   1
## P13 13  0.68 0.46424 0.54   1
## P16 16  0.65 0.42262 0.58   1
## P6   6  0.64 0.41430 0.59   1
## P20 20  0.63 0.39249 0.61   1
## P19 19  0.60 0.35527 0.64   1
## P12 12  0.58 0.33118 0.67   1
## P4   4  0.48 0.22958 0.77   1
## P8   8  0.45 0.20013 0.80   1
## P1   1  0.42 0.17741 0.82   1
## P5   5  0.32 0.10248 0.90   1
## P7   7  0.30 0.08852 0.91   1
## P10 10  0.25 0.06194 0.94   1
## P15 15  0.22 0.04699 0.95   1
## P3   3  0.21 0.04291 0.96   1
## P2   2 -0.02 0.00043 1.00   1
## 
##                 MR1
## SS loadings    6.05
## Proportion Var 0.30
## 
## Mean item complexity =  1
## Test of the hypothesis that 1 factor is sufficient.
## 
## df null model =  190  with the objective function =  35.59 with Chi Square =  1334.44
## df of  the model are 170  and the objective function was  30.06 
## 
## The root mean square of the residuals (RMSR) is  0.09 
## The df corrected root mean square of the residuals is  0.09 
## 
## The harmonic n.obs is  46 with the empirical chi square  140.68  with prob <  0.95 
## The total n.obs was  46  with Likelihood Chi Square =  1107.33  with prob <  3.4e-137 
## 
## Tucker Lewis Index of factoring reliability =  0.065
## RMSEA index =  0.346  and the 90 % confidence intervals are  0.331 0.37
## BIC =  456.46
## Fit based upon off diagonal values = 0.92
## Measures of factor score adequacy             
##                                                    MR1
## Correlation of (regression) scores with factors   0.96
## Multiple R square of scores with factors          0.92
## Minimum correlation of possible factor scores     0.84
print(afe_2, sort = TRUE)
## Factor Analysis using method =  minres
## Call: fa(r = cor_poly, nfactors = 2, n.obs = nrow(base_afe), rotate = "oblimin", 
##     fm = "minres")
## Standardized loadings (pattern matrix) based upon correlation matrix
##     item   MR1   MR2    h2   u2 com
## P12   12  0.71 -0.08 0.451 0.55 1.0
## P9     9  0.67  0.12 0.550 0.45 1.1
## P1     1  0.67 -0.22 0.343 0.66 1.2
## P16   16  0.64  0.09 0.467 0.53 1.0
## P6     6  0.63  0.08 0.457 0.54 1.0
## P11   11  0.60  0.22 0.539 0.46 1.3
## P19   19  0.54  0.12 0.374 0.63 1.1
## P14   14  0.45  0.44 0.600 0.40 2.0
## P10   10  0.42 -0.17 0.133 0.87 1.3
## P3     3  0.40 -0.20 0.119 0.88 1.5
## P7     7  0.27  0.06 0.093 0.91 1.1
## P17   17  0.10  0.86 0.838 0.16 1.0
## P4     4 -0.09  0.72 0.453 0.55 1.0
## P8     8  0.02  0.54 0.298 0.70 1.0
## P15   15 -0.15  0.44 0.151 0.85 1.2
## P18   18  0.39  0.43 0.515 0.49 2.0
## P13   13  0.36  0.43 0.471 0.53 1.9
## P2     2  0.35 -0.42 0.153 0.85 1.9
## P20   20  0.34  0.39 0.398 0.60 2.0
## P5     5  0.12  0.25 0.111 0.89 1.5
## 
##                        MR1  MR2
## SS loadings           4.32 3.19
## Proportion Var        0.22 0.16
## Cumulative Var        0.22 0.38
## Proportion Explained  0.57 0.43
## Cumulative Proportion 0.57 1.00
## 
##  With factor correlations of 
##      MR1  MR2
## MR1 1.00 0.51
## MR2 0.51 1.00
## 
## Mean item complexity =  1.4
## Test of the hypothesis that 2 factors are sufficient.
## 
## df null model =  190  with the objective function =  35.59 with Chi Square =  1334.44
## df of  the model are 151  and the objective function was  29.07 
## 
## The root mean square of the residuals (RMSR) is  0.08 
## The df corrected root mean square of the residuals is  0.09 
## 
## The harmonic n.obs is  46 with the empirical chi square  100.57  with prob <  1 
## The total n.obs was  46  with Likelihood Chi Square =  1051.49  with prob <  9.5e-135 
## 
## Tucker Lewis Index of factoring reliability =  -0.033
## RMSEA index =  0.359  and the 90 % confidence intervals are  0.343 0.385
## BIC =  473.37
## Fit based upon off diagonal values = 0.95
## Measures of factor score adequacy             
##                                                    MR1  MR2
## Correlation of (regression) scores with factors   0.87 0.87
## Multiple R square of scores with factors          0.76 0.76
## Minimum correlation of possible factor scores     0.53 0.52
print(afe_3, sort = TRUE)
## Factor Analysis using method =  minres
## Call: fa(r = cor_poly, nfactors = 3, n.obs = nrow(base_afe), rotate = "oblimin", 
##     fm = "minres")
## Standardized loadings (pattern matrix) based upon correlation matrix
##     item   MR1   MR2   MR3   h2   u2 com
## P16   16  0.85  0.03 -0.13 0.72 0.28 1.1
## P12   12  0.59  0.00  0.21 0.44 0.56 1.3
## P1     1  0.53 -0.14  0.23 0.32 0.68 1.5
## P9     9  0.51  0.21  0.26 0.54 0.46 1.9
## P18   18  0.47  0.44 -0.09 0.57 0.43 2.1
## P3     3  0.46 -0.20 -0.02 0.17 0.83 1.4
## P19   19  0.41  0.18  0.23 0.37 0.63 2.0
## P20   20  0.39  0.39 -0.05 0.42 0.58 2.0
## P6     6  0.39  0.20  0.37 0.48 0.52 2.5
## P7     7  0.36  0.03 -0.06 0.13 0.87 1.1
## P5     5  0.26  0.23 -0.21 0.18 0.82 2.9
## P17   17  0.12  0.85 -0.05 0.81 0.19 1.0
## P4     4  0.05  0.69 -0.27 0.49 0.51 1.3
## P8     8 -0.25  0.66  0.30 0.50 0.50 1.7
## P13   13  0.19  0.51  0.24 0.50 0.50 1.7
## P15   15 -0.28  0.50  0.10 0.23 0.77 1.7
## P14   14  0.34  0.50  0.17 0.60 0.40 2.0
## P2     2  0.18 -0.35  0.28 0.16 0.84 2.4
## P10   10 -0.03 -0.07  0.81 0.63 0.37 1.0
## P11   11  0.33  0.33  0.43 0.61 0.39 2.8
## 
##                        MR1  MR2  MR3
## SS loadings           3.56 3.55 1.75
## Proportion Var        0.18 0.18 0.09
## Cumulative Var        0.18 0.36 0.44
## Proportion Explained  0.40 0.40 0.20
## Cumulative Proportion 0.40 0.80 1.00
## 
##  With factor correlations of 
##      MR1  MR2  MR3
## MR1 1.00 0.44 0.19
## MR2 0.44 1.00 0.20
## MR3 0.19 0.20 1.00
## 
## Mean item complexity =  1.8
## Test of the hypothesis that 3 factors are sufficient.
## 
## df null model =  190  with the objective function =  35.59 with Chi Square =  1334.44
## df of  the model are 133  and the objective function was  28.25 
## 
## The root mean square of the residuals (RMSR) is  0.06 
## The df corrected root mean square of the residuals is  0.08 
## 
## The harmonic n.obs is  46 with the empirical chi square  72.77  with prob <  1 
## The total n.obs was  46  with Likelihood Chi Square =  1002.88  with prob <  2.1e-133 
## 
## Tucker Lewis Index of factoring reliability =  -0.158
## RMSEA index =  0.376  and the 90 % confidence intervals are  0.359 0.403
## BIC =  493.68
## Fit based upon off diagonal values = 0.96
## Measures of factor score adequacy             
##                                                    MR1  MR2  MR3
## Correlation of (regression) scores with factors   0.88 0.89 0.87
## Multiple R square of scores with factors          0.78 0.79 0.76
## Minimum correlation of possible factor scores     0.55 0.58 0.51

GRAFICOS ENTRE VARIABLES

library(esquisse)

#esquisser(datost)

library(ggplot2)

ggplot(datost) +
 aes(x = P16, fill = P18) +
 geom_bar() +
 scale_fill_hue(direction = 1) +
 theme_minimal()

ggplot(datost) +
 aes(x = P2, fill = P8) +
 geom_bar() +
 scale_fill_hue(direction = 1) +
 theme_minimal()

ggplot(datost) +
 aes(x = P1, fill = P20) +
 geom_bar() +
 scale_fill_hue(direction = 1) +
 theme_minimal()

graficos entre de variable cruzada

ggplot(datost) +
 aes(x = P7, fill = P8) +
 geom_bar() +
 scale_fill_hue(direction = 1) +
 theme_minimal()

ggplot(datost) +
 aes(x = P7, fill = CURSO) +
 geom_bar() +
 scale_fill_hue(direction = 1) +
 theme_minimal()

ggplot(datost) +
 aes(x = P5, fill = CURSO) +
 geom_bar() +
 scale_fill_hue(direction = 1) +
 theme_minimal()

ggplot(datost) +
 aes(x = P8, fill = CURSO) +
 geom_bar() +
 scale_fill_hue(direction = 1) +
 theme_minimal()

ggplot(datost) +
 aes(x = P11, fill = CURSO) +
 geom_bar() +
 scale_fill_hue(direction = 1) +
 theme_minimal() 

ggplot(datost) +
  aes(x = P14, fill = P18) +
  geom_bar() +
  scale_fill_hue(direction = 1) +
  theme_minimal()