-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_analysis.R
197 lines (143 loc) · 5.5 KB
/
03_analysis.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# source of pdf files:
# https://intelligence.house.gov/social-media-content/social-media-advertisements.htm
library(pdftools)
library(tidyverse)
library(janitor)
library(lubridate)
library(writexl)
#### run function in step 02 to handle processing of extracting from the pdf file
# source("02_solve_for_all_function.R")
# bring in the results data
data <- readRDS("myresults_formatted_all.rds")
# check if formatted dataset came in successfully
data
# see characteristics of the data columns
glimpse(data)
### let's do some initial aggregate counts
data %>%
count(target_age) %>%
arrange(desc(n))
data %>%
count(target_location) %>%
arrange(desc(n))
data %>%
count(target_pplwhomatch) %>%
arrange(desc(n))
#looking at ads by date
data %>%
count(ad_creation_year) %>%
arrange(desc(n))
data %>%
count(ad_creation_year, ad_creation_month) %>%
arrange(ad_creation_year, ad_creation_month)
#bringing in sum of ad spend
data %>%
group_by(ad_creation_year, ad_creation_month) %>%
summarise(num_ads = n(), sum_spending = sum(ad_spend, na.rm = TRUE)) %>%
arrange(ad_creation_year, ad_creation_month)
#sum of impressions
data %>%
group_by(ad_creation_year, ad_creation_month) %>%
summarise(num_ads = n(), sum_impressions = sum(ad_impressions, na.rm = TRUE)) %>%
arrange(ad_creation_year, ad_creation_month)
### aiming to see if state-by-state spending can be compiled ####
names(data)
forstates <- data %>%
select(ad_spend,
location_state1,
location_state2,
location_state3,
location_state4,
location_state5) %>%
mutate(numads = 1)
#
# forstates %>%
# gather(key = state, value = ad_spend, 2:6, na.rm = FALSE)
tidystates <- forstates %>%
mutate(
statecombo = paste0(location_state1, ",", location_state2, ",", location_state3, ",", location_state4, ",", location_state5)
) %>%
select(numads, ad_spend, statecombo) %>%
separate_rows(statecombo)
tidystates
#group by each state's ad count and spending
tidystates %>%
filter(statecombo != "NA") %>%
group_by(statecombo) %>%
summarise(ad_count = sum(numads, na.rm = TRUE), ad_spending = sum(ad_spend, na.rm = TRUE))
### BY STATE AND YEAR ####
names(data)
forstates_timeline <- data %>%
select(ad_spend,
ad_creation_year,
ad_creation_month,
location_state1,
location_state2,
location_state3,
location_state4,
location_state5) %>%
mutate(numads = 1)
#
# forstates %>%
# gather(key = state, value = ad_spend, 2:6, na.rm = FALSE)
tidystates_timeline <- forstates_timeline %>%
mutate(
statecombo = paste0(location_state1, ",", location_state2, ",", location_state3, ",", location_state4, ",", location_state5)
) %>%
select(numads, ad_spend, ad_creation_year, ad_creation_month, statecombo) %>%
separate_rows(statecombo)
tidystates_timeline
#group by each state's ad count and spending
tidystates_timeline %>%
filter(statecombo != "NA") %>%
group_by(statecombo, ad_creation_year, ad_creation_month) %>%
summarise(ad_count = sum(numads, na.rm = TRUE), ad_spending = sum(ad_spend, na.rm = TRUE))
#write to file
tidystates_timeline %>%
filter(statecombo != "NA") %>%
group_by(statecombo, ad_creation_year, ad_creation_month) %>%
summarise(ad_count = sum(numads, na.rm = TRUE), ad_spending = sum(ad_spend, na.rm = TRUE)) %>%
write_csv("states_bymonth.csv")
#*********************************************************************
###### ANALYZING INTERESTS / PEOPLE WHO MATCH CHARACTERISTICS ########
forinterests_timeline <- data %>%
select(ad_creation_year,
ad_creation_month,
target_pplwhomatch) %>%
mutate(numads = 1)
# forinterests_timeline$ad_targeting_fulltext
#
# forstates %>%
# gather(key = state, value = ad_spend, 2:6, na.rm = FALSE)
interests_working <- forinterests_timeline %>%
mutate(target_pplwhomatch = str_squish(target_pplwhomatch))
interests_working$target_pplwhomatch <- str_remove_all(interests_working$target_pplwhomatch, "Interests: ")
interests_working$target_pplwhomatch <- str_remove_all(interests_working$target_pplwhomatch, "Behaviors: ")
interests_working$target_pplwhomatch <- str_remove_all(interests_working$target_pplwhomatch, "School: ")
interests_working$target_pplwhomatch <- str_remove_all(interests_working$target_pplwhomatch, "Multicultural Affinity: ")
interests_working$target_pplwhomatch <- str_remove_all(interests_working$target_pplwhomatch, "Friends of connections: ")
interests_working$target_pplwhomatch <- str_remove_all(interests_working$target_pplwhomatch, "Politics: ")
interests_working$target_pplwhomatch <- str_remove_all(interests_working$target_pplwhomatch, "Jr.")
tidyinterests_timeline <- interests_working %>%
separate_rows(target_pplwhomatch, sep = ",") %>%
mutate(
target_pplwhomatch = str_squish(target_pplwhomatch)
)
tidyinterests_timeline
#group by each targets's total ad count
tidyinterests_timeline %>%
filter(target_pplwhomatch != "NA",
target_pplwhomatch != "") %>%
group_by(target_pplwhomatch) %>%
summarise(ad_count = sum(numads, na.rm = TRUE)) %>%
ungroup() %>%
arrange(desc(ad_count)) %>%
View()
#group by each targets's total ad count by MONTH
tidyinterests_timeline %>%
filter(target_pplwhomatch != "NA",
target_pplwhomatch != "") %>%
group_by(target_pplwhomatch, ad_creation_year, ad_creation_month) %>%
summarise(ad_count = sum(numads, na.rm = TRUE)) %>%
ungroup() %>%
arrange(ad_creation_year, ad_creation_month, desc(ad_count))