library(caret)
library(MASS)
library(glmnet)
data=as.matrix(oadata2)
colnames(data)=c('y','x1','x2','x17',paste("x",3:16,sep=""))
c=10 #验证c次
set.seed(5000)
r3=matrix(0,c,1)
a=matrix(0,c,2)
merror=matrix(0,c,1)
pea=matrix(0,c,1)
for (k in 1:c) {
# index1=createDataPartition(data[,1], time=1, p=0.8, list=F) #分成3份
# train1=data[index1, ] #train1=train集
# train2=data[-index1, ]
# index2=createDataPartition(train2[,1], time=1, p=0.5, list=F)
# train3=data[index2, ] #train3=调整参数train集
# test=data[-index2, ] #test集
# colnames(train1)=c('y','x1','x2','x17',paste("x",3:16,sep=""))
# colnames(train3)=c('y','x1','x2','x17',paste("x",3:16,sep=""))
# colnames(test)=c('y','x1','x2','x17',paste("x",3:16,sep=""))
index1=createDataPartition(data[,1], time=1, p=0.8, list=F) #分成2份
train=data[index1, ] #train1=train集
test=data[-index1, ]
colnames(train)=c('y','x1','x2','x17',paste("x",3:16,sep=""))
colnames(test)=c('y','x1','x2','x17',paste("x",3:16,sep=""))
# CV for 11 alpha value,取alpha的值
p=10 #alpha精度为1/p
mse=matrix(0,p,1)
for (i in 0:p) {
li=cv.glmnet(x=train[,2:18],y=as.factor(train[,1]), family="multinomial", alpha=i/p,
intercept=FALSE, type.measure="mse")
yhat=predict(li, s=li$lambda.1se, newx=train[,2:18],type="response")
mse[i]=mean((train[,1]-yhat)^2)
}
a[k,1]=which.min(mse) #取mse最小的alpha对应的第几次
a[k,2]=min(mse)
#logit, Elastic Net, alpha=k/10
l=cv.glmnet(x=test[,2:18],y=as.factor(test[,1]), family="multinomial",alpha=a[k,1]/p,
intercept=FALSE, type.measure="mse")
beta=coef(l,s=l$lambda.min) #确定的系数
l.ytest=rowSums(predict(l,s=l$lambda.min,newx=test[,2:18],type="response")*c(0,1,2,3,4))
merror[k,1]=mean((l.ytest-test[,1])^2)
r3[k]=(length(l.ytest)-length(which(l.ytest!=test[,1])))/length(l.ytest)
}
mean(r3) #平均分类误差
al=a[which.min(a[,2]),1]
lf=cv.glmnet(x=data[,2:18],y=as.factor(data[,1]), family="multinomial",alpha=al/p,
intercept=FALSE, type.measure="mse")
beta=coef(l,s=lf$lambda.min)#最终系数
library("kernlab")
#svm,kernel=RBF
s=ksvm(train[,2:18],as.factor(train[,1]),kernel = "rbfdot")
s.ytest=predict(s,test[,2:18],type = "decision")
s.mse=mean((s.ytest-test[,1])^2)
print(s.mse)
k =function(x, y) {
(sum(test[,2:18]*test[,1]) + 1) * exp(0.01 * sum((test[,2:18]-test[,1])^2))
}
class(k) ="kernel"
data("promotergene")
gene <- ksvm(test[,2:18],as.factor(test[,1]), kernel = k, C = 1, cross = 5)
gene
library(GPfit)
library(lhs)
g=GP_fit(train[,2:18], train[,1])
gpre=predict.GP(g,test[,1])
g.ytest = gpre$Y_hat;
g.mse = gpre$MSE;
g.completedata =gpre$complete_data;