<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/examining-the-tweeting-patterns-of-prominent-crossfit-gyms/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://mathewanalytics.com/2018/12/20/examining-the-tweeting-patterns-of-prominent-crossfit-gyms/"> R – Mathew Analytics</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><strong>A. Introduction</strong></p>
<p>The growth of Crossfit has been one of the biggest developments in the fitness industry over the past decade. Promoted as both a physical exercise philosophy and also as a competitive fitness sport, Crossfit is a high-intensity fitness program incorporating elements from several sports and exercise protocols such as high-intensity interval training, Olympic weightlifting, plyometrics, powerlifting, gymnastics, strongman, and so forth. Now with over 10,000 Crossfit affiliated gyms (boxes) throughout the United States, the market has certainly become more saturated and gyms must initiate more unique marketing strategies to attract new members. In this post, I will investigate how some prominent Crossfit boxes are utilizing Twitter to engage with consumers. While Twitter is a great platform for news and entertainment, it is usually not the place for customer acquisition given the lack of targeted messaging. Furthermore, unlike platforms like Instagram,Twitter is simply not an image/video centric tool where followers can view accomplishments from their favorite fitness heroes, witness people achieving their goals, and so forth. Given these shortcomings, I wanted to understand how some prominent Crossfit boxes are actually using their Twitter accounts. In this post, I will investigate tweeting patterns, content characteristics, perform sentiment analysis, and build a predictive model to predict retweets.</p>
<p><strong>B. Extract Data From Twitter</strong></p>
<p>We begin by extracting the desired data from Twitter using the rtweet package in R. There are six prominent Crossfit gyms whose entire Twitter timeline we will use. To get this data, I looped through a vector containing each of their Twitter handles and used the get_timeline function to pull the desired data. Notice that there is a user defined function called add_engineered_features that is used to add a number of extra date columns. That function is available on my GitHub page <a rel="nofollow" href="https://gist.github.com/abmathewks/48141aae0fc77923bb051137c689a173" target="_blank">here</a>.</p>
<pre>

library(rtweet)
library(lubridate)
library(devtools)
library(data.table)
library(ggplot2)
library(hms)
library(scales)

# set working directory
setwd(&quot;~/Desktop/rtweet_crossfit&quot;)

final_dat.tmp &lt;- list()

cf_gyms &lt;- c(&quot;reebokcrossfit5&quot;, &quot;crossfitmayhem&quot;, &quot;crossfitsanitas&quot;, &quot;sfcrossfit&quot;, &quot;cfbelltown&quot;, &quot;WindyCityCF&quot;)

for(each_box in cf_gyms){

message(&quot;Getting data for: &quot;, each_box)

each_cf % data.table()
each_cf$crossfit_name &lt;- each_box
suppressWarnings( add_engineered_dates(each_cf, date_col = &quot;created_at&quot;) )

final_dat.tmp[[each_box]] &lt;- each_cf
message(&quot;&quot;)

}

final_dat &lt;- rbindlist(final_dat.tmp)

final_dat$contains_hashtags &lt;- ifelse(!is.na(final_dat$hashtags), 1, 0)

final_dat$hashtags_count &lt;- lapply(final_dat$hashtags, function(x) length(x[!is.na(x)]))

</pre>
<p><strong>C. Exploratory Data Analysis</strong></p>
<p>Let us start by investigating this data set to get a better understanding of trends and patterns across these various Crossfit boxes. The important thing to note is that not all these twitter accounts are currently active. We can see that crossfitmayhem, sfcrossfit, and WindyCityCF are the only ones who remain active.</p>
<pre>

final_dat[, .(min_max = range(as.Date(created_at))), by=crossfit_name][,label := rep(c(&quot;first_tweet&quot;,&quot;last_tweet&quot;))][]

</pre>
<p><strong><em>C1. Total Number of Tweets</em></strong></p>
<p>Sfcrossfit, which is the oldest of these gyms, has the highest number of tweets. However, when looking at the total number of tweets per active month, they were less active than two other gyms.</p>
<pre>

## total number of tweets 

p1 = final_dat[, .N, by=crossfit_name] %>%
   ggplot(., aes(x=reorder(crossfit_name, N), y=N)) + geom_bar(stat='identity', fill=&quot;steelblue&quot;) +
      coord_flip() + labs(x=&quot;&quot;, y=&quot;&quot;) + ylim(0,3000) + ggtitle(&quot;Total Number of Tweets&quot;) +
         theme(plot.title = element_text(hjust = 0.5),
               axis.ticks.x = element_line(colour = &quot;black&quot;),
               axis.ticks.y = element_line(colour = &quot;black&quot;))  

## number of tweets per active month 

p2 = final_dat[, .(.N, start=lubridate::ymd_hms(min(created_at)), months_active=lubridate::interval(lubridate::ymd_hms(min(created_at)), Sys.Date()) %/% months(1)), by=crossfit_name][,
     .(tweets_per_month = N/months_active), by=crossfit_name] %>%
   ggplot(., aes(x=reorder(crossfit_name, tweets_per_month), y=tweets_per_month)) +
      geom_bar(stat='identity', fill=&quot;steelblue&quot;) + coord_flip() + labs(x=&quot;&quot;, y=&quot;&quot;) + ylim(0,32) +
         theme(plot.title = element_text(hjust = 0.5),
               axis.ticks.x = element_line(colour = &quot;black&quot;),
               axis.ticks.y = element_line(colour = &quot;black&quot;)) +
            ggtitle(&quot;Total Number of Tweets per Active Month&quot;)

## add both plots to a single pane

grid.arrange(p1, p2, nrow=1)

</pre>
<p><a rel="nofollow" href="https://mathewanalytics.files.wordpress.com/2018/12/Rplot.png" target="_blank"><img data-orig-file="https://mathewanalytics.files.wordpress.com/2018/12/Rplot.png" data-orig-size="950,750" data-medium-file="https://mathewanalytics.files.wordpress.com/2018/12/Rplot.png?w=300" data-attachment-id="959" data-image-meta="{" data-image-description="" src="https://mathewanalytics.files.wordpress.com/2018/12/Rplot.png?w=450&h=622&fit=788%2C622" data-permalink="https://mathewanalytics.com/2018/12/20/examining-the-tweeting-patterns-of-prominent-crossfit-gyms/rplot-2/" data-large-file="https://mathewanalytics.files.wordpress.com/2018/12/Rplot.png?w=450&amp;h=622&amp;fit=788%2C622" alt="" data-image-title="Rplot" sizes="(max-width: 788px) 100vw, 788px" data-comments-opened="0" class="wp-image-959" data-recalc-dims="1"></a></p>
<p><em><strong>C2. Total Number of Tweets Over Time</strong></em></p>
<p>The time series for the total number of tweets by month shows that each gym had one or two peaks from 2012 through 2016 where they were aggressively sharing content with their followers. However, over the past two years, each gym has reduced their twitter usage significantly.</p>
<pre>

## total number of tweets by month 

final_dat[, .N, by = .(crossfit_name, created_at_YearMonth)][order(crossfit_name, created_at_YearMonth)][,
      created_at_YearMonth := lubridate::ymd(paste(created_at_YearMonth, &quot;-01&quot;))] %>%
   ggplot(., aes(created_at_YearMonth, N, colour=crossfit_name)) + geom_line(group=1, lwd=0.6) +
      facet_wrap(~crossfit_name) + labs(x=&quot;&quot;, y=&quot;&quot;) + theme(legend.position=&quot;none&quot;) +
         theme(plot.title = element_text(hjust = 0.5),
               axis.ticks.x = element_line(colour = &quot;black&quot;),
               axis.ticks.y = element_line(colour = &quot;black&quot;),
               strip.text.x = element_text(size = 10)) +
            ggtitle(&quot;Total Number of Tweets&quot;)

## total number of tweets by year

ggplot(data = final_dat,
       aes(lubridate::month(created_at, label=TRUE, abbr=TRUE),
           group=factor(lubridate::year(created_at)), color=factor(lubridate::year(created_at))))+
   geom_line(stat=&quot;count&quot;) + geom_point(stat=&quot;count&quot;) +
   facet_wrap(~crossfit_name) + labs(x=&quot;&quot;, colour=&quot;Year&quot;) + xlab(&quot;&quot;) + ylab(&quot;&quot;) +
   theme(plot.title = element_text(hjust = 0.5),
         axis.ticks.x = element_line(colour = &quot;black&quot;),
         axis.ticks.y = element_line(colour = &quot;black&quot;),
         strip.text.x = element_text(size = 10)) +
   ggtitle(&quot;Total Number of Tweets by Year&quot;)

</pre>
<p><a rel="nofollow" href="https://mathewanalytics.files.wordpress.com/2018/12/EDA_3-1.png" target="_blank"><img data-orig-file="https://mathewanalytics.files.wordpress.com/2018/12/EDA_3-1.png?w=456" data-orig-size="739,541" data-medium-file="https://mathewanalytics.files.wordpress.com/2018/12/EDA_3-1.png?w=456?w=300" data-attachment-id="965" data-image-meta="{" data-image-description="" src="https://mathewanalytics.files.wordpress.com/2018/12/EDA_3-1.png?w=456" data-permalink="https://mathewanalytics.com/2018/12/20/examining-the-tweeting-patterns-of-prominent-crossfit-gyms/eda_3-2/" data-large-file="https://mathewanalytics.files.wordpress.com/2018/12/EDA_3-1.png?w=456?w=739" alt="" data-image-title="EDA_3" sizes="(max-width: 739px) 100vw, 739px" data-comments-opened="0" class="wp-image-965" data-recalc-dims="1"></a></p>
<p><a rel="nofollow" href="https://mathewanalytics.files.wordpress.com/2018/12/EDA_3b.png" target="_blank"><img data-orig-file="https://mathewanalytics.files.wordpress.com/2018/12/EDA_3b.png" data-orig-size="900,900" data-medium-file="https://mathewanalytics.files.wordpress.com/2018/12/EDA_3b.png?w=300" data-attachment-id="963" data-image-meta="{" data-image-description="" src="https://mathewanalytics.files.wordpress.com/2018/12/EDA_3b.png?w=450&h=788&fit=788%2C788" data-permalink="https://mathewanalytics.com/2018/12/20/examining-the-tweeting-patterns-of-prominent-crossfit-gyms/eda_3b/" data-large-file="https://mathewanalytics.files.wordpress.com/2018/12/EDA_3b.png?w=450&amp;h=788&amp;fit=788%2C788" alt="" data-image-title="EDA_3b" sizes="(max-width: 788px) 100vw, 788px" data-comments-opened="0" class="wp-image-963" data-recalc-dims="1"></a></p>
<p><em><strong>C3. Tweeting Volume by Year, Month, and Day</strong></em></p>
<p>For each Crossfit gym, I plotted the volume of tweets by year, month, and day. Oddly enough, there really are not any noticeable patterns in these charts.</p>
<pre>

## years with the highest number of tweets 

ggplot(final_dat, aes(created_at_Year)) + geom_bar(fill=&quot;steelblue&quot;) +
   facet_wrap(~crossfit_name) + labs(x=&quot;&quot;, y=&quot;&quot;) +
   theme(plot.title = element_text(hjust = 0.5),
         axis.ticks.x = element_line(colour = &quot;black&quot;),
         axis.ticks.y = element_line(colour = &quot;black&quot;),
         strip.text.x = element_text(size = 10)) + ylim(0,800) +
   ggtitle(&quot;Total Number of Tweets by Year&quot;)

## months with the highest number of tweets 

final_dat[, created_at_YearMonth2 := lubridate::ymd(paste(created_at_YearMonth, &quot;-01&quot;))][] %>%
   ggplot(., aes(lubridate::month(created_at_YearMonth2, label=TRUE, abbr=TRUE))) + geom_bar(fill=&quot;steelblue&quot;) +
   facet_wrap(~crossfit_name) + labs(x=&quot;&quot;, y=&quot;&quot;) +
   theme(plot.title = element_text(hjust = 0.5),
         axis.ticks.x = element_line(colour = &quot;black&quot;),
         axis.ticks.y = element_line(colour = &quot;black&quot;),
         strip.text.x = element_text(size = 10)) + ylim(0,500) +
   ggtitle(&quot;Total Number of Tweets by Month&quot;)

## days with the highest number of tweets 

final_dat[, created_at_YearMonth2 := lubridate::wday(lubridate::ymd(paste(created_at_YearMonth, &quot;-01&quot;)), label=T)][] %>%
   ggplot(., aes(created_at_YearMonth2)) + geom_bar(fill=&quot;steelblue&quot;) +
   facet_wrap(~crossfit_name) + labs(x=&quot;&quot;, y=&quot;&quot;) +
   theme(plot.title = element_text(hjust = 0.5),
         axis.ticks.x = element_line(colour = &quot;black&quot;),
         axis.ticks.y = element_line(colour = &quot;black&quot;),
         strip.text.x = element_text(size = 10)) + ylim(0,600) +
   ggtitle(&quot;Total Number of Tweets by Day&quot;)

</pre>
<p><a rel="nofollow" href="https://mathewanalytics.files.wordpress.com/2018/12/EDA_10.png" target="_blank"><img data-orig-file="https://mathewanalytics.files.wordpress.com/2018/12/EDA_10.png" data-orig-size="900,800" data-medium-file="https://mathewanalytics.files.wordpress.com/2018/12/EDA_10.png?w=300" data-attachment-id="966" data-image-meta="{" data-image-description="" src="https://mathewanalytics.files.wordpress.com/2018/12/EDA_10.png?w=450&h=700&fit=788%2C700" data-permalink="https://mathewanalytics.com/2018/12/20/examining-the-tweeting-patterns-of-prominent-crossfit-gyms/eda_10/" data-large-file="https://mathewanalytics.files.wordpress.com/2018/12/EDA_10.png?w=450&amp;h=700&amp;fit=788%2C700" alt="" data-image-title="EDA_10" sizes="(max-width: 788px) 100vw, 788px" data-comments-opened="0" class="wp-image-966" data-recalc-dims="1"></a></p>
<p><a rel="nofollow" href="https://mathewanalytics.files.wordpress.com/2018/12/EDA_4.png" target="_blank"><img data-orig-file="https://mathewanalytics.files.wordpress.com/2018/12/EDA_4.png?w=456" data-orig-size="739,541" data-medium-file="https://mathewanalytics.files.wordpress.com/2018/12/EDA_4.png?w=456?w=300" data-attachment-id="967" data-image-meta="{" data-image-description="" src="https://mathewanalytics.files.wordpress.com/2018/12/EDA_4.png?w=456" data-permalink="https://mathewanalytics.com/2018/12/20/examining-the-tweeting-patterns-of-prominent-crossfit-gyms/eda_4/" data-large-file="https://mathewanalytics.files.wordpress.com/2018/12/EDA_4.png?w=456?w=739" alt="" data-image-title="EDA_4" sizes="(max-width: 739px) 100vw, 739px" data-comments-opened="0" class="wp-image-967" data-recalc-dims="1"></a></p>
<p><a rel="nofollow" href="https://mathewanalytics.files.wordpress.com/2018/12/EDA_5.png" target="_blank"><img data-orig-file="https://mathewanalytics.files.wordpress.com/2018/12/EDA_5.png?w=456" data-orig-size="739,541" data-medium-file="https://mathewanalytics.files.wordpress.com/2018/12/EDA_5.png?w=456?w=300" data-attachment-id="968" data-image-meta="{" data-image-description="" src="https://mathewanalytics.files.wordpress.com/2018/12/EDA_5.png?w=456" data-permalink="https://mathewanalytics.com/2018/12/20/examining-the-tweeting-patterns-of-prominent-crossfit-gyms/eda_5/" data-large-file="https://mathewanalytics.files.wordpress.com/2018/12/EDA_5.png?w=456?w=739" alt="" data-image-title="EDA_5" sizes="(max-width: 739px) 100vw, 739px" data-comments-opened="0" class="wp-image-968" data-recalc-dims="1"></a></p>
<p><em><strong>C4. Tweeting Volume by Time of Day</strong></em></p>
<p>For most of these gyms, we see that the majority of their tweets came during the second half of the day or early in the morning. Given that most Crossfit boxes have their first classes at around 5am or 6am, and are usually most busy in the evening, this indicates that these businesses are tweeting during those hours where their facility is busiest.</p>
<pre>

## tweet density over the day 

ggplot(data = final_dat) +
   geom_density(aes(x = created_at_Time, y = ..scaled..),
                fill=&quot;steelblue&quot;, alpha=0.3) +
   xlab(&quot;Time&quot;) + ylab(&quot;Density&quot;) +
   scale_x_datetime(breaks = date_breaks(&quot;2 hours&quot;),
                    labels = date_format(&quot;%H:%M&quot;)) +
   facet_wrap(~crossfit_name) + labs(x=&quot;&quot;, y=&quot;&quot;) +
   theme(plot.title = element_text(hjust = 0.5),
         axis.text.x=element_text(angle=90,hjust=1)) +
   theme(plot.title = element_text(hjust = 0.5),
         axis.ticks.x = element_line(colour = &quot;black&quot;),
         axis.ticks.y = element_line(colour = &quot;black&quot;),
         strip.text.x = element_text(size = 10)) +
   ggtitle(&quot;Tweet Density by Time of Day&quot;)

</pre>
<p><a rel="nofollow" href="https://mathewanalytics.files.wordpress.com/2018/12/EDA_6.png" target="_blank"><img data-orig-file="https://mathewanalytics.files.wordpress.com/2018/12/EDA_6.png?w=456" data-orig-size="739,541" data-medium-file="https://mathewanalytics.files.wordpress.com/2018/12/EDA_6.png?w=456?w=300" data-attachment-id="969" data-image-meta="{" data-image-description="" src="https://mathewanalytics.files.wordpress.com/2018/12/EDA_6.png?w=456" data-permalink="https://mathewanalytics.com/2018/12/20/examining-the-tweeting-patterns-of-prominent-crossfit-gyms/eda_6/" data-large-file="https://mathewanalytics.files.wordpress.com/2018/12/EDA_6.png?w=456?w=739" alt="" data-image-title="EDA_6" sizes="(max-width: 739px) 100vw, 739px" data-comments-opened="0" class="wp-image-969" data-recalc-dims="1"></a></p>
<p><em><strong>C5. Source of Tweets</strong></em></p>
<p>While a couple of the gyms are using a marketing management platform like Hootsuite and IFTTT, the majority of Tweets are coming from a Twitter application or some other social media tool such as Facebook and Instagram.</p>
<pre>

## source of tweets

final_dat[, .N, by = .(crossfit_name, source)][order(crossfit_name,-N)][,
        head(.SD, 3),by=crossfit_name] %>%
   ggplot(., aes(x=source, y=N)) +
   geom_bar(stat=&quot;identity&quot;, fill=&quot;steelblue&quot;) + coord_flip() +
   facet_wrap(~crossfit_name) + labs(x=&quot;&quot;, y=&quot;&quot;) +
   labs(x=&quot;&quot;, y=&quot;&quot;, colour=&quot;&quot;) + ylim(0,2500) +
   theme(plot.title = element_text(hjust = 0.5),
         axis.ticks.x = element_line(colour = &quot;black&quot;),
         axis.ticks.y = element_line(colour = &quot;black&quot;)) +
   ggtitle(&quot;Source of Tweets&quot;) 

</pre>
<p><a rel="nofollow" href="https://mathewanalytics.files.wordpress.com/2018/12/eda_7.png" target="_blank"><img data-orig-file="https://mathewanalytics.files.wordpress.com/2018/12/eda_7.png" data-orig-size="834,555" data-medium-file="https://mathewanalytics.files.wordpress.com/2018/12/eda_7.png?w=300" data-attachment-id="970" data-image-meta="{" data-image-description="" src="https://mathewanalytics.files.wordpress.com/2018/12/eda_7.png?w=450&h=524&fit=788%2C524" data-permalink="https://mathewanalytics.com/2018/12/20/examining-the-tweeting-patterns-of-prominent-crossfit-gyms/eda_7/" data-large-file="https://mathewanalytics.files.wordpress.com/2018/12/eda_7.png?w=450&amp;h=524&amp;fit=788%2C524" alt="" data-image-title="eda_7" sizes="(max-width: 788px) 100vw, 788px" data-comments-opened="0" class="wp-image-970" data-recalc-dims="1"></a></p>
<p><strong>D. Content Characteristics</strong></p>
<p>Let us investigate the characteristics of the content that is being tweeted. From looking at average tweet length to the amount of retweets, this information will further expand our understanding of how these prominent Crossfit boxes are utilizing their Twitter accounts.</p>
<p><em><strong>D1. Frequency of Retweets and Original Tweets</strong></em></p>
<p>Only one gym, cfbelltown, had a majority of their tweets being retweets. Furthermore, a plurality of tweets from crossfitmayhem were not original. In the plots showing retweets over time, we can also see that crossfitmayhem did a lot of retweeting from 2014 through 2016, but started pushing original tweets from then on out.</p>
<pre>

## retweets and original tweets 

final_dat[, .N, by=.(crossfit_name,is_retweet)][, percen := N/sum(N), by=.(crossfit_name)][is_retweet==&quot;TRUE&quot;] %>%
   ggplot(., aes(x=reorder(crossfit_name, N), y=percen)) +
      geom_bar(stat=&quot;identity&quot;, position =&quot;dodge&quot;, fill=&quot;steelblue&quot;) + coord_flip() +
         labs(x=&quot;&quot;, y=&quot;&quot;, colour=&quot;&quot;) + ylim(0,0.8) +
            theme(plot.title = element_text(hjust = 0.5),
                  axis.ticks.x = element_line(colour = &quot;black&quot;),
                  axis.ticks.y = element_line(colour = &quot;black&quot;)) +
            ggtitle(&quot;Percentage of Tweets That Were Not Original&quot;)

# total number of retweets over time 

ggplot(data = final_dat, aes(x = created_at, fill = is_retweet)) +
   geom_histogram(bins=48) +
   labs(x=&quot;&quot;, y=&quot;&quot;, colour=&quot;&quot;) +
   scale_fill_manual(values = c(&quot;steelblue2&quot;,&quot;steelblue&quot;), name = &quot;Retweet&quot;) +
   facet_wrap(~crossfit_name) + labs(x=&quot;&quot;, y=&quot;&quot;) +
   theme(plot.title = element_text(hjust = 0.5),
         axis.ticks.x = element_line(colour = &quot;black&quot;),
         axis.ticks.y = element_line(colour = &quot;black&quot;),
         strip.text.x = element_text(size = 10)) +
   ggtitle(&quot;Total Number of Retweets Over Time&quot;)

</pre>
<p><a rel="nofollow" href="https://mathewanalytics.files.wordpress.com/2018/12/content_1.png" target="_blank"><img data-orig-file="https://mathewanalytics.files.wordpress.com/2018/12/content_1.png?w=456" data-orig-size="739,541" data-medium-file="https://mathewanalytics.files.wordpress.com/2018/12/content_1.png?w=456?w=300" data-attachment-id="971" data-image-meta="{" data-image-description="" src="https://mathewanalytics.files.wordpress.com/2018/12/content_1.png?w=456" data-permalink="https://mathewanalytics.com/2018/12/20/examining-the-tweeting-patterns-of-prominent-crossfit-gyms/content_1/" data-large-file="https://mathewanalytics.files.wordpress.com/2018/12/content_1.png?w=456?w=739" alt="" data-image-title="content_1" sizes="(max-width: 739px) 100vw, 739px" data-comments-opened="0" class="wp-image-971" data-recalc-dims="1"></a></p>
<p><a rel="nofollow" href="https://mathewanalytics.files.wordpress.com/2018/12/content_2.png" target="_blank"><img data-orig-file="https://mathewanalytics.files.wordpress.com/2018/12/content_2.png?w=456" data-orig-size="739,541" data-medium-file="https://mathewanalytics.files.wordpress.com/2018/12/content_2.png?w=456?w=300" data-attachment-id="972" data-image-meta="{" data-image-description="" src="https://mathewanalytics.files.wordpress.com/2018/12/content_2.png?w=456" data-permalink="https://mathewanalytics.com/2018/12/20/examining-the-tweeting-patterns-of-prominent-crossfit-gyms/content_2/" data-large-file="https://mathewanalytics.files.wordpress.com/2018/12/content_2.png?w=456?w=739" alt="" data-image-title="content_2" sizes="(max-width: 739px) 100vw, 739px" data-comments-opened="0" class="wp-image-972" data-recalc-dims="1"></a></p>
<p><em><strong>D2. Frequency of Favourited Tweets</strong></em></p>
<p>Crossfitmayhem was the only box where a majority of their tweets were actually favorited at least once. Furthermore, they had a moderately large number of those tweets that were getting favorited three or more times.</p>
<pre>

## amount of favorited tweets 

final_dat[, .N, by=.(crossfit_name,favorite_count)][, percen := N/sum(N), by=.(crossfit_name)][favorite_count==0,] %>%
   ggplot(., aes(x=reorder(crossfit_name,percen), y=percen)) +
   geom_bar(stat=&quot;identity&quot;, fill=&quot;steelblue&quot;) + coord_flip() +
   labs(x=&quot;&quot;, y=&quot;&quot;, colour=&quot;&quot;) + ylim(0,1) +
   theme(plot.title = element_text(hjust = 0.5),
         axis.ticks.x = element_line(colour = &quot;black&quot;),
         axis.ticks.y = element_line(colour = &quot;black&quot;)) +
   ggtitle(&quot;Percentage of Tweets That Were Not Favorited&quot;)

## amount of favorited tweets by count

final_dat[, .N, by=.(crossfit_name,favorite_count)][, percen := N/sum(N), by=.(crossfit_name)][favorite_count &lt;= 10,][order(crossfit_name,favorite_count)] %>%
   ggplot(., aes(x=reorder(favorite_count, -favorite_count), y=percen)) +
   geom_bar(stat=&quot;identity&quot;, fill=&quot;steelblue&quot;) + coord_flip() +
   labs(x=&quot;&quot;, y=&quot;&quot;, colour=&quot;&quot;) +
   facet_wrap(~crossfit_name) + ylim(0,1) +
   theme(plot.title = element_text(hjust = 0.5),
         axis.ticks.x = element_line(colour = &quot;black&quot;),
         axis.ticks.y = element_line(colour = &quot;black&quot;),
         strip.text.x = element_text(size = 10)) +
   ggtitle(&quot;Percentage of Tweets Based on Their Favorite Count&quot;)

</pre>
<p><a rel="nofollow" href="https://mathewanalytics.files.wordpress.com/2018/12/content_3.png" target="_blank"><img data-orig-file="https://mathewanalytics.files.wordpress.com/2018/12/content_3.png?w=456" data-orig-size="739,541" data-medium-file="https://mathewanalytics.files.wordpress.com/2018/12/content_3.png?w=456?w=300" data-attachment-id="973" data-image-meta="{" data-image-description="" src="https://mathewanalytics.files.wordpress.com/2018/12/content_3.png?w=456" data-permalink="https://mathewanalytics.com/2018/12/20/examining-the-tweeting-patterns-of-prominent-crossfit-gyms/content_3/" data-large-file="https://mathewanalytics.files.wordpress.com/2018/12/content_3.png?w=456?w=739" alt="" data-image-title="content_3" sizes="(max-width: 739px) 100vw, 739px" data-comments-opened="0" class="wp-image-973" data-recalc-dims="1"></a></p>
<p><a rel="nofollow" href="https://mathewanalytics.files.wordpress.com/2018/12/content_4.png" target="_blank"><img data-orig-file="https://mathewanalytics.files.wordpress.com/2018/12/content_4.png?w=456" data-orig-size="739,541" data-medium-file="https://mathewanalytics.files.wordpress.com/2018/12/content_4.png?w=456?w=300" data-attachment-id="974" data-image-meta="{" data-image-description="" src="https://mathewanalytics.files.wordpress.com/2018/12/content_4.png?w=456" data-permalink="https://mathewanalytics.com/2018/12/20/examining-the-tweeting-patterns-of-prominent-crossfit-gyms/content_4/" data-large-file="https://mathewanalytics.files.wordpress.com/2018/12/content_4.png?w=456?w=739" alt="" data-image-title="content_4" sizes="(max-width: 739px) 100vw, 739px" data-comments-opened="0" class="wp-image-974" data-recalc-dims="1"></a></p>
<p><em><strong>D3. Use of Hashtags</strong></em></p>
<p>Reebokcrossfit5 had the largest percentage of tweets that contained hashtags while crossfitsanitas and sfcrossfit rarely used hashtags in their tweets. And when it comes to the hashtags that are being used, it seems that these boxes are using the crossfit hashtag and the name of their own gym.</p>
<pre>

## contains hashtags 

final_dat[, .N, by=.(crossfit_name,contains_hashtags)][, percen := N/sum(N), by=.(crossfit_name)][contains_hashtags==1] %>%
   ggplot(., aes(x=reorder(crossfit_name,percen), y=percen)) +
   geom_bar(stat=&quot;identity&quot;, fill=&quot;steelblue&quot;) + coord_flip() +
   labs(x=&quot;&quot;, y=&quot;&quot;, colour=&quot;&quot;) + ylim(0,0.8) +
   theme(plot.title = element_text(hjust = 0.5),
         axis.ticks.x = element_line(colour = &quot;black&quot;),
         axis.ticks.y = element_line(colour = &quot;black&quot;)) +
   ggtitle(&quot;Percentage of Tweets That Contain Hashtags&quot;) 

## frequently used hashtags 

final_dat[hashtags != &quot;&quot;, ][,.N, by=.(crossfit_name, hashtags)][order(crossfit_name, -N)][,
    head(.SD, 3),by=crossfit_name] %>%
   ggplot(., aes(x = reorder(hashtags, -N), y=N)) +
   geom_bar(stat=&quot;identity&quot;, fill=&quot;steelblue&quot;) +
   coord_flip() + ylim(0,150) +
   facet_wrap(~crossfit_name) + labs(x=&quot;&quot;, y=&quot;&quot;, colour=&quot;&quot;) +
   theme(plot.title = element_text(hjust = 0.5),
         axis.ticks.x = element_line(colour = &quot;black&quot;),
         axis.ticks.y = element_line(colour = &quot;black&quot;),
         strip.text.x = element_text(size = 10)) +
   ggtitle(&quot;Most Commonly Used Hashtags&quot;) 

</pre>
<p><a rel="nofollow" href="https://mathewanalytics.files.wordpress.com/2018/12/content_5.png" target="_blank"><img data-orig-file="https://mathewanalytics.files.wordpress.com/2018/12/content_5.png?w=456" data-orig-size="739,541" data-medium-file="https://mathewanalytics.files.wordpress.com/2018/12/content_5.png?w=456?w=300" data-attachment-id="975" data-image-meta="{" data-image-description="" src="https://mathewanalytics.files.wordpress.com/2018/12/content_5.png?w=456" data-permalink="https://mathewanalytics.com/2018/12/20/examining-the-tweeting-patterns-of-prominent-crossfit-gyms/content_5/" data-large-file="https://mathewanalytics.files.wordpress.com/2018/12/content_5.png?w=456?w=739" alt="" data-image-title="content_5" sizes="(max-width: 739px) 100vw, 739px" data-comments-opened="0" class="wp-image-975" data-recalc-dims="1"></a></p>
<p><a rel="nofollow" href="https://mathewanalytics.files.wordpress.com/2018/12/content_7.png" target="_blank"><img data-orig-file="https://mathewanalytics.files.wordpress.com/2018/12/content_7.png?w=456" data-orig-size="739,541" data-medium-file="https://mathewanalytics.files.wordpress.com/2018/12/content_7.png?w=456?w=300" data-attachment-id="976" data-image-meta="{" data-image-description="" src="https://mathewanalytics.files.wordpress.com/2018/12/content_7.png?w=456" data-permalink="https://mathewanalytics.com/2018/12/20/examining-the-tweeting-patterns-of-prominent-crossfit-gyms/content_7/" data-large-file="https://mathewanalytics.files.wordpress.com/2018/12/content_7.png?w=456?w=739" alt="" data-image-title="content_7" sizes="(max-width: 739px) 100vw, 739px" data-comments-opened="0" class="wp-image-976" data-recalc-dims="1"></a></p>
<p><em><strong>D4. Tweet Length</strong></em></p>
<p>The following chart shows the summary statistics pertaining to the length of the tweets from each of the crossfit boxes. crossfitsanitas and WindyCityCF seem to have the longest tweets on average, and sfcrossfit has the shortest tweet.</p>
<pre>

## tweet length 

melt(final_dat[, .(avg_length = mean(display_text_width,na.rm=T),
                   med_length = median(display_text_width,na.rm=T),
                   min_length = min(display_text_width,na.rm=T),
                   max_length = mean(display_text_width,na.rm=T)), by=.(crossfit_name)], id=&quot;crossfit_name&quot;) %>%
   ggplot(., aes(x=reorder(crossfit_name, value), y=value)) +
      geom_bar(stat=&quot;identity&quot;, fill=&quot;steelblue&quot;) + coord_flip() +
         labs(x=&quot;Date&quot;, y=&quot;Number of Tweets&quot;, colour=&quot;Crossfit Box&quot;) +
         theme(plot.title = element_text(hjust = 0.5)) +
      facet_wrap(~variable) + labs(x=&quot;Time&quot;, y=&quot;Number of Tweets&quot;, colour=&quot;Crossfit Box&quot;) +
      theme(axis.text.x=element_text(angle=90,hjust=1)) +
      theme(plot.title = element_text(hjust = 0.5),
            strip.text.x = element_text(size = 10)) + ylim(0,200) +
      ggtitle(&quot;Tweet Length Summary Statistics&quot;) 

</pre>
<p><a rel="nofollow" href="https://mathewanalytics.files.wordpress.com/2018/12/content_6.png" target="_blank"><img data-orig-file="https://mathewanalytics.files.wordpress.com/2018/12/content_6.png?w=456" data-orig-size="739,541" data-medium-file="https://mathewanalytics.files.wordpress.com/2018/12/content_6.png?w=456?w=300" data-attachment-id="977" data-image-meta="{" data-image-description="" src="https://mathewanalytics.files.wordpress.com/2018/12/content_6.png?w=456" data-permalink="https://mathewanalytics.com/2018/12/20/examining-the-tweeting-patterns-of-prominent-crossfit-gyms/content_6/" data-large-file="https://mathewanalytics.files.wordpress.com/2018/12/content_6.png?w=456?w=739" alt="" data-image-title="content_6" sizes="(max-width: 739px) 100vw, 739px" data-comments-opened="0" class="wp-image-977" data-recalc-dims="1"></a></p>
<p><strong>E. Sentiment Analysis</strong></p>
<p>In order to evaluate the emotion associated with the tweets of each crossfit box, I used the syuzhet package. This package is based on emotion lexicon which maps different words with the various emotions (joy, fear, etc.) and sentiment polarity (positive/negative). We’ll have to calculate the emotion score based on the words present in the tweets and plot the same.</p>
<p>We can see that the majority of tweets for each Crossfit box had a largely positive sentiment, trust and anticipation were other common emotions. For cfbelltown and crossfitsanitas, the third most common emotion was negative.</p>
<pre>

## sentiment analysis 

library(syuzhet)

sentiment_scores = list() 

#each_gym=&quot;sfcrossfit&quot;
for(each_gym in unique(final_dat[,crossfit_name])){

   print(each_gym)

   tweet_text = final_dat[crossfit_name==each_gym, text]

   # removing retweets
   tweet_text &lt;- gsub(&quot;(RT|via)((?:\\b\\w*@\\w+)+)&quot;,&quot;&quot;,tweet_text)

   # removing mentions
   tweet_text &lt;- gsub(&quot;@\\w+&quot;,&quot;&quot;,tweet_text)

   all_sentiments &lt;- get_nrc_sentiment((tweet_text))
   #head(all_sentiments)

   final_sentimentscores &lt;- data.table(colSums(all_sentiments))
   #head(final_sentimentscores)

   names(final_sentimentscores) &lt;- &quot;score&quot;
   final_sentimentscores &lt;- cbind(&quot;sentiment&quot;=colnames(all_sentiments), final_sentimentscores)
   final_sentimentscores$gym = each_gym
   #final_sentimentscores

   sentiment_scores[[each_gym]] &lt;- final_sentimentscores

}

sentiment_scores &lt;- rbindlist(sentiment_scores)
dim(sentiment_scores)
sentiment_scores

ggplot(sentiment_scores, aes(x=sentiment, y=score))+
   geom_bar(aes(fill=sentiment), stat = &quot;identity&quot;)+
   theme(legend.position=&quot;none&quot;)+
   xlab(&quot;Sentiments&quot;)+ylab(&quot;Scores&quot;)+ coord_flip() +
   facet_wrap(~gym) + labs(x=&quot;&quot;, y=&quot;&quot;, colour=&quot;&quot;) +
   theme(plot.title = element_text(hjust = 0.5),
         axis.ticks.x = element_line(colour = &quot;black&quot;),
         axis.ticks.y = element_line(colour = &quot;black&quot;),
         strip.text.x = element_text(size = 10)) +
   ggtitle(&quot;Total Sentiment Across All Original Tweets&quot;) 

</pre>
<p><a rel="nofollow" href="https://mathewanalytics.files.wordpress.com/2018/12/sentiment_1.png" target="_blank"><img data-orig-file="https://mathewanalytics.files.wordpress.com/2018/12/sentiment_1.png?w=456" data-orig-size="739,541" data-medium-file="https://mathewanalytics.files.wordpress.com/2018/12/sentiment_1.png?w=456?w=300" data-attachment-id="978" data-image-meta="{" data-image-description="" src="https://mathewanalytics.files.wordpress.com/2018/12/sentiment_1.png?w=456" data-permalink="https://mathewanalytics.com/2018/12/20/examining-the-tweeting-patterns-of-prominent-crossfit-gyms/sentiment_1/" data-large-file="https://mathewanalytics.files.wordpress.com/2018/12/sentiment_1.png?w=456?w=739" alt="" data-image-title="sentiment_1" sizes="(max-width: 739px) 100vw, 739px" data-comments-opened="0" class="wp-image-978" data-recalc-dims="1"></a></p>
<p><strong>F. Predictive Model</strong></p>
<p>Out of curiosity, I wanted to see if a predictive model could be generated to predict whether a tweet would be retweeted. For this task, I looked at just the tweets from sfcrossfit. The target variable was 0 or 1, with zero representing a tweet that was not retweeted and one representing any tweet that was retweeted one or more times. Using the caret package, I trained a random forest on my training data and tried to predict on new data. We can see from the confusion matrix that the model did a very job at predicting whether a tweet would be retweeted or not. Perhaps with some better feature, a better model could have been produced, but I chose not to invest more time on this task.</p>
<pre>

library(tidyverse)
library(tidytext)
library(stringr)
library(caret)
library(tm)

final_dat[1:2]

final_dat[, .N, by=crossfit_name]
final_dat_sub = final_dat[crossfit_name==&quot;sfcrossfit&quot;,]
final_dat_sub[, favorite_count_new := ifelse(favorite_count==0, 0, 1)]
final_dat_sub[, retweet_count_new := ifelse(retweet_count==0, 0, 1)]
final_dat_sub[1:3]
#final_dat_sub$retweet_count_new

splt &lt;- createDataPartition(final_dat_sub$retweet_count_new, p = 0.70, list = F)
train &lt;- final_dat_sub[splt, .(created_at,favorite_count,is_retweet, display_text_width, contains_hashtags, retweet_count_new)]
dim(train)
test &lt;- final_dat_sub[-splt, .(created_at,favorite_count,is_retweet, display_text_width, contains_hashtags, retweet_count_new)]
dim(test)

congress_rf &lt;- train(x = as.matrix(train[,.(year(created_at), month(created_at),favorite_count,is_retweet, display_text_width, contains_hashtags)]),
                     y = factor(train$retweet_count_new),
                     method = &quot;rf&quot;,
                     ntree = 200,
                     trControl = trainControl(method = &quot;oob&quot;))
congress_rf$finalModel
varImpPlot(congress_rf$finalModel)

pred = predict(congress_rf, as.matrix(final_dat_sub[-splt,.(year(created_at), month(created_at),favorite_count,is_retweet, display_text_width, contains_hashtags)]))
confusionMatrix(final_dat_sub[-splt,retweet_count_new], pred, positive=&quot;1&quot;)

</pre>
<p><a rel="nofollow" href="https://mathewanalytics.files.wordpress.com/2018/12/model_2.png" target="_blank"><img data-orig-file="https://mathewanalytics.files.wordpress.com/2018/12/model_2.png" data-orig-size="990,754" data-medium-file="https://mathewanalytics.files.wordpress.com/2018/12/model_2.png?w=300" data-attachment-id="979" data-image-meta="{" data-image-description="" src="https://mathewanalytics.files.wordpress.com/2018/12/model_2.png?w=450&h=600&fit=788%2C600" data-permalink="https://mathewanalytics.com/2018/12/20/examining-the-tweeting-patterns-of-prominent-crossfit-gyms/model_2/" data-large-file="https://mathewanalytics.files.wordpress.com/2018/12/model_2.png?w=450&amp;h=600&amp;fit=788%2C600" alt="" data-image-title="model_2" sizes="(max-width: 788px) 100vw, 788px" data-comments-opened="0" class="wp-image-979" data-recalc-dims="1"></a></p>
<p><strong>G. Conclusion</strong></p>
<p>So there you have it. An investigation of how several prominent Crossfit gyms are using their Twitter accounts to engage with consumers. At the end of the day, I would suggest that any business in the health and wellness space should invest more time on Instagram or YouTube than Twitter when it comes to brand marketing or customer acquisition. Twitter is great for news and entertainment, but it isn’t the ideal platform to share fitness content or inspire new members.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://mathewanalytics.com/2018/12/20/examining-the-tweeting-patterns-of-prominent-crossfit-gyms/"> R – Mathew Analytics</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/DjCdac2JDFM" height="1"></a></div>