Monday, January 12, 2026

Importing Twitter knowledge into Stata


Up to now, we’ve had customers ask if Stata might import Twitter knowledge. So we requested one in all our interns, Dawson Deere (at the moment engaged on his laptop science diploma at Texas A&M College) to see if he might write a brand new command to do that. He used Stata 15’s improved Java plugins function to put in writing a brand new twitter2stata command. To put in twitter2stata, sort


ssc set up twitter2stata, exchange

As soon as put in, you are able to do the next

  • Import tweets based mostly on a search string
. twitter2stata searchtweets "search_string"
  • Import consumer knowledge utilizing a search string
  • . twitter2stata searchusers "search_string"
    
  • Import a specfic consumer’s knowledge
  • . twitter2stata getuser "userId_or_userName"
    
  • Import lists, likes, following, and followers consumer knowledge
  • . twitter2stata likes "userId_or_userName"
    . twitter2stata following "userId_or_userName"
    . twitter2stata followers "userId_or_userName"
    . twitter2stata lists "userId_or_userName"
    
  • Import knowledge a couple of particular checklist
  • . twitter2stata listusers "listId_or_listName"
    . twitter2stata listtweets "listId_or_listName"
    

    The principle function of this put up is to indicate you how one can get the twitter2stata command working in Stata. Beneath are the steps you need to take.

    1. To make use of this command, you need to have a Twitter account. For those who don’t have one, you possibly can create one right here.

    2. Twitter limits the quantity of knowledge you possibly can obtain. For the very best price limits, you need to create a Twitter app. To do that, login to the Twitter web site, and go to https://apps.twitter.com.

      It is best to see

    3. Click on on the Create New App button. You will notice

      graph1

    4. There are three fields you need to fill in: Identify, Description, and Web site. The identify of the appliance have to be distinctive. You may need to strive a couple of occasions to discover a distinctive Twitter utility identify. After getting crammed within the kind, click on on Create your Twitter Utility. Subsequent, click on on the Key and Entry Tokens tab.

      graph1

    5. Click on on the Create my entry token button to generate your entry token and entry token secret. You will notice

      graph1

    6. You will have to repeat the

      • Client Key (API Key)
      • Client Secret (API Secret)
      • Entry Token
      • Entry Token Secret

      and paste them right into a do-file, for instance,

      native consumer_key "xWNlx*N9vESv0ZZBtGdm7fVB"
      native consumer_secret "7D25oVzWeDCHrUlQcp9929@GOcnqWCuUKhDel"
      native access_token "74741598400768-3hAYpZbiDvABPizx5lk57B8CTVyfa"
      native access_token_secret "7HjDf25oVzDWAeDCHrUlQcpfNGOTzcnqWCuUKhDel"
      

      Be certain not to share these with anyone else.

    In the identical do-file, add the command

    twitter2stata setaccess "`consumer_key'" "`consumer_secret'" ///
          "`access_token'" "`access_token_secret'"
    

    to initialize these settings for twitter2stata. For those who don’t use twitter2stata setaccess … earlier than every twitter2stata session, you’ll recieve the error beneath.

    
    . twitter2stata searchtweets "star wars", numtweets(10)
    entry token and entry token secret not set.
    Run twitter2stata setaccess to set your entry token and entry token secret.
    r(198);
    

    My do-file is now

    native consumer_key "xWNlx*N9vESv0ZZBtGdm7fVB"
    native consumer_secret "7D25oVzWeDCHrUlQcp9929@GOcnqWCuUKhDel"
    native access_token "74741598400768-3hAYpZbiDvABPizx5lk57B8CTVyfa"
    native access_token_secret "7HjDf25oVzDWAeDCHrUlQcpfNGOTzcnqWCuUKhDel"
    
    twitter2stata setaccess "`consumer_key'" "`consumer_secret'" ///
         "`access_token'" "`access_token_secret'"
    twitter2stata searchtweets "star wars", numtweets(10)
    checklist user_screen_name user_follower_count user_friend_count, abbreviate(20)
    

    Once I run the do-file, I get

    . twitter2stata searchtweets "star wars", numtweets(10)
    (45 vars, 10 obs)
    
    . checklist user_screen_name user_follower_count user_friend_count, abbreviate(20)
    
         +------------------------------------------------------------+
         | user_screen_name   user_follower_count   user_friend_count |
         |------------------------------------------------------------|
      1. |            024AB                  1297                1077 |
      2. |     StarWarsTime                  2040                1213 |
      3. |      LockerGnome                 24577                 976 |
      4. |           CMG_HD                     8                  35 |
      5. |   dilnyminic1986                     4                  30 |
         |------------------------------------------------------------|
      6. |     StarWarsTime                  2040                1213 |
      7. |   emimsohood1975                     3                   8 |
      8. |  Dan_NinjaRabbit                   712                2252 |
      9. |  KatelynLunsford                    13                  38 |
     10. |        PudseyMac                   335                 444 |
         +------------------------------------------------------------+
    

    Once more, there are limits to the quantity of knowledge Twitter will allow you to import. These limits are subcommand particular and restrict the variety of calls you may make to Twitter’s REST API each quarter-hour. Click on right here, to see a chart of all the information price limits.

    You’ll be able to learn the total particulars of twitter2stata‘s performance in its assist file after putting in it. Dawson was in a position to write this command utilizing Stata 15’s improved Java API along with Java library Twitter4J. In a later put up, I’ll focus on how he went about growing this command and present you ways straightforward it’s to put in writing Java code for Stata.



    Related Articles

    Latest Articles