This post requires some familiarity with the Harry Potter books but I’m committed to making this blog friendly to everyone, even Muggles/Nomajes.
Have you seen Mark Sellors’ blog post series about writing command line utilities in R? It’s a great one but I was a bit puzzled by his using randomness to assign houses in his sorting hat example (he added a new method based on name digest-ing in the meantime).
This is a really cool #rstats tutorial... but since when does the sorting hat assign a random Hogwarts house?! 🧙
♀️🎩🎲➡️🏠🤔 https://t.co/Ff8CHR6jb9— Maëlle Salmon 🐟 (@ma_salmon) 19 de desembre de 2017
This prompted a reply by David Hood who later came up with R code to assign you to a Hogwarts house based on your Twitter activity!
It should be possible to assign House on the basis of Twitter analysis (among R using tweeters). Quatitatively: Original posts - opinionated - GryffindorReplies - social - Slytherinposts links out of Twitter - homework - RavenclawRetweets- keeping it all working - Hufflepuff— David Hood (@Thoughtfulnz) 19 de desembre de 2017
I was thrilled to see David Hood’s sorting hat Github repo and thought it’d be the perfect occasion to answer that fascinating question: are #python users more likely to get into Slytherin than #rstats users?
Another note: I do not care about any Python vs. R fights except for Quidditch games, so go away trolls.
Getting Twitter users
In his repo, David Hood used accounts sampled from their posting on the #rstats hashtag, and uses their scores as a scale against which to rate any user. I had to take a different approach because I wanted to compare R against Python posting on a basis that wouldn’t be R. I chose to use JavaScript posters as a reference set, because you can’t randomly search Twitter users and even that wouldn’t necessarily give me a “representative” set. Therefore, I had to assume something, and I chose to assume the Sorting hat would have chosen JavaScript tweeps as a reference set. I wanted more than 200 JavaScript tweeps and more than 100 R and Python tweeps, respectively, and asked for a bit more from Twitter API because I knew there’d be duplicates from people tweeting about more than one of these languages (like Lynn Cherny for instance). I didn’t do any sample size calculation. Here is how I got users:
library(&quot;magrittr&quot;)
# get users
languages %
knitr::kable()
language
n
javascript
258
python
126
rstats
128
I had actually overestimated the amount of overlap between accounts. Well this does not mean any of these accounts is exclusively related to one of the languages, I have for instance seen rtweet creator Mike Kearney himself tweet about Python and he’s still in my R sample. It’s because the rank in Twitter Python set might be different than the Twitter R rank, but I’ll go on and assume that if an account was returned for R rather than Python, then it’s not a bad representant of R.
Computing the sorting metrics
David Hood calculates a metric for each house, with the logic explained in his Github repo (I feel a heart pinch every time I write that because it is such a great repo that its creator should blog, don’t you think?). I simply used his code after updating rtweet when I realized the output I got from the package had different column names. I probably should have cared a bit more about (Non) Standard Evaluation when wrapping the code in a function but anyway I’m relying on magic!
users %
dplyr::summarise(
slyth = sum(has_reply &amp; !has_reply_to_self) / n(),
huffl = sum(has_retweet) / n(),
raven = sum(has_external_links &amp; !has_retweet) / n(),
griff = (n() - sum(has_reply |
has_retweet |
has_external_links |
has_reply_to_self)
) / n()
) }else{
NULL
}
}
scores