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