-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpick.best.index.R
51 lines (47 loc) · 1.24 KB
/
pick.best.index.R
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
pick.best.index <- structure(function
### Minimizer for local models, described in article section 2.3
### "Picking the optimal model"
(err
### Vector of errors to minimize.
){
nparam <- length(err)
candidates <- which(err==min(err))
if(length(err)==1)return(candidates)
st <- abs(median(candidates)-candidates)
middle <- candidates[which.min(st)]
if(all(diff(err)==0))return(middle)
if(nparam %in% candidates && 1 %in% candidates){
cat("Warning: strange error profile, picking something near the center\n")
print(as.numeric(err))
d <- diff(candidates)>1
if(any(d)){
which(d)[1]
}else{
middle
}
}else if(1 %in% candidates){
max(candidates)
}else if(nparam %in% candidates){
min(candidates)
}else {
middle
}
### Integer index of the minimal error.
},ex=function(){
stopifnot(pick.best.index(rep(0,100))==50)
err <- rep(1,100)
err[5] <- 0
stopifnot(pick.best.index(err)==5)
## should pick the middle
err <- rep(1,100)
err[40:60] <- 0
stopifnot(pick.best.index(err)==50)
## should pick the biggest
err <- rep(1,100)
err[1:60] <- 0
stopifnot(pick.best.index(err)==60)
## should pick the smallest
err <- rep(1,100)
err[50:100] <- 0
stopifnot(pick.best.index(err)==50)
})