Switch to unified view

a b/partyMod/tests/Distributions.Rout.save
1
2
R Under development (unstable) (2014-06-29 r66051) -- "Unsuffered Consequences"
3
Copyright (C) 2014 The R Foundation for Statistical Computing
4
Platform: x86_64-unknown-linux-gnu (64-bit)
5
6
R is free software and comes with ABSOLUTELY NO WARRANTY.
7
You are welcome to redistribute it under certain conditions.
8
Type 'license()' or 'licence()' for distribution details.
9
10
R is a collaborative project with many contributors.
11
Type 'contributors()' for more information and
12
'citation()' on how to cite R or R packages in publications.
13
14
Type 'demo()' for some demos, 'help()' for on-line help, or
15
'help.start()' for an HTML browser interface to help.
16
Type 'q()' to quit R.
17
18
> 
19
> set.seed(290875)
20
> library("party")
21
Loading required package: grid
22
Loading required package: zoo
23
24
Attaching package: 'zoo'
25
26
The following objects are masked from 'package:base':
27
28
    as.Date, as.Date.numeric
29
30
Loading required package: sandwich
31
Loading required package: strucchange
32
Loading required package: modeltools
33
Loading required package: stats4
34
> if (!require("mvtnorm"))
35
+     stop("cannot load package mvtnorm")
36
Loading required package: mvtnorm
37
> 
38
> 
39
> ### get rid of the NAMESPACE
40
> attach(asNamespace("party"))
41
The following objects are masked from package:party:
42
43
    cforest, cforest_classical, cforest_control, cforest_unbiased,
44
    conditionalTree, ctree, ctree_control, ctree_memory, edge_simple,
45
    mob, mob_control, node_barplot, node_bivplot, node_boxplot,
46
    node_density, node_hist, node_inner, node_scatterplot, node_surv,
47
    node_terminal, proximity, ptrafo, reweight, sctest.mob, varimp,
48
    varimpAUC
49
50
> 
51
> ### 
52
> ###
53
> ###    Regression tests for conditional distributions
54
> ###    
55
> ###    functions defined in file `./src/Distributions.c'
56
> 
57
> ### chisq-distribution of quadratic forms
58
> t <- 2.1
59
> df <- 2
60
> storage.mode(t) <- "double"
61
> storage.mode(df) <- "double"
62
> stopifnot(isequal(1 - pchisq(t, df = df), ### P-values!!!
63
+           .Call("R_quadformConditionalPvalue", t, df, PACKAGE = "party")))
64
> 
65
> stopifnot(isequal(2*pnorm(-t), 
66
+           .Call("R_maxabsConditionalPvalue", t, matrix(1), as.integer(1), 0.0, 0.0, 0.0, PACKAGE = "party")))
67
> 
68
> 
69
> maxpts <- 25000
70
> storage.mode(maxpts) <- "integer"
71
> abseps <- 0.0001
72
> releps <- 0
73
> tol <- 1e-10
74
> 
75
> a <- 1.96
76
> b <- diag(2)
77
> 
78
> p1 <- .Call("R_maxabsConditionalPvalue", a, b, maxpts, abseps, releps, tol, PACKAGE = "party")
79
> p2 <- pmvnorm(lower = rep(-a,2), upper = rep(a,2), corr = b)
80
> stopifnot(isequal(round(p1, 3), round(1 - p2, 3)))
81
> 
82
> b <- diag(4)
83
> p1 <- .Call("R_maxabsConditionalPvalue", a, b, maxpts, abseps, releps, tol, PACKAGE = "party")
84
> p2 <- pmvnorm(lower = rep(-a,4), upper = rep(a,4), corr = b)
85
> stopifnot(isequal(round(p1, 3), round(1 - p2, 3)))
86
> 
87
> b <- diag(4)
88
> b[upper.tri(b)] <- c(0.1, 0.2, 0.3)
89
> b[lower.tri(b)] <- t(b)[lower.tri(b)]
90
> p1 <- .Call("R_maxabsConditionalPvalue", a, b, maxpts, abseps, releps, tol, PACKAGE = "party")
91
> p2 <- pmvnorm(lower = rep(-a,4), upper = rep(a,4), corr = b)
92
> stopifnot(isequal(round(p1, 3), round(1 - p2, 3)))
93
> 
94
> ### Monte-Carlo approximation of P-Values, univariate
95
> mydata = data.frame(y = gl(2, 50), x1 = rnorm(100),  
96
+                     x2 = rnorm(100), x3 = rnorm(100))
97
> inp <- initVariableFrame(mydata[,"x1",drop = FALSE], trafo = function(data)
98
+ ptrafo(data, numeric_trafo = rank))
99
> resp <- initVariableFrame(mydata[,"y",drop = FALSE], trafo = NULL, response = TRUE)
100
> ls <- new("LearningSample", inputs = inp, responses = resp,
101
+           weights = rep(1, inp@nobs), nobs = nrow(mydata),
102
+           ninputs = inp@ninputs)
103
> tm <- ctree_memory(ls)
104
> varctrl <- new("VariableControl")
105
> varctrl@teststat <- factor("max", levels = c("max", "quad"))
106
> varctrl@pvalue <- FALSE
107
> gtctrl <- new("GlobalTestControl")
108
> gtctrl@testtype <- factor("MonteCarlo", levels = levels(gtctrl@testtype))
109
> gtctrl@nresample <- as.integer(19999)
110
> 
111
> pvals <- .Call("R_GlobalTest", ls, ls@weights, tm, varctrl, gtctrl, PACKAGE = "party")
112
> wstat <- abs(qnorm(wilcox.test(x1 ~ y, data = mydata, 
113
+              exact = FALSE, correct = FALSE)$p.value/2))
114
> wpval <- wilcox.test(x1 ~ y, data = mydata, exact = TRUE)$p.value
115
> stopifnot(isequal(wstat, pvals[[1]]))
116
> stopifnot(abs(wpval - (1 - pvals[[2]])) < 0.01)
117
> 
118
> ### Monte-Carlo approximations of P-Values, multiple inputs
119
> mydata = data.frame(y = gl(2, 50), x1 = rnorm(100),  
120
+                     x2 = rnorm(100), x3 = rnorm(100))
121
> inp <- initVariableFrame(mydata[,c("x1", "x2", "x3"),
122
+                                 drop = FALSE], trafo = function(data)
123
+ ptrafo(data, numeric_trafo = rank))
124
> resp <- initVariableFrame(mydata[,"y",drop = FALSE], trafo = NULL, response = TRUE)
125
> ls <- new("LearningSample", inputs = inp, responses = resp,
126
+           weights = rep(1, inp@nobs), nobs = nrow(mydata),
127
+           ninputs = inp@ninputs)
128
> tm <- ctree_memory(ls)
129
> varctrl <- new("VariableControl")
130
> varctrl@teststat <- factor("max", levels = c("max", "quad"))
131
> varctrl@pvalue <- TRUE
132
> gtctrl <- new("GlobalTestControl")
133
> gtctrl@testtype <- factor("Univariate", levels = levels(gtctrl@testtype))
134
> gtctrl@nresample <- as.integer(19999)
135
> 
136
> pvals <- .Call("R_GlobalTest", ls, ls@weights, tm, varctrl, gtctrl, PACKAGE = "party")
137
> wstat <- c(abs(qnorm(wilcox.test(x1 ~ y, data = mydata, 
138
+                exact = FALSE, correct = FALSE)$p.value/2)),
139
+            abs(qnorm(wilcox.test(x2 ~ y, data = mydata, 
140
+                exact = FALSE, correct = FALSE)$p.value/2)),
141
+            abs(qnorm(wilcox.test(x3 ~ y, data = mydata, 
142
+                exact = FALSE, correct = FALSE)$p.value/2)))
143
> wpval <- c(wilcox.test(x1 ~ y, data = mydata, 
144
+                exact = FALSE, correct = FALSE)$p.value,
145
+            wilcox.test(x2 ~ y, data = mydata, 
146
+                exact = FALSE, correct = FALSE)$p.value,
147
+            wilcox.test(x3 ~ y, data = mydata, 
148
+                exact = FALSE, correct = FALSE)$p.value)
149
> stopifnot(isequal(wstat, pvals[[1]]))
150
> stopifnot(isequal(wpval, 1 - pvals[[2]]))
151
> 
152
> ### Monte-Carlo approximations of P-Values, min-P approach
153
> gtctrl@testtype <- factor("MonteCarlo", levels = levels(gtctrl@testtype))
154
> gtctrl@nresample <- as.integer(19999)
155
> pvals <- .Call("R_GlobalTest", ls, ls@weights, tm, varctrl, gtctrl, PACKAGE = "party")
156
> stopifnot(isequal(wstat, pvals[[1]]))
157
> stopifnot(all(wpval < (1 - pvals[[2]])))
158
> 
159
> proc.time()
160
   user  system elapsed 
161
  0.956   0.040   0.996