-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting_higher-order_models.R
52 lines (40 loc) · 1.74 KB
/
testing_higher-order_models.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
library(ranger)
library(nnet)
library(dplyr)
library(measures)
all_models_results <- readRDS("./data/all_models_results.rds")
dat <- select(all_models_results, c("dataset", "fold", "Nuclear", "Membrane", "Tat", "Sec", "IM", "OM", "TM", "P_IM"))
dat[is.na(dat)] <- 0
lm_res <- lapply(unique(dat[["fold"]]), function(ith_fold) {
train_dat <- filter(dat, fold != ith_fold) %>%
select(-fold)
test_dat <- filter(dat, fold == ith_fold)
lm_model <- multinom(dataset ~ ., dat, model = TRUE)
test_dat %>%
select(c("dataset", "fold")) %>%
mutate(Prediction = predict(lm_model, test_dat),
Probability = predict(lm_model, test_dat, type = "probs"))
}) %>% bind_rows()
rf_res <- lapply(unique(dat[["fold"]]), function(ith_fold) {
train_dat <- filter(dat, fold != ith_fold) %>%
select(-fold) %>%
mutate(dataset = as.factor(dataset))
test_dat <- filter(dat, fold == ith_fold)
rf_model <- ranger(data = train_dat, dependent.variable.name = "dataset",
write.forest = TRUE, probability = TRUE, verbose = FALSE)
res <- predict(rf_model, test_dat)[["predictions"]]
test_dat %>%
select(c("dataset", "fold")) %>%
mutate(Prediction = colnames(res)[max.col(res)],
Probability = sapply(1:nrow(res), function(i) max(res[i,])))
}) %>% bind_rows()
group_by(lm_res, fold) %>%
summarise(Accuracy = ACC(dataset, Prediction),
Kappa = KAPPA(dataset, Prediction),
AU1U = multiclass.AU1U(Probability, dataset))
group_by(rf_res, fold) %>%
summarise(Accuracy = ACC(dataset, Prediction),
Kappa = KAPPA(dataset, Prediction),
AU1U = multiclass.AU1U(Probability, dataset))
table(lm_res[["dataset"]], lm_res[["Prediction"]])
table(rf_res[["dataset"]], rf_res[["Prediction"]])