-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathREADME.Rmd
136 lines (106 loc) · 5.48 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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# dammmdatagen <a href="https://doktormike.github.io/dammmdatagen/"><img src="man/figures/logo.png" align="right" height="139" /></a>
The goal of dammmdatagen is to make it easy for marketing mix modeling professionals to get access to realistic data sets where the ground truth is known. This fascilitates our development and provides in the end more value for all stakeholders of MMM.
## Build status etc
<!-- badges: start -->
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[![R-CMD-check](https://github.com/DoktorMike/dammmdatagen/workflows/R-CMD-check/badge.svg)](https://github.com/DoktorMike/dammmdatagen/actions)
[![Codecov test coverage](https://codecov.io/gh/DoktorMike/dammmdatagen/branch/master/graph/badge.svg)](https://app.codecov.io/gh/DoktorMike/dammmdatagen?branch=master)
<!-- badges: end -->
## Installation
You can install dammmdatagen from github with:
```{r gh-installation, eval = FALSE}
# install.packages("devtools")
devtools::install_github("DoktorMike/dammmdatagen")
```
## Quick start
This is a basic example which shows you how to generate a small 1 year data set.
```{r example, cache=TRUE}
# load useful libraries
library(dammmdatagen)
# generate a basic data set
mydf <- generateCovariatesData()
head(mydf)
```
Say that you would like to also generate a response variable to fit a model to. Then you could use the highlevel API function below.
```{r fullexample, cache=TRUE}
library(dammmdatagen)
library(ggplot2)
library(dplyr)
library(tidyr)
library(scales)
ret <- generateRetailData()
dates <- ret[["covariates"]][["Macro"]][["date"]]
qplot(dates, ret[["response"]]) + geom_line() + ylim(0, NA)
# entrytocolname <- function(x) a <- ret[["effects"]][[x]] %>% setNames(c(tolower(paste0(x, "_", names(.)))))
entrytocolname <- function(x) tibble::tibble(rowSums(ret[["effects"]][[x]])) %>% setNames(x)
Reduce(dplyr::bind_cols, lapply(names(ret[["effects"]]), entrytocolname)) %>%
dplyr::mutate(date = dates) %>%
tidyr::pivot_longer(-date, names_to = "variable", values_to = "value") %>%
ggplot2::ggplot(ggplot2::aes(x = date, y = value, fill = variable)) +
ggplot2::geom_bar(stat = "identity") + ggplot2::theme_minimal() +
ggplot2::ylab("Units sold") +
ggplot2::xlab("")
```
We can do a lot more of course! In this small snippet we'll generate 1 month worth of competitor media spendings data and plot that out.
```{r competitorspendplot, fig.width=10, message=FALSE, warning=FALSE, paged.print=FALSE, cache=TRUE}
library(dammmdatagen)
library(ggplot2)
library(dplyr)
library(tidyr)
library(scales)
generateCompetitorData(fromDate = Sys.Date() - 30, toDate = Sys.Date()) %>%
gather("competitor", "spend", -"date") %>%
ggplot(aes(y = spend, x = date, fill = competitor)) +
geom_bar(stat = "identity", position = position_stack()) +
theme_minimal() +
scale_y_continuous(labels = dollar_format(prefix = "kr. "))
```
Just as we can generate competitor spending data we can also generate macroeconomical data. These types of indicators are typically slow moving over time with minor temporal differences.
```{r macroecondataplot, fig.width=10, message=FALSE, warning=FALSE, paged.print=FALSE, cache=TRUE}
generateMacroData(fromDate = Sys.Date() - 30, toDate = Sys.Date()) %>%
gather("indicator", "value", -"date") %>%
ggplot(aes(y = value, x = date, color = indicator)) +
geom_line(size = 1.5) +
theme_minimal()
```
## Event type data
Event data are modeled as a poisson distribution with a low incidence.
```{r eventdata1, fig.width=10, message=FALSE, warning=FALSE, paged.print=FALSE, cache=TRUE}
generateEventData(Sys.Date() - 265, Sys.Date()) %>%
gather(type, value, -date) %>%
ggplot(aes(y = value, x = date, fill = type)) +
geom_bar(stat = "identity") +
theme_minimal()
```
The incidence can of course be controlled. This is done via the freq parameter.
```{r eventdata2, fig.width=10, message=FALSE, warning=FALSE, paged.print=FALSE, cache=TRUE}
generateEventData(Sys.Date() - 265, Sys.Date(), freq = 0.1) %>%
gather(type, value, -date) %>%
ggplot(aes(y = value, x = date, fill = type)) +
geom_bar(stat = "identity") +
theme_minimal()
```
## Media generation
Generating media is in general a bit more complicated as we need more information since in MMM models that's what we primarily care about. So we need three data.frames; the net, the impressions and the cpms. We also differentiate between offline and online media. This difference is rather artificial right now but it's to futureproof the package.
```{r onlineimpdata, fig.width=10, message=FALSE, warning=FALSE, paged.print=FALSE, cache=TRUE}
mydflist <- generateOnlineData(Sys.Date() - 30, Sys.Date())
mydflist[["impression"]] %>%
gather(type, impression, -date) %>%
ggplot(aes(y = impression, x = date, fill = type)) +
geom_bar(stat = "identity") +
theme_minimal()
```
## Code of Conduct
Please note that the dammmdatagen project is released with a [Contributor Code of Conduct](https://doktormike.github.io/dammmdatagen/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.