# Example Analysis using the tm package

#### Irucka Embry

#### 2016-09-21

# Examples

    library(install.load)
    load_package("iemisctext", "tm") # load needed packages using the load_package function from the install.load package (it is assumed that you have already installed these packages)

    # anarchy
    # Example 1

    data(anarchy)

    # create a document term matrix of anarchy
    a <- DocumentTermMatrix(anarchy)

    # find terms used > 5 times
    findFreqTerms(a, 5)

    ##  [1] "`'tis"   "`and"    "`let"    "`thou"   "all"     "and"     "are"
    ##  [8] "art"     "blood"   "but"     "earth"   "england" "english" "every"
    ## [15] "for"     "from"    "had"     "have"    "her"     "his"     "its"
    ## [22] "let"     "like"    "make"    "not"     "one"     "over"    "own"
    ## [29] "such"    "that"    "the"     "their"   "them"    "they"    "those"
    ## [36] "thou"    "through" "thy"     "upon"    "was"     "when"    "where"
    ## [43] "which"   "who"     "will"    "with"    "your"

    # Zipf's law
    Zipf_plot(a)

    ## (Intercept)           x
    ##   3.4810675  -0.5440241

    # war_prayer
    # Example 1

    data(war_prayer)

    # create a document term matrix of war_prayer
    wp <- DocumentTermMatrix(war_prayer)

    # find terms used > 5 times
    findFreqTerms(wp, 5)

    ##  [1] "and"    "for"    "from"   "god"    "have"   "help"   "his"
    ##  [8] "our"    "prayed" "that"   "the"    "their"  "them"   "upon"
    ## [15] "war"    "was"    "which"  "who"    "with"   "you"    "your"

    # Zipf's law
    Zipf_plot(wp)

    ## (Intercept)           x
    ##    2.824209   -0.478659

    # war_racket
    # Example 1

    data(war_racket)

    # create a document term matrix of war_racket
    wr <- DocumentTermMatrix(war_racket)

    # find terms used > 5 times
    findFreqTerms(wr, 5)

    ##   [1] "about"         "all"           "american"      "and"
    ##   [5] "another"       "any"           "are"           "arms"
    ##   [9] "average"       "back"          "bankers"       "been"
    ##  [13] "before"        "being"         "bonds"         "bought"
    ##  [17] "boys"          "business"      "but"           "called"
    ##  [21] "came"          "can"           "can't"         "cannot"
    ##  [25] "capital"       "cent"          "cent."         "chapter"
    ##  [29] "cost"          "could"         "did"           "does"
    ##  [33] "dollars"       "don't"         "during"        "each"
    ##  [37] "earnings"      "eighteen"      "end"           "even"
    ##  [41] "every"         "few"           "for"           "france"
    ##  [45] "from"          "general"       "germany"       "get"
    ##  [49] "got"           "government"    "great"         "had"
    ##  [53] "has"           "have"          "his"           "home"
    ##  [57] "how"           "huge"          "hundred"       "increase"
    ##  [61] "industry"      "international" "into"          "it,"
    ##  [65] "it."           "its"           "just"          "kill"
    ##  [69] "less"          "let"           "let's"         "liberty"
    ##  [73] "like"          "limit"         "little"        "lot"
    ##  [77] "made"          "make"          "manufacturers" "many"
    ##  [81] "men"           "might"         "more"          "mosquito"
    ##  [85] "munitions"     "must"          "nation"        "never"
    ##  [89] "no."           "normal"        "not"           "now"
    ##  [93] "off"           "one"           "only"          "other"
    ##  [97] "our"           "out"           "over"          "own"
    ## [101] "paid"          "part"          "pay"           "pays"
    ## [105] "people"        "per"           "period"        "profit"
    ## [109] "profits"       "profits."      "put"           "racket"
    ## [113] "sam"           "same"          "ships"         "should"
    ## [117] "smash"         "sold"          "soldier"       "soldiers"
    ## [121] "some"          "states"        "steel"         "still"
    ## [125] "take"          "than"          "that"          "the"
    ## [129] "their"         "them"          "then"          "there"
    ## [133] "these"         "they"          "this"          "those"
    ## [137] "thousands"     "three"         "time"          "told"
    ## [141] "too"           "too."          "total"         "two"
    ## [145] "uncle"         "united"        "until"         "upon"
    ## [149] "use"           "very"          "war"           "war,"
    ## [153] "war."          "was"           "well"          "well,"
    ## [157] "went"          "were"          "what"          "when"
    ## [161] "where"         "whether"       "who"           "will"
    ## [165] "with"          "without"       "world"         "would"
    ## [169] "year"          "year."         "yearly"        "years"
    ## [173] "yes,"          "you"           "your"

    # Zipf's law
    Zipf_plot(wr)

    ## (Intercept)           x
    ##    5.212469   -0.728547

    # connect_dots
    # Example 1

    data(connect_dots)

    # create a document term matrix of connect_dots
    cd <- DocumentTermMatrix(connect_dots)

    # find terms used > 5 times
    findFreqTerms(cd, 5)

    ## [1] "and"       "for"       "has"       "have"      "must"      "our"
    ## [7] "petroleum" "that"      "the"

    # Zipf's law
    Zipf_plot(cd)

    ## (Intercept)           x
    ##   2.5493059  -0.4666954

    # us_them
    # Example 1

    data(us_them)

    # create a document term matrix of us_them
    ut <- DocumentTermMatrix(us_them)

    # find terms used > 5 times
    findFreqTerms(ut, 5)

    ##  [1] "all"     "and"     "for"     "freedom" "gave"    "know"    "not"
    ##  [8] "our"     "tha"     "the"     "their"   "them"    "they"    "will"
    ## [15] "won't"

    # Zipf's law
    Zipf_plot(ut)

    ## (Intercept)           x
    ##   2.9193372  -0.5665568

    # climate_strange
    # Example 1

    data(climate_strange)

    # create a document term matrix of climate_strange
    cs <- DocumentTermMatrix(climate_strange)

    # find terms used > 5 times
    findFreqTerms(cs, 5)

    ##  [1] "all"     "are"     "can"     "earth"   "for"     "have"    "life"
    ##  [8] "must"    "not"     "our"     "science" "that"    "the"     "this"
    ## [15] "your"

    # Zipf's law
    Zipf_plot(cs)

    ## (Intercept)           x
    ##   2.2297149  -0.3823361

    library(install.load)
    load_package("iemisctext", "tm") # load needed packages using the load_package function from the install.load package (it is assumed that you have already installed these packages)

    # anarchy
    # Example 2

    data(anarchy)

    # View text representation of anarchy
    inspect(anarchy[[1]])

    # war_prayer
    # Example 2

    data(war_prayer)

    # View text representation of war_prayer
    inspect(war_prayer[[1]])

    # war_racket
    # Example 2

    data(war_racket)

    # View text representation of war_racket
    inspect(war_racket[[1]])

    # connect_dots
    # Example 2

    data(connect_dots)

    # View text representation of connect_dots
    inspect(connect_dots[[1]])

    # us_them
    # Example 2

    data(us_them)

    # View text representation of us_them
    inspect(us_them[[1]])

    # climate_strange
    # Example 2

    data(climate_strange)

    # View text representation of climate_strange
    inspect(climate_strange[[1]])