-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathto_delete
More file actions
121 lines (88 loc) · 3.7 KB
/
Copy pathto_delete
File metadata and controls
121 lines (88 loc) · 3.7 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# --------------------------
# 1. Recommendation system - collaborative filtering
# --------------------------
# Load necessary libraries
library(dplyr)
library(tidyr)
library(recommenderlab)
library(tidyverse)
## Loading out data
df_t <- read_csv("original_df.csv")
unique_tweet_df <-read_csv("unique_tweets_data.csv")
# Ensure label_numeric is binary (1 for YES, 0 for NO)
df_t$label_numeric <- ifelse(df_t$label_task1_1 == "YES", 1, 0)
# Pivot the data from long to wide format: annotator_id vs tweet_id
rating_matrix <- df_t %>%
select(annotator_id, id_EXIST, label_numeric) %>%
pivot_wider(names_from = id_EXIST, values_from = label_numeric, values_fill = list(label_numeric = NA))
head(rating_matrix)
# Convert to matrix format
mat <- as.matrix(rating_matrix[,-1]) # remove annotator_id column
rownames(mat) <- rating_matrix$annotator_id
# Convert to realRatingMatrix
bin_mat <- as(mat, "realRatingMatrix")
# print the bin_mat
as(bin_mat, "matrix")[1:6, 1:5]
# train-test split (80% of users for training)
set.seed(123)
evaluation_scheme <- evaluationScheme(bin_mat, method = "split", train = 0.8, given = 2)
# train a user-based collaborative filtering (UBCF)
recommender_model <- Recommender(getData(evaluation_scheme, "train"), method = "UBCF")
# predict labels for the test set (ratings type)
predictions <- predict(recommender_model, getData(evaluation_scheme, "known"), type = "ratings")
# evaluate model performance
error <- calcPredictionAccuracy(predictions, getData(evaluation_scheme, "unknown"))
# print RMSE, MSE, MAE
print(error)
# print predictions
as(predictions, "matrix")[1:10, 1:5]
summary(rating_matrix)
# create the matrix of the prediction, in order to create a feature nemd cf_bias with the bias for each anotator
cf_matrix <- as(predictions, "matrix")
cf_matrix[1:10, 1:5]
# crete the column cf_bias with the mean for the bias for each anotator
annotator_bias <- rowMeans(cf_matrix, na.rm = TRUE)
bias_df <- data.frame(annotator_id = names(annotator_bias), cf_bias = annotator_bias)
print(bias_df)
### Evaluating some of the results given in the cf_bias columns for the anotator.
### CASE 1 - cf_bias = -0.010904255, Annotator_411
inital_df <- read_csv("original_df.csv")
inital_df <- inital_df[,-1]
inital_df[inital_df$annotator_id == "Annotator_411", c("id_EXIST", "label_task1_1", "annotator_id")]
inital_df %>%
filter(annotator_id == "Annotator_411") %>%
count( annotator_id, label_task1_1)
# --------------------------
# A tibble: 2 × 3
# annotator_id label_task1_1 n
# <chr> <chr> <int>
# Annotator_411 NO 35
# Annotator_411 YES 13
# --------------------------
# In this case the anotator labels more tweets as NO.
### CASE 2 - cf_bias = 1.006666667, Annotator_725
inital_df[inital_df$annotator_id == "Annotator_725", c("id_EXIST", "label_task1_1", "annotator_id")]
inital_df %>%
filter(annotator_id == "Annotator_725") %>%
count( annotator_id, label_task1_1)
# --------------------------
# A tibble: 2 × 3
# annotator_id label_task1_1 n
# <chr> <chr> <int>
# Annotator_725 NO 12
# Annotator_725 YES 38
# --------------------------
# In this case the anotator labels more tweets as Yes.
### CASE 3 - cf_bias = 0.502752504, Annotator_683
inital_df[inital_df$annotator_id == "Annotator_683", c("id_EXIST", "label_task1_1", "annotator_id")]
inital_df %>%
filter(annotator_id == "Annotator_683") %>%
count( annotator_id, label_task1_1)
# --------------------------
# A tibble: 2 × 3
# annotator_id label_task1_1 n
# <chr> <chr> <int>
# Annotator_683 NO 26
# Annotator_683 YES 24
# --------------------------
# In this case the anotator labels the same amount of NO and YES.