-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask5_old_version.R
More file actions
69 lines (53 loc) · 2.22 KB
/
Copy pathTask5_old_version.R
File metadata and controls
69 lines (53 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
library(dplyr)
library(tidyr)
library(recommenderlab)
library(tidyverse)
library(caret)
df_t <- read_csv("original_df.csv")
df_t$label_numeric <- ifelse(df_t$label_task1_1 == "YES", 1, 0)
# Create group_id, where the anotator is defined by its demographics
df_t$group_id <- paste(df_t$gender, df_t$age, df_t$ethnicity, df_t$education, df_t$continent, sep = "_")
df_t$group_id <- as.character(df_t$group_id)
# Create wide matrix (users x tweets)
rating_matrix_df <- df_t %>%
select(group_id, id_EXIST, label_numeric) %>%
pivot_wider(names_from = id_EXIST, values_from = label_numeric,
values_fill = list(label_numeric = NA))
# Convert to matrix
rating_matrix_mat <- as.matrix(rating_matrix_df[, -1])
rownames(rating_matrix_mat) <- rating_matrix_df$group_id
# Binarize the matrix and handle NA
rating_matrix_mat[rating_matrix_mat > 0] <- 1
rating_matrix_mat[is.na(rating_matrix_mat)] <- 0
# Convert to binaryRatingMatrix
brm <- as(rating_matrix_mat, "binaryRatingMatrix")
# 2. Build UBCF model
ubcf_model <- Recommender(brm, method = "UBCF", parameter = list(method = "cosine", nn = 50))
# 3. Predict for all annotator-tweet pairs
# Predict rating probabilities for all annotators
predicted <- predict(ubcf_model, brm, type = "ratingMatrix")
# Convert to matrix
pred_matrix <- as(predicted, "matrix")
# 5. Merge predictions back to df
# Convert to character to match matrix
df_t$group_id <- as.character(df_t$group_id)
df_t$id_EXIST <- as.character(df_t$id_EXIST)
# Merge CF scores as weights
df_t$cf_weight <- mapply(function(user, item) {
if (user %in% rownames(pred_matrix) && item %in% colnames(pred_matrix)) {
return(pred_matrix[user, item])
} else {
return(NA)
}
}, df_t$group_id, df_t$id_EXIST)
ggplot(df_t, aes(x = 1, y = cf_weight, color = as.factor(label_task1_1))) +
geom_jitter(width = 0.3, height = 0, size = 1) +
scale_color_brewer(palette = "Set1") +
labs(color = "Label") +
theme_minimal() +
theme(axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank())
print(pred_matrix)
saveRDS(pred_matrix, file = "cf_matrix.rds")
write.csv(df_t, "cf_weights.csv", row.names = FALSE)