Stat 133 Homework 7


In [27]:
library(DataComputing)
library(repr)

options(repr.plot.width=6.5, repr.plot.height=5)

In [4]:
Stations <- mosaic::read.file("http://tiny.cc/dcf/DC-Stations.csv")
data_site <- "http://tiny.cc/dcf/2014-Q4-Trips-History-Data-Small.rds" 
Trips <- readRDS(gzcon(url(data_site)))


Reading data with read.csv()

In [7]:
head(Trips)


Out[7]:
durationsdatesstationedateestationbikenoclient
3447580h 9m 15s2014-11-06 16:26:0015th & L St NW2014-11-06 16:35:0015th & L St NWW00169Registered
1132510h 47m 21s2014-10-12 11:30:003rd & D St SE2014-10-12 12:17:00Jefferson Dr & 14th St SWW01482Registered
6337562h 46m 22s2014-12-27 14:24:0010th & E St NW2014-12-27 17:10:0010th & E St NWW21346Casual
4668620h 15m 15s2014-11-23 16:42:004th & M St SW2014-11-23 16:57:005th & K St NWW00647Casual
4743320h 18m 33s2014-11-24 17:29:001st & Washington Hospital Center NW2014-11-24 17:47:00Columbus Circle / Union StationW21580Registered
5815970h 2m 36s2014-12-15 13:11:0011th & Kenyon St NW2014-12-15 13:14:00Park Rd & Holmead Pl NWW21286Registered

Time of the Day

1.


In [6]:
Trips %>% 
    ggplot(aes(x = sdate)) + 
    geom_density()


2.

This is certainly just a kernel density estimation for the variable sdate


In [9]:
Trips %>% 
    mutate(time_of_day = lubridate::hour(sdate) + lubridate::minute(sdate) / 60) %>%
    ggplot(aes(x=time_of_day)) +
        geom_density()


3.


In [19]:
Trips %>% 
    mutate(
        time_of_day = lubridate::hour(sdate) + lubridate::minute(sdate) / 60,
        day_of_week = lubridate::wday(sdate)
    ) %>%
    ggplot(aes(x=time_of_day)) +
        geom_density() +
        facet_wrap(~day_of_week)


4.


In [23]:
Trips %>% 
    mutate(
        time_of_day = lubridate::hour(sdate) + lubridate::minute(sdate) / 60,
        day_of_week = lubridate::wday(sdate)
    ) %>%
    ggplot(aes(x=time_of_day)) +
        geom_density(aes(fill=client), alpha=0.4, color=NA) +
        facet_wrap(~day_of_week)


5.


In [24]:
Trips %>% 
    mutate(
        time_of_day = lubridate::hour(sdate) + lubridate::minute(sdate) / 60,
        day_of_week = lubridate::wday(sdate)
    ) %>%
    ggplot(aes(x=time_of_day)) +
        geom_density(aes(fill=client), alpha=0.4, color=NA, position = position_stack()) +
        facet_wrap(~day_of_week)


The last one is better.

6.


In [29]:
Trips %>% 
    mutate(
        time_of_day = lubridate::hour(sdate) + lubridate::minute(sdate) / 60,
        wday = ifelse(lubridate::wday(sdate) %in% c(1,7), "weekend", "weekday")
    ) %>%
    ggplot(aes(x=time_of_day)) +
        geom_density(aes(fill=client), alpha=0.4, color=NA, position = position_stack()) +
        facet_wrap(~wday)



In [ ]: