Introduction

The goal of this project is just to display that you’ve gotten used to working with the data and that you are on track to create your prediction algorithm. Please submit a report on R Pubs that explains your exploratory analysis and your goals for the eventual app and algorithm. This document should be concise and explain only the major features of the data you have identified and briefly summarize your plans for creating the prediction algorithm and Shiny app in a way that would be understandable to a non-data scientist manager. You should make use of tables and plots to illustrate important summaries of the data set. The motivation for this project is to:

  1. Demonstrate that you’ve downloaded the data and have successfully loaded it in.

  2. Create a basic report of summary statistics about the data sets.

  3. Report any interesting findings that you amassed so far.

  4. Get feedback on your plans for creating a prediction algorithm and Shiny app.

Review criteria:

  1. Does the link lead to an HTML page describing the exploratory analysis of the training data set?

  2. Has the data scientist done basic summaries of the three files? Word counts, line counts and basic data tables?

  3. Has the data scientist made basic plots, such as histograms to illustrate features of the data?

  4. Was the report written in a brief, concise style, in a way that a non-data scientist manager could appreciate?

Preparation of data

Check if the dataset zip file exists. If not, download and unzip it

options(timeout = 500)

link <- "https://d396qusza40orc.cloudfront.net/dsscapstone/dataset/Coursera-SwiftKey.zip"

download.file(link, destfile = "Coursera-SwiftKey.zip", mode = "wb")

unzip("Coursera-SwiftKey.zip")

Define file paths for the different text sources

blogs   <- "./final/en_US/en_US.blogs.txt"
news    <- "./final/en_US/en_US.news.txt"
twitter <- "./final/en_US/en_US.twitter.txt" 

Calculate file sizes in megabytes

blogs_size   <- file.size(blogs)/(2^20)
news_size    <- file.size(news)/(2^20)
twitter_size <- file.size(twitter)/(2^20)

Read lines from each text source

blogs_lines   <- (readLines(blogs, skipNul = TRUE))
news_lines    <- (readLines(news,  skipNul = TRUE))
## Warning in readLines(news, skipNul = TRUE): incomplete final line found on
## './final/en_US/en_US.news.txt'
twitter_lines <- (readLines(twitter, skipNul = TRUE))

Count total lines for each source

blogs_total_lines   <- length(blogs_lines)
news_total_lines    <- length(news_lines)
twitter_total_lines <- length(twitter_lines)

Calculate number of characters per line

blogs_chars_line   <- nchar(blogs_lines)
news_chars_line    <- nchar(news_lines)
twitter_chars_line <- nchar(twitter_lines)

Calculate total characters for each source

blogs_total_chars   <- sum(blogs_chars_line)
news_total_chars    <- sum(news_chars_line)
twitter_total_chars <- sum(twitter_chars_line)

Count words per line for each source (requires stringi package)

library(stringi)
blogs_words_line <- stri_count_words(blogs_lines)
news_words_line <- stri_count_words(news_lines)
twitter_words_line <- stri_count_words(twitter_lines)

Calculate total words for each source

blogs_total_words   <- sum(blogs_words_line)
news_total_words    <- sum(news_words_line)
twitter_total_words <- sum(twitter_words_line)

Create a dataframe summarizing the results

final_values <- data.frame(Files = c("blogs file", "news file", "twitter file"),
                           Size  = c(blogs_size, news_size, twitter_size),
                           Lines = c(blogs_total_lines, news_total_lines, twitter_total_lines),
                           Chars = c(blogs_total_chars, news_total_chars, twitter_total_chars),
                           Words = c(blogs_total_words, news_total_words, twitter_total_words))

Use knitr package for better table formatting

library(knitr)
kable(final_values)
Files Size Lines Chars Words
blogs file 200.4242 899288 206824509 37546806
news file 196.2775 77259 15639408 2674561
twitter file 159.3641 2360148 162122861 30096690

Setting seed for reproducibility

set.seed(12345)

Sampling words per line for each source

blogs_words_line_sample <- sample(blogs_words_line, length(blogs_words_line) * 0.01)
news_words_line_sample <- sample(news_words_line, length(news_words_line) * 0.01)
twitter_words_line_sample <- sample(twitter_words_line, length(twitter_words_line) * 0.01)

Free up memory by removing large objects

rm(blogs_words_line, news_words_line, twitter_words_line)

Plot histograms for words per line in each source

hist(blogs_words_line_sample,
     main="Words per Line in Blogs",
     xlab="Words per Line",
     col="green") 

hist(news_words_line_sample,
     main="Words per Line in News",
     xlab="Words per Line",
     col="red")

hist(twitter_words_line_sample,
     main="Words per Line in Twitter",
     xlab="Words per Line",
     col="lightblue")

Conclusion

In the final plot, the impact of Twitter’s word limit is evident. However, across all three files, the word count per line is relatively low, indicating that the messages are concise. This brevity will be a significant factor to consider during the implementation of the prediction algorithm.