-
Notifications
You must be signed in to change notification settings - Fork 3
/
README.Rmd
260 lines (170 loc) · 6.55 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
---
output:
md_document:
variant: markdown_github
---
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
# The 'datzen' package for R
The **datzen** package provides three categories of commonly used miscellaneous functions to achieve Data Zen
`٩(^ᴗ^)۶`
* Themed Branding
* Special Computations
* Data In/Out
The goal of this package is to provide data analysts a few tools to rapidly prototype analysis scripts.
```{r message=FALSE}
# devtools::install_github('mikejacktzen/datzen',force=TRUE)
library(datzen)
library(dplyr)
```
## Themed Branding
### I forgot about the weird base R name of that link function used in logistic regression.
Uhhh, it was what's his face! The one with the logis!
Oh, you mean `logit`?
```{r}
# ?logit
identical(logit(0.5),stats::qlogis(0.5))
xx=seq(from=0.01,to=0.99,by=0.01);txtplot::txtplot(x=xx,y=logit(xx))
```
I forgot about the weird name of its inverse function too!
Oh yeah... his ugly cousin, plogis.
I know him as `expit`.
```{r}
# ?expit
identical(expit(10),stats::plogis(10))
xxx=seq(from=-10,to=10,by=1);txtplot::txtplot(x=xxx,y=expit(xxx))
```
### Ever wanted a random string of garbage?
I'm too much of a homo-sapien, my phrases are never garbled enough (I blame my parents). I wish I could be more like a computer, or a homo-erectus.
```{r}
dats_wat_she_said = 10
junk = garble(size_out=dats_wat_she_said)
junk
```
### Is your workspace environment too clutered?
It was like that when I got here.
```{r}
jimmy = 'johns'
nuts = 'planters'
ls()
```
I want to remove **All But Deeze** ...
```{r}
rmabd('nuts')
ls()
```
## Special Computations
### Here you go, have this `lm` object I made. Bye!
Oh yeah, call me when you reach peak MSE!
```{r}
model = lm(data = iris,
Sepal.Length ~ Species + as.numeric(Species) +
Species:Sepal.Width + as.factor(Sepal.Width) +
as.factor(Sepal.Width)*as.factor(Petal.Length) +
as.numeric(Species)*as.factor(Petal.Length) +
poly(Sepal.Width,degree = 2))
```
What's in it? What'd you do to it? Does it have cooties?
```{r}
class_df_from_term(model,class_post_formula = FALSE)
```
Did you hear? That `data.frame` standing over there started hanging around with the wrong crowd. Boy [I tell you hwat](https://www.youtube.com/watch?v=p-JgNTvTA4E&feature=youtu.be), `lm` totally changed him.
```{r}
class_df_from_term(model,class_post_formula = TRUE)
```
What happened `Petal.Length` ? You used to be cool.
Since you met up with `lm` and started interacting with `Species` you think you're too much of a `factor` for your old crew, the `numeric` types, huh?
Get out of my face!
## Data In/Out
### Ever wanted a generic simulated dataset to prototype modeling commands?
How did you know? I'm tired of using `data(iris)` because the column names aren't boring enough. Turn up the boring! I just want something fast and generic to swap out later.
```{r}
simlm(p=7,n=5,output_meta=TRUE)
simlm(p=3,n=100,coef_true = c(69,23,7),output_meta=FALSE) %>% lm(data=., y ~ -1+.)
```
### Ever wanted to read in 5 random rows of some physical spreadsheet?
Someone handed me this dumb csv with 100 columns. I don't even know what's in it!
```{r message=FALSE}
set.seed(1); m = matrix(rnorm(100*100),ncol=100,nrow=100)
csv = data.frame(m)
dim(csv)
tf <- tempfile()
write.csv(csv,tf,row.names=FALSE)
```
So... of course. I just want a taste of the data. Give me a taste!
I'll use the subset to prototype something that works. Later, I'll run it on the whole shebang.
```{r message=FALSE}
freadss(input=tf,ss=5) %>% dim
```
### The Glorious Franken-function `itersave()`
Ever wanted to just brute force some for loop? Aw poop, it crapped the bed during iteration 69. Now I have to manually restart it. I hope it doesn't do it again. I'm running out of patience, and linen.
```{r error=TRUE}
shaq = function(meatbag){
if(meatbag %in% 'scrub'){return('dunk on em')}
if(meatbag %in% 'sabonis'){return('elbow his face')}
if(!(meatbag %in% c('scrub','sabonis'))){
stop('shaq is confused')}
}
meatbags = c('scrub','sabonis','scrub','kobe')
names(meatbags) = paste0('arg_',seq_along(meatbags))
testthat::expect_failure(lapply(meatbags,FUN=shaq))
```
Uh, some error confused Shaq.
*enter, stage trap door*
"Meet `itersave()` "
*front row faints*
"It's... hideously beautiful"
In a nutshell, `itersave` works like `lapply` but when it meets an ugly, unskilled, unqualified, and ungraceful error it will keep trucking along like Shaquille The Diesel O'Neal hitchhiking a ride on Chris Dudley's [back](https://www.youtube.com/watch?v=0ICBi-ku-G0)
```{r message=FALSE}
mainDir=paste0(getwd(),'/tests/proto/')
subDir='/temp/'
# list.files(paste0(mainDir,subDir))
unlink(list.files(paste0(mainDir,subDir),full.names = TRUE),recursive=TRUE)
itersave(func_user=shaq,
vec_arg_func=meatbags,
mainDir,subDir)
```
The meatbags that Shaq successfully put into bodybags.
```{r}
print('the successes')
list.files(paste0(mainDir,subDir))
```
It'll also book keep any errors along the way via [purrr](http://purrr.tidyverse.org/)::`safely()` and [R.utils](https://cran.r-project.org/web/packages/R.utils/index.html)::`withTimeout()`.
```{r}
print('the failures')
list.files(paste0(mainDir,subDir,'/failed/'))
```
Along with the **out**, itersave has an **in** companion
*enter, zipline from balcony*
"meet `iterload()` "
*audience faints*
```{r message=FALSE}
iterload(paste0(mainDir,subDir,'/failed'))
```
Ah, it was the 4th argument, Kobe, that boggled Shaq's mind.
Hmm, Shaq wisened up in Miami. He also fattened up in Phoenix, Cleveland, Boston, Hawaii, Catalina, etc.
```{r message=FALSE}
shaq_wiser = function(meatbag){
if(meatbag %in% 'scrub'){return('dunk on em')}
if(meatbag %in% 'sabonis'){return('elbow his face')}
if(meatbag %in% 'kobe'){return('breakup & makeup')}
if(!(meatbag %in% c('scrub','sabonis','kobe'))){
stop('shaq is confused')}
}
itersave(func_user=shaq_wiser,
vec_arg_func=meatbags,
mainDir,subDir)
```
So, give me the whole shebang. What was the whole story of Shaq's road trip?
```{r message=FALSE}
out_il = iterload(paste0(mainDir,subDir))
cbind(meatbags,out_il)
```
By itself `purrr::map` with `purrr::safely` is great, but by design, it'll do everything in one shot (eg batch results). This is not ideal when working with stuff online.
For web data in the wild, expect the unexpected. You have non-homogeneous edge cases aplenty. These Chris Dudley looking edge cases are just waiting in the bushes for you.
Dunk thru them.