test.R 2.4 KB
Newer Older
Armin Rauschenberger's avatar
Armin Rauschenberger committed
1

Armin Rauschenberger's avatar
Armin Rauschenberger committed
2 3
#--- Initialisation ---

Armin Rauschenberger's avatar
Armin Rauschenberger committed
4
set.seed(1)
Armin Rauschenberger's avatar
Armin Rauschenberger committed
5 6 7 8 9 10
n <- 30; q <- 3; p <- 20
Y <- matrix(c(rnorm(n),rbinom(n,size=1,prob=0.5),rpois(n,lambda=4)),
            nrow=n,ncol=q)
X <- matrix(rnorm(n*p),nrow=n,ncol=p)
family <- c("gaussian","binomial","poisson")
foldid <- palasso:::.folds(y=Y[,2],nfolds=5)
Armin Rauschenberger's avatar
Armin Rauschenberger committed
11

Armin Rauschenberger's avatar
Armin Rauschenberger committed
12 13 14 15 16
for(alpha in c(0.05,0.95)){
  
  object <- mixnet::mixnet(Y=Y,X=X,family=family,alpha.base=alpha,foldid=foldid)
  
  glmnet <- list()
Armin Rauschenberger's avatar
Armin Rauschenberger committed
17
  for(i in seq_len(q)){
Armin Rauschenberger's avatar
Armin Rauschenberger committed
18
    glmnet[[i]] <- glmnet::cv.glmnet(x=X,y=Y[,i],family=family[i],alpha=alpha,foldid=foldid,)
Armin Rauschenberger's avatar
Armin Rauschenberger committed
19
  }
Armin Rauschenberger's avatar
Armin Rauschenberger committed
20 21 22 23 24 25 26 27 28
  
  #--- Equality glmnet and mixnet ---
  
  testthat::test_that("lambda: glmnet = mixnet",{
    for(i in seq_len(q)){
      a <- glmnet[[i]]$lambda
      b <- object$base[[i]]$lambda
      max <- min(length(a),length(b))
      cond <- all(a[seq_len(max)]==b[seq_len(max)])
Armin Rauschenberger's avatar
Armin Rauschenberger committed
29 30
      testthat::expect_true(cond)
    }
Armin Rauschenberger's avatar
Armin Rauschenberger committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  })
  
  testthat::test_that("lambda.min: glmnet = mixnet",{
    for(i in seq_len(q)){
      a <- glmnet[[i]]$lambda.min
      b <- object$base[[i]]$lambda.min
      cond <- (a==b)
      testthat::expect_true(cond)
    }
  })
  
  testthat::test_that("cvm: glmnet = mixnet",{
    for(i in seq_len(q)){
      a <- glmnet[[i]]$cvm
      b <- object$base[[i]]$cvm
      max <- min(length(a),length(b))
      cond <- all(abs(a[seq_len(max)]-b[seq_len(max)])<1e-06)
      testthat::expect_true(cond)
    }
  })
  
  testthat::test_that("glmnet.fit: glmnet = mixnet",{
    for(i in seq_len(q)){
      a <- glmnet[[i]]$glmnet.fit
      b <- object$base[[i]]$glmnet.fit
      names <- setdiff(x=names(a),y="call")
      for(j in names){
        cond <- all(a[[j]]==b[[j]])
        testthat::expect_true(cond)
      }
    }
  })
  
  #--- Coherence mixnet ---
  
  testthat::test_that("predict: glmnet = mixnet",{
    a <- mixnet:::predict.mixnet(object=object,newx=X)$base
    for(i in seq_len(q)){
      b <- stats::predict(object=glmnet[[i]],newx=X,type="response",s="lambda.min")
      cond <- all(a[,i]==b)
      testthat::expect_true(cond)
    }
  })
  
  #--- Equivalence stacking and pooling ---
  
  testthat::test_that("stacking = pooling",{
    pred0 <- mixnet:::predict.mixnet(object,newx=X)$meta
    coef <- mixnet:::coef.mixnet(object)
    pred1 <- matrix(data=NA,nrow=n,ncol=q)
    for(i in seq_len(q)){
      pred1[,i] <- mixnet:::.mean.function(coef$alpha[i] + X %*% coef$beta[,i],family=family[i])
    }
    cond <- all(abs(pred0-pred1)<1e-06)
    testthat::expect_true(cond)
  })
  
}