# \donttest{
set.seed(10)
###
#1. continuous y
###
n=200*2 #n=200 & 200 for training & test sets
x=matrix(rnorm(n*10),n,10) #10 predictors
z=matrix(rnorm(n*10),n,10) #10 biomarkers
xcut=median(x[,1])
subgr=1*(x[,1]=xcut) #2 subgroups
lp=rep(NA,n)
for(i in 1:n)
lp[i]=1+3*z[i,subgr[i]]
y=lp+rnorm(n,0,1)
idx.nex=sample(1:n,n*1/2,replace=FALSE)
ynew=y[idx.nex]
xnew=x[idx.nex,]
znew=z[idx.nex,]
y=y[-idx.nex]
x=x[-idx.nex,]
z=z[-idx.nex,]
fit1=tgml(y,x,z,ynew=ynew,xnew=xnew,znew=znew)
fit1$mse_new
plot(fit1$y_hat_new~ynew,ylab="Predicted y",xlab="ynew")
###
#2. binary y
###
x=matrix(rnorm(n*10),n,10) #10 predictors
z=matrix(rnorm(n*10),n,10) #10 biomarkers
xcut=median(x[,1])
subgr=1*(x[,1]=xcut) #2 subgroups
lp=rep(NA,n)
for(i in 1:n)
lp[i]=1+3*z[i,subgr[i]]
prob=1/(1+exp(-lp))
y=rbinom(n,1,prob)
y=as.factor(y)
idx.nex=sample(1:n,n*1/2,replace=FALSE)
ynew=y[idx.nex]
xnew=x[idx.nex,]
znew=z[idx.nex,]
y=y[-idx.nex]
x=x[-idx.nex,]
z=z[-idx.nex,]
fit2=tgml(y,x,z,ynew=ynew,xnew=xnew,znew=znew)
fit2$auc_new
plot(fit2$roc_new)
###
#3. add new ML models
# 1) write two functions:
# c_xx & c_xx_predict if y is continuous or
# b_xx & b_xx.predict if y is binary
# 2) update MLlist that includes xx, not c_xx nor b_xx.
# 3) run tgml using updated MLlist.
# The below is an example of adding ridge regression.
###
#3.1. ridge regression for continuous y.
c_ridge=function(y,x){
x=data.matrix(x)
suppressWarnings(try(fit<-glmnet::cv.glmnet(x,y,alpha=0),silent=TRUE))
return(fit)
}
c_ridge_predict=function(fit,xnew){
y.hat=rep(NA,nrow(xnew))
if(!is.null(fit)){
xnew=data.matrix(xnew)
y.hat=as.numeric(predict(fit,newx=xnew,s="lambda.min",type="response"))
}
return(y.hat)
}
#3.2. ridge regression for binary y.
b_ridge=function(y,x){
x=data.matrix(x)
fit=NULL
suppressWarnings(try(fit<-glmnet::cv.glmnet(x,y,alpha=1,family="binomial"),silent=TRUE))
return(fit)
}
b_ridge_predict=function(fit,xnew){
y.hat=rep(NA,nrow(xnew))
if(!is.null(fit)){
xnew=data.matrix(xnew)
y.hat=as.numeric(predict(fit,newx=xnew,s="lambda.min",type="response"))
}
return(y.hat)
}
#3.3. update MLlist
MLlist=c("lasso","ridge")
fit3=tgml(y,x,z,ynew=ynew,xnew=xnew,znew=znew,MLlist=MLlist)
fit3$auc_new
plot(fit3$roc_new)
# }
Run the code above in your browser using DataLab