## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"#,
  #fig.width = 12,
  #fig.height = 8
)

need <- c("knitr", "doMC")
ok <- vapply(need, requireNamespace, quietly = TRUE, FUN.VALUE = logical(1))
if (!all(ok)) {
  knitr::knit_exit()  
}

require(knitr)

## ----eval = FALSE-------------------------------------------------------------
# # The typical glmnet pipeline: train, plot and predict,
# # using elasticnet parameter 0.2.
# fit = glmnet(X, y, alpha = 0.2)
# plot(fit)
# test.preds = predict(fit, Xtest)
# 
# # The typical ptLasso pipeline: train, plot and predict,
# # using pretraining parameter 0.5.
# fit = ptLasso(X, y, groups, alpha = 0.5)
# plot(fit)
# test.preds = predict(fit, Xtest, groupstest)

## ----eval = FALSE-------------------------------------------------------------
# fit = cv.ptLasso(X, y, groups, alpha = 0.5)
# plot(fit)
# test.preds = predict(fit, Xtest, groupstest)

## ----setup--------------------------------------------------------------------
require(ptLasso)

## ----coefs, echo = FALSE, eval = FALSE----------------------------------------
# coef.table = cbind(cbind(
#   3*(1:5),
#   c(3, rep(0, 4)),
#   c(0, 3, rep(0, 3)),
#   c(rep(0, 2), 3, rep(0, 2)),
#   c(rep(0, 3), 3, 0),
#   c(rep(0, 4), 3)
# ), 0)
# rownames(coef.table) = paste0("group ", 1:5)
# colnames(coef.table) = c("1-10", "11-20", "21-30", "31-40", "41-59", "51-60", "61-120")
# 
# kable(
#   coef.table, booktabs = TRUE,
#   caption = 'Coefficients for simulating input grouped data'
# )

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

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

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


## -----------------------------------------------------------------------------
fit <- ptLasso(x, y, groups, alpha = 0.5)

## ----fig.width=7, fig.height=6, dpi=100---------------------------------------
plot(fit)

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

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

## -----------------------------------------------------------------------------
all.coefs = coef(fit, s= "lambda.min")
names(all.coefs)

## -----------------------------------------------------------------------------
length(all.coefs$pretrain)

## -----------------------------------------------------------------------------
head(all.coefs$pretrain[[1]])

## -----------------------------------------------------------------------------
cvfit <- cv.ptLasso(x, y, groups)
cvfit

## ----fig.width=5, fig.height=4, dpi=100---------------------------------------
plot(cvfit)

## -----------------------------------------------------------------------------
preds = predict(cvfit, xtest, groupstest=groupstest, ytest=ytest)
preds

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

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

set.seed(1234)

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

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

## -----------------------------------------------------------------------------
cvfit <- cv.ptLasso(x, y, groups, alphalist = c(0, 0.5, 1))
cvfit

## -----------------------------------------------------------------------------
###############################################
# Common alpha for all groups:
###############################################
predict(cvfit, xtest, groupstest, ytest=ytest)

###############################################
# Different alpha for each group:
###############################################
predict(cvfit, xtest, groupstest, ytest=ytest, alphatype = "varying")

## ----eval = FALSE-------------------------------------------------------------
# # Default:
# fit <- ptLasso(x, y, groups, alpha = 0.5, overall.lambda = "lambda.1se")
# 
# # Alternative:
# fit <- ptLasso(x, y, groups, alpha = 0.5, overall.lambda = "lambda.min")

## ----eval = FALSE-------------------------------------------------------------
# fit <- ptLasso(x, y, groups,
#                alpha = 0.5,    # pretraining parameter
#                en.alpha = 0.2) # elasticnet parameter

## -----------------------------------------------------------------------------
fit <- ptLasso(x, y, groups, alpha = 0.5, verbose = TRUE)

## ----eval = FALSE-------------------------------------------------------------
# fit <- ptLasso(x, y, groups, alpha = 0.5, trace.it = TRUE)

## ----eval = FALSE-------------------------------------------------------------
# fit <- ptLasso(x, y, groups, alpha = 0.5, verbose = TRUE, trace.it = TRUE)

## ----eval = FALSE-------------------------------------------------------------
# overall.model = cv.glmnet(x, y, keep = TRUE)
# individual.models = lapply(1:5,
#                            function(kk) cv.glmnet(x[groups == kk, ],
#                                                   y[groups == kk],
#                                                   keep = TRUE))
# 
# fit <- ptLasso(x, y, groups,
#                fitoverall = overall.model,
#                fitind = individual.models,
#                verbose = TRUE)

## ----eval = FALSE-------------------------------------------------------------
# fit <- ptLasso(x, y, groups, fitoverall = overall.model, verbose = TRUE)

## ----eval = FALSE-------------------------------------------------------------
# fit <- ptLasso(x, y, groups, fitind = individual.models, verbose = TRUE)

## ----eval = FALSE-------------------------------------------------------------
# cvfit <- cv.ptLasso(x, y, groups, group.intercepts = FALSE)

## ----eval=FALSE---------------------------------------------------------------
# require(doMC)
# registerDoMC(cores = 4)
# fit = ptLasso(x, y, groups = groups, family = "gaussian", type.measure = "mse",
#               parallel=TRUE)

