Computational Text Analysis and
Social Media Research using R


Justin Chun-ting Ho


Learning Objectives:

  1. Basic R (Day 1 & 2)
  2. Collecting social media data (Day 3)
  3. Analysing text using R (Day 4 & 5)

(Realistic) Learning Objectives:

By the end of the course, you will be able to:
  1. Know what software and packages are available for various research tasks
  2. Copy and paste a chunk of codes and make it work
  3. Read documentation and R scripts so that you can further your learning independently

    Introduction to R

    R & R Studio

    Create a new project

    1. Under the File menu, click on New project, choose New directory, then New project
    2. Enter a name for this new folder, and choose a convenient location
    3. Click on Create project
    4. Create a new file where we will type our scripts

    Interaction with R

    • Type commands directly into the console and press 'Enter'
    • Execute commands directly from the script editor using 'Ctrl' + 'Enter' ('Cmd' + 'Return' for Macs)

    Tidyverse

    %>%

    Grammar of Graphics

    Collecting Social Media Data

    What is an API?

    Documentations:

    Warning!

    • API is NOT designed for research's purposes!
    • What you can get depends on the platform's mercy!
    • It is prone to error and bias!

    Available Endpoints and Limitations

    Facebook:

    • Graph API: Public pages data only (no personal profile); Maximum of 600 posts per page per year; Known bias exists (See: Ho, 2020)
    • CrowdTangle: Public Facebook pages and Instagram accounts; Limitations on type of data; Available to university researchers on selected topics only

    Twitter:

    • Search API: Limited to tweets published in the past 7 days
    • Streaming API: ~2% of Twittersphere; Known bias exists (See: Morstatter et al., 2014)
    • Academic Research Product Track: Available to academic researchers only; Full-archive access; Sounds too good to be true

    Conclusion:

    • Know what you can (can't) get
    • Collect data while you still can
      (it might not be the case tomorrow)
    • Observe Terms of Service
      (and other platform's restrictions)

    Time for Data Collection

    Text Analysis: The Basics

    “Systematic, objective, quantitative analysis of message characteristics"

    Kimberly A. Neuendorf, The Content Analysis Guidebook

    Types of Text Analysis


    Degree of human involvement:

    • Human coding (100%)
    • Supervised
    • Unsupervised (0%)

    Type of Objective:

    • Scaling
    • Classification

    (Source: Justin Grimmer and Brandon Stewart, 2013)

    4 Principles of Text Analysis

    (Read: Grimmer, J. and Steward, B. (2013). Text as Data: The Promise and Pitfalls of Automatic Content Analysis Methods for Political Texts. Political Analysis, 21(3), 267-297. doi:10.1093/pan/mps028)

    1. All Quantitative Models of Language Are Wrong
—But Some Are Useful

    • Data generation process for any text is a mystery
    • All methods necessarily fail to provide an accurate account of the data-generating process
    • Meanings change drastically: “Time flies like an arrow. Fruit flies like a banana.”

    2. Quantitative Methods Augment Humans, Not Replace Them

    • Text Analysis will not eliminate the need for careful thought nor remove the necessity of reading
    • Rather than replace humans, computers amplify human abilities

    3. There Is No Globally Best Method for Automated Text Analysis

    • Different research questions and designs need different models
    • The same model will perform well on some data sets, but poorly on other

    4. Validate, Validate, Validate

    • Results can be misleading or simply wrong
    • Supervised methods: able to reliably replicate human coding
    • Unsupervised methods: the measures are as conceptually valid

    Bag of Words Assumption


    - Word order doesn’t matter

    - The followings mean exactly the same:

    • I enjoy eating food and being with my family
    • I enjoy eating my family and being with food
    • and being eating enjoy family food I my with

    Original Text

    ## <<PlainTextDocument>>
    ## Metadata:  7
    ## Content:  chars: 181
    ##
    ## Article 1. All human beings are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.

    Remove Punctuation

    ## <<PlainTextDocument>>
    ## Metadata:  7
    ## Content:  chars: 178
    ##
    ## Article 1 All human beings are born free and equal in dignity and rights They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood

    To Lower Case

    ## <<PlainTextDocument>>
    ## Metadata:  7
    ## Content:  chars: 178
    ##
    ## article 1 all human beings are born free and equal in dignity and rights they are endowed with reason and conscience and should act towards one another in a spirit of brotherhood

    Remove Numbers

    ## <<PlainTextDocument>>
    ## Metadata:  7
    ## Content:  chars: 177
    ##
    ## article  all human beings are born free and equal in dignity and rights they are endowed with reason and conscience and should act towards one another in a spirit of brotherhood

    Remove Stopwords

    ## <<PlainTextDocument>>
    ## Metadata:  7
    ## Content:  chars: 135
    ##
    ## article   human beings  born free  equal  dignity  rights   endowed  reason  conscience   act towards one another   spirit  brotherhood

    Stemming

    ## <<PlainTextDocument>>
    ## Metadata:  7
    ## Content:  chars: 108
    ##
    ## articl human be born free equal digniti right endow reason conscienc act toward one anoth spirit brotherhood

    Optional: Create N-Gram

    
    ##
    ## article_1 1_all all_human human_beings beings_are are_born born_free free_and and_equal equal_in
    

    Create DFM

    
    ##     Terms
    ## Docs act anoth articl born brotherhood conscienc digniti endow equal free
    ##    1   1     1      1    1           1         1       1     1     1    1
    ##    2   0     0      1    0           0         0       0     0     0    1
    

    The Output

    Time for R

    Keyword Analysis

    Keyword Analysis


    What is a Keyword?

    “A keyword may be defined as a word which occurs with unusual frequency in a given text. This does not mean high frequency but unusual frequency, by comparison with a reference corpus of some kind”



    (Scott, M. (1997). PC analysis of key words - and key key words. System, 25(2), 233-45.)

    Keyword Analysis


    What is Keyness?

    “The keyness of a keyword represents the value of log-likelihood or Chi-square statistics; in other words it provides an indicator of a keyword’s importance as a content descriptor for the appeal.”

    (Scott, M. (1997). PC analysis of key words - and key key words. System, 25(2), 233-45.)

    Keyword Analysis


    What is a Chi-squared test?

    Comparison between the observed frequency and expected frequency.

    Time for R

    Sentiment Analysis

    What is Sentiment Analysis?


    Sentiment analysis, also called opinion mining, is the field of study that analyzes people’s opinions, sentiments, evaluations, appraisals, attitudes, and emotions towards entities such as products, services, organizations, individuals, issues, events, topics, and their attributes.

    What is Sentiment?

    • Opinions (Good vs Bad)
    • Emotions (Happy vs Sad)
    • Attitudes (Like vs Dislike)

      Levels of Analysis


      - Document level

      - Sentence level

      - Entity and Aspect level

      Examples of Usage


      - Product Review

      - Public Opinion

      - Voters Support

      - Wellbeing/Mental Health

      Lexicons


      - "Dictionary" for sentiment

      - Popular Lexicons:

      • LIWC
      • Lexicoder Sentiment Dictionary (postive, negative)
      • AFINN (postive to negative from +5 to -5)
      • Bing (postive, netgative)
      • NRC (postive, netgative, anger, anticipation, disgust, fear, joy, sadness, surprise, trust)

      Challenges


      1. Opposite orientations in different applications.

      “This camera sucks.” vs
      “This vacuum cleaner really sucks.”

      Challenges


      2. Sentence containing sentiment words may not express any sentiment.

      “If I can find a good camera in the shop, I will buy it.”

      Challenges


      3. Sarcastism

      “What a Genius! You uploaded your passwords to Github!”

      Note: Very common in political discussion, especially on social media.

      Challenges


      4. Sentences without sentiment words can also imply opinions.

      “This car burns a lot of fuel.”

      Time for R

      Twitter: @justin_ct_ho
      Github: justinchuntingho
      Email: justin.chunting.ho@sciencespo.fr