-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathREADME.Rmd
142 lines (103 loc) · 3.93 KB
/
README.Rmd
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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
message = F,
warning = F,
error = F
)
```
# cohorts <a><img src='man/figures/cohorts_ppt.png' align="right" height="180" /></a>
<!-- badges: start -->
[![R-CMD-check](https://github.com/PeerChristensen/cohorts/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/PeerChristensen/cohorts/actions/workflows/R-CMD-check.yaml)
[![CRAN status](https://www.r-pkg.org/badges/version/cohorts)](https://CRAN.R-project.org/package=cohorts)
[![Downloads](https://cranlogs.r-pkg.org/badges/grand-total/cohorts)](https://cranlogs.r-pkg.org/badges/grand-total/cohorts)
<!-- badges: end -->
Creating cohort tables from event data is complicated and requires several lines of code.
The cohorts package lets users convert data frames to cohort tables in both long and wide formats with simple functions. Users may choose between day and month level cohorts.
## Installation
You can install the released version of cohorts from [CRAN](https://CRAN.R-project.org) with:
```{r install-1, eval=F}
install.packages("cohorts")
```
And the development version from [GitHub](https://github.com/) with:
```{r install-2,eval=F}
# install.packages("devtools")
devtools::install_github("PeerChristensen/cohorts")
```
## Creating a month level cohort table
In this example, we use a dataset consisting of customer IDs and invoice dates.
```{r online-cohorts}
library(cohorts)
head(online_cohorts)
```
We can then turn this into a cohort table where each customer ID is tracked from the first invoice month until the last month in the period.
```{r wide-cohort-month}
online_cohorts %>%
cohort_table_month(CustomerID, InvoiceDate)
```
## Creating a day level cohort table
If we need to track activity on a daily basis, we can instead use the `cohort_table_month()` function.
```{r wide-cohort-day}
gamelaunch %>%
cohort_table_day(userid, eventDate)
```
## Converting to percentages
In order to see the percent of remaining customers in subsequent periods, we can pipe the above code into the `cohort_table_pct()` function.
```{r}
gamelaunch %>%
cohort_table_day(userid, eventDate) %>%
cohort_table_pct(decimals = 1)
```
## Left-shifted cohort tables
Another option is to shift cohort tables left. Here, we align cohorts such that date columns are replaced by time periods, i.e. t0, t1, t2 etc.
To left-shift a cohort table, we can use the `shift_left()` function.
```{r}
gamelaunch %>%
cohort_table_day(userid, eventDate) %>%
shift_left()
```
We can also get the raw numbers as percentages.
```{r}
gamelaunch %>%
cohort_table_day(userid, eventDate) %>%
shift_left_pct()
```
## Line plots
To visualize the data, we can turn a cohort table into long format and create a line plot.
In this example, we select only the first seven cohorts.
```{r}
library(tidyverse)
gamelaunch_long <- gamelaunch %>%
cohort_table_day(userid, eventDate) %>%
shift_left_pct() %>%
pivot_longer(-cohort) %>%
mutate(time = as.numeric(str_remove(name,"t")))
gamelaunch_long %>%
filter(value > 0, cohort <= 7, time > 0) %>%
ggplot(aes(time, value, colour = factor(cohort), group = cohort)) +
geom_line(size = 1.5) +
geom_point(size = 1.5) +
theme_light()
```
## Cohort tables plotted
Another way to plot a cohort table is by means of tiles. In this case we provide the percentages and colour the tiles accordingly.
```{r}
gamelaunch_long %>%
filter(time > 0, value > 0) %>%
ggplot(aes(time, reorder(cohort, desc(cohort)))) +
geom_raster(aes(fill = log(value))) +
coord_equal(ratio = 1) +
geom_text(aes(label = glue::glue("{round(value,0)}%")), size = 2, color = "snow") +
scale_fill_gradient(guide = F) +
theme_minimal() +
theme(panel.grid = element_blank(),
panel.border = element_blank()) +
labs(y= "cohort")
```