Challenge #3

1. Import the data and packages

library(readr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
library(ggplot2)
data<- read_csv("figure-data.csv")
## Rows: 221 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl  (7): Social Media, Twitter, Big Data, Google Trend, Facebook, Cell Phon...
## date (1): date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

2. Explore the data

dim(data)
## [1] 221   8
names(data)
## [1] "date"                "Social Media"        "Twitter"            
## [4] "Big Data"            "Google Trend"        "Facebook"           
## [7] "Cell Phone"          "Call Detail Records"
str(data)
## spc_tbl_ [221 × 8] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ date               : Date[1:221], format: "2020-01-29" "2020-01-30" ...
##  $ Social Media       : num [1:221] 0 0 0 0 0 0 0 0 0 0 ...
##  $ Twitter            : num [1:221] 0 0 0 0 0 0 0 0 0 0 ...
##  $ Big Data           : num [1:221] 0 0 0 0 0 0 0 0 0 0 ...
##  $ Google Trend       : num [1:221] 1 1 1 1 1 1 1 1 1 1 ...
##  $ Facebook           : num [1:221] 0 0 0 0 0 0 0 0 0 0 ...
##  $ Cell Phone         : num [1:221] 0 0 0 0 0 0 0 0 0 0 ...
##  $ Call Detail Records: num [1:221] 0 0 0 0 0 0 0 0 0 0 ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   date = col_date(format = ""),
##   ..   `Social Media` = col_double(),
##   ..   Twitter = col_double(),
##   ..   `Big Data` = col_double(),
##   ..   `Google Trend` = col_double(),
##   ..   Facebook = col_double(),
##   ..   `Cell Phone` = col_double(),
##   ..   `Call Detail Records` = col_double()
##   .. )
##  - attr(*, "problems")=<pointer: 0xb433bf400>
head(data)
## # A tibble: 6 × 8
##   date       `Social Media` Twitter `Big Data` `Google Trend` Facebook
##   <date>              <dbl>   <dbl>      <dbl>          <dbl>    <dbl>
## 1 2020-01-29              0       0          0              1        0
## 2 2020-01-30              0       0          0              1        0
## 3 2020-01-31              0       0          0              1        0
## 4 2020-02-01              0       0          0              1        0
## 5 2020-02-02              0       0          0              1        0
## 6 2020-02-03              0       0          0              1        0
## # ℹ 2 more variables: `Cell Phone` <dbl>, `Call Detail Records` <dbl>

3. Format the data

data_long <- data %>% pivot_longer( cols = -date, names_to = "source", values_to = "value")
head(data_long)
## # A tibble: 6 × 3
##   date       source       value
##   <date>     <chr>        <dbl>
## 1 2020-01-29 Social Media     0
## 2 2020-01-29 Twitter          0
## 3 2020-01-29 Big Data         0
## 4 2020-01-29 Google Trend     1
## 5 2020-01-29 Facebook         0
## 6 2020-01-29 Cell Phone       0

4. Average activity by data source

source_summary <- data_long %>% group_by(source) %>% summarize( average_activity = mean(value, na.rm = TRUE))
source_summary
## # A tibble: 7 × 2
##   source              average_activity
##   <chr>                          <dbl>
## 1 Big Data                       54.9 
## 2 Call Detail Records             2.19
## 3 Cell Phone                      8.51
## 4 Facebook                       28.0 
## 5 Google Trend                   31.3 
## 6 Social Media                  207.  
## 7 Twitter                        86.2

5. Number of observations

source_count <- data_long %>% count(source)
source_count
## # A tibble: 7 × 2
##   source                  n
##   <chr>               <int>
## 1 Big Data              221
## 2 Call Detail Records   221
## 3 Cell Phone            221
## 4 Facebook              221
## 5 Google Trend          221
## 6 Social Media          221
## 7 Twitter               221

6. Activity over time

daily_activity <- data_long %>% group_by(date, source) %>% summarize( daily_activity = mean(value, na.rm = TRUE), .groups = "drop")
head(daily_activity)
## # A tibble: 6 × 3
##   date       source              daily_activity
##   <date>     <chr>                        <dbl>
## 1 2020-01-29 Big Data                         0
## 2 2020-01-29 Call Detail Records              0
## 3 2020-01-29 Cell Phone                       0
## 4 2020-01-29 Facebook                         0
## 5 2020-01-29 Google Trend                     1
## 6 2020-01-29 Social Media                     0

7. Key Insight: Average Activity

Social Media had the highest average activity during the study period with Call Detail having the lowest average activity during the same period.

source_summary %>%
arrange(desc(average_activity))
## # A tibble: 7 × 2
##   source              average_activity
##   <chr>                          <dbl>
## 1 Social Media                  207.  
## 2 Twitter                        86.2 
## 3 Big Data                       54.9 
## 4 Google Trend                   31.3 
## 5 Facebook                       28.0 
## 6 Cell Phone                      8.51
## 7 Call Detail Records             2.19

8. Key Insight: Changes over Time

Social Media showed the largest change in activity during the study period as Call Detail Records showed the smallest change.

daily_activity %>%
group_by(source) %>%
summarize(
minimum_activity = min(daily_activity, na.rm = TRUE),
maximum_activity = max(daily_activity, na.rm = TRUE)
)
## # A tibble: 7 × 3
##   source              minimum_activity maximum_activity
##   <chr>                          <dbl>            <dbl>
## 1 Big Data                           0              129
## 2 Call Detail Records                0                6
## 3 Cell Phone                         0               20
## 4 Facebook                           0               72
## 5 Google Trend                       1               78
## 6 Social Media                       0              530
## 7 Twitter                            0              206

9. Visualization

ggplot(daily_activity, aes(x = date, y = daily_activity, color = source)) + geom_line() + labs( title = "Activity Across Data Sources Over Time", x = "Date", y = "Activity Level", color = "Data Source" ) + theme_minimal()

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.