-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path.Rhistory
512 lines (512 loc) · 14.5 KB
/
.Rhistory
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
group_by(contract_type) %>%
summarize(table(contract_type))
df_new %>%
group_by(event_type) %>%
summarize(table(event_type))
df_new %>%
group_by(contract_type) %>%
summarize(table(contract_type))
df_new %>%
group_by(event_type) %>%
summarize(table(event_type))
table(df_new$contract_type)
table(df_new$event_type)
?table
table(df[,c("contract_type", "event_type")])
df[,c("contract_type", "event_type")]
df[,c("contract_type")]
table(df_new[,c("contract_type", "event_type")])
table(df_new[,c("contract_type", "event_type")])
table(df_new[df_new$event_type == "rebill", $rebill_sequence])
table(df_new[df_new$event_type == "rebill", "rebill_sequence"])
head(
df_new[df_new$event_type == "rebill", "rebill_sequence"]
)
summary(
df_new[df_new$event_type == "rebill", "rebill_sequence"]
)
table(df_new[,c("contract_type", "event_type")])
df_ending = runQueryBI("
SELECT
*
FROM
bi.dim_contract
WHERE
contract_end_date > NOW() - INTERVAL 1 MONTH
", db="bi")
table(df_ending[df_new$event_type == "rebill", "rebill_sequence"])
boxplot(df_ending[df_new$event_type == "rebill", "rebill_sequence"])
library(nippon)
install.packages("nippon")
install.packages("Nippon")
library(nippon)
library(Nippon)
gpclibPermit()
if (require(RColorBrewer)) {
cols <- rev(brewer.pal(12,"Set2"))
}else{
cols <- gray.colors(12)
}
JapanPrefecturesMap(col = cols, axes = TRUE)
source('~/.active-rstudio-document', echo=TRUE)
remove.packages("choroplethr")
install.packages("choroplethr")
library(choroplethr)
library(ggplot2)
library(rcolorbrewer)
choroplethr(df_pop_zip, "zip") + scale_fill_brewer(palette=2)
choroplethr(df_pop_zip, "zip") + scale_fill_brewer(palette=2)
choroplethr(df_pop_zip, "zip") + scale_fill_brewer(palette=3)
choroplethr(df_pop_county, "county") + scale_fill_brewer(palette=2)
choroplethr(df_pop_state, "state") + scale_fill_brewer(palette=2)
df_pop_state[2,2] = df_pop_state[2,2]*100
choroplethr(df_pop_state, "state") + scale_fill_brewer(palette=2)
choroplethr(df_pop_zip, "zip") + scale_color_brewer(palette=2)# scale_fill_brewer(palette=2)
source('~/.active-rstudio-document', echo=TRUE)
source('~/.active-rstudio-document', echo=TRUE)
nrow(df)
library(dplyr)
update.packages()
library(dplyr)
head(df)
t = table(df$t)
boxplot(t)
hist(t)
?summarize
df_2 = summarize(df, t, n=count())
df_2 = group_by(df, t) summarize(df, t, n=count())
df_2 = group_by(df, t)
df_3 = summarize(df_2, n=count())
df_3 = summarize(df_2, count = n())
df_3
?geom_hist
?geom_bar
ggplot(df_3, aes(x=t, y=count)) + geom_hist(stat="identity")
ggplot(df_3, aes(x=t, y=count)) + geom_bar(stat="identity")
df_3
df %>%
group_by(orig_contract_config) %>%
head()
df %>%
group_by(orig_contract_config) %>%
summarize(count=n())
df_big_hist = df %>%
group_by(t) %>%
summarize(df, count=n())
ggplot(df_big_hist, aes(x=t, y=count)) +
geom_bar(stat="identity") + # looks like the 1 case of 13 is being dropped....
ggtitle("Histogram of months in this dataset")
df_big_hist = df %>%
group_by(t) %>%
summarize(df, count=n())
head(df)
df %>%
group_by(t) %>%
summarize(df, count=n())
df_big_hist = df %>%
group_by(t) %>%
summarize(count=n())
ggplot(df_big_hist, aes(x=t, y=count)) +
geom_bar(stat="identity") + # looks like the 1 case of 13 is being dropped....
ggtitle("Histogram of months in this dataset")
df_big_hist = df %>%
group_by(t) %>%
summarize(count=n())
ggplot(df_big_hist, aes(x=t, y=count)) +
geom_bar(stat="identity") + # looks like the 1 case of 13 is being dropped....
ggtitle("Histogram of months in this dataset")
df_big_hist = df %>%
group_by(t) %>%
summarize(count=n())
ggplot(df_big_hist, aes(x=t, y=count)) +
geom_bar(stat="identity") + # looks like the 1 case of 13 is being dropped....
ggtitle("Histogram of months in this dataset")
ggplot(df_big_hist, aes(t)) + geom_bar()
ggplot(df_big_hist, aes(t)) + geom_bar(stat="identity")
df_big_hist
ggplot(df_big_hist, aes(t)) + geom_bar()
ggplot(df_big_hist, aes(t)) + geom_bar(stat="identity")
df %>%
group_by(t) %>%
summarize(count=n()) %>%
ggplot(aes(x=t, y=count)) +
geom_bar(stat="identity") + # looks like the 1 case of 13 is being dropped....
ggtitle("Histogram of months in this dataset")
df %>%
group_by(orig_contract_config) %>%
summarize(count=n()) %>%
ggplot(aes(x=orig_contract_config, y=count))
df %>%
group_by(orig_contract_config) %>%
summarize(count=n())
df %>%
group_by(orig_contract_config) %>%
summarize(count=n()) %>%
ggplot(aes(x=orig_contract_config, y=count)) +
geom_bar(stat="identity") +
ggtitle("Original Contract Configuration Frequency")
?df_president_ts
library(choroplethr)
?df_president_ts
make_histogram = function(df, t)
{
df %>%
group_by(t) %>%
summarize(count=n()) %>%
ggplot(aes(x=t, y=count)) +
geom_bar(stat="identity") +
ggtitle(t)
}
make_histogram(df, "all")
head(df_2)
df=df %>% group_by(orig_contract_config) %>% summarize(count=n())
head(df)
df = runQueryBI("SELECT * FROM analytics.dtsm");
rm(df_2)
rm(df_3)
rm(df_big_hist)
d=df %>% group_by(t) %>% summarize(count=n())
d
d$count = d$count/d$count[1]
d
# for modeling or visualization, remove periods that are in the future
future_filter <- function (df) {
filter(df, period_end < Sys.Date() - 1)
}
# to build a life-table from the data
life_table <- function (df, condition) {
condition <- c(condition, 't')
group_var <- lapply(condition, as.symbol)
regroup(df, group_var) %>%
summarize(cases = n(), cancels = sum(cancel), hazard = cancels / cases, period_surv = 1 - hazard) %>%
mutate(survival = order_by(t, cumprod(period_surv)))
}
head(df)
life_table_df <- life_table(future_filter(df), 'orig_contract_config')
p <- ggplot(life_table_df, aes(x = t, y = survival, color=orig_contract_config)) + geom_line()
p
?filter
head(life_table_df)
life_table_df
source('~/.active-rstudio-document', echo=TRUE)
source('~/.active-rstudio-document', echo=TRUE)
head(df)
bak=d
df$d=as.POSIXlt(df$d)
ggplot(df, aes(d, num_over_36)) + geom_line()
ggplot(df, aes(d, num_over_36)) + geom_line() + ggitle("Number of emails over Weight 36 each day\nlast 2 years")
ggplot(df, aes(d, num_over_36)) + geom_line() + ggtitle("Number of emails over Weight 36 each day\nlast 2 years")
ggplot(df, aes(d, num_over_36)) +
geom_line() +
ggtitle("Number of emails over Weight 36 each day\nlast 2 years") +
labs(x="Day", y="Number of Emails")
q = "
SELECT
d, COUNT(*) num_over_15
FROM
daily_email_weights
WHERE
weight > 15
GROUP BY
1
"
df = runQuery(q, "LeadInbox")
head(df)
bak=d
df$d=as.POSIXlt(df$d)
ggplot(df, aes(d, num_over_15)) +
geom_line() +
ggtitle("Number of emails over Weight 36 each day\nlast 2 years") +
labs(x="Day", y="Number of Emails")
rm(d)
rm(q)
q_36 = "
SELECT
d, COUNT(*) num_over_36
FROM
daily_email_weights
WHERE
weight > 36
GROUP BY
1
"
df_36 = runQuery(q_36, "LeadInbox")
df$d=as.POSIXlt(df_36$d)
ggplot(df, aes(df_36, num_over_36)) +
geom_line() +
ggtitle("Number of emails over Weight 36 each day\nlast 2 years") +
labs(x="Day", y="Number of Emails")
q_36 = "
SELECT
d, COUNT(*) num_over_36
FROM
daily_email_weights
WHERE
weight > 36
GROUP BY
1
"
df_36 = runQuery(q_36, "LeadInbox")
df_36$d=as.POSIXlt(df_36$d)
ggplot(df_36, aes(df_36, num_over_36)) +
geom_line() +
ggtitle("Number of emails over Weight 36 each day\nlast 2 years") +
labs(x="Day", y="Number of Emails")
ggplot(df_36, aes(df, num_over_36)) +
geom_line() +
ggtitle("Number of emails over Weight 36 each day\nlast 2 years") +
labs(x="Day", y="Number of Emails")
ggplot(df_36, aes(d, num_over_36)) +
geom_line() +
ggtitle("Number of emails over Weight 36 each day\nlast 2 years") +
labs(x="Day", y="Number of Emails")
q_15 = "
SELECT
d, COUNT(*) num_over_15
FROM
daily_email_weights
WHERE
weight > 15
GROUP BY
1
"
df_15 = runQuery(q, "LeadInbox")
df_15 = runQuery(q_15, "LeadInbox")
df_15 = runQuery(q_15, "LeadInbox")
df_15$d=as.POSIXlt(df_15$d)
ggplot(df_15, aes(d, num_over_15)) +
geom_line() +
ggtitle("Number of emails over Weight 36 each day\nlast 2 years") +
labs(x="Day", y="Number of Emails") +
ylim(15)
ggplot(df_15, aes(d, num_over_15)) +
geom_line() +
ggtitle("Number of emails over Weight 36 each day\nlast 2 years") +
labs(x="Day", y="Number of Emails")
ggplot(df_15, aes(d, num_over_15)) +
geom_line() +
ggtitle("Number of emails over Weight 15 each day\nlast 2 years") +
labs(x="Day", y="Number of Emails") +
ylim(c(0,15))
ggplot(df_15, aes(d, num_over_15)) +
geom_line() +
ggtitle("Number of emails over Weight 15 each day\nlast 2 years") +
labs(x="Day", y="Number of Emails") +
ylim(c(0,550))
ggplot(df_15, aes(d, num_over_15)) +
geom_line() +
ggtitle("Number of emails over Weight 15 each day\nlast 2 years") +
labs(x="Day", y="Number of Emails")
ggplot(df_15, aes(d, num_over_15)) +
geom_line() +
ggtitle("Number of emails over Weight 15 each day\nlast 2 years") +
labs(x="Day", y="Number of Emails") +
ylim(c(0,550))
ggplot(df_36, aes(d, num_over_36)) +
geom_line() +
ggtitle("Number of emails over Weight 36 each day\nlast 2 years") +
labs(x="Day", y="Number of Emails")
ggplot(df_36, aes(d, num_over_36)) +
geom_line() +
ggtitle("Number of emails over Weight 36 each day\nlast 2 years") +
labs(x="Day", y="Number of Emails") +
ylim(c(0,550)
ggplot(df_36, aes(d, num_over_36)) +
geom_line() +
ggtitle("Number of emails over Weight 36 each day\nlast 2 years") +
labs(x="Day", y="Number of Emails") +
ylim(c(0,550)
)
ggplot(df_36, aes(d, num_over_36)) +
geom_line() +
ggtitle("Number of emails over Weight 36 each day\nlast 2 years") +
labs(x="Day", y="Number of Emails") +
ylim(c(0,550))
summary(df_36$num_over_36)
boxplot(df_36$num_over_36)
q_got_thru = "
select
*
from
Message JOIN MessageSender USING(messageId)
JOIN MessageContent USING(messageId)
JOIN MessageSource USING(messageId)
JOIN MessageStatus USING(messageId)
WHERE
email='[email protected]' AND
DATE(creationDate)='2014-10-23'
ORDER BY
creationDate DESC
";
df_got_thru=runQuery(q_got_thru, "LeadInbox")
df_got_thru$creationDate=as.POSIXlt(df_got_thru$creationDate)
df_got_thru$spam=0
q_spam = "
select
*
from
BlockedEmail
WHERE
email_from='[email protected]'
";
df_spam=runQuery(q_spam, "Spam")
df_spam$spam=1
head(df_got_thru)
df_spam$creationDate=as.POSIXlt(df_spam$creationDate)
df_spam$creationDate=as.POSIXlt(df_spam$creationDate)
df_spam$creationDate=as.POSIXlt(df_spam$creation_date)
df_got_thru=df_got_thru[,c("creationDate", "spam")]
df_spam=df_spam[,c("creationDate", "spam")]
df_final=rbind(df_spam,df_got_thru)
ggplot(df_final, aes(creationDate, spam)) + geom_point()
head(df_final)
class(df_final$creationDate)
class(df_final$spam)
ggplot(df_final, aes(creationDate, spam)) + geom_point()
ggplot(df_final, aes(creationDate, spam))
library(ggplot2)
ggplot(df_final, aes(creationDate, spam))
ggplot(df_final)
plot(df_final)
update.packages()
remove.packages("ggplot2")
q_blocked = "
SELECT
DATE(creation_date) day,
email_type,
COUNT(*) num_emails
FROM
BlockedEmail
WHERE
DATE(creation_date) > NOW() - INTERVAL 1 YEAR
AND email_type IN (88, 55, 81, 121)
GROUP BY
1, 2
"
df_blocked=runQuery(q_blocked, "Spam")
df_blocked$day=as.POSIXct(df_blocked$day)
ggplot(df_blocked, aes(x=day, fill=as.factor(email_type)) + geom_bar()
ggplot(df_blocked, aes(x=day, fill=as.factor(email_type))) + geom_bar()
email_type
ggplot(df_blocked, aes(x=day, fill=as.factor(email_type))) + geom_bar()
source('~/.active-rstudio-document', echo=TRUE)
source('~/.active-rstudio-document', echo=TRUE)
df$day = as.POSIXlt(df$day)
ggplot(df, aes(day, num_email_addresses)) + geom_line()
source('~/.active-rstudio-document', echo=TRUE)
source('~/.active-rstudio-document', echo=TRUE)
source('~/.active-rstudio-document', echo=TRUE)
plot(df_ts)
plot(df_components)
plot(df_seasonallyadjusted);
library(choroplethr)
data(df_pop_county)
county_choropleth(df_pop_county)
county_choropleth(df_pop_county, buckets=1)
library(choroplethr)
data(df_pop_state)
choro = StateChoropleth$new(df_pop_state)
choro$show_labels=FALSE
choro$render()
?state_choropleth
data(state.names)
library(choroplethr)
data(state.names)
data(state.regions)
data(state.region)
data(state.regions)
library(choroplethrMaps)
data(state.regions)
data(state.regions)
library(choroplethrMaps)
?state.regions
data(state.regions)
head(state.regions)
update.packages()
remove.packages("choroplethr")
remove.packages("choroplethrMaps")
install.packages("choroplethr")
install.packages("choroplethr")
source('~/.active-rstudio-document', echo=TRUE)
source('~/.active-rstudio-document', echo=TRUE)
install.packages("choroplethr")
install_github("trulia/choroplethr")
library(choroplethr)
source('~/.active-rstudio-document', echo=TRUE)
install_github("trulia/choroplethrMaps")
library(choroplethrMaps)
library(choroplethr)
source('~/.active-rstudio-document', echo=TRUE)
head(test_df)
source('~/.active-rstudio-document', echo=TRUE)
source('~/.active-rstudio-document', echo=TRUE)
source('~/.active-rstudio-document', echo=TRUE)
update.packages()
library("shiny", lib.loc="/Library/Frameworks/R.framework/Versions/3.0/Resources/library")
runExample("01_hello")
runExample("02_text") # tables and data frames
runExample("03_reactivity")
runExample("04_mpg")
runExample("05_sliders")
runExample("06_tabsets")
runExample("07_widgets")
runExample("08_html")
runExample("09_upload")
shiny::runApp()
shiny::runApp('code/R/shiny')
counties <- readRDS("census-app/data/counties.rds")
getwd()
setwd("R/shiny")
getwd()
setwd("code/r/")
setwd("shiny")
counties <- readRDS("census-app/data/counties.rds")
counties <- readRDS("data/counties.rds")
head(counties)
library(maps)
library(mapproj)
install.packages("mapproj")
library(mapproj)
percent_map(counties$white, "darkgreen", "% white")
# Note: percent map is designed to work with the counties data set
# It may not work correctly with other data sets if their row order does
# not exactly match the order in which the maps package plots counties
percent_map <- function(var, color, legend.title, min = 0, max = 100) {
# generate vector of fill colors for map
shades <- colorRampPalette(c("white", color))(100)
# constrain gradient to percents that occur between min and max
var <- pmax(var, min)
var <- pmin(var, max)
percents <- as.integer(cut(var, 100,
include.lowest = TRUE, ordered = TRUE))
fills <- shades[percents]
# plot choropleth map
map("county", fill = TRUE, col = fills,
resolution = 0, lty = 0, projection = "polyconic",
myborder = 0, mar = c(0,0,0,0))
# overlay state borders
map("state", col = "white", fill = FALSE, add = TRUE,
lty = 1, lwd = 1, projection = "polyconic",
myborder = 0, mar = c(0,0,0,0))
# add a legend
inc <- (max - min) / 4
legend.text <- c(paste0(min, " % or less"),
paste0(min + inc, " %"),
paste0(min + 2 * inc, " %"),
paste0(min + 3 * inc, " %"),
paste0(max, " % or more"))
legend("bottomleft",
legend = legend.text,
fill = shades[c(1, 25, 50, 75, 100)],
title = legend.title)
}
percent_map(counties$white, "darkgreen", "% white")
shiny::runApp()
shiny::runApp()
shiny::runApp()
shiny::runApp()
runApp("stockVis")
getwd()
runApp("stockVis")
library(shiny)
runApp("stockVis")