Skip to content

Instantly share code, notes, and snippets.

@Dpananos
Created July 6, 2018 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dpananos/00fab2b5602fe1063bb3d38641765919 to your computer and use it in GitHub Desktop.
Save Dpananos/00fab2b5602fe1063bb3d38641765919 to your computer and use it in GitHub Desktop.
Analyze your twitter favorites over time
library(tidyverse)
library(lubridate)
library(rtweet)
user.name = 'PhDemetri'
#Get user's most recent tweets
user = get_timeline(user = user.name, n = 5000)
#Clean the data.
#Remove outliers and get a reasonable time.
#This part will depend on the user
user.data = user %>%
filter(created_at<round_date(today(), unit ='month'),
favorite_count<300, #change this if you are routinely wracking up numbers like this
year(created_at)>2016)
#Count number of tweets and sum up favorites.
#Create a variable for months since begining fo time period
user.data = user.data %>%
mutate(month = round_date(created_at, unit = 'month')) %>%
group_by(month) %>%
summarise(N = n(), fc = sum(favorite_count)) %>%
ungroup %>%
mutate(t = interval(min(month),month)%/%months(1))
#Get an idea of the trend
user.data %>%
ggplot(aes(month,fc/N))+
geom_line()+
geom_smooth()+
theme_minimal()+
theme(aspect.ratio = 1/2)
#Here is the model
#Use negative binomial. You could use a poisson glm,
#but my data seems to be overdispersed.
model = MASS::glm.nb(fc ~ t + offset(log(N)), data = user.data)
#summary
summary(model)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment