This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(tidyverse) | |
| library(quanteda) | |
| library(spacyr) | |
| get_passive <- function(text, ratio = F){ | |
| d <- spacy_parse(text, dependency = T) | |
| aux_pass <- d %>% | |
| mutate(doc_id = as.numeric(str_remove_all(doc_id, "text"))) %>% | |
| group_by(doc_id) %>% |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(tidyverse) | |
| library(caret) | |
| library(lime) | |
| #fit | |
| lasso_caret<- train(data = mtcars, mpg~., method = "glmnet", | |
| tuneGrid = expand.grid(alpha = 1, | |
| lambda = 0)) | |
| #LIME | |
| explainer <- lime(mtcars, lasso_caret) #here the train data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| cat_state <- function(a, abbreviation=F) { | |
| Swing = c("Wisconsin", "Virginia", "Michigan", "Pennsylvania", | |
| "New Hampshire", "Colorado", "Nevada", "North Carolina", | |
| "Florida", "Ohio", "Arizona", "Iowa", "Georgia") | |
| Swing_abb = state.abb[match(Swing,state.name)] | |
| Democrat = c("New Mexico", "Minnesota", "Maine", "Oregon", | |
| "New Jersey", "Delaware", "Connecticut", "Illinois", | |
| "Washington", "Rhode Island", "New York", "California", | |
| "Massachusetts", "Hawaii", "Maryland", "Vermont") | |
| Democrat_abb = state.abb[match(Democrat,state.name)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(rJava) | |
| library(coreNLP) | |
| initCoreNLP() | |
| #in this case, 'test' is a data frame with a col named 'text' | |
| for (i in 1:dim(test)[1]) { | |
| cat(paste0(i / dim(test)[1] * 100, '% completed')) | |
| ann <- annotateString(paste(test$text[i]), format = c("obj"), outputFile = NA, includeXSL = FALSE) | |
| gd <- getDependency(ann) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| novad <- read.csv("Download_NoVAD_csv.csv") #Change for you local csv file | |
| novad$Word <- as.character(novad$Word) | |
| library(stringr) | |
| library(tm) | |
| NoVAD <- function(a){ | |
| a <- as.character(a) | |
| sms_corpus <- Corpus(VectorSource(a)) | |
| #translate all letters to lower case | |
| clean_corpus <- tm_map(sms_corpus, tolower) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ANEW function | |
| ANEW_txt <- read.table("Downloaded_ANEW_txt.txt", header = T, sep = "\t") #you should download ANEW first | |
| ANEW_txt$Word <- as.character(ANEW_txt$Word) | |
| library(stringr) | |
| library(tm) | |
| ANEW <- function(a){ | |
| a <- as.character(a) |