-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathREADME.Rmd
316 lines (239 loc) · 8.58 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
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
---
title: "wakefield"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
md_document:
toc: true
toc_depth: 4
---
```{r, echo=FALSE}
desc <- suppressWarnings(readLines("DESCRIPTION"))
regex <- "(^Version:\\s+)(\\d+\\.\\d+\\.\\d+)"
loc <- grep(regex, desc)
ver <- gsub(regex, "\\2", desc[loc])
library(pacman)
# verbadge <- sprintf('<a href="https://img.shields.io/badge/Version-%s-orange.svg"><img src="https://img.shields.io/badge/Version-%s-orange.svg" alt="Version"/></a></p>', ver, ver)
verbadge <- ''
p_load(dplyr, wakefield, knitr, tidyr, ggplot2)
````
```{r, echo=FALSE}
knit_hooks$set(htmlcap = function(before, options, envir) {
if(!before) {
paste('<p class="caption"><b><em>',options$htmlcap,"</em></b></p>",sep="")
}
})
knitr::opts_knit$set(self.contained = TRUE, cache = FALSE)
knitr::opts_chunk$set(fig.path = "tools/figure/")
```
[![Project Status: Active - The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/0.1.0/active.svg)](https://www.repostatus.org/#active)
[![Build Status](https://travis-ci.org/trinker/wakefield.svg?branch=master)](https://travis-ci.org/trinker/wakefield)
[![Coverage Status](https://s3.amazonaws.com/assets.coveralls.io/badges/coveralls_0.svg)](https://coveralls.io/github/trinker/wakefield)
[![DOI](https://zenodo.org/badge/5398/trinker/wakefield.svg)](https://dx.doi.org/10.5281/zenodo.17172)
[![](https://cranlogs.r-pkg.org/badges/wakefield)](https://cran.r-project.org/package=wakefield)
`r verbadge`
**wakefield** is designed to quickly generate random data sets. The user passes `n` (number of rows) and predefined vectors to the `r_data_frame` function to produce a `dplyr::tbl_df` object.
![](tools/wakefield_logo/r_wakefield.png)
# Installation
To download the development version of **wakefield**:
Download the [zip ball](https://github.com/trinker/wakefield/zipball/master) or [tar ball](https://github.com/trinker/wakefield/tarball/master), decompress and run `R CMD INSTALL` on it, or use the **pacman** package to install the development version:
```r
if (!require("pacman")) install.packages("pacman")
pacman::p_load_gh("trinker/wakefield")
pacman::p_load(dplyr, tidyr, ggplot2)
```
# Contact
You are welcome to:
* submit suggestions and bug-reports at: <https://github.com/trinker/wakefield/issues>
* send a pull request on: <https://github.com/trinker/wakefield/>
* compose a friendly e-mail to: <tyler.rinker@gmail.com>
# Demonstration
## Getting Started
The `r_data_frame` function (random data frame) takes `n` (the number of rows) and any number of variables (columns). These columns are typically produced from a **wakefield** variable function. Each of these variable functions has a pre-set behavior that produces a named vector of n length, allowing the user to lazily pass unnamed functions (optionally, without call parenthesis). The column name is hidden as a `varname` attribute. For example here we see the `race` variable function:
```{r}
race(n=10)
attributes(race(n=10))
```
When this variable is used inside of `r_data_frame` the `varname` is used as a column name. Additionally, the `n` argument is not set within variable functions but is set once in `r_data_frame`:
```{r}
r_data_frame(
n = 500,
race
)
```
The power of `r_data_frame` is apparent when we use many modular variable functions:
```{r}
r_data_frame(
n = 500,
id,
race,
age,
sex,
hour,
iq,
height,
died
)
```
There are `r length(variables())` **wakefield** based variable functions to chose from, spanning **R**'s various data types (see `?variables` for details).
```{r, results='asis', echo=FALSE, comment=NA, warning=FALSE, htmlcap="Available Variable Functions"}
p_load(pander, xtable)
variables("matrix", ncol=5) %>%
xtable() %>%
print(type = 'html', include.colnames = FALSE, include.rownames = FALSE,
html.table.attributes = '')
#matrix(c(sprintf("`%s`", vect), blanks), ncol=4) %>%
# pandoc.table(format = "markdown", caption = "Available variable functions.")
```
However, the user may also pass their own vector producing functions or vectors to `r_data_frame`. Those with an `n` argument can be set by `r_data_frame`:
```{r}
r_data_frame(
n = 500,
id,
Scoring = rnorm,
Smoker = valid,
race,
age,
sex,
hour,
iq,
height,
died
)
```
```{r}
r_data_frame(
n = 500,
id,
age, age, age,
grade, grade, grade
)
```
While passing variable functions to `r_data_frame` without call parenthesis is handy, the user may wish to set arguments. This can be done through call parenthesis as we do with `data.frame` or `dplyr::data_frame`:
```{r}
r_data_frame(
n = 500,
id,
Scoring = rnorm,
Smoker = valid,
`Reading(mins)` = rpois(lambda=20),
race,
age(x = 8:14),
sex,
hour,
iq,
height(mean=50, sd = 10),
died
)
```
## Random Missing Observations
Often data contains missing values. **wakefield** allows the user to add a proportion of missing values per column/vector via the `r_na` (random `NA`). This works nicely within a **dplyr**/**magrittr** `%>%` *then* pipeline:
```{r}
r_data_frame(
n = 30,
id,
race,
age,
sex,
hour,
iq,
height,
died,
Scoring = rnorm,
Smoker = valid
) %>%
r_na(prob=.4)
```
## Repeated Measures & Time Series
The `r_series` function allows the user to pass a single **wakefield** function and dictate how many columns (`j`) to produce.
```{r}
set.seed(10)
r_series(likert, j = 3, n=10)
```
Often the user wants a numeric score for Likert type columns and similar variables. For series with multiple factors the `as_integer` converts all columns to integer values. Additionally, we may want to specify column name prefixes. This can be accomplished via the variable function's `name` argument. Both of these features are demonstrated here.
```{r}
set.seed(10)
as_integer(r_series(likert, j = 5, n=10, name = "Item"))
```
`r_series` can be used within a `r_data_frame` as well.
```{r}
set.seed(10)
r_data_frame(n=100,
id,
age,
sex,
r_series(likert, 3, name = "Question")
)
```
```{r}
set.seed(10)
r_data_frame(n=100,
id,
age,
sex,
r_series(likert, 5, name = "Item", integer = TRUE)
)
```
### Related Series
The user can also create related series via the `relate` argument in `r_series`. It allows the user to specify the relationship between columns. `relate` may be a named list of \code{c("operation", "mean", "sd")} or a short hand string of the form of `"fM_sd"` where:
- `f` is one of (+, -, *, /)
- `M` is a mean value
- `sd` is a standard deviation of the mean value
For example you may use `relate = "*4_1"`. If `relate = NULL` no relationship is generated between columns. I will use the short hand string form here.
#### Some Examples With Variation
```{r}
r_series(grade, j = 5, n = 100, relate = "+1_6")
r_series(age, 5, 100, relate = "+5_0")
r_series(likert, 5, 100, name ="Item", relate = "-.5_.1")
r_series(grade, j = 5, n = 100, relate = "*1.05_.1")
```
#### Adjust Correlations
Use the `sd` command to adjust correlations.
```{r}
round(cor(r_series(grade, 8, 10, relate = "+1_2")), 2)
round(cor(r_series(grade, 8, 10, relate = "+1_0")), 2)
round(cor(r_series(grade, 8, 10, relate = "+1_20")), 2)
round(cor(r_series(grade, 8, 10, relate = "+15_20")), 2)
```
#### Visualize the Relationship
```{r, fig.height=7, fig.width=11}
dat <- r_data_frame(12,
name,
r_series(grade, 100, relate = "+1_6")
)
dat %>%
gather(Time, Grade, -c(Name)) %>%
mutate(Time = as.numeric(gsub("\\D", "", Time))) %>%
ggplot(aes(x = Time, y = Grade, color = Name, group = Name)) +
geom_line(size=.8) +
theme_bw()
```
## Expanded Dummy Coding
The user may wish to expand a `factor` into `j` dummy coded columns. The `r_dummy` function expands a factor into `j` columns and works similar to the `r_series` function. The user may wish to use the original factor name as the prefix to the `j` columns. Setting `prefix = TRUE` within `r_dummy` accomplishes this.
```{r}
set.seed(10)
r_data_frame(n=100,
id,
age,
r_dummy(sex, prefix = TRUE),
r_dummy(political)
)
```
## Visualizing Column Types
It is helpful to see the column types and `NA`s as a visualization. The `table_heat` (also the `plot` method assigned to `tbl_df` as well) can provide visual glimpse of data types and missing cells.
```{r, fig.height=7, fig.width=11}
set.seed(10)
r_data_frame(n=100,
id,
dob,
animal,
grade, grade,
death,
dummy,
grade_letter,
gender,
paragraph,
sentence
) %>%
r_na() %>%
plot(palette = "Set1")
```