Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

## Appendix to Session 8: Intro to Text Mining in R, ML Estimation + Binomial Logistic Regression

Welcome to Introduction to R for Data Science, Session 8: Intro to Text Mining in R, ML Estimation + Binomial Logistic Regression [Web-scraping with tm.plugin.webmining. The tm package corpora structures: assessing document metadata and content. Typical corpus transformations and Term-Document Matrix production. A simple binomial regression model with tf-idf scores as features and its shortcommings due to sparse data. Reminder: Maximum Likelihood Estimation with Nelder-Mead from optim().]

The course is co-organized by Data Science Serbia and Startit. You will find all course material (R scripts, data sets, SlideShare presentations, readings) on these pages.

Check out the Course Overview to acess the learning material presented thus far.

Data Science Serbia Course Pages [in Serbian]

Startit Course Pages [in Serbian]

## Summary of Session 8, 17. June 2016 :: Intro to Text Mining in R + Binomial Logistic Regression.

Intro to Text Mining in R + Binomial Logistic Regression. Intro to Text Mining in R + Binomiral Logistic Regression: Web-scraping with tm.plugin.webmining. The tm package corpora structures: assessing document metadata and content. Typical corpus transformations and Term-Document Matrix production. A simple binomial regression model with tf-idf scores as features and its shortcommings due to sparse data. Reminder: Maximum Likelihood Estimation with Nelder-Mead from optim().

Session 8 R Script

## R script :: Session 8

Split data into training and test

```#### w. training vs. test data set
# split into test and training
dim(dataSet)
choice <- sample(1:475,250,replace = F)
test <- which(!(c(1:475) %in% choice))
trainData <- dataSet[choice,]
newData <- dataSet[test,]
# check!
sum(dataSet\$Category[choice])/length(choice) # proportion of dotCom in training
sum(dataSet\$Category[test])/length(choice) # proportion of dotCom in test

# Binomial Logistic Regression: use glm w. logit link
bLRmodel <- glm(Category ~.,
control = list(maxit = 500),
data=trainData)

sumLR <- summary(bLRmodel)
sumLR

# Coefficients
sumLR\$coefficients
class(sumLR\$coefficients)
coefLR <- as.data.frame(sumLR\$coefficients)
# Wald statistics significant? (this Wald z is normally distributed)
coefLR <- coefLR[order(-coefLR\$Estimate), ]
w <- which((coefLR\$`Pr(>|z|)` < .05)&(!(rownames(coefLR) == "(Intercept)")))
# which predictors worked?
rownames(coefLR)[w]

# plot coefficients {ggplot2}
plotFrame <- coefLR[w,]
plotFrame\$Estimate <- round(plotFrame\$Estimate,2)
plotFrame\$Features <- rownames(plotFrame)
plotFrame <- plotFrame[order(-plotFrame\$Estimate), ]
plotFrame\$Features <- factor(plotFrame\$Features, levels = plotFrame\$Features, ordered=T)
ggplot(data = plotFrame, aes(x = plotFrame\$Features, y = plotFrame\$Estimate)) +
geom_line(group=1) + geom_point(color="red", size=2.5) + geom_point(color="white", size=2) +
xlab("Features") + ylab("Regression Coefficients") +
ggtitle("Logistic Regression: Coeficients (sig. Wald test)") +
theme(axis.text.x = element_text(angle=90))

# fitted probabilities
fitted(bLRmodel)
hist(fitted(bLRmodel),50)
plot(density(fitted(bLRmodel)),
main = "Predicted Probabilities: Density")
polygon(density(fitted(bLRmodel)),
col="red",
border="black")

# Prediction from the model
predictions <- predict(bLRmodel,
newdata=newData,
type='response')

predictions <- ifelse(predictions >= 0.5,1,0)
trueCategory <- newData\$Category

meanClasError <- mean(predictions != trueCategory)
accuracy <- 1-meanClasError
accuracy # probably rather poor..? - Why? - Think!

# Try to train a binomial regression model many times by randomly assigning
# documents to the training and test data set
# What happens? Why?

# *Look* at your data set and *think* about it before actually modeling it.```
{"ft_lang_label":"__label__en","ft_lang_prob":0.66465825,"math_prob":0.933511,"size":4657,"snap":"2023-40-2023-50","text_gpt3_token_len":1157,"char_repetition_ratio":0.11562433,"word_repetition_ratio":0.2672131,"special_character_ratio":0.2385656,"punctuation_ratio":0.15716097,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9981282,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-07T07:27:29Z\",\"WARC-Record-ID\":\"<urn:uuid:818030ac-4f38-441f-80fa-6408cce0ea0e>\",\"Content-Length\":\"107958\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0e7888ac-97d1-4bdb-809e-821650c12175>\",\"WARC-Concurrent-To\":\"<urn:uuid:e872f5a8-e835-4fc5-8a3d-2053cd7d49f0>\",\"WARC-IP-Address\":\"172.67.211.236\",\"WARC-Target-URI\":\"https://www.r-bloggers.com/2016/06/introduction-to-r-for-data-science-session-8-appendix/\",\"WARC-Payload-Digest\":\"sha1:VSFTTJQY55WLS2RBNT3GO6LOI7VXXMKD\",\"WARC-Block-Digest\":\"sha1:EKTWAAGNMVROHX6PGZRWK56IC5YV6KVN\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100650.21_warc_CC-MAIN-20231207054219-20231207084219-00302.warc.gz\"}"}