Switch to unified view

a b/partyMod/tests/Distributions.R
1
2
set.seed(290875)
3
library("party")
4
if (!require("mvtnorm"))
5
    stop("cannot load package mvtnorm")
6
7
8
### get rid of the NAMESPACE
9
attach(asNamespace("party"))
10
11
### 
12
###
13
###    Regression tests for conditional distributions
14
###    
15
###    functions defined in file `./src/Distributions.c'
16
17
### chisq-distribution of quadratic forms
18
t <- 2.1
19
df <- 2
20
storage.mode(t) <- "double"
21
storage.mode(df) <- "double"
22
stopifnot(isequal(1 - pchisq(t, df = df), ### P-values!!!
23
          .Call("R_quadformConditionalPvalue", t, df, PACKAGE = "party")))
24
25
stopifnot(isequal(2*pnorm(-t), 
26
          .Call("R_maxabsConditionalPvalue", t, matrix(1), as.integer(1), 0.0, 0.0, 0.0, PACKAGE = "party")))
27
28
29
maxpts <- 25000
30
storage.mode(maxpts) <- "integer"
31
abseps <- 0.0001
32
releps <- 0
33
tol <- 1e-10
34
35
a <- 1.96
36
b <- diag(2)
37
38
p1 <- .Call("R_maxabsConditionalPvalue", a, b, maxpts, abseps, releps, tol, PACKAGE = "party")
39
p2 <- pmvnorm(lower = rep(-a,2), upper = rep(a,2), corr = b)
40
stopifnot(isequal(round(p1, 3), round(1 - p2, 3)))
41
42
b <- diag(4)
43
p1 <- .Call("R_maxabsConditionalPvalue", a, b, maxpts, abseps, releps, tol, PACKAGE = "party")
44
p2 <- pmvnorm(lower = rep(-a,4), upper = rep(a,4), corr = b)
45
stopifnot(isequal(round(p1, 3), round(1 - p2, 3)))
46
47
b <- diag(4)
48
b[upper.tri(b)] <- c(0.1, 0.2, 0.3)
49
b[lower.tri(b)] <- t(b)[lower.tri(b)]
50
p1 <- .Call("R_maxabsConditionalPvalue", a, b, maxpts, abseps, releps, tol, PACKAGE = "party")
51
p2 <- pmvnorm(lower = rep(-a,4), upper = rep(a,4), corr = b)
52
stopifnot(isequal(round(p1, 3), round(1 - p2, 3)))
53
54
### Monte-Carlo approximation of P-Values, univariate
55
mydata = data.frame(y = gl(2, 50), x1 = rnorm(100),  
56
                    x2 = rnorm(100), x3 = rnorm(100))
57
inp <- initVariableFrame(mydata[,"x1",drop = FALSE], trafo = function(data)
58
ptrafo(data, numeric_trafo = rank))
59
resp <- initVariableFrame(mydata[,"y",drop = FALSE], trafo = NULL, response = TRUE)
60
ls <- new("LearningSample", inputs = inp, responses = resp,
61
          weights = rep(1, inp@nobs), nobs = nrow(mydata),
62
          ninputs = inp@ninputs)
63
tm <- ctree_memory(ls)
64
varctrl <- new("VariableControl")
65
varctrl@teststat <- factor("max", levels = c("max", "quad"))
66
varctrl@pvalue <- FALSE
67
gtctrl <- new("GlobalTestControl")
68
gtctrl@testtype <- factor("MonteCarlo", levels = levels(gtctrl@testtype))
69
gtctrl@nresample <- as.integer(19999)
70
71
pvals <- .Call("R_GlobalTest", ls, ls@weights, tm, varctrl, gtctrl, PACKAGE = "party")
72
wstat <- abs(qnorm(wilcox.test(x1 ~ y, data = mydata, 
73
             exact = FALSE, correct = FALSE)$p.value/2))
74
wpval <- wilcox.test(x1 ~ y, data = mydata, exact = TRUE)$p.value
75
stopifnot(isequal(wstat, pvals[[1]]))
76
stopifnot(abs(wpval - (1 - pvals[[2]])) < 0.01)
77
78
### Monte-Carlo approximations of P-Values, multiple inputs
79
mydata = data.frame(y = gl(2, 50), x1 = rnorm(100),  
80
                    x2 = rnorm(100), x3 = rnorm(100))
81
inp <- initVariableFrame(mydata[,c("x1", "x2", "x3"),
82
                                drop = FALSE], trafo = function(data)
83
ptrafo(data, numeric_trafo = rank))
84
resp <- initVariableFrame(mydata[,"y",drop = FALSE], trafo = NULL, response = TRUE)
85
ls <- new("LearningSample", inputs = inp, responses = resp,
86
          weights = rep(1, inp@nobs), nobs = nrow(mydata),
87
          ninputs = inp@ninputs)
88
tm <- ctree_memory(ls)
89
varctrl <- new("VariableControl")
90
varctrl@teststat <- factor("max", levels = c("max", "quad"))
91
varctrl@pvalue <- TRUE
92
gtctrl <- new("GlobalTestControl")
93
gtctrl@testtype <- factor("Univariate", levels = levels(gtctrl@testtype))
94
gtctrl@nresample <- as.integer(19999)
95
96
pvals <- .Call("R_GlobalTest", ls, ls@weights, tm, varctrl, gtctrl, PACKAGE = "party")
97
wstat <- c(abs(qnorm(wilcox.test(x1 ~ y, data = mydata, 
98
               exact = FALSE, correct = FALSE)$p.value/2)),
99
           abs(qnorm(wilcox.test(x2 ~ y, data = mydata, 
100
               exact = FALSE, correct = FALSE)$p.value/2)),
101
           abs(qnorm(wilcox.test(x3 ~ y, data = mydata, 
102
               exact = FALSE, correct = FALSE)$p.value/2)))
103
wpval <- c(wilcox.test(x1 ~ y, data = mydata, 
104
               exact = FALSE, correct = FALSE)$p.value,
105
           wilcox.test(x2 ~ y, data = mydata, 
106
               exact = FALSE, correct = FALSE)$p.value,
107
           wilcox.test(x3 ~ y, data = mydata, 
108
               exact = FALSE, correct = FALSE)$p.value)
109
stopifnot(isequal(wstat, pvals[[1]]))
110
stopifnot(isequal(wpval, 1 - pvals[[2]]))
111
112
### Monte-Carlo approximations of P-Values, min-P approach
113
gtctrl@testtype <- factor("MonteCarlo", levels = levels(gtctrl@testtype))
114
gtctrl@nresample <- as.integer(19999)
115
pvals <- .Call("R_GlobalTest", ls, ls@weights, tm, varctrl, gtctrl, PACKAGE = "party")
116
stopifnot(isequal(wstat, pvals[[1]]))
117
stopifnot(all(wpval < (1 - pvals[[2]])))