[0aeb11]: / allFunctions.R

Download this file

49 lines (39 with data), 1.3 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
fitModel <- function(iter = 5, ...){
error = TRUE
i = 1
while ((error) && (i < 5)){
model <- try(train(...))
error <- class(model) == "try-error"
i = i + 1
}
if (i == iter) {
model = NA
warning("Algorithm did not converge. Returning NAs")
}
return(model)
}
predictModel <- function(model, newdata){
if (class(model) == "train"){
predicted = predict.train(model, newdata = newdata)
ts.acc = sum(test.cond == as.numeric(predicted)) / length(test.cond)
}
if (class(model) != "train"){
ts.acc = NA
}
return(ts.acc)
}
foldIndex <- function(data, nSamp = NULL, nFolds = 5, repeats = 2){
if(is.null(nSamp)) n = nrow(data)
else n = nSamp
indIn <- indOut <- list()
for (j in 1:repeats){
tmpIn = createFolds(1:n, k = nFolds, list = TRUE, returnTrain = TRUE)
tmpOut = lapply(tmpIn, function(x)c(1:n)[-x])
indIn = c(indIn, tmpIn)
indOut = c(indOut, tmpOut)
}
nms = paste(rep(paste("Fold", 1:nFolds, sep = ""), repeats),
rep(paste(".Rep", 1:repeats, sep = ""), c(rep(nFolds, repeats))), sep = "")
names(indIn) <- names(indOut) <- nms
return(list(indexIn = indIn, indexOut = indOut))
}