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
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
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 | 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
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")
}
# Short item wording (paraphrased from the questionnaire)
item_labels <- c(
TR1 = "Stimulates interest in learning",
TR2 = "Clarifies course value and meaning",
TR3 = "Encourages course enjoyment",
TR4 = "Supports academic progress",
TR5 = "Ensures basic course understanding",
TR6 = "Helps prevent poor results",
TR7 = "Helps prevent course failure",
TR8 = "Accessible for academic support",
TR9 = "Creates a trusting atmosphere",
TR10 = "Shows genuine concern",
TR11 = "Teaches to best ability",
TR12 = "Organizes teaching effectively",
TR13 = "Organizes engaging lessons",
ASLE1 = "AI improves learning effectiveness",
ASLE2 = "AI improves outcomes or grades",
ASLE3 = "AI improves outcomes vs. working alone",
ASLE4 = "AI improves academic skills",
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)))
| Construct | Descriptive label | Variable | Min | Max | M | SD | Missing n |
|---|---|---|---|---|---|---|---|
| Teacher responsibility (TR) | Stimulates interest in learning | TR1 | 1 | 5 | 3.95 | 0.95 | 0 |
| Teacher responsibility (TR) | Clarifies course value and meaning | TR2 | 1 | 5 | 4.00 | 0.94 | 0 |
| Teacher responsibility (TR) | Encourages course enjoyment | TR3 | 1 | 5 | 3.93 | 0.91 | 0 |
| Teacher responsibility (TR) | Supports academic progress | TR4 | 1 | 5 | 3.96 | 0.88 | 0 |
| Teacher responsibility (TR) | Ensures basic course understanding | TR5 | 1 | 5 | 4.01 | 0.88 | 0 |
| Teacher responsibility (TR) | Helps prevent poor results | TR6 | 1 | 5 | 3.93 | 0.91 | 0 |
| Teacher responsibility (TR) | Helps prevent course failure | TR7 | 1 | 5 | 3.91 | 0.92 | 0 |
| Teacher responsibility (TR) | Accessible for academic support | TR8 | 1 | 5 | 3.96 | 0.90 | 0 |
| Teacher responsibility (TR) | Creates a trusting atmosphere | TR9 | 1 | 5 | 3.96 | 0.88 | 0 |
| Teacher responsibility (TR) | Shows genuine concern | TR10 | 1 | 5 | 3.91 | 0.89 | 0 |
| Teacher responsibility (TR) | Teaches to best ability | TR11 | 1 | 5 | 3.98 | 0.91 | 0 |
| Teacher responsibility (TR) | Organizes teaching effectively | TR12 | 1 | 5 | 4.00 | 0.89 | 0 |
| Teacher responsibility (TR) | Organizes engaging lessons | TR13 | 1 | 5 | 3.97 | 0.89 | 0 |
| AI-supported learning effectiveness (ASLE) | AI improves learning effectiveness | ASLE1 | 1 | 5 | 3.86 | 0.90 | 0 |
| AI-supported learning effectiveness (ASLE) | AI improves outcomes or grades | ASLE2 | 1 | 5 | 3.90 | 0.89 | 0 |
| AI-supported learning effectiveness (ASLE) | AI improves outcomes vs. working alone | ASLE3 | 1 | 5 | 3.85 | 0.90 | 0 |
| AI-supported learning effectiveness (ASLE) | AI improves academic skills | 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"))
# 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)))
| 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"))
Students who gave the same answer to all 21 clustering items (TR, ASLE, FSC) are dropped before clustering. The descriptive tables above use the full sample; everything from here on uses the remaining students.
Why they are dropped:
Caveat: none of these students gave identical answers on all 32 items. They did vary on the well-being (SPW) items, so they are treated as uninformative for clustering, not as careless or invalid. Most of them answered 4 or 5, so the retained sample agrees slightly less overall than the full sample.
cluster_blocks <- item_blocks[c("TR", "ASLE", "FSC")]
cluster_items <- unlist(cluster_blocks, use.names = FALSE)
# Drop uniform responders: students whose answers have zero variance across
# the 21 clustering items (e.g. 4 on every TR, ASLE and FSC item).
# Their standardized rows are identical to each other, so they would merge at
# height 0 and form an artificial block; their difference scores are all 0.
flat <- apply(X[, cluster_items], 1, var) == 0
cat(sprintf("Dropped %d students with identical answers on all %d clustering items; %d remain.\n",
sum(flat), length(cluster_items), sum(!flat)))
## Dropped 135 students with identical answers on all 21 clustering items; 1423 remain.
print(table(`Answer given to every item` = X[flat, cluster_items[1]]))
## Answer given to every item
## 1 2 3 4 5
## 5 2 17 63 48
# Subset every student-level object together so rows stay aligned downstream
X <- X[!flat, , drop = FALSE]
scores <- scores[!flat, , drop = FALSE]
demo <- demo[!flat, , drop = 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_name <- "Students' psychological well-being (mean)"
spw_col <- colorRamp2(c(1, 5), c("white", "black"))
# Spelled-out construct names on top, item wording at the bottom
heat_titles <- c(TR = "Teacher\nresponsibility",
ASLE = "AI-supported\nlearning\neffectiveness",
FSC = "Fellow-student\ncollaboration")
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 = heat_titles[levels(construct_heat)],
column_title_gp = gpar(fontsize = 13, fontface = "bold"),
column_labels = item_labels[colnames(X_heat)],
column_names_gp = gpar(fontsize = 10), column_names_max_height = unit(8, "cm"),
show_row_names = FALSE, column_gap = unit(2, "mm"),
...
)
row_annotation <- function(extra = NULL, extra_col = list(), size = unit(5, "mm")) {
df <- setNames(data.frame(scores[, "SPW"]), spw_name)
if (!is.null(extra)) df <- data.frame(extra, df, check.names = FALSE)
rowAnnotation(
df = df,
col = c(extra_col, setNames(list(spw_col), spw_name)),
annotation_legend_param = c(
lapply(setNames(nm = names(extra_col)), function(v) list(border = "black")),
setNames(list(list(at = 1:5, border = "black",
title = "Students' psychological\nwell-being (mean)")), spw_name)),
simple_anno_size = size,
gap = unit(1.5, "mm"),
border = TRUE, # outline so white cells stay visible
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())
}
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)")
| node | height | gap_above | n | mean_agreement | first_branch | second_branch |
|---|---|---|---|---|---|---|
| 1422 | 1.255 | NA | 1423 | 3.917 | node 1421 (n = 750) | node 1420 (n = 673) |
| 1421 | 1.002 | 0.253 | 750 | 4.330 | node 1416 (n = 713) | node 1405 (n = 37) |
| 1420 | 0.997 | 0.258 | 673 | 3.456 | node 1417 (n = 115) | node 1419 (n = 558) |
| 1419 | 0.978 | 0.019 | 558 | 3.350 | node 1409 (n = 114) | node 1418 (n = 444) |
| 1418 | 0.949 | 0.029 | 444 | 3.192 | node 1386 (n = 18) | node 1415 (n = 426) |
| 1417 | 0.943 | 0.054 | 115 | 3.973 | node 1413 (n = 30) | node 1410 (n = 85) |
| 1416 | 0.935 | 0.067 | 713 | 4.354 | node 1414 (n = 680) | node 1408 (n = 33) |
| 1415 | 0.902 | 0.047 | 426 | 3.163 | node 1378 (n = 4) | node 1411 (n = 422) |
| 1414 | 0.895 | 0.039 | 680 | 4.372 | node 1412 (n = 674) | node 1345 (n = 6) |
| 1413 | 0.889 | 0.053 | 30 | 4.005 | node 1394 (n = 28) | node 1230 (n = 2) |
| 1412 | 0.881 | 0.014 | 674 | 4.375 | node 1406 (n = 587) | node 1403 (n = 87) |
| 1411 | 0.858 | 0.044 | 422 | 3.157 | node 1388 (n = 4) | node 1400 (n = 418) |
| 1410 | 0.857 | 0.086 | 85 | 3.962 | node 1396 (n = 63) | node 1399 (n = 22) |
| 1409 | 0.836 | 0.142 | 114 | 3.963 | node 1401 (n = 21) | node 1407 (n = 93) |
| 1408 | 0.824 | 0.110 | 33 | 3.988 | node 1389 (n = 9) | node 1383 (n = 24) |
| 1407 | 0.800 | 0.036 | 93 | 3.956 | node 1392 (n = 63) | node 1287 (n = 30) |
| 1406 | 0.796 | 0.086 | 587 | 4.452 | node 1404 (n = 582) | node 1199 (n = 5) |
| 1405 | 0.795 | 0.207 | 37 | 3.869 | node 1359 (n = 8) | node 1395 (n = 29) |
| 1404 | 0.792 | 0.004 | 582 | 4.457 | node 1398 (n = 577) | node 1335 (n = 5) |
| 1403 | 0.786 | 0.096 | 87 | 3.856 | node 1311 (n = 10) | node 1402 (n = 77) |
| 1402 | 0.765 | 0.021 | 77 | 3.845 | node 1344 (n = 5) | node 1382 (n = 72) |
| 1401 | 0.765 | 0.071 | 21 | 3.995 | node 1385 (n = 8) | node 1350 (n = 13) |
| 1400 | 0.764 | 0.093 | 418 | 3.153 | node 1351 (n = 29) | node 1397 (n = 389) |
| 1399 | 0.761 | 0.095 | 22 | 3.781 | node 1384 (n = 16) | node 1360 (n = 6) |
| 1398 | 0.757 | 0.034 | 577 | 4.461 | node 1391 (n = 564) | node 1357 (n = 13) |
All students in one tree, no split. Only the 21 clustering items are shown; mean students’ psychological well-being (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)
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
## 750 673
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")
| Cluster | n | % | TR | ASLE | FSC | SPW | Overall | Balance | ASLE − TR | ASLE − FSC |
|---|---|---|---|---|---|---|---|---|---|---|
| Cluster 1 | 750 | 52.71 | 4.33 | 4.32 | 4.33 | 4.19 | 4.33 | -0.01 | -0.01 | -0.01 |
| Cluster 2 | 673 | 47.29 | 3.52 | 3.34 | 3.36 | 3.39 | 3.41 | -0.11 | -0.18 | -0.03 |
# 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)")
| Measure | Cluster | AI-leaning (> 0) % | Equal (= 0) % | Human-leaning (< 0) % |
|---|---|---|---|---|
| Balance | Cluster 1 | 53.9 | 1.3 | 44.8 |
| Balance | Cluster 2 | 41.2 | 1.0 | 57.8 |
| ASLE − TR | Cluster 1 | 52.3 | 5.2 | 42.5 |
| ASLE − TR | Cluster 2 | 37.3 | 3.0 | 59.7 |
| ASLE − FSC | Cluster 1 | 29.5 | 39.1 | 31.5 |
| ASLE − FSC | Cluster 2 | 36.8 | 24.5 | 38.6 |
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
}
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)
}
# Black-to-white greys for each demographic (binary variables are pure black/white)
demo_colors <- lapply(demo, function(z)
setNames(colorRampPalette(c("black", "white"))(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")
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.19 | 0.52 | 4.14 |
| Cluster 2 | 3.39 | 0.61 | 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) = 518.85, p = 7.53e-115, epsilon² = 0.365
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")
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")
| Measure | Cluster | n | Spearman rho with SPW | p |
|---|---|---|---|---|
| Balance | Cluster 1 | 750 | 0.005 | 0.889 |
| Balance | Cluster 2 | 673 | -0.103 | 0.007 |
| ASLE − TR | Cluster 1 | 750 | 0.019 | 0.596 |
| ASLE − TR | Cluster 2 | 673 | -0.133 | 0.001 |
| ASLE − FSC | Cluster 1 | 750 | -0.039 | 0.287 |
| ASLE − FSC | Cluster 2 | 673 | -0.067 | 0.083 |
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 | 7.6| 92.4|
## |Cluster 2 | 7.4| 92.6|
##
##
## Table: Type of university (row %)
##
## | | Private/non-public| Public|
## |:---------|------------------:|------:|
## |Cluster 1 | 5.5| 94.5|
## |Cluster 2 | 5.3| 94.7|
##
##
## Table: Year of study (row %)
##
## | | Year 1| Year 2| Year 3| Year 4| Other|
## |:---------|------:|------:|------:|------:|-----:|
## |Cluster 1 | 34.5| 28.3| 23.5| 13.6| 0.1|
## |Cluster 2 | 33.1| 33.4| 18.7| 14.6| 0.1|
##
##
## Table: Major (row %)
##
## | | Education| Economics| Engineering| Languages| Other|
## |:---------|---------:|---------:|-----------:|---------:|-----:|
## |Cluster 1 | 96.4| 1.1| 2.5| 0.0| 0.0|
## |Cluster 2 | 95.4| 1.3| 2.4| 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 | 14.3| 41.5| 44.3|
## |Cluster 2 | 22.6| 36.4| 41.0|