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

# data simulation
Armin Rauschenberger's avatar
Armin Rauschenberger committed
3
list <- cornet:::.simulate(n=100,p=200)
Armin Rauschenberger's avatar
Armin Rauschenberger committed
4
5
6
7
y <- list$y; X <- list$X

# penalised regression
cutoff <- 1
Armin Rauschenberger's avatar
Armin Rauschenberger committed
8
foldid <- palasso:::.folds(y=y>cutoff,nfolds=10)
Armin Rauschenberger's avatar
Armin Rauschenberger committed
9
fit <- cornet::cornet(y=y,cutoff=cutoff,X=X,foldid=foldid)
Armin Rauschenberger's avatar
Armin Rauschenberger committed
10
11
12
13
net <- list()
net$gaussian <- glmnet::cv.glmnet(y=y,x=X,family="gaussian",foldid=foldid)
net$binomial <- glmnet::cv.glmnet(y=y>cutoff,x=X,family="binomial",foldid=foldid)

Armin Rauschenberger's avatar
Armin Rauschenberger committed
14
15
#--- cornet equals glmnet ---

Armin Rauschenberger's avatar
Armin Rauschenberger committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
for(dist in c("gaussian","binomial")){
  
  testthat::test_that("cross-validated loss",{
    a <- fit[[dist]]$sigma.cvm
    b <- net[[dist]]$cvm
    diff <- abs(a[seq_along(b)]-b)
    testthat::expect_true(all(diff<1e-06))
  })
  
  testthat::test_that("optimal lambda",{
    a <- fit[[dist]]$lambda.min
    b <- net[[dist]]$lambda.min
    testthat::expect_true(a==b)
  })
  
  testthat::test_that("lambda sequence",{
    a <- fit[[dist]]$lambda
    b <- net[[dist]]$lambda
    testthat::expect_true(all(a[seq_along(b)]==b))
  })
  
  testthat::test_that("predicted values",{
    a <- stats::predict(object=fit[[dist]],newx=X)
    b <- stats::predict(object=net[[dist]]$glmnet.fit,newx=X)
    testthat::expect_true(all(a==b))
  })
  
Armin Rauschenberger's avatar
Armin Rauschenberger committed
43
44
45
46
47
48
  testthat::test_that("coefficients",{
    a <- fit[[dist]]$beta
    b <- net[[dist]]$glmnet.fit$beta
    testthat::expect_true(all(a==b))
  })
  
Armin Rauschenberger's avatar
Armin Rauschenberger committed
49
50
}

Armin Rauschenberger's avatar
Armin Rauschenberger committed
51
52
53
#--- other checks ---

testthat::test_that("predicted probabilities",{ # important!
Armin Rauschenberger's avatar
Armin Rauschenberger committed
54
  a <- cornet:::predict.cornet(object=fit,newx=X)$binomial
Armin Rauschenberger's avatar
Armin Rauschenberger committed
55
56
57
58
  b <- as.numeric(stats::predict(object=net$binomial,newx=X,s="lambda.min",type="response"))
  testthat::expect_true(all(a==b))
})

Armin Rauschenberger's avatar
Armin Rauschenberger committed
59
60
61
62
63
64
65
66
testthat::test_that("estimated coefficients",{ # important!
  a <- cornet:::coef.cornet(fit)
  b <- as.numeric(stats::coef(object=net$gaussian,s="lambda.min"))
  c <- as.numeric(stats::coef(object=net$binomial,s="lambda.min"))
  cond <- all(a[,"beta"]==b) & all(a[,"gamma"]==c)
  testthat::expect_true(cond)
})

Armin Rauschenberger's avatar
Armin Rauschenberger committed
67
68
69
70
71
72
testthat::test_that("tuning parameters",{
  a <- (0 <= fit$sigma.min) & is.finite(fit$sigma.min)
  b <- (0 <= fit$pi.min) & (fit$pi.min <= 1)
  c <- min(fit$cvm) == fit$cvm[names(fit$sigma.min),names(fit$pi.min)]
  testthat::expect_true(all(a,b,c))
})
Armin Rauschenberger's avatar
Armin Rauschenberger committed
73

Armin Rauschenberger's avatar
Armin Rauschenberger committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
testthat::test_that("print function",{
  a <- cornet:::print.cornet(fit)
  testthat::expect_true(is.null(a))
})

testthat::test_that("plot function",{
  a <- cornet:::plot.cornet(fit)
  testthat::expect_true(is.null(a))
})

testthat::test_that("hidden compare function",{
  res <- cornet:::.compare(y=y,cutoff=cutoff,X=X,nfolds=2)
  cornet:::.check(x=res$resid.pvalue,min=0,max=1,type="vector")
})

testthat::test_that("hidden test function",{
  p <- cornet:::.test(y=y,cutoff=cutoff,X=X)
  cornet:::.check(x=p,min=0,max=1,type="scalar")
})