# NOT RUN {
library(Matrix)
set.seed(123)
n.obs <- 150
n.vars <- 25
true.beta.mat <- array(NA, dim = c(3, n.vars))
true.beta.mat[1,] <- c(-0.5, -1, 0, 0, 2, rep(0, n.vars - 5))
true.beta.mat[2,] <- c(0.5, 0.5, -0.5, -0.5, 1, -1, rep(0, n.vars - 6))
true.beta.mat[3,] <- c(0, 0, 1, 1, -1, rep(0, n.vars - 5))
rownames(true.beta.mat) <- c("1,0", "1,1", "0,1")
true.beta <- as.vector(t(true.beta.mat))
x.sub1 <- matrix(rnorm(n.obs * n.vars), n.obs, n.vars)
x.sub2 <- matrix(rnorm(n.obs * n.vars), n.obs, n.vars)
x.sub3 <- matrix(rnorm(n.obs * n.vars), n.obs, n.vars)
x <- as.matrix(rbind(x.sub1, x.sub2, x.sub3))
conditions <- as.matrix(cbind(c(rep(1, 2 * n.obs), rep(0, n.obs)),
c(rep(0, n.obs), rep(1, 2 * n.obs))))
y <- rnorm(n.obs * 3, sd = 3) + drop(as.matrix(bdiag(x.sub1, x.sub2, x.sub3)) %*% true.beta)
fit <- cv.vennLasso(x = x, y = y, groups = conditions, nfolds = 3)
fitted.coef <- predict(fit$vennLasso.fit, type = "coefficients", s = fit$lambda.min)
(true.coef <- true.beta.mat[match(dimnames(fit$vennLasso.fit$beta)[[1]],
rownames(true.beta.mat)),])
round(fitted.coef, 2)
## effects smaller for logistic regression
true.beta.mat <- true.beta.mat / 2
true.beta <- true.beta / 2
# logistic regression example#'
y <- rbinom(n.obs * 3, 1,
prob = 1 / (1 + exp(-drop(as.matrix(bdiag(x.sub1, x.sub2, x.sub3)) %*% true.beta))))
bfit <- cv.vennLasso(x = x, y = y, groups = conditions, family = "binomial",
nfolds = 3)
fitted.coef <- predict(bfit$vennLasso.fit, type = "coefficients", s = bfit$lambda.min)
(true.coef <- true.beta.mat[match(dimnames(bfit$vennLasso.fit$beta)[[1]],
rownames(true.beta.mat)),])
round(fitted.coef, 2)
# }
Run the code above in your browser using DataLab