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 | |
PeterFankhauserIDS | ed93d2e | 2021-02-20 14:51:13 +0100 | [diff] [blame] | 9 | # Test |
| 10 | |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 11 | ngramfile<-"gold03_anno_ml_synfeat_nstopw" |
| 12 | |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 13 | setwd(dirname(rstudioapi::getSourceEditorContext()$path)) |
| 14 | stopwords <- readLines(con = "../data/stopwords.txt",encoding="UTF-8") |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 15 | 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] | 16 | syfeaturenames <- read.csv("../data/syfeatures.tsv", header = TRUE, sep = "\t", dec=".", quote="", encoding="UTF-8",stringsAsFactors=FALSE) |
| 17 | # syfeaturenames$navalue<-sapply(syfeaturenames$navalue,as.numeric) |
| 18 | |
| 19 | deleteStopwords = function(wl, stopwords = NULL) { |
| 20 | wl[!(wl %in% stopwords)] |
| 21 | } |
| 22 | |
Marc Kupietz | 631800f | 2021-02-19 17:27:26 +0100 | [diff] [blame] | 23 | oringramme <- oringramme %>% |
| 24 | filter(CO_IDIOM < 2) # just two classes: 0 no idiom, 1 idiom |
| 25 | |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 26 | # Reduce number of classes, treat null values, add NSTOPW, change names for SY features |
| 27 | |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 28 | ngramme <- oringramme %>% |
| 29 | add_column(NSTOPW = sapply(oringramme$tokens,function(x) length(deleteStopwords(tolower(unlist(strsplit(x," "))),stopwords)))) %>% |
| 30 | # select(-matches("CO_TOKEN.*"), -tokens) %>% |
| 31 | select(-matches("CO_TOKEN.*")) %>% # keep tokens for interpretability |
| 32 | mutate(across(matches(".rank.*"), ~ replace_na(.x, 1000))) %>% |
| 33 | 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] | 34 | rename_at(syfeaturenames$innames, ~ syfeaturenames[syfeaturenames$innames==.x,]$synames ) %>% |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 35 | mutate(across(everything(), ~ replace_na(.x, 0))) %>% |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 36 | mutate(CO_IDIOM = as.factor(if_else(CO_IDIOM !=1, "0", "1"))) # just two classes: 0 no idiom, 1 idiom |
| 37 | |
| 38 | # Optional |
| 39 | write.table(ngramme,file=paste("../data/",ngramfile,"_cosy.csv",sep=""), sep = "\t", quote=F) |
| 40 | |
| 41 | # featuresets |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 42 | |
| 43 | 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") |
| 44 | syvars <- c(syfeaturenames$synames,"NSTOPW") |
| 45 | vars <- c(covars,syvars) |
| 46 | |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 47 | # formulae for training and testing rf |
| 48 | |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 49 | fmla <- as.formula(paste("CO_IDIOM ~ ", paste(vars, collapse= "+"))) |
| 50 | fmlaco <- as.formula(paste("CO_IDIOM ~ ", paste(covars, collapse= "+"))) |
| 51 | fmlasy <- as.formula(paste("CO_IDIOM ~ ", paste(syvars, collapse= "+"))) |
| 52 | |
| 53 | # Simple train/test split |
| 54 | |
| 55 | trainRows <- sample(nrow(ngramme), nrow(ngramme)*0.8, replace = FALSE) |
| 56 | train <- ngramme[trainRows,] |
| 57 | test <- ngramme[setdiff(1:nrow(ngramme),trainRows),] |
| 58 | |
| 59 | rf_classifier = randomForest(fmla, train, ntree=100, mtry=10, importance=TRUE) |
| 60 | |
| 61 | # only SY features |
| 62 | # rf_classifier = randomForest(fmlasy, train, ntree=100, mtry=10, importance=TRUE) |
| 63 | |
| 64 | prediction_for_table <- predict(rf_classifier, test %>% select(-CO_IDIOM)) |
| 65 | |
| 66 | # different cutoff for prediction |
| 67 | # prediction_for_table <- predict(rf_classifier, test %>% select(-CO_IDIOM), cutoff = c(0.8, 0.2)) |
| 68 | |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 69 | confusion <- table(observed=test$CO_IDIOM,predicted=prediction_for_table) |
| 70 | conf <- confusionMatrix(confusion, positive= "1") |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 71 | print(conf) |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 72 | # Sensitivity is precision of class 1 |
| 73 | # Pos Pred Value is recall |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 74 | varImpPlot(rf_classifier) |
| 75 | |
| 76 | # optional resampling with smote |
| 77 | |
| 78 | smoted.data <- SMOTE(fmla, subset(train, select = c("CO_IDIOM", vars)), perc.over = 1200, perc.under = 100) |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 79 | rf_classifier = randomForest(fmla, smoted.data, ntree=100, mtry=10, importance=TRUE) |
| 80 | prediction_for_table <- predict(rf_classifier,test %>% select(-CO_IDIOM)) |
| 81 | confusion <- table(observed=test$CO_IDIOM,predicted=prediction_for_table) |
Marc Kupietz | 0932a78 | 2021-02-19 17:39:47 +0100 | [diff] [blame] | 82 | conf <- confusionMatrix(confusion, positive = "1") |
| 83 | print(conf) |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 84 | |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 85 | # Using estimates by random forest on entire dataset |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 86 | |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 87 | library(randomForest) |
| 88 | rf_classifier_full = randomForest(fmla, data=ngramme, ntree=100, mtry=2, importance=TRUE, cutoff=c(0.8,0.2)) |
| 89 | rf_classifier_full |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 90 | # class.error is 1 - recall |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 91 | varImpPlot(rf_classifier_full) |
| 92 | |
| 93 | # Feature ranking |
| 94 | |
| 95 | # rf features as table |
| 96 | |
| 97 | # correlated features seem to split their rankings |
| 98 | |
| 99 | rfranks<-importance(rf_classifier_full)[,3:4] |
| 100 | |
| 101 | # ttest |
| 102 | |
| 103 | idioms<-ngramme %>% filter(CO_IDIOM==1) |
| 104 | nonidioms<-ngramme %>% filter(CO_IDIOM!=1) |
| 105 | |
| 106 | ttestPvalues<-sapply(vars, |
| 107 | function(sel) t.test(idioms[sel],nonidioms[sel])$p.value) |
| 108 | |
| 109 | # information gain |
| 110 | # multiply by 1000 to avoid undersized bins |
| 111 | # features are ranked individually not matter their correlation |
| 112 | igain<-information.gain(fmla, data=ngramme%>%mutate_at(vars, ~ . * 1000),unit="log2") |
| 113 | |
| 114 | featureRanks<-cbind(rfranks,igain,ttestPvalues) |
| 115 | |
| 116 | #randomForestExplainer::explain_forest(rf_classifier ) |
| 117 | |
| 118 | # averate estimates and feature ranks over 10 runs |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 119 | |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 120 | errrate<-0 |
| 121 | conf<-matrix(0,2,3) |
| 122 | featureRanks<-matrix(0,4,length(vars)) |
| 123 | for (i in 1:10) { |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 124 | rfc =randomForest(fmla, data=ngramme, ntree=100, importance=TRUE) |
| 125 | #rfc =randomForest(fmla, data=ngramme, ntree=100, importance=TRUE, cutoff=c(0.8,0.2)) |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 126 | errrate<-errrate+rfc$err.rate[100,1] |
| 127 | conf<-conf+rfc$confusion |
| 128 | featureRanks<-featureRanks+ |
| 129 | cbind(importance(rfc)[,3:4], |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 130 | information.gain(fmla, data=ngramme%>%mutate_at(vars, ~ . * 1000),unit="log2"), |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 131 | sapply(vars, |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 132 | function(sel) t.test(idioms[sel],nonidioms[sel])$p.value)) |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 133 | print(errrate/i) |
| 134 | conf1<-round( |
| 135 | rbind( |
| 136 | cbind(conf[,1:2]/i,(1-conf[,3]/i)*100), |
| 137 | c(100*diag(conf[,1:2])/colSums(conf[,1:2]),NA), |
| 138 | c(rowSums(conf[,1:2]/i),NA)),digits=2) |
| 139 | colnames(conf1)<-c("0","1","rec") |
| 140 | rownames(conf1)<-c("0","1","prec","sum") |
| 141 | print(conf1) |
| 142 | } |
| 143 | featureRanks<-featureRanks/10 |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 144 | colnames(featureRanks)<-c("MeanDecreaseAccuracy","MeanDecreaseGini","InformationGain","Ttest") |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 145 | |
| 146 | |
| 147 | |
| 148 | |