## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----echo=TRUE----------------------------------------------------------------
require(ptLasso)

## -----------------------------------------------------------------------------
set.seed(1234)

out = binomial.example.data()
x = out$x; y = out$y; groups = out$groups

outtest = binomial.example.data()
xtest = outtest$x; ytest = outtest$y; groupstest = outtest$groups

## -----------------------------------------------------------------------------
fit = ptLasso(x, y, groups, alpha = 0.5, family = "binomial")

predict(fit, xtest, groupstest, ytest = ytest)


## -----------------------------------------------------------------------------
fit = ptLasso(x, y, groups, alpha = 0.5, family = "binomial", 
              type.measure = "auc")

predict(fit, xtest, groupstest, ytest = ytest)

## -----------------------------------------------------------------------------
fit = ptLasso(x, y, groups, alpha = 0.5, family = "binomial", 
              type.measure = "auc", 
              en.alpha = .5)
predict(fit, xtest, groupstest, ytest = ytest)

## ----eval = FALSE-------------------------------------------------------------
# ##################################################
# # Fit:
# ##################################################
# fit = cv.ptLasso(x, y, groups, family = "binomial", type.measure = "auc")
# 
# ##################################################
# # Predict with a common alpha for all groups:
# ##################################################
# predict(fit, xtest, groupstest, ytest = ytest)
# 
# ##################################################
# # Predict with a different alpha for each group:
# ##################################################
# predict(fit, xtest, groupstest, ytest = ytest, alphatype = "varying")

## -----------------------------------------------------------------------------
require(survival)

## -----------------------------------------------------------------------------
set.seed(1234)

n = 600; ntrain = 300
p = 50
     
x = matrix(rnorm(n*p), n, p)
beta1 = c(rnorm(5), rep(0, p-5))

beta2 = runif(p) * beta1 # Shared support
beta2 = beta2 + c(rep(0, 5), rnorm(5), rep(0, p-10)) # Individual features

beta3 = runif(p) * beta1 # Shared support
beta3 = beta3 + c(rep(0, 10), rnorm(5), rep(0, p-15)) # Individual features

# Randomly split into groups
groups = sample(1:3, n, replace = TRUE)

# Compute survival times:
survival = x %*% beta1
survival[groups == 2] = x[groups == 2, ] %*% beta2
survival[groups == 3] = x[groups == 3, ] %*% beta3
survival = survival + rnorm(n)
survival = survival + 1.1 * abs(min(survival))

# Censoring times from a random uniform distribution:
censoring = runif(n, min = 1, max = 10)

# Did we observe surivival or censoring?
y = Surv(pmin(survival, censoring), survival <= censoring)

# Split into train and test:
xtest = x[-(1:300), ]
ytest = y[-(1:300), ]
groupstest = groups[-(1:300)]

x = x[1:300, ]
y = y[1:300, ]
groups = groups[1:300]

## -----------------------------------------------------------------------------
############################################################
# Default -- use partial likelihood as the type.measure:
############################################################
fit = ptLasso(x, y, groups, alpha = 0.5, family = "cox")
predict(fit, xtest, groupstest, ytest = ytest)

############################################################
# Alternatively -- use the C index:
############################################################
fit = ptLasso(x, y, groups, alpha = 0.5, family = "cox", type.measure = "C")
predict(fit, xtest, groupstest, ytest = ytest)

## ----eval=FALSE---------------------------------------------------------------
# ##################################################
# # Fit:
# ##################################################
# fit = cv.ptLasso(x, y, groups, family = "cox", type.measure = "C")
# 
# ##################################################
# # Predict with a common alpha for all groups:
# ##################################################
# predict(fit, xtest, groupstest, ytest = ytest)
# 
# ##################################################
# # Predict with a different alpha for each group:
# ##################################################
# predict(fit, xtest, groupstest, ytest = ytest, alphatype = "varying")

