# GSE41169 (training dataset) x, GSE36064 (prediction dataset) x2. Outcome y "Age".
# Training- and prediction datasets must be in R matrix format.
# Simple toy-method to remove NA (should only be used for this example!)
for(i in 1:473731){
	temp<-x[,i]
	temp[which(is.na(temp))]<-median(temp, na.rm=T)
	x[,i]<-temp
}

# Set a seed so that the results can be reproduced
set.seed( 1 )

# Decide best suited alpha value, only 4-fold cross validation due to the small dataset
foldid=sample(1:4,size=length(y),replace=TRUE)
cv1=cv.glmnet(x=x,y=y,foldid=foldid,alpha=1)
cv075=cv.glmnet(x=x,y=y,foldid=foldid,alpha=.75)
cv05=cv.glmnet(x=x,y=y,foldid=foldid,alpha=.5)
cv025=cv.glmnet(x=x,y=y,foldid=foldid,alpha=.25)
cv0=cv.glmnet(x=x,y=y,foldid=foldid,alpha=0)

# Plot
par(mfrow=c(2,3))
plot(cv1);plot(cv075);plot(cv05);plot(cv025);plot(cv0)
plot(log(cv1$lambda),cv1$cvm,pch=19,col="red",xlab="log(Lambda)",ylab=cv1$name)
points(log(cv05$lambda),cv05$cvm,pch=19,col="grey")
points(log(cv0$lambda),cv0$cvm,pch=19,col="blue")
points(log(cv075$lambda),cv075$cvm,pch=19,col="orange")
points(log(cv025$lambda),cv025$cvm,pch=19,col="green")
legend("topleft",legend=c("alpha= 1", "alpha= .75", "alpha= .5", "alpha= .25", "alpha= 0"),pch=19,col=c("red", "orange", "grey", "green", "blue"))

# Make prediction with alpha=1 (Lasso), since that has the lowest MSE, for the lowest penalty term lambda (log(lambda))
# as well as the most parsimonious model
mod.cv=cv.glmnet(y=y, x=x, alpha=1, nfold=5)

# Check out estimated lambdas
# Minimum lambda
mod.cv$lambda.min
# Minimum lambda + 1 standard error ("One standard error rule")
mod.cv$lambda.1se

# Carry out respective predictions on methylation matrix x2
mypred1=predict(mod.cv, newx=x2, s=mod.cv$lambda.1se)
mypred2=predict(mod.cv, newx=x2, s=mod.cv$lambda.min)
