/
cv.models.rmd
639 lines (507 loc) · 26.8 KB
/
cv.models.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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
---
title: "Quick Start Guide for cv.models"
author: "Michio Oguro"
date: "`r Sys.Date()`"
output:
html_document:
fig_width: 6
fig_height: 6
fig_retina: 2
dpi: 300
dev.args: list(dpi = 300)
toc: true
toc_depth: 2
md_extensions: -ascii_identifiers
vignette: >
%\VignetteIndexEntry{Quick start guide for cv.models}
%\VignetteEngine{knitr::rmarkdown}
\usepackage[utf8]{inputenc}
---
[日本語](cv.models.j.html)
```{R, preparation, echo = FALSE, message = FALSE, include = FALSE}
library(cv.models)
library(gbm)
set.seed(12345)
```
# Introduction
This package provides simple interface for cross validation and hyper-parameter tuning for statistical/machine learning models.
Currently, I'm designing specification of APIs and object therefore they can be changed in future version.
If you find bugs or if you have any requests, please let me know by [GitHub](https://github.com/Marchen/cv.models) or email: paste following code into R to get my address: rawToChar(as.raw(c(109, 111, 103, 64, 102, 102, 112, 114, 105, 46, 97, 102, 102, 114, 99, 46, 103, 111, 46, 106, 112))).
# Installation
To install the package, copy & paste following script into R console.
Currently, it may produce many warnings related with compilation of documents, but it does not affect any functionality of the package.
```{R, install, eval = FALSE}
install.packages(
c("R6", "model.adapter", "cv.models"), type = "source",
repos = c(
"http://florivory.net/R/repos", options()$repos
)
)
```
# Basic usage
## Evaluation of predictive ability of a model by cross-validation
`cv.models` function evaluate predictive ability of a model.
Most basic usage of the `cv.models` function is putting call of statistical/machine learning model function **directory** into `cv.models` as follow.
```{R, basic_usage}
library(cv.models)
data(iris)
cv <- cv.models(glm(Petal.Length ~ ., data = iris))
print(cv)
```
In the call of statistical/machine learning models, please use `data` argument for specifying data like `glm(Petal.Length ~ Petal.Width, data = iris)`.
Currently, model call with `$` operator such as `glm(iris$Petal.Length ~ iris$Petal.Width)` can not work.
If someone needs to use latter case, please contact me.
Users can use `cv.models` by passing `call` object for statistical/machine learning models.
```{R, basic_usage2}
# Using substitute() function
call.glm <- substitute(glm(Petal.Length ~ ., data = iris))
cv <- cv.models(call.glm)
print(cv)
# Using call() function
call.glm <- call("glm", Petal.Length ~ ., data = iris)
cv <- cv.models(call.glm)
print(cv)
```
Objects of some statistical/machine learning models can be used for cross-validation.
Only model objects keeping original `call` such as `glm` or `gbm` can be used for `cv.models`.
```{R, wrong_usage}
model <- glm(Petal.Length ~ ., data = iris)
cv <- cv.models(model)
print(cv)
```
## Metrics calculated for model evaluation
`cv.models` automatically calculates appropriate metrics of model evaluation by detecting type of the model (i.e., regression or classification models).
Currently `cv.models` calculates following metrics.
### Regression model
Name of metrics |Column |Definition/Explanation
----------------------------------|-----------|---------------------------------------------------------------------------------------
**Mean squared error (MSE)** |"mse" |$MSE = mean((prediction - response) ^ 2)$
**Root mean squared error (RMSE)**|"rmse" |$RMSE = sqrt(mean((prediction - response) ^ 2))$
**R squared** |"r.squared"|$R ^ 2 = cor(prediction, response) ^ 2$<br>**This is calculated only for referencing purpose.** I don't recommend this metrics because adjustment of intercept and slope is done during calculation. For example, idel models produce prediction on a line y = x when plotting actual and predicted values. However, when plotted points of actual and predicted values are completely on a line of y = -2x + 1 (in other words, completely wrong prediction model), value of R squared become 1.
**Spearman's ρ** |"spearman" |$\rho = cor(prediction, response, method = "spearman")$
**Kendall's τ** |"kendall" |$\tau = cor(prediction, response, method = "kendall")$
**Q squared** |"q.squared"|$Q ^ 2 = 1 - \sum((prediction - response) ^ 2) / \sum((response - mean(response)) ^ 2)$, <br>See Consonni et al. (2009).<br>Currently, mean values of each fold were used for mean of data ($mean(response)$) by default. On the other hand, mean value of all data is used when `aggregate.method = "join"` (see below) is specified. I will consider to find better default for improving consistency.
### Classification models
Name of metrics |Column |Definition/Explanation
------------------------------------------|--------------|---------------------------------------------------------------------------
**Optimal threshold** |"threshold" |Optimal threshold separating presence/absence. By default, determined by Youden's J.
**Specificity** |"specificity" |[See Wikipedia](https://en.wikipedia.org/wiki/Sensitivity_and_specificity)
**Sensitivity** |"sensitivity" |
**Accuracy** |"accuracy" |
**True negative count (TN)** |"tn" |
**True positive count (TP)** |"tp" |
**False negative count (FN)** |"fn" |
**False positive count (FP)** |"fp" |
**Negative predictive value (NPV)** |"npv" |
**Positive predictive value (PPV)** |"ppv" |
**Diagnostic likelihood ratio (DRL)+** |"dlr.positive"|$DRL.positive = Sensitivity / (1 - Specificity)$, <br>See Lopez-Raton et al. (2014)
**Diagnostic likelihood ratio (DRL)-** |"dlr.negative"|$DRL.negative = (1 - Sensitivity) / Specificity$, <br>See Lopez-Raton et al. (2014)
**Matthews correlation coefficient (MCC)**|"mcc" |
**Informedness** |"informedness"|$Informedness = Sensitivity + Specificity - 1$
**Markedness** |"markedness" |$Markedness = PPV + NPV - 1$
**Log-likelihood** |"loglik" |$Log_likelihood = \sum(log(p * y + (1 - p) * (1 - y)))$, <br>See Lawson et al. (2014)
**Likelihood based R squared** |"rsq.loglik" |See Lawson et al. (2014)
**Cohen's Kappa** |"kappa" |See Cohen (1960)
## Using models requiring additional arguments for predict method
For some models like `gbm` requires additional argument(s) for `predict` method which can affect predictive ability.
To use such models with `cv.models`, specify the argument(s) as additional argument(s) of `cv.models`.
```{R, predict_args}
library(gbm)
cv <- cv.models(
gbm(Petal.Length ~ ., data = iris, distribution = "gaussian", n.cores = 1),
n.trees = 50
)
print(cv)
```
## Controlling positive class
By default, `cv.models` determines positive class of the response variable by following rules.
1. If the response variable is numeric, use `1` as the positive class.
2. If the response variable is logical, use `TRUE` as the positive class.
3. If the response variable is factor, use the first level (i.e., `levels(response)[1]`) as the positive class.
4. If the response variable is character, use the first unique element (i.e., `unique(response)[1]`) as the positive class.
When using factor or character as the response variable of the model, you can use `positive.class` argument to control which class is used for positive class.
Classes which are not treated as positive class are treated as negative class.
In the following example, `cv.models` treat versicolor in the `iris` data as the positive class, and setosa and virginica are treated as the negative class.
```{R, positive_class}
cv <- cv.models(
gbm(Species ~ ., data = iris, n.trees = 50, distribution = "multinomial"),
n.cores = 1, n.trees = 50, positive.class = "versicolor"
)
print(cv)
```
## Controlling calculation of metrics
By default, `cv.models` calculates performance metrics for each fold and then averages them.
But this method can't work for Leave-One-Out cross validation (LOOCV) or data with class imbalance in the response variable.
I'm not sure what is the best method handling this situation, but currently by specifying `aggregate.method = "join"`, `cv.models` calculate metrics after joining results from all folds.
```{R, aggregate_method}
cv <- cv.models(
gbm(Petal.Length ~ ., data = iris, distribution = "gaussian", n.cores = 1),
aggregate.method = "join", n.trees = 50
)
print(cv)
```
In this situation, SD of each metrics become NA.
## Use Class Stratification
When class imbalance exists in the response variable, random dividing of dataset into folds can create fold(s) without observation with specific class and can disrupt cross validation.
Class stratification (a method to divide dataset into folds with keeping frequency of classes) can help this situation.
To use class stratification, specify `stratify = TRUE` in `cv.models`.
```{R, class_stratification}
library(randomForest)
# Create dataset with small frequency of setosa.
test.data <- subset(iris, Species != "setosa")
test.data <- rbind(test.data, head(subset(iris, Species == "setosa"), 10))
# Cross validation.
cv <- cv.models(
randomForest(Species ~ ., data = iris, n.cores = 1),
aggregate.method = "join", n.trees = 50, stratify = TRUE
)
print(cv)
```
Currently, class stratification is implemented only for classification models.
## Controlling method for optimal threshold determination
Previous version of `cv.models` used Youden's J with `coords` function in `pROC` package to find optimal threshold dividing positive and negative classes.
After ver. 0.1.0, `cv.models` uses `optimal.cutpoints` function in `OptimalCutpoints` package and this allows us to use other metrics for determining optimal threshold.
By default, `cv.models` still use Youden's J for determination of the threshold, but users can specify a `list` having options passed to `optimal.cutpoints` for the `cutpoint.options` argument to control method used for threshold determination.
In the following example, sensitivity, instead of Youden's J, is maximized by specifying `list(methods = "MaxSe")` for `cutpoint.options`.
```{R, cutoff_example1}
cv <- cv.models(
gbm(Species ~ ., data = iris, n.trees = 50, distribution = "multinomial"),
n.cores = 1, n.trees = 50, positive.class = "setosa",
cutpoint.options = list(methods = "MaxSe")
)
print(cv)
```
`optimal.cutpoints` can determine multiple thresholds simultaneously.
For example, by specifying `list(methods = c("MaxSe", "MaxSp"))` for `cutpoint.options`, `cv.models` can calculate two sets of metrics with the thresholds determined by sensitivity and specificity, yet they bring same results... :).
```{R, cutoff_example2}
cv <- cv.models(
gbm(Species ~ ., data = iris, n.trees = 50, distribution = "multinomial"),
n.cores = 1, n.trees = 50, positive.class = "setosa",
cutpoint.options = list(methods = c("MaxSe", "MaxSp"))
)
print(cv)
```
Users can specify `methods`, `op.prev` and `control` of `optimal.cutpoints` in the list used for `cutpoint.options` and other options are ignored.
For the details of `optimal.cutpoints` see the manual and reference (Lopez-Raton et al. 2014) of it.
## Changing number of folds
To change number of folds, change `folds` argument.
```{R, folds}
cv <- cv.models(
gbm(Petal.Length ~ ., data = iris, distribution = "gaussian", n.cores = 1),
n.trees = 50, folds = 5
)
print(cv)
```
## Fix random number
Because cross validation and some modeling methods use stochastic processes, `cv.models` returns slightly different results every time.
To fix the result, specify some number for `seed` argument.
When `seed` is set, `cv.models` returns same result independent of parallel computation.
```{R, set_seed}
# Results are different each time.
cv <- cv.models(
gbm(Petal.Length ~ ., data = iris, distribution = "gaussian", n.cores = 1),
n.trees = 50
)
print(cv)
cv <- cv.models(
gbm(Petal.Length ~ ., data = iris, distribution = "gaussian", n.cores = 1),
n.trees = 50
)
print(cv)
# If seed is set, same result is returned every time.
cv <- cv.models(
gbm(Petal.Length ~ ., data = iris, distribution = "gaussian", n.cores = 1),
n.trees = 50, seed = 12345
)
print(cv)
cv <- cv.models(
gbm(Petal.Length ~ ., data = iris, distribution = "gaussian", n.cores = 1),
n.trees = 50, seed = 12345
)
print(cv)
```
## Use user specified grouping for cross validation
By default, `cv.models` randomly separates data into several folds and runs cross validation.
However, when each observation are not independent each other (e.g., when dataset having some internal structure such as spatial autocorrelation), cross validation with random separation would bring optimistic estimates of model performance measures (Roberts et al. 2017).
In this situation, blocked cross validation would bring better results (Roberts et al. 2017) and groupings of datasets produced by other packages such as `blockCV` package (Valavi et al. 2018) can be used for cross validation.
To use user defined grouping rather than default random separation of dataset, specify a vector representing grouping of observations for `group` argument of `cv.models`.
For the `group` argument, a vector of `character`, `factor`, `integer` and `logical` having same length as the number of observation can be used.
Following example shows testing predictive ability of the model for different species using the `iris` data.
```{R, grouped_cross_validation}
cv <- cv.models(
randomForest(Petal.Length ~ ., data = iris, n.cores = 1, n.trees = 50),
n.cores = 1, group = iris$Species
)
print(cv)
```
## Control parallel processing
By default, `cv.models` uses all logical/physical cores.
To control number of cores (i.e., processes) used for cross validation, specify a number for `n.cores` argument.
```{R, n_cores}
# Cross validation with 2 cores.
cv <- cv.models(
gbm(Petal.Length ~ ., data = iris, distribution = "gaussian", n.cores = 1),
n.trees = 50, n.cores = 2
)
print(cv)
```
## Hyper-parameter selection
`cv.models` can conduct grid search to find which combination of hyper-parameters can bring model(s) with best performance using cross validation.
To conduct grid search for hyper-parameters, supply a named list having vector of candidate parameters for `grid` argument.
In the following example, `cv.models` calculates performance measures with cross validation for all combination of hyper-parameters (in this example, `1` and `5` for `interaction.depth` and `1` and `10` for `n.minobsinnode`) of `gbm`
```{R, hyperparameter1}
# See the effect of hyper parameter on predictive ability of the model.
cv <- cv.models(
gbm(Petal.Length ~ ., data = iris, distribution = "gaussian", n.cores = 1),
n.trees = 50,
grid = list(interaction.depth = c(1, 5), n.minobsinnode = c(1, 10))
)
print(cv)
```
For models having extra argument(s) for `predict` method affecting predictive ability (e.g., `n.tree` argument of `gbm`), users can specify a list of candidate parameter(s) for `grid.predict` to conduct grid search for such parameter(s).
```{R, hyperparmeter2}
# See the effect of hyper parameter on predictive ability of the model.
cv <- cv.models(
gbm(Petal.Length ~ ., data = iris, distribution = "gaussian", n.cores = 1),
grid = list(interaction.depth = c(1, 5), n.minobsinnode = c(1, 10)),
grid.predict = list(n.trees = c(10, 50, 80))
)
print(cv)
```
## Extract model(s) with best predictive ability
After grid search, you can extract model(s) with best predictive ability using `find.best.models` function for the result of `cv.models`.
In the following example, `find.best.models` function extracts model(s) with the highest value of Q^2.
```{R, bestmodel}
# See the effect of hyper parameter on predictive ability of the model.
cv <- cv.models(
gbm(Petal.Length ~ ., data = iris, distribution = "gaussian", n.cores = 1),
grid = list(interaction.depth = c(1, 5), n.minobsinnode = c(1, 10)),
grid.predict = list(n.trees = c(10, 50, 80))
)
print(cv)
# Get best model.
best <- find.best.models(cv, "q.squared")
print(best)
```
The result of `find.best.models` function is object of `cv.best.models`, which is a list of `cv.result` object.
```{R, bestmodel2}
# See the class of the result of find.best.models().
class(best)
# The results of find.best.models() is a list of cv.result object.
class(best[[1]])
print(best[[1]])
```
## Extracting data from cv.models object
Using following functions, you can extract information from the result of `cv.models`.
```{R, extract_data}
cv <- cv.models(
gbm(Petal.Length ~ ., data = iris, distribution = "gaussian", n.cores = 1),
grid = list(interaction.depth = c(1, 5), n.minobsinnode = c(1, 10)),
grid.predict = list(n.trees = c(10, 50, 80))
)
# Extract prediction of the 10th model.
# The result is a data.frame having three columns:
# "response" having original values of response variable,
# "prediction" having prediction of the model,
# "index" having index of response variable in the original dataset.
fit <- extract.fit(cv, 10)
head(fit)
# Extract the detailed information of 10th model.
# The result is an object of cv.result.
extract.result(cv, 10)
# Extract table of performance metrics.
extract.metrics(cv)
```
The `extract.fit` function can be used for `cv.best.models` and `cv.result` objects.
```{R, extract_data2}
# Extract prediction from the first model of best models.
best <- find.best.models(cv, "q.squared")
fit <- extract.fit(best, 1)
head(fit)
# Extract the 10th model as a cv.result object.
result <- extract.result(cv, 10)
# Extract prediction of the 10th model.
fit <- extract.fit(result)
head(fit)
```
## Plotting
You can use `plot` function to show relationship between predicted and actual values.
Each point represents pair of prediction and actual values, and the line represents $Y = X$.
Currently, plotting can't work with classification model.
```{R, plot}
# Plot predicted and actual values obtained from lm model.
cv <- cv.models(
lm(Petal.Length ~ ., data = iris)
)
plot(cv)
# See the effect of hyper parameter on predictive ability of the model.
cv <- cv.models(
gbm(
Petal.Length ~ ., data = iris, weights = iris$Sepal.Width,
distribution = "gaussian", n.cores = 1
),
grid = list(interaction.depth = c(1, 5), n.minobsinnode = c(1, 10)),
grid.predict = list(n.trees = c(10, 50, 80))
)
# Show the relationship between prediction and actual values in the 10th result.
plot(cv, 10)
```
# Technical details
to be continued...
# Model specific information
## gbm
### n.trees
When using `gbm`, please specify a larger or equal value of `n.trees` in `gbm` than the values used for `grid.predict`.
When a smaller value of `n.trees` is used in the call of `gbm` compared with values used for `grid.predict`, larger values of `n.trees` than the value specified in the call have no effect on the result.
```{R, gbm_wrong_ntree}
# gbm is called with n.trees = 10,
# but n.trees of 5, 10, 50 and 80 is used in grid.predict.
cv <- cv.models(
gbm(
Petal.Length ~ ., data = iris, weights = iris$Sepal.Width,
distribution = "gaussian", n.trees = 10
),
grid.predict = list(n.trees = c(5, 10, 50, 80))
)
# In this situation, values of n.trees more than the value specified in gbm (10)
# in grid.predict has no effect on predictive ability of the model and
# same results are returned.
print(cv)
```
### weights
When `formula` is defined outside of function call, and `weights` is specified following way, `cv.models` can't work with `gbm`.
```{R, gbm_weights_1, eval = FALSE}
# This can work.
cv <- cv.models(
gbm(
Petal.Length ~ ., data = iris, weights = iris$Sepal.Width,
distribution = "gaussian", n.trees = 10
),
n.trees = 10, n.cores = 1
)
# For unknown reason, this can't work.
f <- Petal.Length ~ .
cv <- cv.models(
gbm(
f, data = iris, weights = iris$Sepal.Width,
distribution = "gaussian", n.trees = 10
),
n.trees = 10, n.cores = 1
)
```
For a workaround, you can use `weights` by specifying without `iris$`.
```{R, gbm_weights_2}
f <- Petal.Length ~ .
cv <- cv.models(
gbm(
f, data = iris, weights = Sepal.Width,
distribution = "gaussian", n.trees = 10
),
n.trees = 10, n.cores = 1
)
```
## Funcitons using additional data (e.g., weights of gbm and glm)
Some functions use additional data such as `weights` argument of `glm` and `gbm`.
In this situation, function call produced by `call` can't be used for `cv.models`.
```{R, call_with_other_data_error, eval = FALSE}
# call object produced by call function with weights can't be used for cv.models.
call.gbm <- call(
"gbm", Petal.Length ~ ., data = iris, weights = iris$Sepal.Width,
distribution = "gaussian", n.trees = 10, n.cores = 1
)
# Error!
cv <- cv.models(call.gbm, n.trees = 10)
# Same for GLM.
call.glm <- call(
"glm", Petal.Length ~ ., data = iris, weights = iris$Sepal.Width
)
cv <- cv.models(call.glm)
```
For a workaround please use `substitute` function instead of `call` function.
Also, please use variable name without `iris$` for `weights`.
```{R, call_with_other_data_ok}
# Making call with substitute.
call.gbm <- substitute(
gbm(
Petal.Length ~ ., data = iris, weights = Sepal.Width,
distribution = "gaussian", n.trees = 10, n.cores = 1
)
)
# OK!
cv <- cv.models(call.gbm, n.trees = 10)
# Same for GLM.
call.glm <- substitute(
glm(Petal.Length ~ ., data = iris, weights = Sepal.Width)
)
cv <- cv.models(call.glm)
```
## Modelw with `offset` terms
`predict` methods of some models don't adjust offset terms. Therefore, `cv.models` adjusts effect of offset terms if:
* The model is a regression model.
* The model has only one offset term.
* The offset was pecified in the format of `formula = y ~ x + offset(param)` or `offset = param`, rather than `formula = y ~ x + offset(data$param)` or `offset = data$param`.
* The prediction are calculated in response scale.
* The model is not `glmmML` and `ranger`
When adjusting offset for models with link function, `cv.models` assumes that offset values in the `data` is response scale and transformed in the model call as follows.
**When pre-transformed data is specified for offset term, the adjustment doesn't work correctly. Note that because `cv.models` can't know transformation of data, it can't produce warning or error messages.**
```{R}
cv <- cv.models(
glm(Petal.Length ~ Sepal.Width + offset(log(Petal.Width)), data = iris)
)
```
### `ranger::ranger`
It's not clear whether `predict` method of `ranger::ranger` adjusts offset terms or not. Therefore, currently `cv.models` doesn't adjust offset terms for `ranger` models.
### `glmmML::glmmML`
`glmmML` function in `glmmML` package doesn't provide `predict` method therefore `cv.models` implemented `predict` method for it. However, because the way of offset adjustment for `glmmML` is not clear, curent version of `cv.models` doesn't adjust offset terms for `glmmML`.
## Models with random effect
Currently, `cv.models` use population-level prediction (i.e., prediction ignoring random effect) for performance evaluation.
# Known issues
* **To many kinds of complicated objects**
The functions in this package create `cv.models`, `cv.best.models` and `cv.result` objects.
This may be refactored in the future version.
* **Almost no tests!**
Test codes for the package is still in development.
Some function may not be working correctly.
* **Always AUC become more than 0.5**
~~When the model is worse than random prediction, i.e., if it produce reversed prediction, pROC::coords switches positive and negative class when calculating AUC.~~
I'm not sure for the way of AUC calculation in OptimalCutpoints package.
* **Definition of MSE**
Some literature defined MSE as:
MSE = sum((response - predict) ^ 2) / (sample size - number of parameter)
This is different for the definition used for the one used in this package.
* **Best model selection with Diagnostic Likelihood Ratio**
Currently, `cv.models` considers models with higher Diagnostic Likelihood Ratio for better model, but I'm not confident this is OK. If you have some reference for the answer, please let me know.
# References
* Cohen, J. 1960. A Coefficient of Agreement for Nominal Scales. Educational and Psychological Measurement 20:37-46.
* Consonni, V., D. Ballabio, and R. Todeschini. 2009. Comments on the Definition of the Q2 Parameter for QSAR Validation. Journal of Chemical Information and Modeling 49:1669-1678.
* Lawson CR, Hodgson JA, Wilson RJ, et al. 2014. Prevalence, thresholds and the performance of presence-absence models. Methods in Ecology and Evolution 5:54-64.
* Lopez-Raton, M., M. X. Rodriguez-Alvarez, C. Cadarso-Suarez, and F. Gude-Sampedro. 2014. OptimalCutpoints: An R Package for Selecting Optimal Cutpoints in Diagnostic Tests. Journal of Statistical Software 61:36.
* Roberts, D. R., V. Bahn, S. Ciuti, M. S. Boyce, J. Elith, G. Guillera-Arroita, S. Hauenstein, J. J. Lahoz-Monfort, B. Schröder, W. Thuiller, D. I. Warton, B. A. Wintle, F. Hartig, and C. F. Dormann. 2017. Cross-validation strategies for data with temporal, spatial, hierarchical, or phylogenetic structure. Ecography 40:913-929.
* Valavi, R., J. Elith, J. J. Lahoz-Monfort, and G. Guillera-Arroita. 2018. blockCV: an R package for generating spatially or environmentally separated folds for k-fold cross-validation of species distribution models. Methods in Ecology and Evolution 00:1-8.
# Version history
* 2019-07-09: 0.1.4
* Add Spearman's rho and Kendall's tau as performance measures for regression models.
* Start continuous integration with Travis-CI.
* Minor bug fixes.
* 2018-11-10: 0.1.3
* Add `group` argument which supply user defined grouping for `cv.models`.
* 2018-09-14: 0.1.2
* Compatible with `model.adapter` package ver.0.1.0.
* Implement Class Stratification.
* 2018-01-31: 0.1.1
* Bug fix: `cv.models` does not run cross validation when called with `call()` function or model object.
* 2018-01-28: 0.1.0
* `find.best.models` uses seed of random number specified in `cv.models` to fix the result.
* Dependency on `pROC` package is replaced with `OptimalCutpoints` package and optimal threshold can be calculated other criteria than Youden's J. By this modification, `cv.models` doesn't calculate metrics such as `1-npv` and does calculate Diagnostic Likelihood Ratio.
* Implemented Cohen's Kappa for classification model.
* Bug fix: parallel processing produce different result even when seed of random number is specified.
* Bug fix: wrong message from print.cv.best.models().
* 2017-09-10: 0.0.4
* Experimental: add log-likelihood and likelihood based R^2 for metrics for classification models.
* Bug fix: wrong assignment of CPU cores.
* 2017-07-07: 0.0.3
* Bug fix: occasional failure for calculation.
* 2017-07-07: 0.0.2
* Bug fix: correspondence between explanatory and response variable was wrong.
* 2017-07-05: 0.0.1
* Start versioning.