-
Notifications
You must be signed in to change notification settings - Fork 1
/
generic_data_generation.Rmd
206 lines (179 loc) · 4.53 KB
/
generic_data_generation.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
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
---
title: "Generic Data Generation"
author: "Gigi"
date: "`r Sys.Date()`"
output:
html_document:
toc: yes
---
```{r setup, include=FALSE}
rm( list = ls() )
knitr::opts_chunk$set(
echo = TRUE,
fig.height = 8,
fig.width = 11,
cache = FALSE
)
library( tidyverse )
source( 'lib.R' )
# source( 'utility.R' )
```
# Data Generation
The aim of this document is to help make data generation as easy as possible.
By using a set of generic functions hopefully it can be relatively easy to build up a complete data set from scratch.
## Setting initial parameters
```{r set_general_parameters}
```
## Building the initial table
Set any required variables and read in any tables that are required.
```{r set_tbl_options}
# Define basic column data types
col_types = cols(
out_value = col_character(),
odds = col_double()
)
```
```{r read_in_tbls}
# date related tables
years_odds_tbl <- read_csv(
'data_in/years.csv',
col_types = col_types
) %>%
calcCumulative
months_odds_tbl <- read_csv(
'data_in/months.csv',
col_types = col_types
) %>%
calcCumulative
year_month_adj_odds_tbl <- read_csv(
'data_in/year_month_override.csv',
col_types = col_types
) %>%
calcCumulative
# policy type information
policy_type_odds_tbl <- read_csv(
'data_in/policy_type.csv',
col_types = col_types
) %>%
calcCumulative
policy_type_2_odds_tbl <- read_csv(
'data_in/policy_type_level_2.csv',
col_types = col_types
) %>%
calcCumulative
policy_type_3_odds_tbl <- read_csv(
'data_in/policy_type_level_3.csv',
col_types = col_types
) %>%
calcCumulative
```
### Policies table
Policy table starting with the dates of the policy start.
```{r}
# initialise the table with years
first_tbl <- 10000 %>%
level1Values( years_odds_tbl, . ) %>%
# convert to a better data object
data.frame( year = . ) %>%
as.tibble %>%
# set the months
group_by( year ) %>%
mutate(
month = level1Values( months_odds_tbl, n() ),
month = level1AdjValues(
month,
year,
year_month_adj_odds_tbl,
n()
),
policy_type = level1Values(
policy_type_odds_tbl,
n()
)
) %>%
# assign a day for the policy to start
group_by( month ) %>%
mutate(
day = sample(
seq(
1,
months_odds_tbl %>%
filter( out_value == month[1] ) %>%
select( max_days ) %>%
.[[1]]
),
n(),
replace = TRUE
)
) %>%
# put the full policy date as a full string
ungroup %>%
mutate(
policy_date = as.Date(
paste0( year, '/', month, '/', day ),
"%Y/%m/%d"
)
) %>%
# second level policy type
group_by( policy_type ) %>%
mutate(
policy_type_level_2 = level2Values(
policy_type_2_odds_tbl,
policy_type,
n()
)
) %>%
# Add in third level policy type and policy reference
group_by( policy_type, policy_type_level_2 ) %>%
mutate(
policy_type_level_3 = level3Values(
policy_type_3_odds_tbl,
policy_type,
policy_type_level_2,
n(),
default = "0"
),
policy_code = plyr::mapvalues(
policy_type_level_3,
from = c( policy_type_3_odds_tbl$out_value, "0" ),
to = c( policy_type_3_odds_tbl$code, "ZZ" )
),
policy_ref = paste0(
policy_code,
str_pad( row_number( year ), 6, "left", "0" ),
sample( LETTERS, 1 ),
sample( 1:10, 1 )
)
) %>%
# Add in a premium amount and an incident count
ungroup %>%
mutate(
premium = numericValues(
n(),
type = "unif",
min = 5,
max = 15,
multiplier = 20,
round = 2
),
incidents = numericValues(
n(),
type = "chisq",
df = 4,
multiplier = 1/5
)
) %>%
# Remove intermediate columns
select( -policy_code )
glimpse( first_tbl )
```
Create the incidents table.
```{r incidents-tbl}
# Based off the incident counts create a base incidents table
second_tbl <- first_tbl[
rep( rownames( first_tbl ) %>% as.integer, first_tbl$incidents ),
] %>%
# Remove intermediate columns
select( -incidents )
glimpse( second_tbl )
```