Data import

The source is Nguyen’s (2026) Mendeley Data file: 1,558 respondents, 32 five-point items (13 TR, 4 ASLE, 4 FSC, 11 SPW), and five demographic variables.

DATA_URL  <- "https://data.mendeley.com/public-files/datasets/zvdf6893rk/files/1e1789df-1b0f-4834-a23f-2d5a931e5448/file_downloaded"
DATA_FILE <- Sys.getenv("MIDTERM_DATA",
                        file.path(tempdir(), "student_wellbeing_higher_education.xlsx"))
if (!file.exists(DATA_FILE)) download.file(DATA_URL, DATA_FILE, mode = "wb", quiet = TRUE)

raw <- readxl::read_excel(DATA_FILE, sheet = 1, .name_repair = "minimal")
names(raw) <- stringr::str_squish(names(raw))
# Two demographic headers are mislabelled in the source file
rename_map <- c("truong hocAcademic year" = "Type of university",
                "Type of institution"     = "Year of study")
hit <- names(raw) %in% names(rename_map)
names(raw)[hit] <- rename_map[names(raw)[hit]]

dim(raw)
## [1] 1558   37

Variables and coding

Item columns are identified by their prefix. Demographic codes are replaced with the questionnaire labels.

# Item blocks, in the order used throughout
all_items   <- grep("^(TR|ASLE|FSC|SPW)\\s*\\d+$", names(raw), value = TRUE)
item_blocks <- split(all_items, factor(sub("\\s*\\d+$", "", all_items),
                                       levels = c("TR", "ASLE", "FSC", "SPW")))
stopifnot(identical(unname(lengths(item_blocks)), c(13L, 4L, 4L, 11L)))
item_cols <- unlist(item_blocks, use.names = FALSE)
construct <- factor(rep(names(item_blocks), lengths(item_blocks)), levels = names(item_blocks))

# Demographic columns (located by name so small header differences do not break the code)
find_col <- function(pattern) {
  m <- grep(pattern, names(raw), ignore.case = TRUE, value = TRUE)
  if (length(m) != 1) stop("Expected one column matching '", pattern, "', found: ",
                           paste(m, collapse = ", "))
  m
}
demo_cols <- c(
  "Gender"                     = find_col("gender"),
  "Type of university"         = find_col("^Type of university$"),
  "Year of study"              = find_col("^Year of study$"),
  "Major"                      = find_col("major"),
  "Duration of regular AI use" = find_col("duration")
)

# Questionnaire labels (code = label), in the same order as demo_cols
demo_labels <- list(
  "Gender"                     = c("1" = "Male", "2" = "Female", "3" = "Other"),
  "Type of university"         = c("1" = "Private/non-public", "2" = "Public"),
  "Year of study"              = c("1" = "Year 1", "2" = "Year 2", "3" = "Year 3",
                                   "4" = "Year 4", "5" = "Other"),
  "Major"                      = c("1" = "Education", "2" = "Economics", "3" = "Engineering",
                                   "4" = "Languages", "5" = "Other"),
  "Duration of regular AI use" = c("1" = "Less than 1 year", "2" = "From 1 to under 2 years",
                                   "3" = "2 years or more")
)

apply_labels <- function(x, labs) {
  x <- as.character(x)
  unknown <- setdiff(unique(na.omit(x)), names(labs))
  if (length(unknown)) stop("Codes without a label: ", paste(unknown, collapse = ", "))
  droplevels(factor(unname(labs[x]), levels = labs))  # keep questionnaire order
}

demo <- as.data.frame(Map(apply_labels, setNames(raw[demo_cols], names(demo_cols)), demo_labels),
                      check.names = FALSE)

lapply(demo, table, useNA = "ifany")
## $Gender
## 
##   Male Female 
##    112   1446 
## 
## $`Type of university`
## 
## Private/non-public             Public 
##                 85               1473 
## 
## $`Year of study`
## 
## Year 1 Year 2 Year 3 Year 4  Other 
##    529    477    330    220      2 
## 
## $Major
## 
##   Education   Economics Engineering   Languages       Other 
##        1498          17          36           4           3 
## 
## $`Duration of regular AI use`
## 
##        Less than 1 year From 1 to under 2 years         2 years or more 
##                     285                     613                     660

Data checks and descriptive analysis

X <- data.matrix(raw[, item_cols])
rownames(X) <- paste0("S", seq_len(nrow(X)))

stopifnot(!anyNA(X), all(X %in% 1:5))

cat("Respondents giving the same answer to all 32 items:", sum(apply(X, 1, var) == 0), "\n")
## Respondents giving the same answer to all 32 items: 0

Reliability is checked before items are summarized. A negative corrected item–total correlation would suggest a reverse-coded item.

cronbach_alpha <- function(m) {
  k <- ncol(m)
  k / (k - 1) * (1 - sum(apply(m, 2, var)) / var(rowSums(m)))
}
min_item_total <- function(m) {
  min(sapply(seq_len(ncol(m)), function(j) cor(m[, j], rowSums(m[, -j, drop = FALSE]))))
}

scores <- sapply(item_blocks, function(cols) rowMeans(X[, cols, drop = FALSE]))

desc <- data.frame(
  Construct = names(item_blocks),
  Items     = lengths(item_blocks),
  Mean      = colMeans(scores),
  SD        = apply(scores, 2, sd),
  Alpha     = sapply(item_blocks, function(cols) cronbach_alpha(X[, cols])),
  Min_item_total_r = sapply(item_blocks, function(cols) min_item_total(X[, cols]))
)
knitr::kable(desc, digits = 2, row.names = FALSE,
             caption = "Construct means (1–5), Cronbach's alpha, and lowest corrected item–total correlation")
Construct means (1–5), Cronbach’s alpha, and lowest corrected item–total correlation
Construct Items Mean SD Alpha Min_item_total_r
TR 13 3.96 0.73 0.96 0.75
ASLE 4 3.88 0.76 0.88 0.72
FSC 4 3.89 0.74 0.88 0.71
SPW 11 3.81 0.70 0.94 0.69
round(cor(scores), 2)
##        TR ASLE  FSC  SPW
## TR   1.00 0.72 0.73 0.68
## ASLE 0.72 1.00 0.74 0.69
## FSC  0.73 0.74 1.00 0.74
## SPW  0.68 0.69 0.74 1.00

Descriptive statistics for study variables

Item-level descriptives for all 32 items. Missing n is counted in the raw file before any processing.

# Write a table to CSV, formatting the named columns with sprintf (e.g. to keep trailing zeros)
save_csv <- function(df, file, fmt) {
  df[names(fmt)] <- Map(sprintf, fmt, df[names(fmt)])
  write.csv(df, file, row.names = FALSE, fileEncoding = "UTF-8")
}

# Item wording. FSC and SPW are from the questionnaire;
# TODO: replace the TR and ASLE placeholders with the questionnaire wording.
item_labels <- c(
  setNames(paste0("[TODO: wording for TR", 1:13, "]"), paste0("TR", 1:13)),
  setNames(paste0("[TODO: wording for ASLE", 1:4, "]"), paste0("ASLE", 1:4)),
  FSC1  = "Exchanges ideas with peers",
  FSC2  = "Coordinates and divides tasks",
  FSC3  = "Discusses tasks with peers",
  FSC4  = "Peer feedback improves assignments",
  SPW1  = "Feels optimistic",
  SPW2  = "Feels useful",
  SPW3  = "Feels relaxed",
  SPW4  = "Has plenty of energy",
  SPW5  = "Deals with problems well",
  SPW6  = "Thinks clearly",
  SPW7  = "Feels good about self",
  SPW8  = "Feels confident",
  SPW9  = "Makes decisions independently",
  SPW10 = "Interested in new things",
  SPW11 = "Feels cheerful"
)
construct_names <- c(TR   = "Teacher responsibility (TR)",
                     ASLE = "AI-supported learning effectiveness (ASLE)",
                     FSC  = "Fellow-student collaboration (FSC)",
                     SPW  = "Students' psychological well-being (SPW)")

item_desc <- data.frame(
  Construct           = construct_names[as.character(construct)],
  `Descriptive label` = item_labels[item_cols],
  Variable            = item_cols,
  Min = apply(X, 2, min), Max = apply(X, 2, max),
  M   = colMeans(X),      SD  = apply(X, 2, sd),
  `Missing n`         = colSums(is.na(raw[item_cols])),
  check.names = FALSE, row.names = NULL
)

knitr::kable(item_desc, digits = 2, row.names = FALSE,
             caption = sprintf("Descriptive statistics for study variables (N = %d)", nrow(raw)))
Descriptive statistics for study variables (N = 1558)
Construct Descriptive label Variable Min Max M SD Missing n
Teacher responsibility (TR) [TODO: wording for TR1] TR1 1 5 3.95 0.95 0
Teacher responsibility (TR) [TODO: wording for TR2] TR2 1 5 4.00 0.94 0
Teacher responsibility (TR) [TODO: wording for TR3] TR3 1 5 3.93 0.91 0
Teacher responsibility (TR) [TODO: wording for TR4] TR4 1 5 3.96 0.88 0
Teacher responsibility (TR) [TODO: wording for TR5] TR5 1 5 4.01 0.88 0
Teacher responsibility (TR) [TODO: wording for TR6] TR6 1 5 3.93 0.91 0
Teacher responsibility (TR) [TODO: wording for TR7] TR7 1 5 3.91 0.92 0
Teacher responsibility (TR) [TODO: wording for TR8] TR8 1 5 3.96 0.90 0
Teacher responsibility (TR) [TODO: wording for TR9] TR9 1 5 3.96 0.88 0
Teacher responsibility (TR) [TODO: wording for TR10] TR10 1 5 3.91 0.89 0
Teacher responsibility (TR) [TODO: wording for TR11] TR11 1 5 3.98 0.91 0
Teacher responsibility (TR) [TODO: wording for TR12] TR12 1 5 4.00 0.89 0
Teacher responsibility (TR) [TODO: wording for TR13] TR13 1 5 3.97 0.89 0
AI-supported learning effectiveness (ASLE) [TODO: wording for ASLE1] ASLE1 1 5 3.86 0.90 0
AI-supported learning effectiveness (ASLE) [TODO: wording for ASLE2] ASLE2 1 5 3.90 0.89 0
AI-supported learning effectiveness (ASLE) [TODO: wording for ASLE3] ASLE3 1 5 3.85 0.90 0
AI-supported learning effectiveness (ASLE) [TODO: wording for ASLE4] ASLE4 1 5 3.90 0.87 0
Fellow-student collaboration (FSC) Exchanges ideas with peers FSC1 1 5 3.82 0.89 0
Fellow-student collaboration (FSC) Coordinates and divides tasks FSC2 1 5 3.89 0.86 0
Fellow-student collaboration (FSC) Discusses tasks with peers FSC3 1 5 3.93 0.84 0
Fellow-student collaboration (FSC) Peer feedback improves assignments FSC4 1 5 3.93 0.86 0
Students’ psychological well-being (SPW) Feels optimistic SPW1 1 5 3.75 0.92 0
Students’ psychological well-being (SPW) Feels useful SPW2 1 5 3.90 0.86 0
Students’ psychological well-being (SPW) Feels relaxed SPW3 1 5 3.83 0.92 0
Students’ psychological well-being (SPW) Has plenty of energy SPW4 1 5 3.79 0.92 0
Students’ psychological well-being (SPW) Deals with problems well SPW5 1 5 3.77 0.88 0
Students’ psychological well-being (SPW) Thinks clearly SPW6 1 5 3.79 0.86 0
Students’ psychological well-being (SPW) Feels good about self SPW7 1 5 3.82 0.87 0
Students’ psychological well-being (SPW) Feels confident SPW8 1 5 3.74 0.89 0
Students’ psychological well-being (SPW) Makes decisions independently SPW9 1 5 3.75 0.87 0
Students’ psychological well-being (SPW) Interested in new things SPW10 1 5 3.92 0.87 0
Students’ psychological well-being (SPW) Feels cheerful SPW11 1 5 3.87 0.87 0
save_csv(item_desc, "table_item_descriptives.csv", c(M = "%.2f", SD = "%.2f"))

Demographic characteristics

# Variable name and missing count are shown only on each variable's first row
demo_desc <- do.call(rbind, lapply(names(demo_cols), function(v) {
  counts <- table(demo[[v]])                     
  blanks <- rep("", length(counts) - 1)
  data.frame(Variable = c(v, blanks), Category = names(counts), n = as.vector(counts),
             `%` = round(100 * as.vector(counts) / nrow(raw), 1),
             `Missing n` = c(sum(is.na(raw[[demo_cols[[v]]]])), blanks),
             check.names = FALSE)
}))

knitr::kable(demo_desc, digits = 1, row.names = FALSE,
             caption = sprintf("Demographic characteristics of the sample (N = %d)", nrow(raw)))
Demographic characteristics of the sample (N = 1558)
Variable Category n % Missing n
Gender Male 112 7.2 0
Female 1446 92.8
Type of university Private/non-public 85 5.5 0
Public 1473 94.5
Year of study Year 1 529 34.0 0
Year 2 477 30.6
Year 3 330 21.2
Year 4 220 14.1
Other 2 0.1
Major Education 1498 96.1 0
Economics 17 1.1
Engineering 36 2.3
Languages 4 0.3
Other 3 0.2
Duration of regular AI use Less than 1 year 285 18.3 0
From 1 to under 2 years 613 39.3
2 years or more 660 42.4
save_csv(demo_desc, "table_demographics.csv", c(`%` = "%.1f"))

Clustering matrix

cluster_blocks <- item_blocks[c("TR", "ASLE", "FSC")]
cluster_items  <- unlist(cluster_blocks, use.names = FALSE)
weights        <- rep(1 / sqrt(lengths(cluster_blocks)), lengths(cluster_blocks))

X_cluster <- sweep(scale(X[, cluster_items]), 2, weights, `*`)

uncentered_cor_dist <- function(m) {
  norm <- sqrt(rowSums(m^2))
  if (any(norm == 0)) stop("Some rows are all zero; correlation is undefined.")
  pmax(1 - tcrossprod(m / norm), 0)
}
D_cor <- uncentered_cor_dist(X_cluster)   

leaf_order <- function(merge, node) {
  out <- integer(0); stack <- node
  while (length(stack)) {
    j <- stack[1]; stack <- stack[-1]
    if (j < 0) out <- c(out, -j) else stack <- c(merge[j, ], stack)
  }
  out
}
leaves_under <- function(hc, node) hc$labels[leaf_order(hc$merge, node)]

orient_dend <- function(hc, score) {
  s <- score[hc$labels]
  m <- hc$merge
  tot <- cnt <- numeric(nrow(m))
  side <- function(j) if (j < 0) c(s[-j], 1) else c(tot[j], cnt[j])
  for (i in seq_len(nrow(m))) {
    a <- side(m[i, 1]); b <- side(m[i, 2])
    if (a[1] / a[2] < b[1] / b[2]) m[i, ] <- m[i, 2:1]
    tot[i] <- a[1] + b[1]; cnt[i] <- a[2] + b[2]
  }
  hc$merge <- m
  hc$order <- leaf_order(m, nrow(m))
  hc
}

agreement  <- rowMeans(X[, cluster_items])   # mean 1-5 answer on the 21 clustering items
hc_full    <- orient_dend(hclust(as.dist(D_cor), method = "average"), agreement)
n_students <- length(hc_full$order)

X_heat         <- X[, cluster_items]
construct_heat <- factor(rep(names(cluster_blocks), lengths(cluster_blocks)),
                         levels = names(cluster_blocks))
response_col   <- colorRamp2(1:5, c("#2166AC", "#92C5DE", "#F7F7F7", "#F4A582", "#B2182B"))
spw_col        <- colorRamp2(c(1, 3, 5), c("#F7FCF5", "#74C476", "#00441B"))

response_heatmap <- function(...) Heatmap(
  X_heat, name = "Response", col = response_col,
  heatmap_legend_param = list(at = 1:5, labels = c("1 Strongly disagree", "2", "3 Neutral",
                                                  "4", "5 Strongly agree")),
  column_split = construct_heat, column_title = levels(construct_heat),
  show_row_names = FALSE, column_names_gp = gpar(fontsize = 8), column_gap = unit(2, "mm"),
  ...
)

row_annotation <- function(extra = NULL, extra_col = list(), size = unit(5, "mm")) {
  df <- data.frame(`Mean SPW` = scores[, "SPW"], check.names = FALSE)
  if (!is.null(extra)) df <- data.frame(extra, df, check.names = FALSE)
  rowAnnotation(
  df  = df,
  col = c(extra_col, list(`Mean SPW` = spw_col)),
  annotation_legend_param = list(`Mean SPW` = list(at = 1:5)),
  simple_anno_size = size,
  gap = unit(1.5, "mm"),           
  annotation_name_side = "bottom", annotation_name_gp = gpar(fontsize = 10)
  )
}

# Draw the heatmap and save the same figure to PDF
draw_and_save <- function(ht, file, width, height) {
  show <- function() draw(ht, heatmap_legend_side = "bottom", annotation_legend_side = "bottom",
                          merge_legend = TRUE)
  show()
  pdf(file, width = width, height = height); show(); invisible(dev.off())
}

Merge table for the highest nodes

gap above is the parent’s height minus this node’s height. A large gap means the subtree stays separate for a long stretch of the tree before it joins anything else, which is a sign that it is a distinct group.

top_nodes <- (n_students - 1):(n_students - 25)   # the 25 highest merges
branch_label <- function(j) {
  if (j < 0) return(sprintf("student %s (n = 1)", hc_full$labels[-j]))
  sprintf("node %d (n = %d)", j, length(leaves_under(hc_full, j)))
}
parent_of <- integer(n_students - 1)
is_node   <- hc_full$merge > 0
parent_of[hc_full$merge[is_node]] <- row(hc_full$merge)[is_node]

merge_table <- do.call(rbind, lapply(top_nodes, function(i) {
  ids <- leaves_under(hc_full, i)
  data.frame(node = i,
             height = hc_full$height[i],
             gap_above = if (parent_of[i] > 0) hc_full$height[parent_of[i]] - hc_full$height[i] else NA,
             n = length(ids),
             mean_agreement = mean(agreement[ids]),
             first_branch  = branch_label(hc_full$merge[i, 1]),
             second_branch = branch_label(hc_full$merge[i, 2]))
}))
knitr::kable(merge_table, digits = 3, row.names = FALSE,
             caption = "Highest merges (first branch = the one drawn on top / left)")
Highest merges (first branch = the one drawn on top / left)
node height gap_above n mean_agreement first_branch second_branch
1557 1.287 NA 1558 3.932 node 1554 (n = 851) node 1556 (n = 707)
1556 1.001 0.286 707 3.433 node 1553 (n = 125) node 1555 (n = 582)
1555 0.994 0.007 582 3.314 node 1412 (n = 2) node 1552 (n = 580)
1554 0.972 0.315 851 4.346 node 1551 (n = 804) node 1548 (n = 47)
1553 0.956 0.044 125 3.987 node 1544 (n = 85) node 1542 (n = 40)
1552 0.946 0.048 580 3.312 node 1547 (n = 124) node 1546 (n = 456)
1551 0.923 0.049 804 4.369 node 1550 (n = 773) node 1535 (n = 31)
1550 0.892 0.031 773 4.381 node 1549 (n = 768) node 1518 (n = 5)
1549 0.883 0.009 768 4.383 node 1543 (n = 681) node 1534 (n = 87)
1548 0.875 0.097 47 3.963 node 1528 (n = 16) node 1545 (n = 31)
1547 0.865 0.081 124 3.941 node 1522 (n = 53) node 1537 (n = 71)
1546 0.856 0.090 456 3.141 node 1517 (n = 4) node 1540 (n = 452)
1545 0.837 0.038 31 3.868 node 1492 (n = 4) node 1530 (n = 27)
1544 0.833 0.124 85 4.036 node 1520 (n = 54) node 1539 (n = 31)
1543 0.821 0.062 681 4.451 node 1541 (n = 674) node 1511 (n = 7)
1542 0.809 0.147 40 3.883 node 1503 (n = 6) node 1538 (n = 34)
1541 0.809 0.012 674 4.456 node 1536 (n = 664) node 1449 (n = 10)
1540 0.801 0.055 452 3.137 node 1505 (n = 13) node 1533 (n = 439)
1539 0.799 0.034 31 3.919 node 1411 (n = 4) node 1524 (n = 27)
1538 0.794 0.015 34 3.847 node 1498 (n = 8) node 1521 (n = 26)
1537 0.793 0.073 71 3.922 node 1523 (n = 57) node 1466 (n = 14)
1536 0.791 0.018 664 4.464 node 1529 (n = 659) node 1482 (n = 5)
1535 0.787 0.137 31 4.063 node 1509 (n = 23) node 1515 (n = 8)
1534 0.782 0.101 87 3.852 node 1532 (n = 18) node 1512 (n = 69)
1533 0.775 0.026 439 3.119 node 1514 (n = 18) node 1531 (n = 421)

Full heatmap, one tree for everyone

All students in one tree, no split. Only the 21 clustering items are shown; mean SPW (not used in clustering) is shown as an annotation.

ht_full <- response_heatmap(
  cluster_rows = hc_full, row_dend_reorder = FALSE, row_dend_width = unit(6, "cm"),
  cluster_columns = FALSE,
  right_annotation = row_annotation(size = unit(6, "mm")),
  use_raster = TRUE, raster_quality = 4,
  raster_device = "png", raster_device_param = list(type = "quartz")
)
draw_and_save(ht_full, "heatmap_full_tree.pdf", width = 20, height = 18)

Cutting the tree into clusters

Based on the tree and the merge table above, the tree is cut into 2 clusters, the clearest split in the tree (the root merge, with by far the largest height gap). Clusters are numbered by their mean score on TR, ASLE, and FSC (Cluster 1 = highest).

K <- 2
cluster_id <- cutree(hc_full, k = K)[rownames(X)]

student_score  <- rowMeans(scores[, c("TR", "ASLE", "FSC")])
original_order <- names(sort(tapply(student_score, cluster_id, mean), decreasing = TRUE))
cluster_rank   <- factor(match(as.character(cluster_id), original_order),
                         levels = seq_len(K), labels = paste("Cluster", seq_len(K)))
cluster_colors <- setNames(c("#4477AA", "#228833", "#CCBB44", "#AA3377",
                             "#66CCEE", "#EE6677", "#BBBBBB", "#000000")[seq_len(K)],
                           levels(cluster_rank))
table(cluster_rank)
## cluster_rank
## Cluster 1 Cluster 2 
##       851       707

RQ1: Cluster profiles

Three difference scores compare perceived AI benefit (ASLE) with human support. For each, positive values mean the student rates AI benefit above that kind of human support, 0 means equal, and negative values mean human support is rated higher.

  • Balance = ASLE − mean(TR, FSC): AI vs. human support overall (teachers and peers weighted equally).
  • ASLE − TR: AI vs. teacher responsibility.
  • ASLE − FSC: AI vs. fellow-student collaboration.

Splitting Balance matters because the two parts can cancel out: a student who rates AI above peers but below teachers can still have Balance ≈ 0.

Overall is the mean of all 21 clustering items; if clusters differ mainly on Overall and little on the difference scores, they mostly reflect a general tendency to agree.

diff_scores <- data.frame(
  Balance      = scores[, "ASLE"] - (scores[, "TR"] + scores[, "FSC"]) / 2,
  `ASLE − TR`  = scores[, "ASLE"] - scores[, "TR"],
  `ASLE − FSC` = scores[, "ASLE"] - scores[, "FSC"],
  check.names = FALSE
)

by_cluster <- function(df, f) {
  do.call(rbind, lapply(names(df), function(v) {
    do.call(rbind, lapply(levels(cluster_rank), function(g) {
      in_g <- cluster_rank == g
      data.frame(Measure = v, Cluster = factor(g, levels(cluster_rank)),
                 f(df[in_g, v], in_g), check.names = FALSE)
    }))
  }))
}
ci95    <- function(x) qt(0.975, length(x) - 1) * sd(x) / sqrt(length(x))
mean_ci <- function(x, ...) data.frame(Mean = mean(x), Lower = mean(x) - ci95(x),
                                       Upper = mean(x) + ci95(x))

profile_vars <- data.frame(scores, Overall = student_score, diff_scores, check.names = FALSE)
profile <- data.frame(Cluster = levels(cluster_rank),
                      n   = as.vector(table(cluster_rank)),
                      `%` = as.vector(100 * prop.table(table(cluster_rank))),
                      t(sapply(split(profile_vars, cluster_rank), colMeans)),
                      check.names = FALSE)
knitr::kable(profile, digits = 2, row.names = FALSE,
             caption = "Mean construct scores (1–5) and difference scores by cluster")
Mean construct scores (1–5) and difference scores by cluster
Cluster n % TR ASLE FSC SPW Overall Balance ASLE − TR ASLE − FSC
Cluster 1 851 54.62 4.35 4.32 4.36 4.16 4.34 -0.04 -0.03 -0.04
Cluster 2 707 45.38 3.49 3.34 3.33 3.39 3.39 -0.07 -0.15 0.02
# Share of students leaning towards AI (> 0), equal (= 0), or towards human support (< 0)
lean_table <- by_cluster(diff_scores, function(x, ...) data.frame(
  `AI-leaning (> 0) %`    = 100 * mean(x > 1e-9),
  `Equal (= 0) %`         = 100 * mean(abs(x) <= 1e-9),
  `Human-leaning (< 0) %` = 100 * mean(x < -1e-9),
  check.names = FALSE))
knitr::kable(lean_table, digits = 1, row.names = FALSE,
             caption = "Direction of each difference score within each cluster (% of students)")
Direction of each difference score within each cluster (% of students)
Measure Cluster AI-leaning (> 0) % Equal (= 0) % Human-leaning (< 0) %
Balance Cluster 1 44.3 14.1 41.6
Balance Cluster 2 43.0 4.5 52.5
ASLE − TR Cluster 1 43.7 17.2 39.1
ASLE − TR Cluster 2 38.3 6.8 54.9
ASLE − FSC Cluster 1 22.6 47.6 29.8
ASLE − FSC Cluster 2 39.2 26.6 34.2
profile_long <- by_cluster(data.frame(scores, diff_scores, check.names = FALSE), mean_ci)

# Readable axis labels
construct_labels <- c(TR   = "Teacher\nresponsibility\n(TR)",
                      ASLE = "AI-supported\nlearning\n(ASLE)",
                      FSC  = "Fellow-student\ncollaboration\n(FSC)",
                      SPW  = "Psychological\nwell-being\n(SPW)*")
diff_labels <- c(Balance      = "AI vs. human\nsupport overall\n(Balance)",
                 `ASLE − TR`  = "AI vs.\nteachers\n(ASLE − TR)",
                 `ASLE − FSC` = "AI vs.\npeers\n(ASLE − FSC)")

# Rows of profile_long for the given measures
profile_subset <- function(labels) {
  d <- subset(profile_long, Measure %in% names(labels))
  d$Measure <- factor(d$Measure, levels = names(labels), labels = labels)
  d
}

Heatmap with clustered items

cluster_students <- function(m) {
  ids <- rownames(m)
  orient_dend(hclust(as.dist(D_cor[ids, ids, drop = FALSE]), method = "average"), agreement)
}

item_mean <- colMeans(X)
cluster_items_fun <- function(m) {
  D <- uncentered_cor_dist(t(scale(t(m))))   # standardize each item across students
  orient_dend(hclust(as.dist(D), method = "average"), item_mean)
}

demo_palette <- c("#7896B5", "#D5AC87", "#8FAE9C", "#B49AB8",
                  "#C68D91", "#A6ADB5", "#C8BD8A", "#87B4B5")
demo_colors  <- lapply(demo, function(z) setNames(demo_palette[seq_len(nlevels(z))], levels(z)))

ht_items <- response_heatmap(
  row_split = cluster_rank, cluster_row_slices = FALSE, row_title_rot = 0,
  cluster_rows = cluster_students, row_dend_reorder = FALSE, row_dend_width = unit(2, "cm"),
  cluster_column_slices = FALSE,
  cluster_columns = cluster_items_fun, column_dend_reorder = FALSE,
  row_gap = unit(2, "mm"),
  right_annotation = row_annotation(demo, demo_colors)
)
draw_and_save(ht_items, "heatmap_full_items_clustered.pdf", width = 20, height = 19)

fig_constructs <- ggplot(profile_subset(construct_labels),
                         aes(Measure, Mean, colour = Cluster, group = Cluster)) +
  geom_line(linewidth = 0.9) +
  geom_errorbar(aes(ymin = Lower, ymax = Upper), width = 0.12) +
  geom_point(size = 3) +
  geom_text(aes(label = sprintf("%.2f", Mean)), vjust = -1.1, size = 3.6, show.legend = FALSE) +
  scale_colour_manual(values = cluster_colors) +
  scale_y_continuous(limits = c(1, 5), breaks = 1:5,
                     labels = c("1\nStrongly disagree", "2", "3\nNeutral", "4", "5\nStrongly agree")) +
  labs(x = NULL, y = "Mean response (95% CI)", colour = NULL,
       title = "Mean construct scores by cluster",
       subtitle = "Higher = more agreement; error bars = 95% confidence intervals",
       caption = "* SPW was not used to form the clusters.")
fig_constructs

ggsave("fig_profile_constructs.png", fig_constructs, width = 8, height = 5.5, dpi = 300, bg = "white")
dodge <- position_dodge(width = 0.45)
fig_differences <- ggplot(profile_subset(diff_labels), aes(Measure, Mean, colour = Cluster)) +
  geom_hline(yintercept = 0, colour = "grey50", linetype = 2) +
  geom_errorbar(aes(ymin = Lower, ymax = Upper), width = 0.15, position = dodge) +
  geom_point(size = 3, position = dodge) +
  annotate("text", x = 0.5, y = c(0.47, -0.47),
           label = c("AI rated higher", "Human support rated higher"),
           hjust = 0, colour = "grey40", size = 3.6, fontface = "italic") +
  
  geom_text(aes(label = sprintf("%+.2f", Mean),
                y = ifelse(Mean >= 0, Upper, Lower),
                vjust = ifelse(Mean >= 0, -0.7, 1.6)),
            size = 3.6, position = dodge, show.legend = FALSE) +
  scale_colour_manual(values = cluster_colors) +

  scale_y_continuous(limits = c(-0.5, 0.5), breaks = seq(-0.5, 0.5, 0.25)) +
  labs(x = NULL, y = "Mean difference (95% CI)", colour = NULL,
       title = "Perceived AI benefit relative to human support by cluster",
       subtitle = "Difference = ASLE minus the human-support score; 0 = rated equally")
fig_differences

ggsave("fig_profile_differences.png", fig_differences, width = 8, height = 5.5, dpi = 300, bg = "white")

RQ2: Well-being and balance across profiles

spw <- scores[, "SPW"]
spw_summary <- aggregate(spw ~ cluster_rank,
                         FUN = function(v) c(Mean = mean(v), SD = sd(v), Median = median(v)))
knitr::kable(do.call(data.frame, spw_summary), digits = 2,
             col.names = c("Cluster", "Mean SPW", "SD", "Median"))
Cluster Mean SPW SD Median
Cluster 1 4.16 0.54 4.09
Cluster 2 3.39 0.63 3.36
kw <- kruskal.test(spw ~ cluster_rank)
epsilon_sq <- unname(kw$statistic) / (length(spw) - 1)
cat(sprintf("Kruskal–Wallis H(%d) = %.2f, p = %.3g, epsilon² = %.3f\n",
            kw$parameter, kw$statistic, kw$p.value, epsilon_sq))
## Kruskal–Wallis H(1) = 520.58, p = 3.16e-115, epsilon² = 0.334
spw_df    <- data.frame(Cluster = cluster_rank, SPW = spw)
spw_means <- by_cluster(data.frame(SPW = spw), mean_ci)

set.seed(1)
ggplot(spw_df, aes(x = Cluster, y = SPW, colour = Cluster)) +
  geom_jitter(width = 0.25, height = 0.02, alpha = 0.3, size = 1.3) +
  geom_line(data = spw_means, aes(y = Mean, group = 1), colour = "black", linewidth = 0.9) +
  geom_point(data = spw_means, aes(y = Mean, fill = Cluster), shape = 21,
             size = 5, colour = "black", stroke = 1) +
  geom_label(data = spw_means, aes(y = Mean, label = sprintf("M = %.2f", Mean)),
             nudge_x = 0.38, colour = "black", size = 4, label.size = 0) +
  scale_colour_manual(values = cluster_colors) +
  scale_fill_manual(values = cluster_colors) +
  scale_y_continuous(limits = c(1, 5), breaks = 1:5) +
  labs(y = "Mean SPW score (1–5)", x = NULL,
       title = "SPW by cluster",
       subtitle = "Dots = students; large points = cluster means") +
  theme(legend.position = "none")

AI–human difference scores and SPW

Each panel shows one difference score against SPW. Large points with crosshairs are cluster means with 95% confidence intervals; the dashed line marks 0, where perceived AI benefit equals that kind of human support.

measure_levels <- names(diff_scores)
diff_long <- data.frame(
  Measure    = factor(rep(measure_levels, each = length(spw)), levels = measure_levels),
  Cluster    = cluster_rank,
  Difference = unlist(diff_scores, use.names = FALSE),
  SPW        = spw
)
diff_means <- by_cluster(diff_scores, function(x, in_g) data.frame(
  Difference = mean(x), SPW = mean(spw[in_g]), d_ci = ci95(x), s_ci = ci95(spw[in_g])))
diff_means$Measure <- factor(diff_means$Measure, levels = measure_levels)

ggplot(diff_long, aes(Difference, SPW, colour = Cluster)) +
  geom_vline(xintercept = 0, colour = "grey60", linetype = 2) +
  geom_jitter(width = 0.03, height = 0.03, alpha = 0.2, size = 1) +
  geom_smooth(method = "lm", formula = y ~ x, se = FALSE, linewidth = 0.7) +
  geom_errorbar(data = diff_means, aes(ymin = SPW - s_ci, ymax = SPW + s_ci),
                width = 0.1, linewidth = 1, colour = "black") +
  geom_errorbarh(data = diff_means, aes(xmin = Difference - d_ci, xmax = Difference + d_ci),
                 height = 0.1, linewidth = 1, colour = "black") +
  geom_point(data = diff_means, aes(fill = Cluster), shape = 21, size = 4.5,
             colour = "black", stroke = 1) +
  facet_wrap(~ Measure, nrow = 1) +
  scale_colour_manual(values = cluster_colors) +
  scale_fill_manual(values = cluster_colors, guide = "none") +
  scale_y_continuous(limits = c(1, 5), breaks = 1:5) +
  labs(x = "Difference score (positive = AI benefit rated above human support)",
       y = "Mean SPW score (1–5)", colour = NULL,
       title = "Perceived AI vs. human support and well-being",
       subtitle = "Lines: within-cluster linear trend") +
  theme(strip.text = element_text(size = 12, face = "bold"))

within_r <- by_cluster(diff_scores, function(x, in_g) {
  ct <- suppressWarnings(cor.test(x, spw[in_g], method = "spearman"))
  data.frame(n = sum(in_g), `Spearman rho with SPW` = unname(ct$estimate), p = ct$p.value,
             check.names = FALSE)
})
knitr::kable(within_r, digits = 3, row.names = FALSE,
             caption = "Within-cluster association between each difference score and SPW")
Within-cluster association between each difference score and SPW
Measure Cluster n Spearman rho with SPW p
Balance Cluster 1 851 0.031 0.360
Balance Cluster 2 707 -0.017 0.648
ASLE − TR Cluster 1 851 0.053 0.124
ASLE − TR Cluster 2 707 -0.076 0.043
ASLE − FSC Cluster 1 851 -0.025 0.459
ASLE − FSC Cluster 2 707 0.026 0.487

Demographic composition of clusters

for (v in names(demo)) {
  tab <- round(100 * prop.table(table(cluster_rank, demo[[v]]), 1), 1)
  print(knitr::kable(tab, caption = paste(v, "(row %)")))
}
## 
## 
## Table: Gender (row %)
## 
## |          | Male| Female|
## |:---------|----:|------:|
## |Cluster 1 |  6.9|   93.1|
## |Cluster 2 |  7.5|   92.5|
## 
## 
## Table: Type of university (row %)
## 
## |          | Private/non-public| Public|
## |:---------|------------------:|------:|
## |Cluster 1 |                5.9|   94.1|
## |Cluster 2 |                5.0|   95.0|
## 
## 
## Table: Year of study (row %)
## 
## |          | Year 1| Year 2| Year 3| Year 4| Other|
## |:---------|------:|------:|------:|------:|-----:|
## |Cluster 1 |   34.3|   28.7|     23|   13.9|   0.1|
## |Cluster 2 |   33.5|   33.0|     19|   14.4|   0.1|
## 
## 
## Table: Major (row %)
## 
## |          | Education| Economics| Engineering| Languages| Other|
## |:---------|---------:|---------:|-----------:|---------:|-----:|
## |Cluster 1 |      96.6|       0.9|         2.4|       0.0|   0.1|
## |Cluster 2 |      95.6|       1.3|         2.3|       0.6|   0.3|
## 
## 
## Table: Duration of regular AI use (row %)
## 
## |          | Less than 1 year| From 1 to under 2 years| 2 years or more|
## |:---------|----------------:|-----------------------:|---------------:|
## |Cluster 1 |             15.2|                    40.4|            44.4|
## |Cluster 2 |             22.1|                    38.0|            39.9|