-
Notifications
You must be signed in to change notification settings - Fork 0
/
RvalPractice3.Rmd
350 lines (202 loc) · 7.21 KB
/
RvalPractice3.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
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
---
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 <-
# v <-
## main data set as a data.frame
#df <-
# data <-
##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 3: Take away exercises on: error correction, complex output validation, Bayesian validation
## Error Detection: more on error location
Which field values should we correct, on each record?
```{r error location, echo=TRUE, eval=FALSE}
library(errorlocate)
rules <- validator(price >500, carat > 0.2) # check summary!
data <- ggplot2::diamonds
error_locations <- locate_errors(data, rules)
head(values(error_locations))
summary(error_locations)
data_marked_errors <- replace_errors(data, rules)
# faulty data was replaced with NA
print(data_marked_errors)
er <- errors_removed(data_marked_errors)
#print(er)
summary(er)
#er$errors
sum(is.na(data))
sum(is.na(data_marked_errors)) # our values to be imputed!
```
## Error correction: apply modifier rules
```{r modifier rules, echo=TRUE, eval=FALSE}
library(dcmodify)
df <- ggplot2::diamonds
m <- modifier(if (price <= 500) price <- 500)
modified <- modify(df, m)
head(modified, 3)
```
## Error correction: modifier rules discovery option: association rules discovery
```{r association rules, echo=TRUE, eval=FALSE}
df <- ggplot2::diamonds[,2:4]
#(citation("arules"))
library(arules)
tdata <- as(df, "transactions")
#method 1 of clustering: eclat algorithm
eclat_res <- inspect(eclat(tdata,
parameter = list(supp=0.07, maxlen=15)))
eclat_plot <- itemFrequencyPlot(tdata, topN=10,
type="absolute", main="item freguency")
summary_data <- summary(tdata)
eclat_summary <- summary(eclat(tdata,
parameter = list(supp=0.07, maxlen=15)))
#method 2 of clustering: apriori algorithm
rules <- apriori(tdata)
apriori_summary <- summary(rules)
apriori_res <- inspect(rules)
```
## Error correction: imputation and adjustments
```{r , echo=TRUE, eval=FALSE}
library(simputation)
dat <- ggplot2::diamonds
dat[1:3,1] <- dat[3:7,2] <- NA
head(dat,10)
da1 <- impute_lm(dat, carat ~ clarity+depth+price)
head(da1,3)
da2 <- impute_median(da1, cut ~ x+y+z)
head(da2,3)
#chaining all these methods is possible, with %>% of magrittr - package
```
## Comparing data sets
```{r , echo=TRUE, eval=FALSE}
vrules <- validate::validator(price >500, carat > 0.2) # check summary!
input <- ggplot2::diamonds
m <- dcmodify::modifier(if (price <= 500) price <- 500)
cleaned <- dcmodify::modify(df, m)
comparison <- validate::compare(vrules
, input , cleaned
)
comparison
par(mfrow=c(2,1))
barplot(comparison)
plot(comparison)
```
Create more comparisons!
# Validation of results with data mining methods
## Identify clusters
```{r clusters, echo=TRUE, eval=FALSE}
df <- diamonds[1:2000,c("carat", "depth", "price")]
# find optimal number of clusters
dff <- scale(df)
### factoextra::fviz_nbclust(dff, kmeans, method = "gap_stat")
# compute and visualise
set.seed(123)
km.res <- kmeans(dff, 5, nstart = 25)
# visualize
factoextra::fviz_cluster(km.res, data = dff,
ellipse.type = "convex",
palette = "jco",
repel = TRUE,
ggtheme = ggplot2::theme_minimal())
# try to find an optimum!
# compare also with PAM clustering
# Compute PAM
pam.res <- cluster::pam(dff, 5)
# Visualize
factoextra::fviz_cluster(pam.res)
# try to find an optimum!
```
## Exploring and reviewing models
See the example in the slides of Part 3. Try new data!
```{r models, glm or ts: residuals and goodness of fit, echo=TRUE, eval=FALSE}
# if a glm type of model, then
#rev_model(model_a)
##if a time series model, then
##rev_model_ts(model_a)
```
## Exploring and reviewing time series characteristics
See the example in the slides of Part 3. Try new data!
```{r time series characteristics: univariate and multivariate, echo=TRUE, eval=FALSE}
#for all time series, univariate
#rev_ts_univ(tsuniv)
##for any multivariate time series
##rev_ts_multiv(tsmultiv)
```
# Alternative: Bayesian validation, simultaneous detection and correction of errors
Try this with different parameters and number of iterations.
Try new data!
```{r Bayesian validation, echo=TRUE, eval=FALSE}
library(EditImputeCont)
## read the toy example data, which has two ratio edits and a balance edit
data(SimpleEx)
data1 = readData(Y.original=SimpleEx$D.obs, ratio=SimpleEx$Ratio.edit,
range=NULL, balance=SimpleEx$Balance.edit)
## create and initialize the model with 15 DP mixture components
model1 = createModel(data.obj=data1, K=15)
## Run an iteration of MCMC
model1$Iterate()
dim(model1$Y.edited)
## [1] 1000 4
# Edit-imputed datasets of n=1000 records with p=4 variables
## Please see the example in the demo folder for more details
```