-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_rf_test.R
149 lines (108 loc) · 5.36 KB
/
run_rf_test.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
run_rf_test <- function(features.training,
features.testing,
filenamestem,
ntree=500,
mtry=5,
genres_only=TRUE,
fringe_is_poetry=FALSE) {
matrices_no_cutoff <- list()
# Precaution
names(features.training) <- gsub(" ", "_", names(features.training))
is_poetry <- features.training$is_poetry
features.training$is_poetry <- is_poetry
# Change names for cforest
levels(features.training$is_poetry) <- gsub("FALSE", "NONPOETRY", levels(features.training$is_poetry))
levels(features.training$is_poetry) <- gsub("TRUE", "POETRY", levels(features.training$is_poetry))
is_poetry <- features.training$is_poetry
# On the fly! (Part 1)
if ("author" %in% names(features.training)) {
if (fringe_is_poetry) {
poetry_authors <- features.training$author[which(is_poetry=="POETRY" | is_poetry=="HARDCORE" | is_poetry=="FRINGE")]
} else {
poetry_authors <- features.training$author[which(is_poetry=="POETRY" | is_poetry=="HARDCORE")]
}
features.training$author <- is_known_author(features.training$author,
poetry_authors=poetry_authors,
ignore_NA=TRUE)
} else if ("varia_author" %in% names(features.training)) {
if (fringe_is_poetry) {
poetry_authors <- features.training$varia_author[which(is_poetry=="POETRY" | is_poetry=="HARDCORE")]
} else {
poetry_authors <- features.training$varia_author[which(is_poetry=="POETRY" | is_poetry=="HARDCORE")]
}
features.training$varia_author <- is_known_author(features.training$varia_author,
poetry_authors=poetry_authors,
ignore_NA=TRUE)
}
varNames <- names(features.training)[!names(features.training) %in% c("is_poetry")]
varNames1 <- paste(varNames, collapse="+")
rfForm <- as.formula(paste("is_poetry", varNames1, sep=" ~ "))
#ctrl <- trainControl(method = "boot",
# number = 1,
# repeats = 5,
# summaryFunction = hmeasureCaret,
# classProbs=TRUE,
# allowParallel = TRUE,
# verboseIter=FALSE,
# returnData=FALSE,
# savePredictions=FALSE)
# On the fly! (Part 2)
if ("author" %in% names(features.testing)) {
features.testing$author <- is_known_author(features.testing$author,
poetry_authors=poetry_authors,
ignore_NA=TRUE)
} else if ("varia_author" %in% names(features.testing)) {
features.testing$varia_author <- is_known_author(features.testing$varia_author,
poetry_authors=poetry_authors,
ignore_NA=TRUE)
}
retRF_no_cutoff <- randomForest::randomForest(rfForm,
features.training,
ntree=ntree,
importance=TRUE,
replace=FALSE,
mtry=mtry)
# Get the last portion as the test group
#features2 <- rbindlist(features.split[set_no])
features2 <- features.testing
# Precaution
names(features2) <- gsub(" ", "_", names(features2))
# Change names for cforest
levels(features2$is_poetry) <- gsub("FALSE", "NONPOETRY", levels(features2$is_poetry))
levels(features2$is_poetry) <- gsub("TRUE", "POETRY", levels(features2$is_poetry))
is_poetry2 <- features2$is_poetry
# On the fly! (Part 2)
if ("author" %in% names(features2)) {
features2$author <- is_known_author(features2$author,
poetry_authors=poetry_authors,
ignore_NA=TRUE)
} else if ("varia_author" %in% names(features2)) {
features2$varia_author <- is_known_author(features2$varia_author,
poetry_authors=poetry_authors,
ignore_NA=TRUE)
if (levels(features2$varia_author) == c("FALSE")) {
levels(features2$varia_author) <- c("FALSE", "TRUE")
} else if (levels(features2$varia_author) == c("TRUE")) {
levels(features2$varia_author) <- c("TRUE", "FALSE")
}
}
# Get prediction
features2$is_poetry <- NULL
#print(features2$varia_author)
prediction_no_cutoff <- predict(retRF_no_cutoff, features2)
return(prediction_no_cutoff)
cm_no_cutoff <- confusionMatrix(data=prediction_no_cutoff, reference=is_poetry2, positive="POETRY")
cm_df <- convert_cm_to_df(cm_no_cutoff)
#matrices_no_cutoff[[1]] <- cm_df
gc()
#aggregated_results_no_cutoff <- aggregate_cm_dynamically(matrices_no_cutoff)
sink(file = paste0(outputpath, "/", filenamestem ,"_confusionMatrix_combined_no_cutoff.txt"),
append=FALSE)
#aggregated_results_no_cutoff <- aggregate_confusion_matrix(matrices_no_cutoff)
width <- getOption("width")
options("width"=1000)
print(cm_df)
options("width"=width)
sink()
return(cm_df)
}