Switch to unified view

a b/partyMod/tests/LinearStatistic-regtest.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
> 
35
> ### get rid of the NAMESPACE
36
> attach(asNamespace("party"))
37
The following objects are masked from package:party:
38
39
    cforest, cforest_classical, cforest_control, cforest_unbiased,
40
    conditionalTree, ctree, ctree_control, ctree_memory, edge_simple,
41
    mob, mob_control, node_barplot, node_bivplot, node_boxplot,
42
    node_density, node_hist, node_inner, node_scatterplot, node_surv,
43
    node_terminal, proximity, ptrafo, reweight, sctest.mob, varimp,
44
    varimpAUC
45
46
> 
47
> ### 
48
> ###
49
> ###    Regression tests for linear statistics, expectations and covariances
50
> ###    
51
> ###    functions defined in file `./src/LinearStatistics.c'    
52
> 
53
> ### tests for function C_LinearStatistic
54
> ### Linear Statistics
55
> x = matrix(c(rep.int(1,4), rep.int(0,6)), ncol = 1)
56
> y = matrix(1:10, ncol = 1)
57
> weights = rep(1, 10)
58
> linstat = LinearStatistic(x, y, weights)
59
> stopifnot(isequal(linstat, sum(1:4)))
60
> 
61
> weights[1] = 0
62
> linstat = LinearStatistic(x, y, weights) 
63
> stopifnot(isequal(linstat, sum(2:4)))
64
> 
65
> xf <- gl(3, 10)
66
> yf <- gl(3, 10)[sample(1:30)]
67
> x <- sapply(levels(xf), function(l) as.numeric(xf == l))
68
> colnames(x) <- NULL
69
> y <- sapply(levels(yf), function(l) as.numeric(yf == l))
70
> colnames(y) <- NULL
71
> weights <- sample(1:30)
72
> linstat <- LinearStatistic(x, y, weights) 
73
> stopifnot(isequal(linstat, as.vector(t(x) %*% diag(weights) %*% y)))
74
> 
75
> xf <- factor(cut(rnorm(6000), breaks = c(-Inf, -2, 0.5, Inf)))
76
> x <- sapply(levels(xf), function(l) as.numeric(xf == l))
77
> yf <- factor(cut(rnorm(6000), breaks = c(-Inf, -0.5, 1.5, Inf)))
78
> y <- sapply(levels(yf), function(l) as.numeric(yf == l))
79
> weights <- rep(1, nrow(x))
80
> colnames(x) <- NULL
81
> colnames(y) <- NULL
82
> weights <- rep(1, 6000)
83
> linstat <- LinearStatistic(x, y, weights)
84
> stopifnot(isequal(as.vector(table(xf, yf)), linstat))
85
> stopifnot(isequal(as.vector(t(x)%*%y), linstat))
86
> 
87
> 
88
> ### tests for function C_ExpectCovarInfluence
89
> eci <- ExpectCovarInfluence(y, weights)
90
> isequal(eci@sumweights, sum(weights))
91
[1] TRUE
92
> isequal(eci@expectation, drop(weights %*% y / sum(weights)))
93
[1] TRUE
94
> ys <- t(t(y) - eci@expectation)
95
> stopifnot(isequal(eci@covariance, (t(ys) %*% (weights * ys)) /
96
+                   sum(weights)))
97
> 
98
> ### tests for function C_ExpectCovarLinearStatistic
99
> ### Conditional Expectation and Variance (via Kruskal-Wallis statistic)
100
> 
101
> ### case 1: p > 1, q = 1
102
> group <- gl(3, 5)
103
> x <- sapply(levels(group), function(l) as.numeric(group == l))
104
> y <- matrix(1:15, ncol = 1)
105
> weights <- rep(1, 15)
106
> 
107
> linstat <- LinearStatistic(x, y, weights)
108
> expcov <- ExpectCovarLinearStatistic(x, y, weights)
109
> KW <- quadformTestStatistic(linstat, expcov@expectation, expcov@covariance)
110
> kts <- kruskal.test(y ~ group)$statistic
111
> stopifnot(isequal(KW, kts))
112
> 
113
> ### case 2: p = 1, q > 1
114
> linstat <- LinearStatistic(y, x, weights)
115
> expcov <- ExpectCovarLinearStatistic(y, x, weights)
116
> KW <- quadformTestStatistic(linstat, expcov@expectation, expcov@covariance)
117
> kts <- kruskal.test(y ~ group)$statistic
118
> stopifnot(isequal(KW, kts))
119
> 
120
> ### case 3: p = 1, q = 1
121
> x <- x[,1,drop = FALSE]
122
> linstat <- LinearStatistic(x, y, weights)
123
> expcov <- ExpectCovarLinearStatistic(x, y, weights)
124
> KW <- quadformTestStatistic(linstat, expcov@expectation, expcov@covariance)
125
> kts <- kruskal.test(y ~ as.factor(x))$statistic
126
> stopifnot(isequal(KW, kts))
127
> 
128
> ### case 4: p > 1, q > 1 via chisq.test
129
> n <- 900
130
> xf <- gl(3, n / 3)
131
> yf <- gl(3, n / 3)[sample(1:n)]
132
> x <- sapply(levels(xf), function(l) as.numeric(xf == l))
133
> colnames(x) <- NULL
134
> y <- sapply(levels(yf), function(l) as.numeric(yf == l))
135
> colnames(y) <- NULL
136
> weights <- rep(1, n)
137
> linstat <- LinearStatistic(x, y, weights)
138
> expcov <- ExpectCovarLinearStatistic(x, y, weights)
139
> chi <- quadformTestStatistic(linstat, expcov@expectation, expcov@covariance)
140
> chis <- chisq.test(table(xf, yf))$statistic
141
> stopifnot(isequal(round(chi, 1), round(chis, 1)))
142
> 
143
> ### tests for function C_PermutedLinearStatistic
144
> ### Linear Statistics with permuted indices
145
> x <- matrix(rnorm(100), ncol = 2)
146
> y <- matrix(rnorm(100), ncol = 2)
147
> weights <- rep(1, 50)
148
> indx <- 1:50
149
> perm <- 1:50
150
> stopifnot(isequal(LinearStatistic(x, y, weights), 
151
+                   PermutedLinearStatistic(x, y, indx, perm)))
152
> x <- matrix(1:10000, ncol = 2)
153
> y <- matrix(1:10000, ncol = 2)
154
> 
155
> for (i in 1:100) {
156
+   indx <- sample(1:ncol(y), replace = TRUE)
157
+   perm <- sample(1:ncol(y), replace = TRUE)
158
+ 
159
+   stopifnot(isequal(as.vector(t(x[indx,]) %*% y[perm, ]),
160
+                     PermutedLinearStatistic(x, y, indx, perm)))
161
+ }
162
> 
163
> proc.time()
164
   user  system elapsed 
165
  0.776   0.020   0.793