{ viewerdialog "twitter2stata" "dialog twitter2stata"}{ viewerjumpto "Syntax" "twitter2stata##syntax"}{ viewerjumpto "Options for twitter2stata search" "twitter2stata##search_options"}{ viewerjumpto "Description" "twitter2stata##description"}{ viewerjumpto "Remarks/Examples" "twitter2stata##remarks"}{ viewerjumpto "Authors" "twitter2stata##author"}

Title

twitter2stata -- Import data from Twitter

Syntax

Set Twitter OAuth setting for your Twitter application

twitter2stata setaccess "consumer_key"
"consumer_secret"
"access_token"
"access_secret"

Import tweet data that match a specified search string

twitter2stata searchtweets "search_string"
[, search_options fetch_options]

Import user data on 1000 users that match the specified search string

twitter2stata searchusers "search_string" [, clear]

Import data for the specified user

twitter2stata getuser "userId_or_userName"
[, clear]

Import most recent like tweet data for a specified user

twitter2stata likes "userId_or_userName"
[, clear sinceid(tweetid)]

Import most recent data on users the specified user is following

twitter2stata following "userId_or_userName"
[, clear]

Import most recent data on users the specified user is followed by

twitter2stata followers "userId_or_userName"
[, clear]

Import most recent data on all lists the specified user either subscribes to or is a member of

twitter2stata lists "userId_or_userName"
[, list_options]

Import most recent tweet data for a specified user

twitter2stata tweets "userId_or_userName"
[, clear fetch_options]

Import most recent user data for the specified list

twitter2stata listusers "listId_or_listName"
[, list_options]

Import most recent tweet data on the specified list

twitter2stata listtweets "listId_or_listName"
[, clear fetch_options]

consumer_key is a valid OAuth consumer key for a user's Twitter application.

consumer_secret is a valid OAuth consumer secret for a user's Twitter application.

access_token is a valid OAuth access token for a user account.

access_secret is a valid OAuth access token secret for a user account.

search_string specifies the word or phrase to be searched for.

userId_or_userName is a valid Twitter user id (747413565984800768)
or username (KCrowStataCorp).

listId_or_listName is a valid Twitter list id (747413565984800768) or
list name and list owner (statalist Stata).

search_options

lang(langcode|all) restrict tweets to the given language

numtweets(#) set number of tweets to import

daterange:([s_datestr][, e_datestr]) load tweet data within the last seven day date range

geo(latitude,longitude,radius[,unit]) restrict data to tweets by users in a given location

type(mixed|recent|popular) set the type of tweets to be imported

clear clear data in memory before loading Twitter data

langcode is a valid IS0 639-1 code.

s_datestr and e_datestr should be in YYYY-MM-DD format.

unit is either mi or km; default is mi.

fetch_options

sinceid(tweetid) import tweets after tweetid

maxid(tweetid) import tweets before tweetid

tweetid is the string id that identifies a tweet.

list_options

members import member data only; default is subscriber data only

clear clear data in memory before loading data

Description

twitter2stata imports data from Twitter
using Twitter's REST API.
Before using these commands, you must first grant twitter2stata access to
your account.

twitter2stata setaccess sets the OAuth settings to access your Twitter
application.

twitter2stata searchtweets imports tweet data that match the specified string.
The search string will be converted to a UTF-8/URL-encoded string. This string
cannot exceed 500 characters.

twitter2stata searchusers imports user data on 1000 users that
match the specified search string. The command mimics the user search on
Twitter's website.

twitter2stata getuser imports user data for the specified user.
twitter2stata likes imports the most recent like
tweet dfdata for the specified user. twitter2stata following and
twitter2stata followers import data on the most recent users the
specified user is following or is followed by, respectively.
twitter2stata lists imports the most recent data on all lists the specified
user either subscribes to or is a member of. twitter2stata tweets imports
the most recent tweet data for the specified user.

twitter2stata listusers and twitter2stata listtweets import user
data and tweet data, respectively, for the specified list.

There are limits to the amount of data you can import. twitter2stata will
import all data avaliable or as much data as the rate limit will allow. The rate limits
are subcommand specific. See Twitters's website for these
rate limits. Commands
that import tweet data return r(since_id) or r(max_id) or both. These
allow you to remember your last fetch starting and ending points (tweetids).
You can use r(since_id) to fetch the data that have been added since the
previous fetch 15 minutes ago. To search back in time (up to seven days), use
r(max_id).

Options

Search

lang(langcode|all) restricts tweets to the given language.
The default language for tweets data is English (en). You can specify
all, which will return tweets for all languages.

numtweets(#) specifies the maximum number of tweet data to import. If
the number of tweet data imported is less than the number you specified, that
is all the tweet data available for the search string. The default
is to import all tweet data available (unless the rate limit is reached).

daterange([s_datestr],e_datestr) restricts
tweets returned to a given date. The date must be no older that seven days.
s_datestr specifies the oldest day you want data returned from.
e_datestr is the most recent date you want data returned from.
If s_datestr is not specified, then s_datestr is seven days before today .
If e_datestr is not specified, then today is assumed. The format for
both s_datestr and e_datestr is YYYY-MM-DD.

geo(latitude, longitude, radius[, unit])
restricts data to tweets by users located within the given radius of the given
latitude and longitude. The radius unit can be kilometers km or miles
mi. The default radius unit is miles.

type(mixed|recent|popular) specifies the type of
tweets to import. mixed, the default, includes both popular and real-time
tweets. recent imports only the most recent tweets. popular imports
only the most popular tweets.

clear specifies that the data in memory should be replaced
with Twitter data.

Fetch

sinceid(tweetid) imports tweet data that have been added since tweetid.

maxid(tweetid) imports tweet data that came before tweetid.

List

members with twitter2stata lists imports the lists the given
user is a member of. members with twitter2stata listusers imports
user data on the members of the given list. The default for both commands is
to return subscriber (not member) data.

clear specifies that the data in memory should be replaced
with Twitter data.

Remarks

Before you start using twitter2stata, you must first setup a Twitter application.
See the Stata blog
for instructions on how to do this. Once you have your Twitter application, copy the
consumer key, consumer secret, access token, and access token secret strings
into a do-file for use between Stata sessions. You use the command

. twitter2stata setaccess "PNvEVSvy0BtGdZZZ" ///
"CMQTcbW3lBoXx5bETqLBYBd2iz9YYYYYUUU" ///
"74741359848A076lTxYlATS6EgKxq5MnSP01Y" ///
"lkKb0ZvhJhg@u4MTBxEtFOpKG6DM8hCIFv6UdmAE"

to access your account data between Stata sessions. We do not recommend you share
these strings with others because this could limit the amount of data you are allowed
to download.

If you recieve the error r(655) this means you have exceeded a Twitter
API rate limit. You must wait 15 minutes before using the same command again.
You might look at using the sleep command to help with this error.

Examples

Search tweet data for the string "star wars", importing tweets based on
both recent data and popular data

. twitter2stata searchtweets "star wars"

Search tweet data for the string "star wars", for the dates 2017-07-06 through
2017-07-12, importing the first 1000 (or as many as you can) tweets

. twitter2stata searchtweets "star wars", numtweets(1000) ///
daterange("2017-07-06", "2017-07-12")

Search tweet data for the string "star wars", from users located in a 20-mile
radius of College Station, TX, importing the first 1000 (or as many as you can) tweets.

. twitter2stata searchtweets "star wars", numtweets(1000) ///
geo(30.6280, -96.3344, 20)

Stored results

twitter2stata stores the following in r():

Scalars

r(max_id) the oldest tweet id of last query to the Twitter API

r(since_id) the most recent tweet id of last query to the Twitter API

License

twitter2stata uses Twitter4j Java
library. See the below license information.

Copyright 2007 Yusuke Yamamoto

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
Distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Author

Kevin Crow, Dawson Deere

StataCorp LLC

kcrow@stata.com