Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 1 | library(caret) |
| 2 | library(tidyverse) |
| 3 | library(DMwR) |
| 4 | library(randomForest) |
| 5 | library(FSelector) |
| 6 | # library(randomForestExplainer) |
| 7 | # may need to: options(expressions = 5e5) to avoid stackoverflow for installing package |
| 8 | |
Marc Kupietz | 358a296 | 2021-02-22 07:55:49 +0100 | [diff] [blame] | 9 | set.seed(42) |
| 10 | |
PeterFankhauserIDS | ed93d2e | 2021-02-20 14:51:13 +0100 | [diff] [blame] | 11 | # Test |
| 12 | |
PeterFankhauserIDS | 3f97f36 | 2021-02-23 10:28:07 +0100 | [diff] [blame] | 13 | ngramfile<-"gold03_anno_ml_synfeat_nstopw" # 2nd dataset |
| 14 | # ngramfile <-"goldstandard01_anno_ml_synfeat_nstop1" # 1st dataset |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 15 | |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 16 | setwd(dirname(rstudioapi::getSourceEditorContext()$path)) |
| 17 | stopwords <- readLines(con = "../data/stopwords.txt",encoding="UTF-8") |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 18 | oringramme <- read.csv(paste("../data/",ngramfile,".csv",sep=""), header = TRUE, sep = "\t", dec=".", quote="", encoding="UTF-8",stringsAsFactors=FALSE) |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 19 | syfeaturenames <- read.csv("../data/syfeatures.tsv", header = TRUE, sep = "\t", dec=".", quote="", encoding="UTF-8",stringsAsFactors=FALSE) |
| 20 | # syfeaturenames$navalue<-sapply(syfeaturenames$navalue,as.numeric) |
| 21 | |
| 22 | deleteStopwords = function(wl, stopwords = NULL) { |
| 23 | wl[!(wl %in% stopwords)] |
| 24 | } |
| 25 | |
Marc Kupietz | 631800f | 2021-02-19 17:27:26 +0100 | [diff] [blame] | 26 | oringramme <- oringramme %>% |
PeterFankhauserIDS | 3f97f36 | 2021-02-23 10:28:07 +0100 | [diff] [blame] | 27 | mutate(CO_IDIOM = ifelse(is.na(CO_IDIOM),0,CO_IDIOM)) %>% # treat NAs as 0 |
Marc Kupietz | 631800f | 2021-02-19 17:27:26 +0100 | [diff] [blame] | 28 | filter(CO_IDIOM < 2) # just two classes: 0 no idiom, 1 idiom |
| 29 | |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 30 | # Reduce number of classes, treat null values, add NSTOPW, change names for SY features |
| 31 | |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 32 | ngramme <- oringramme %>% |
| 33 | add_column(NSTOPW = sapply(oringramme$tokens,function(x) length(deleteStopwords(tolower(unlist(strsplit(x," "))),stopwords)))) %>% |
| 34 | # select(-matches("CO_TOKEN.*"), -tokens) %>% |
| 35 | select(-matches("CO_TOKEN.*")) %>% # keep tokens for interpretability |
| 36 | mutate(across(matches(".rank.*"), ~ replace_na(.x, 1000))) %>% |
| 37 | mutate(across(c("dice", "lfmd", "llr", "ld", "pmi"), ~ replace_na(.x, min(.x) - 1))) %>% |
Marc Kupietz | aced270 | 2021-02-19 19:09:29 +0100 | [diff] [blame] | 38 | rename_at(syfeaturenames$innames, ~ syfeaturenames[syfeaturenames$innames==.x,]$synames ) %>% |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 39 | mutate(across(everything(), ~ replace_na(.x, 0))) %>% |
Marc Kupietz | 201e6f3 | 2021-02-22 12:34:13 +0100 | [diff] [blame] | 40 | mutate(CO_IDIOM = as.factor(if_else(CO_IDIOM == 1, "idiom", "no_idiom"))) # just two classes: 0 no idiom, 1 idiom |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 41 | |
| 42 | # Optional |
| 43 | write.table(ngramme,file=paste("../data/",ngramfile,"_cosy.csv",sep=""), sep = "\t", quote=F) |
| 44 | |
| 45 | # featuresets |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 46 | |
| 47 | covars <- c("CO_LL", "CO_Z", "CO_G", "CO_T", "CO_LOGDICE", "CO_PMI", "CO_MI3", "CO_DEREKO", "CO_SGT", "CO_WIN5_VEC","CO_WIN5_VEC_AUTOSEM") |
| 48 | syvars <- c(syfeaturenames$synames,"NSTOPW") |
| 49 | vars <- c(covars,syvars) |
| 50 | |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 51 | # formulae for training and testing rf |
| 52 | |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 53 | fmla <- as.formula(paste("CO_IDIOM ~ ", paste(vars, collapse= "+"))) |
| 54 | fmlaco <- as.formula(paste("CO_IDIOM ~ ", paste(covars, collapse= "+"))) |
| 55 | fmlasy <- as.formula(paste("CO_IDIOM ~ ", paste(syvars, collapse= "+"))) |
| 56 | |
| 57 | # Simple train/test split |
| 58 | |
| 59 | trainRows <- sample(nrow(ngramme), nrow(ngramme)*0.8, replace = FALSE) |
| 60 | train <- ngramme[trainRows,] |
| 61 | test <- ngramme[setdiff(1:nrow(ngramme),trainRows),] |
| 62 | |
Marc Kupietz | 355d548 | 2021-02-22 17:13:56 +0100 | [diff] [blame] | 63 | cat("Random Forest\n") |
Marc Kupietz | 65733b2 | 2021-02-22 08:09:08 +0100 | [diff] [blame] | 64 | |
Marc Kupietz | 13f67ed | 2021-02-22 07:55:03 +0100 | [diff] [blame] | 65 | rf_classifier = randomForest(fmla, train, importance=TRUE) |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 66 | |
| 67 | # only SY features |
PeterFankhauserIDS | a5b2acf | 2021-02-22 18:14:18 +0100 | [diff] [blame] | 68 | # rf_classifier = randomForest(fmlasy, train, importance=TRUE) |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 69 | |
| 70 | prediction_for_table <- predict(rf_classifier, test %>% select(-CO_IDIOM)) |
| 71 | |
Marc Kupietz | 201e6f3 | 2021-02-22 12:34:13 +0100 | [diff] [blame] | 72 | res <- confusionMatrix(prediction_for_table, test$CO_IDIOM, positive= "idiom") |
Marc Kupietz | 13f67ed | 2021-02-22 07:55:03 +0100 | [diff] [blame] | 73 | print(res) |
Marc Kupietz | 355d548 | 2021-02-22 17:13:56 +0100 | [diff] [blame] | 74 | collected_results <- bind_cols("rf" = res$byClass) |
PeterFankhauserIDS | c262278 | 2021-02-21 18:10:01 +0100 | [diff] [blame] | 75 | |
| 76 | # Sensitivity is recall of class 1 |
| 77 | # Pos Pred Value is precision |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 78 | varImpPlot(rf_classifier) |
| 79 | |
Marc Kupietz | 355d548 | 2021-02-22 17:13:56 +0100 | [diff] [blame] | 80 | cat("Random Forest with cutoff\n") |
| 81 | prediction_for_table <- predict(rf_classifier,test %>% select(-CO_IDIOM), cutoff = c(0.2, 0.8)) |
| 82 | res <- confusionMatrix(prediction_for_table,test$CO_IDIOM, positive = "idiom") |
| 83 | collected_results <- bind_cols(collected_results, "rf with cutoff" = res$byClass) |
| 84 | print(res) |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 85 | |
Marc Kupietz | 355d548 | 2021-02-22 17:13:56 +0100 | [diff] [blame] | 86 | cat("With SMOTE resampled training data\n") |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 87 | smoted.data <- SMOTE(fmla, subset(train, select = c("CO_IDIOM", vars)), perc.over = 1200, perc.under = 100) |
Marc Kupietz | 13f67ed | 2021-02-22 07:55:03 +0100 | [diff] [blame] | 88 | rf_classifier = randomForest(fmla, smoted.data, importance=TRUE) |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 89 | prediction_for_table <- predict(rf_classifier,test %>% select(-CO_IDIOM)) |
Marc Kupietz | 201e6f3 | 2021-02-22 12:34:13 +0100 | [diff] [blame] | 90 | res <- confusionMatrix(prediction_for_table,test$CO_IDIOM, positive = "idiom") |
Marc Kupietz | 355d548 | 2021-02-22 17:13:56 +0100 | [diff] [blame] | 91 | collected_results <- bind_cols(collected_results, "rf with SMOTE" = res$byClass) |
Marc Kupietz | 13f67ed | 2021-02-22 07:55:03 +0100 | [diff] [blame] | 92 | print(res) |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 93 | |
Marc Kupietz | 355d548 | 2021-02-22 17:13:56 +0100 | [diff] [blame] | 94 | cat("With SMOTE and cutoff\n") |
Marc Kupietz | 201e6f3 | 2021-02-22 12:34:13 +0100 | [diff] [blame] | 95 | prediction_for_table <- predict(rf_classifier,test %>% select(-CO_IDIOM), cutoff = c(0.2, 0.8)) |
| 96 | res <- confusionMatrix(prediction_for_table,test$CO_IDIOM, positive = "idiom") |
Marc Kupietz | 355d548 | 2021-02-22 17:13:56 +0100 | [diff] [blame] | 97 | collected_results <- bind_cols(collected_results, "rf with SMOTE and cutoff" = res$byClass) |
Marc Kupietz | 1be40eb | 2021-02-22 08:10:29 +0100 | [diff] [blame] | 98 | print(res) |
| 99 | |
Marc Kupietz | 355d548 | 2021-02-22 17:13:56 +0100 | [diff] [blame] | 100 | collected_results <- collected_results %>% |
| 101 | round(3) %>% |
| 102 | add_column(measure = names(res$byClass)) %>% |
| 103 | column_to_rownames("measure") |
| 104 | |
| 105 | View(collected_results) |
Marc Kupietz | 1be40eb | 2021-02-22 08:10:29 +0100 | [diff] [blame] | 106 | |
PeterFankhauserIDS | 03d4ece | 2021-02-22 20:58:28 +0100 | [diff] [blame] | 107 | # Analysing tradeoff between Fscore, Recall, Precision for various cutoffs |
PeterFankhauserIDS | d2c893a | 2021-02-22 21:16:06 +0100 | [diff] [blame] | 108 | # full range from precision almost 100% to recall almost 100% |
PeterFankhauserIDS | 03d4ece | 2021-02-22 20:58:28 +0100 | [diff] [blame] | 109 | rf_classifier = randomForest(fmla, train, importance=TRUE) |
Marc Kupietz | b1b0336 | 2021-02-23 09:32:16 +0100 | [diff] [blame] | 110 | cvalues<-tibble() |
Marc Kupietz | 347a039 | 2021-02-23 09:56:05 +0100 | [diff] [blame] | 111 | for (c in c(seq(from=0.4,to=0.99,by=0.025),0.999)) { |
| 112 | prediction_for_table <- predict(rf_classifier, test %>% select(-CO_IDIOM), cutoff = c(1-c, c)) |
PeterFankhauserIDS | 03d4ece | 2021-02-22 20:58:28 +0100 | [diff] [blame] | 113 | conf<-confusionMatrix(prediction_for_table, test$CO_IDIOM, positive = "idiom") |
Marc Kupietz | b1b0336 | 2021-02-23 09:32:16 +0100 | [diff] [blame] | 114 | cvalues <-bind_rows(cvalues, c(cutoff=c, conf$byClass)) |
PeterFankhauserIDS | 03d4ece | 2021-02-22 20:58:28 +0100 | [diff] [blame] | 115 | } |
Marc Kupietz | b1b0336 | 2021-02-23 09:32:16 +0100 | [diff] [blame] | 116 | cvalues %>% |
| 117 | select(c("cutoff", "Recall", "Precision", "F1", "Specificity", "Balanced Accuracy")) %>% |
| 118 | pivot_longer(!cutoff, names_to=c("measure")) %>% |
PeterFankhauserIDS | 3f97f36 | 2021-02-23 10:28:07 +0100 | [diff] [blame] | 119 | ggplot(aes(cutoff, value, colour=measure)) + geom_line() + |
| 120 | scale_x_continuous(breaks = scales::pretty_breaks(n = 10)) + |
| 121 | scale_y_continuous(breaks = scales::pretty_breaks(n = 10)) |
PeterFankhauserIDS | 03d4ece | 2021-02-22 20:58:28 +0100 | [diff] [blame] | 122 | |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 123 | # Using estimates by random forest on entire dataset |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 124 | |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 125 | library(randomForest) |
Marc Kupietz | 13f67ed | 2021-02-22 07:55:03 +0100 | [diff] [blame] | 126 | rf_classifier_full = randomForest(fmla, data=ngramme, importance=TRUE) |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 127 | rf_classifier_full |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 128 | # class.error is 1 - recall |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 129 | varImpPlot(rf_classifier_full) |
| 130 | |
| 131 | # Feature ranking |
| 132 | |
| 133 | # rf features as table |
| 134 | |
| 135 | # correlated features seem to split their rankings |
| 136 | |
| 137 | rfranks<-importance(rf_classifier_full)[,3:4] |
| 138 | |
| 139 | # ttest |
| 140 | |
Marc Kupietz | 201e6f3 | 2021-02-22 12:34:13 +0100 | [diff] [blame] | 141 | idioms<-ngramme %>% filter(CO_IDIOM == "idiom") |
| 142 | nonidioms<-ngramme %>% filter(CO_IDIOM != "idiom") |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 143 | |
| 144 | ttestPvalues<-sapply(vars, |
| 145 | function(sel) t.test(idioms[sel],nonidioms[sel])$p.value) |
| 146 | |
| 147 | # information gain |
| 148 | # multiply by 1000 to avoid undersized bins |
PeterFankhauserIDS | a5b2acf | 2021-02-22 18:14:18 +0100 | [diff] [blame] | 149 | # features are ranked individually no matter their correlation |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 150 | igain<-information.gain(fmla, data=ngramme%>%mutate_at(vars, ~ . * 1000),unit="log2") |
| 151 | |
| 152 | featureRanks<-cbind(rfranks,igain,ttestPvalues) |
| 153 | |
| 154 | #randomForestExplainer::explain_forest(rf_classifier ) |
| 155 | |
| 156 | # averate estimates and feature ranks over 10 runs |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 157 | |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 158 | errrate<-0 |
| 159 | conf<-matrix(0,2,3) |
| 160 | featureRanks<-matrix(0,4,length(vars)) |
| 161 | for (i in 1:10) { |
Marc Kupietz | 13f67ed | 2021-02-22 07:55:03 +0100 | [diff] [blame] | 162 | rfc =randomForest(fmla, data=ngramme, importance=TRUE) |
PeterFankhauserIDS | a5b2acf | 2021-02-22 18:14:18 +0100 | [diff] [blame] | 163 | #rfc =randomForest(fmla, data=ngramme, importance=TRUE, cutoff=c(0.2, 0.8)) |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 164 | errrate<-errrate+rfc$err.rate[100,1] |
| 165 | conf<-conf+rfc$confusion |
| 166 | featureRanks<-featureRanks+ |
| 167 | cbind(importance(rfc)[,3:4], |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 168 | information.gain(fmla, data=ngramme%>%mutate_at(vars, ~ . * 1000),unit="log2"), |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 169 | sapply(vars, |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 170 | function(sel) t.test(idioms[sel],nonidioms[sel])$p.value)) |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 171 | print(errrate/i) |
| 172 | conf1<-round( |
| 173 | rbind( |
| 174 | cbind(conf[,1:2]/i,(1-conf[,3]/i)*100), |
| 175 | c(100*diag(conf[,1:2])/colSums(conf[,1:2]),NA), |
| 176 | c(rowSums(conf[,1:2]/i),NA)),digits=2) |
Marc Kupietz | 201e6f3 | 2021-02-22 12:34:13 +0100 | [diff] [blame] | 177 | colnames(conf1)<-c("1","0","rec") |
| 178 | rownames(conf1)<-c("1","0","prec","sum") |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 179 | print(conf1) |
| 180 | } |
| 181 | featureRanks<-featureRanks/10 |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 182 | colnames(featureRanks)<-c("MeanDecreaseAccuracy","MeanDecreaseGini","InformationGain","Ttest") |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 183 | |
| 184 | |
| 185 | |
| 186 | |