-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasoc_rules.R
82 lines (61 loc) · 1.89 KB
/
asoc_rules.R
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
# Asociation Rules ----------
#____________________________
library(tidyverse)
library(arules)
game_ratings_df_n <- read_csv("tables/game_ratings_df_n.csv")
transactions <- game_ratings_df_n %>%
filter(rating >5) %>%
group_by(user_id) %>%
summarise(games = list(game_matrix_id)) %>%
pull(games)
transactions_ar <- as(transactions, "transactions")
gc() %>% invisible()
#' Solo el 1% es el evento que nos interesa
rules <- apriori(transactions_ar,
# appearance = list(default="lhs",rhs=quiero),
# parameter = list(supp = 0.001, conf = 0.1)
parameter = list(supp = 0.005,conf = 0.2)
)
rules_df <- inspect(rules)
names(rules_df)[2] <- "impl"
rules_df <- rules_df %>%
tbl_df %>%
mutate_at(c("lhs","rhs"),as.character) %>%
select(-impl)
rules_df <-
rules_df %>% arrange(lhs,-lift) %>% mutate(rhs = rhs %>% str_replace_all("[\\{\\}]","") %>% as.numeric)
rules_df %>%
saveRDS(file = "models/arules.RDS")
# rules_df %>%
rules_df %>%
arrange(lhs,-confidence) %>%
# filter(lhs %>% str_detect(","))
filter(lhs == "{228,900}")
recommend_rules <- function(elements){
rules_df %>%
filter(lhs == paste0("{",str_c(elements,collapse = ","),"}"))
}
recommend_rules(c(228,900))
recommend_all_rules <- function(elements){
elements <- sort(elements)
if(length(elements) < 2){
elements_2 <- NULL
elements_3 <- NULL
} else if(length(elements) <3){
elements_2 <- combn(elements,2,list)
} else {
elements_2 <- combn(elements,2,list)
elements_3 <- combn(elements,3,list)
}
lhs_df <- data_frame(lhs = c(elements, combn(elements,2,list) ,combn(elements,3,list)))
lhs_df %>%
mutate(rhs = map(lhs,recommend_rules)) %>%
select(-lhs) %>%
unnest %>%
filter(!(rhs %in% elements)) %>%
arrange(-confidence) %>%
select(rhs) %>%
distinct() %>%
slice(1:9) %>%
pull
}