-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.R
47 lines (39 loc) · 1.08 KB
/
utils.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
require(foreach)
require(doMC)
create.instances <- function(data, lag=7, testPercentage=0.2)
{
rows <- dim(data)[1]
nInst <- rows - lag
insts <- array(NA, c(nInst,lag))#vector("list", nInst)
res <- rep(NA, nInst)
for (i in 1:nInst)
{
insts[i,] <- data[i:(i+lag-1),]
res[i] <- data[(i+lag),]
}
nTests <- as.integer(nInst*testPercentage)
tests <- sample(1:nInst, nTests)
training <- setdiff(1:nInst, tests)
ret = list(insts=insts[training,], res=res[training], tInsts = insts[tests,], tRes=res[tests])
ret
}
cross.validation <- function(m, x, y, k=10)
{
index <- 1:nrow(x)
index <- sample(index,nrow(x))
foldSize <- nrow(x) / k
registerDoMC(cores=8)
errors <- foreach(i = 1:k, .combine='c') %dopar% {
model <- clone(m)
testSet <- index[(1+(k-1)*foldSize):(k*foldSize)]
trainSet <- setdiff(index,testSet)
if(class(model) == "adaboostr")
train(model,x[trainSet,],y[trainSet])
else # mlp
sapply(trainSet, function(j) train(model,x[j,], y[j]))
yhat <- sapply(testSet, function(j) predict(model, x[j,]))
diff <- y[testSet] - yhat
mean(diff^2)
}
errors
}