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

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

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

# Define constants
n = 1000         # Total number of samples
ntrain = 650     # Number of training samples
p = 500          # Number of features
sigma = 2        # Standard deviation of noise
     

# Generate covariate matrix
x = matrix(rnorm(n * p), n, p)

# Define coefficients for responses 1 and 2
beta1 = c(rep(1, 5), rep(0.5, 5), rep(0, p - 10))
beta2 = c(rep(1, 5), rep(0, 5), rep(0.5, 5), rep(0, p - 15))

mu = cbind(x %*% beta1, x %*% beta2)
y  = cbind(mu[, 1] + sigma * rnorm(n), 
           mu[, 2] + sigma * rnorm(n))

cat("SNR for the two tasks:", round(diag(var(mu)/var(y-mu)), 2))
cat("Correlation between two tasks:", cor(y[, 1], y[, 2]))

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

x = x[1:ntrain, ]
y = y[1:ntrain, ]

## ----fig.width=7, fig.height=7, dpi=100---------------------------------------
fit = ptLasso(x, y, use.case = "multiresponse", nfolds = 3)
plot(fit)

## -----------------------------------------------------------------------------
fit = cv.ptLasso(x, y, nfolds = 3, use.case = "multiresponse")
plot(fit)

## -----------------------------------------------------------------------------
preds = predict(fit, xtest, ytest = ytest)
preds

## -----------------------------------------------------------------------------
preds = predict(fit, xtest, ytest = ytest, alphatype = "varying")
preds

