a b/partyMod/tests/LinearStatistic-regtest.R
1
2
set.seed(290875)
3
library("party")
4
5
### get rid of the NAMESPACE
6
attach(asNamespace("party"))
7
8
### 
9
###
10
###    Regression tests for linear statistics, expectations and covariances
11
###    
12
###    functions defined in file `./src/LinearStatistics.c'    
13
14
### tests for function C_LinearStatistic
15
### Linear Statistics
16
x = matrix(c(rep.int(1,4), rep.int(0,6)), ncol = 1)
17
y = matrix(1:10, ncol = 1)
18
weights = rep(1, 10)
19
linstat = LinearStatistic(x, y, weights)
20
stopifnot(isequal(linstat, sum(1:4)))
21
22
weights[1] = 0
23
linstat = LinearStatistic(x, y, weights) 
24
stopifnot(isequal(linstat, sum(2:4)))
25
26
xf <- gl(3, 10)
27
yf <- gl(3, 10)[sample(1:30)]
28
x <- sapply(levels(xf), function(l) as.numeric(xf == l))
29
colnames(x) <- NULL
30
y <- sapply(levels(yf), function(l) as.numeric(yf == l))
31
colnames(y) <- NULL
32
weights <- sample(1:30)
33
linstat <- LinearStatistic(x, y, weights) 
34
stopifnot(isequal(linstat, as.vector(t(x) %*% diag(weights) %*% y)))
35
36
xf <- factor(cut(rnorm(6000), breaks = c(-Inf, -2, 0.5, Inf)))
37
x <- sapply(levels(xf), function(l) as.numeric(xf == l))
38
yf <- factor(cut(rnorm(6000), breaks = c(-Inf, -0.5, 1.5, Inf)))
39
y <- sapply(levels(yf), function(l) as.numeric(yf == l))
40
weights <- rep(1, nrow(x))
41
colnames(x) <- NULL
42
colnames(y) <- NULL
43
weights <- rep(1, 6000)
44
linstat <- LinearStatistic(x, y, weights)
45
stopifnot(isequal(as.vector(table(xf, yf)), linstat))
46
stopifnot(isequal(as.vector(t(x)%*%y), linstat))
47
48
49
### tests for function C_ExpectCovarInfluence
50
eci <- ExpectCovarInfluence(y, weights)
51
isequal(eci@sumweights, sum(weights))
52
isequal(eci@expectation, drop(weights %*% y / sum(weights)))
53
ys <- t(t(y) - eci@expectation)
54
stopifnot(isequal(eci@covariance, (t(ys) %*% (weights * ys)) /
55
                  sum(weights)))
56
57
### tests for function C_ExpectCovarLinearStatistic
58
### Conditional Expectation and Variance (via Kruskal-Wallis statistic)
59
60
### case 1: p > 1, q = 1
61
group <- gl(3, 5)
62
x <- sapply(levels(group), function(l) as.numeric(group == l))
63
y <- matrix(1:15, ncol = 1)
64
weights <- rep(1, 15)
65
66
linstat <- LinearStatistic(x, y, weights)
67
expcov <- ExpectCovarLinearStatistic(x, y, weights)
68
KW <- quadformTestStatistic(linstat, expcov@expectation, expcov@covariance)
69
kts <- kruskal.test(y ~ group)$statistic
70
stopifnot(isequal(KW, kts))
71
72
### case 2: p = 1, q > 1
73
linstat <- LinearStatistic(y, x, weights)
74
expcov <- ExpectCovarLinearStatistic(y, x, weights)
75
KW <- quadformTestStatistic(linstat, expcov@expectation, expcov@covariance)
76
kts <- kruskal.test(y ~ group)$statistic
77
stopifnot(isequal(KW, kts))
78
79
### case 3: p = 1, q = 1
80
x <- x[,1,drop = FALSE]
81
linstat <- LinearStatistic(x, y, weights)
82
expcov <- ExpectCovarLinearStatistic(x, y, weights)
83
KW <- quadformTestStatistic(linstat, expcov@expectation, expcov@covariance)
84
kts <- kruskal.test(y ~ as.factor(x))$statistic
85
stopifnot(isequal(KW, kts))
86
87
### case 4: p > 1, q > 1 via chisq.test
88
n <- 900
89
xf <- gl(3, n / 3)
90
yf <- gl(3, n / 3)[sample(1:n)]
91
x <- sapply(levels(xf), function(l) as.numeric(xf == l))
92
colnames(x) <- NULL
93
y <- sapply(levels(yf), function(l) as.numeric(yf == l))
94
colnames(y) <- NULL
95
weights <- rep(1, n)
96
linstat <- LinearStatistic(x, y, weights)
97
expcov <- ExpectCovarLinearStatistic(x, y, weights)
98
chi <- quadformTestStatistic(linstat, expcov@expectation, expcov@covariance)
99
chis <- chisq.test(table(xf, yf))$statistic
100
stopifnot(isequal(round(chi, 1), round(chis, 1)))
101
102
### tests for function C_PermutedLinearStatistic
103
### Linear Statistics with permuted indices
104
x <- matrix(rnorm(100), ncol = 2)
105
y <- matrix(rnorm(100), ncol = 2)
106
weights <- rep(1, 50)
107
indx <- 1:50
108
perm <- 1:50
109
stopifnot(isequal(LinearStatistic(x, y, weights), 
110
                  PermutedLinearStatistic(x, y, indx, perm)))
111
x <- matrix(1:10000, ncol = 2)
112
y <- matrix(1:10000, ncol = 2)
113
114
for (i in 1:100) {
115
  indx <- sample(1:ncol(y), replace = TRUE)
116
  perm <- sample(1:ncol(y), replace = TRUE)
117
118
  stopifnot(isequal(as.vector(t(x[indx,]) %*% y[perm, ]),
119
                    PermutedLinearStatistic(x, y, indx, perm)))
120
}