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 | c262278 | 2021-02-21 18:10:01 +0100 | [diff] [blame] | 69 | confusionMatrix(prediction_for_table, test$CO_IDIOM,positive= "1") |
| 70 | |
| 71 | # Sensitivity is recall of class 1 |
| 72 | # Pos Pred Value is precision |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 73 | varImpPlot(rf_classifier) |
| 74 | |
| 75 | # optional resampling with smote |
| 76 | |
| 77 | 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] | 78 | rf_classifier = randomForest(fmla, smoted.data, ntree=100, mtry=10, importance=TRUE) |
| 79 | prediction_for_table <- predict(rf_classifier,test %>% select(-CO_IDIOM)) |
PeterFankhauserIDS | c262278 | 2021-02-21 18:10:01 +0100 | [diff] [blame] | 80 | confusionMatrix(prediction_for_table,test$CO_IDIOM, positive = "1") |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 81 | |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 82 | # Using estimates by random forest on entire dataset |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 83 | |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 84 | library(randomForest) |
PeterFankhauserIDS | ecc9c4c | 2021-02-21 18:26:07 +0100 | [diff] [blame^] | 85 | rf_classifier_full = randomForest(fmla, data=ngramme, ntree=100, mtry=2, importance=TRUE) |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 86 | rf_classifier_full |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 87 | # class.error is 1 - recall |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 88 | varImpPlot(rf_classifier_full) |
| 89 | |
| 90 | # Feature ranking |
| 91 | |
| 92 | # rf features as table |
| 93 | |
| 94 | # correlated features seem to split their rankings |
| 95 | |
| 96 | rfranks<-importance(rf_classifier_full)[,3:4] |
| 97 | |
| 98 | # ttest |
| 99 | |
| 100 | idioms<-ngramme %>% filter(CO_IDIOM==1) |
| 101 | nonidioms<-ngramme %>% filter(CO_IDIOM!=1) |
| 102 | |
| 103 | ttestPvalues<-sapply(vars, |
| 104 | function(sel) t.test(idioms[sel],nonidioms[sel])$p.value) |
| 105 | |
| 106 | # information gain |
| 107 | # multiply by 1000 to avoid undersized bins |
| 108 | # features are ranked individually not matter their correlation |
| 109 | igain<-information.gain(fmla, data=ngramme%>%mutate_at(vars, ~ . * 1000),unit="log2") |
| 110 | |
| 111 | featureRanks<-cbind(rfranks,igain,ttestPvalues) |
| 112 | |
| 113 | #randomForestExplainer::explain_forest(rf_classifier ) |
| 114 | |
| 115 | # averate estimates and feature ranks over 10 runs |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 116 | |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 117 | errrate<-0 |
| 118 | conf<-matrix(0,2,3) |
| 119 | featureRanks<-matrix(0,4,length(vars)) |
| 120 | for (i in 1:10) { |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 121 | rfc =randomForest(fmla, data=ngramme, ntree=100, importance=TRUE) |
| 122 | #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] | 123 | errrate<-errrate+rfc$err.rate[100,1] |
| 124 | conf<-conf+rfc$confusion |
| 125 | featureRanks<-featureRanks+ |
| 126 | cbind(importance(rfc)[,3:4], |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 127 | information.gain(fmla, data=ngramme%>%mutate_at(vars, ~ . * 1000),unit="log2"), |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 128 | sapply(vars, |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 129 | function(sel) t.test(idioms[sel],nonidioms[sel])$p.value)) |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 130 | print(errrate/i) |
| 131 | conf1<-round( |
| 132 | rbind( |
| 133 | cbind(conf[,1:2]/i,(1-conf[,3]/i)*100), |
| 134 | c(100*diag(conf[,1:2])/colSums(conf[,1:2]),NA), |
| 135 | c(rowSums(conf[,1:2]/i),NA)),digits=2) |
| 136 | colnames(conf1)<-c("0","1","rec") |
| 137 | rownames(conf1)<-c("0","1","prec","sum") |
| 138 | print(conf1) |
| 139 | } |
| 140 | featureRanks<-featureRanks/10 |
PeterFankhauserIDS | d1f3df8 | 2021-02-20 14:44:01 +0100 | [diff] [blame] | 141 | colnames(featureRanks)<-c("MeanDecreaseAccuracy","MeanDecreaseGini","InformationGain","Ttest") |
Marc Kupietz | c3bf350 | 2021-02-19 17:18:57 +0100 | [diff] [blame] | 142 | |
| 143 | |
| 144 | |
| 145 | |