-
Notifications
You must be signed in to change notification settings - Fork 1
/
assess.R
69 lines (69 loc) · 2.41 KB
/
assess.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
CvSampling<- function(Nobs=1000,K=5){
#======================
# return sample row number: used to sampling the subset to cross-validation predition.
# rank<-CvSampling(Nobs=dim(data)[1],K=5)
# train=x[[1]]$train
# test=x[[1]]$test
#====================
rs <- runif(Nobs)
id <- seq(Nobs)[order(rs)]
k <- as.integer(Nobs*seq(1,K-1)/K)
k <- matrix(c(0,rep(k,each=2),Nobs),ncol=2,byrow=TRUE)
k[,1] <- k[,1]+1
l <- lapply(seq.int(K),function(x,k,d) list(train=d[!(seq(d) %in% seq(k[x,1],k[x,2]))], test=d[seq(k[x,1],k[x,2])]),k=k,d=id)
return(l)
}
chan<-function(x){
x[x<0.5]<-0; x[x>=0.5]<-1
x
}
assess<-function(model,train,test,trainlabel,testlabel){
t1<-table(chan(predict(model, train)), trainlabel)
t2<-table(chan(predict(model, test)), testlabel)
if(nrow(t1)==2 && nrow(t2)==2){
sen.train<-t1[2,2]/(t1[1,2]+t1[2,2])
spe.train<-t1[1,1]/(t1[2,1]+t1[1,1])
sen.test<-t2[2,2]/(t2[1,2]+t2[2,2])
spe.test<-t2[1,1]/(t2[2,1]+t2[1,1])
accu.train<-(t1[1,1]+t1[2,2])/sum(t1)
accu.test<-(t2[1,1]+t2[2,2])/sum(t2)
rlt<-c(sen.train,spe.train, accu.train,sen.test,spe.test,accu.test)
rlt
}
}
assess2<-function(model1,model2,trainlabel,testlabel){
t1<-table(model1,trainlabel)
t2<-table(model2,testlabel)
sen.train<-t1[2,2]/(t1[1,2]+t1[2,2])
spe.train<-t1[1,1]/(t1[2,1]+t1[1,1])
sen.test<-t2[2,2]/(t2[1,2]+t2[2,2])
spe.test<-t2[1,1]/(t2[2,1]+t2[1,1])
accu.train<-(t1[1,1]+t1[2,2])/sum(t1)
accu.test<-(t2[1,1]+t2[2,2])/sum(t2)
rlt<-c(sen.train,spe.train, accu.train,sen.test,spe.test,accu.test)
rlt
}
assess3<-function(model,train,test,trainlabel,testlabel){
t1<-table((predict(model, train)), trainlabel)
t2<-table((predict(model, test)), testlabel)
sen.train<-t1[2,2]/(t1[1,2]+t1[2,2])
spe.train<-t1[1,1]/(t1[2,1]+t1[1,1])
sen.test<-t2[2,2]/(t2[1,2]+t2[2,2])
spe.test<-t2[1,1]/(t2[2,1]+t2[1,1])
accu.train<-(t1[1,1]+t1[2,2])/sum(t1)
accu.test<-(t2[1,1]+t2[2,2])/sum(t2)
rlt<-c(sen.train,spe.train, accu.train,sen.test,spe.test,accu.test)
rlt
}
assess4<-function(model1,model2,trainlabel,testlabel){
t1<-table(chan(model1), trainlabel)
t2<-table(chan(model2), testlabel)
sen.train<-t1[2,2]/(t1[1,2]+t1[2,2])
spe.train<-t1[1,1]/(t1[2,1]+t1[1,1])
sen.test<-t2[2,2]/(t2[1,2]+t2[2,2])
spe.test<-t2[1,1]/(t2[2,1]+t2[1,1])
accu.train<-(t1[1,1]+t1[2,2])/sum(t1)
accu.test<-(t2[1,1]+t2[2,2])/sum(t2)
rlt<-c(sen.train,spe.train, accu.train,sen.test,spe.test,accu.test)
rlt
}