-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRvalPractice2.Rmd
More file actions
395 lines (249 loc) · 9.09 KB
/
RvalPractice2.Rmd
File metadata and controls
395 lines (249 loc) · 9.09 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
---
title: "Automated report example"
author: "Author"
date: "March 2021"
output:
pdf_document: default
word_document: default
html_document: default
params:
year:
label: Year
value: 2017
input: slider
min: 2010
max: 2021
step: 1
sep: ''
data:
label: 'Input dataset:'
value: some_data.csv
input: file
---
\vspace{0.3cm}
```{r}
# the parameters in the Rmd-file header can be chosen interactively,
# by using "Knit with parameters"
# or directly, by running:
# rmarkdown::render("RvalPractice0.Rmd", params = list(
# year = 2021,
# file = "our_favourite.csv"
# ))
# Try this!
```
# Useful links:
- for downloading and installing R, RStudio on MacOSX, see the example at:
https://web.stanford.edu/~kjytay/courses/stats32-aut2018/Session%201/Installation%20for%20Mac.html
- for using open data from the web:
https://theodi.org/article/how-to-use-r-to-access-data-on-the-web/
(_hint_: use functions like read.csv(), read.url(), various API's or the RCurl-package )
# Important notes:
- We may all raise issues in the github repository
https://github.com/violetacln/learnRval
about any questions, proposals, ideas we like to share!
- We do as much as possible here, in our interactive meeting:
- we run the examples included in these RvalPractice - files
- we could also propose new ones and/or run code on different data.
# Introduction:
## Data-set and main information needed for validation
about: time-variables, modeled - variables, imputed-variables, if the case
Assume a data-frame has been built.
Assume rules set has been built.
Example of notations:
```{r datadef, echo=TRUE, eval=FALSE}
## data rules, built, read into a data frame and then a validator object
# vrules <-
## modifier rules, built, read into a data frame and then a validator object
# mrules <-
## main data set as a data.frame
#df <-
##data sets which need to be compared (could be same data measured at
##different moments in time), as data.frames
#df1<-
#df2<-
##time series we need to check, if it is ths case
#tsuniv
#tsmultiv
```
__Important note: when we want to create our report and show all results, we make "eval=TRUE" in the R-chunks which follow.__
# Part 2: Rules' validity and more input/output data analysis
We might like to first practice reading/writing _rules_ from/into files or dataframes, as explained in Part 2 pf the course slides.
```{r, eval=FALSE, echo=TRUE}
# create in R
v <- validator(year>2000, month==6)
# export rules into a file
export_yaml(v,file="my_rules.yaml") # check where it is!
# read rules from file
v0 <- validator(.file="my_rules.yaml")
# keep rules into a data frame
df_rules <- as.data.frame(v)
```
Check structure of this objects!
## Verifying consistency and properties of the rules themselves
```{r, eval=FALSE, echo=TRUE}
vrules <- validator( rule1 = price > 100
, rule2 = price < 100
)
validatetools::is_infeasible(vrules)
validatetools::detect_infeasible_rules(vrules)
validatetools::make_feasible(vrules)
validatetools::is_contradicted_by(vrules, "rule1")
```
Then let us try an imaginary trade data example:
```{r, eval=FALSE, echo=TRUE}
vrules <- validator( if (country == "IS") imported_value == 0
, product %in% c("bananas", "potatoes")
, if (product == "bananas" & country == "IS") imported_value !=0
)
validatetools::is_infeasible(vrules)
validatetools::detect_infeasible_rules(vrules)
validatetools::make_feasible(vrules)
validatetools::is_contradicted_by(vrules, "V1")
validatetools::simplify_rules(vrules)
```
Could we build more examples?
```{r, eval=FALSE, echo=TRUE}
vrules <- validator( happy >= 5, happy <=5)
validatetools::detect_fixed_variables(vrules)
validatetools::simplify_fixed_variables(vrules)
```
```{r, eval=FALSE, echo=TRUE}
vrules <- validator( r1 = if (product_type == "exported") export_value > 0
, r2 = product_type == "exported"
)
#
validatetools::simplify_conditional(vrules)
```
Also, we could try:
```{r, eval=FALSE, echo=TRUE}
vrules <- validator( rule1 = rain_chance > 40
, rule2 = rain_chance > 80
)
validatetools::detect_redundancy(vrules)
# rule1 is superfluous
validatetools::remove_redundancy(vrules)
```
Then we would like to locate the errors (which fields of which records) and impute "correct" values...
We could debate about the correctness as well!
We will do this in the next Rmd-practicing file (RvalPractice3.Rmd), which is optional but worth experimenting with.
Before that, we complete our data exploration.
Make eval=TRUE, if you want to obtain a nice report for your favorite data!
## Exploring input or output data
### Uni/Multivariate analysis
```{r , echo=TRUE, eval=FALSE}
# chose your favorite data set; df <- ggplot2::diamonds
# names of variables which are discrete and continuous
df <- ggplot2::diamonds
dnames <-names(DataExplorer::split_columns(df)$discrete)
cnames <- names(DataExplorer::split_columns(df)$continuous)
# marginal distributions: see Part 1.
# cumulative distribution functions of numerical variables
plots_cumulative <- lapply(cnames, FUN=function(var) {
ggplot2::ggplot(df, ggplot2::aes(.data[[var]])) +
ggplot2::stat_ecdf(geom = "point") +
ggplot2::xlab(var) +
ggplot2::ylab("cumulative prob")
}
)
plots_cumulative
# see warning about df[[var]]), which I replaced with .data[[var]]
```
### Variability in data
```{r variability, echo=TRUE, eval=FALSE}
df <- ggplot2::diamonds
dnames <- names(DataExplorer::split_columns(df)$discrete)
cnames <- names(DataExplorer::split_columns(df)$continuous)
#continuous variables ---
print("mean, sd, skewness, standardised kurtosis and
standardised 5th and 6th cumulants are
calculated for continuous variables,
by using the package SimMultiCorrData ")
c_res <- c("var1","", "", "", "", "", "")
var1 <- character()
for (var1 in cnames){
c_res <- cbind(c_res, c(var1,
SimMultiCorrData::calc_moments(df[[var1]])))
}
c_res
#continue: discrete variables ---
d_res <- c("var1","var2","","","", "")
var1 <- character()
var2 <- character()
for (var1 in dnames){
for (var2 in dnames)
{
d_res <- cbind(d_res,
c(var1, var2,
funModeling::infor_magic(input =
df[[var1]],target =df[[var2]])))
}
}
d_res
```
$en, mi, ig, gr$ are: maximum total entropy, mutual information/entropy,
information gain between input and target, information gain ratio
between input and target
### Outliers' detection
```{r outliers, echo=TRUE, eval=FALSE}
df <- ggplot2::diamonds
## qqplots of continuous variables
outliers_cont <- DataExplorer::plot_qq(df)
## boxplots by each discrete
outliers_by_Discretes <- lapply(dnames, FUN=function(varr) {
DataExplorer::plot_boxplot( df, by=varr ,
geom_boxplot_args = list("outlier.color"="red"))
}
)
## univariate limits, Tukey method
outliers_table_Tukey <-
knitr::kable(
lapply(cnames, FUN=function(x0) {
c(
x0,
funModeling::tukey_outlier(as.data.frame(df)[[x0]])
)
}
)
#, format="markdown"
, col.names = " ", caption="Interquartiles based: Tukey method"
)
outliers_table_Tukey
## univariate limits, using Hampel (median based)
outliers_table_Hampel <-
knitr::kable(
lapply(cnames, FUN=function(x0) {
c( x0, funModeling::hampel_outlier(df[[x0]]) )
}
)
#, format="markdown"
, col.names = " ", caption="Median based: Hampel"
)
outliers_table_Hampel
```
### Checking assumptions about data
```{r assumptions about data, echo=TRUE, eval=FALSE}
print("the assumption about data to be checked: distributional difference")
# check if df1 and df2 come from different distributions:
# use resampling methods and/or KL measure
# example
df1 = diamonds[1:501,]$price
df2= diamonds[1000:1500,]$price
## or we could get random samples
## or could compare other sets , like:
df1a <- diamonds[which(diamonds$color=="D"),]$price[1:100]
df2a <- diamonds[which(diamonds$color=="G"),]$price[1:100]
#continuous
library(LaplacesDemon)
kld <- LaplacesDemon::KLD(px=df1,py=df2)
kld
#or resampling based tests, using sm::density.compare
group.index <- rep( 1:2, c(length(df1), length(df2)) )
sm::sm.density.compare(c(px=df1,py=df2),
group = group.index, model = "equal")
## note that a plot is generated automatically by this function
group.index <- rep( 1:2, c(length(df1a), length(df2a)) )
sm::sm.density.compare(c(px=df1a,py=df2a),
group = group.index, model = "equal")
# may investigate other choices of data frames to be compared!
```
Do you know some other interesting methods/packages for such checks?