|
a |
|
b/partyMod/src/Convenience.c |
|
|
1 |
|
|
|
2 |
/** |
|
|
3 |
Some convenience functions |
|
|
4 |
*\file Convenience.c |
|
|
5 |
*\author $Author$ |
|
|
6 |
*\date $Date$ |
|
|
7 |
*/ |
|
|
8 |
|
|
|
9 |
#include "party.h" |
|
|
10 |
|
|
|
11 |
|
|
|
12 |
/** |
|
|
13 |
Linear statistic of x, y, and weights |
|
|
14 |
and its conditional expectation and covariance \n |
|
|
15 |
*\param x values of the transformation |
|
|
16 |
*\param p dimension of the transformation |
|
|
17 |
*\param y values of the influence function |
|
|
18 |
*\param q dimension of the influence function |
|
|
19 |
*\param weights case weights |
|
|
20 |
*\param n number of observations |
|
|
21 |
*\param cexpcovinf logical: recompute exp and cov of the influence fct |
|
|
22 |
*\param expcovinf an object of class `ExpectCovarInfluence' |
|
|
23 |
*\param ans return value; an object of class `LinStatExpectCovar' |
|
|
24 |
*/ |
|
|
25 |
|
|
|
26 |
void C_LinStatExpCov(const double *x, const int p, |
|
|
27 |
const double *y, const int q, |
|
|
28 |
const double *weights, const int n, |
|
|
29 |
const int cexpcovinf, SEXP expcovinf, SEXP ans) { |
|
|
30 |
|
|
|
31 |
C_LinearStatistic(x, p, y, q, weights, n, |
|
|
32 |
REAL(GET_SLOT(ans, PL2_linearstatisticSym))); |
|
|
33 |
if (cexpcovinf) |
|
|
34 |
C_ExpectCovarInfluence(y, q, weights, n, expcovinf); |
|
|
35 |
C_ExpectCovarLinearStatistic(x, p, y, q, weights, n, |
|
|
36 |
expcovinf, ans); |
|
|
37 |
} |
|
|
38 |
|
|
|
39 |
|
|
|
40 |
/** |
|
|
41 |
Moore-Penrose inverse of the covariance matrix \n |
|
|
42 |
*\param linexpcov an object of class `LinStatExpectCovarMPinv' |
|
|
43 |
*\param tol tolerance |
|
|
44 |
*/ |
|
|
45 |
|
|
|
46 |
void C_LinStatExpCovMPinv(SEXP linexpcov, double tol) { |
|
|
47 |
|
|
|
48 |
int pq, pqn; |
|
|
49 |
|
|
|
50 |
/* correct working dimension */ |
|
|
51 |
pq = get_dimension(linexpcov); |
|
|
52 |
|
|
|
53 |
/* reduce dimension to non-zero variance part */ |
|
|
54 |
C_linexpcovReduce(linexpcov); |
|
|
55 |
|
|
|
56 |
/* reduced dimension */ |
|
|
57 |
pqn = get_dimension(linexpcov); |
|
|
58 |
INTEGER(GET_SLOT(GET_SLOT(linexpcov, PL2_svdmemSym), PL2_pSym))[0] = pqn; |
|
|
59 |
|
|
|
60 |
/* compute MPinv in reduced dimension */ |
|
|
61 |
C_MPinv(GET_SLOT(linexpcov, PL2_covarianceSym), tol, |
|
|
62 |
GET_SLOT(linexpcov, PL2_svdmemSym), linexpcov); |
|
|
63 |
|
|
|
64 |
/* make sure to reset svdmem to original dimension; |
|
|
65 |
the dimension of linexpcov is reset in C_TestStatistic */ |
|
|
66 |
INTEGER(GET_SLOT(GET_SLOT(linexpcov, PL2_svdmemSym), PL2_pSym))[0] = pq; |
|
|
67 |
} |
|
|
68 |
|
|
|
69 |
|
|
|
70 |
/** |
|
|
71 |
Compute test statistic |
|
|
72 |
*\param linexpcov an object of class `LinStatExpectCovar' |
|
|
73 |
*\param type integer, 1 (maxabs) or 2 (quadform) |
|
|
74 |
*\param tol tolerance |
|
|
75 |
*/ |
|
|
76 |
|
|
|
77 |
double C_TestStatistic(const SEXP linexpcov, const int type, const double tol) { |
|
|
78 |
|
|
|
79 |
int pq; |
|
|
80 |
double ans = 0.0; |
|
|
81 |
|
|
|
82 |
/* this is possibly the reduced one, see C_LinStatExpCovMPinv */ |
|
|
83 |
pq = get_dimension(linexpcov); |
|
|
84 |
|
|
|
85 |
switch(type) { |
|
|
86 |
/* maxabs-type test statistic */ |
|
|
87 |
case 1: |
|
|
88 |
ans = C_maxabsTestStatistic( |
|
|
89 |
REAL(GET_SLOT(linexpcov, PL2_linearstatisticSym)), |
|
|
90 |
REAL(GET_SLOT(linexpcov, PL2_expectationSym)), |
|
|
91 |
REAL(GET_SLOT(linexpcov, PL2_covarianceSym)), |
|
|
92 |
pq, tol); |
|
|
93 |
break; |
|
|
94 |
/* quadform-type test statistic */ |
|
|
95 |
case 2: |
|
|
96 |
ans = C_quadformTestStatistic( |
|
|
97 |
REAL(GET_SLOT(linexpcov, PL2_linearstatisticSym)), |
|
|
98 |
REAL(GET_SLOT(linexpcov, PL2_expectationSym)), |
|
|
99 |
REAL(GET_SLOT(linexpcov, PL2_MPinvSym)), pq); |
|
|
100 |
break; |
|
|
101 |
default: error("C_TestStatistic: undefined value for type argument"); |
|
|
102 |
} |
|
|
103 |
|
|
|
104 |
/* dimension was potentially reduced; reset to initial value */ |
|
|
105 |
INTEGER(GET_SLOT(linexpcov, PL2_dimensionSym))[0] = |
|
|
106 |
LENGTH(GET_SLOT(linexpcov, PL2_linearstatisticSym)); |
|
|
107 |
|
|
|
108 |
return(ans); |
|
|
109 |
} |
|
|
110 |
|
|
|
111 |
|
|
|
112 |
/** |
|
|
113 |
Compute asymptotic conditional P-value |
|
|
114 |
*\param tstat test statistic |
|
|
115 |
*\param linexpcov an object of class `LinStatExpectCovar' |
|
|
116 |
*\param type integer, 1 (maxabs) or 2 (quadform) |
|
|
117 |
*\param tol tolerance |
|
|
118 |
*\param maxpts argument to C_maxabsConditionalPvalue |
|
|
119 |
*\param releps argument to C_maxabsConditionalPvalue |
|
|
120 |
*\param abseps argument to C_maxabsConditionalPvalue |
|
|
121 |
*/ |
|
|
122 |
|
|
|
123 |
double C_ConditionalPvalue(const double tstat, SEXP linexpcov, |
|
|
124 |
const int type, double tol, |
|
|
125 |
int *maxpts, double *releps, double *abseps) { |
|
|
126 |
|
|
|
127 |
int pq; |
|
|
128 |
double ans = 1.0; |
|
|
129 |
|
|
|
130 |
pq = get_dimension(linexpcov); |
|
|
131 |
|
|
|
132 |
switch(type) { |
|
|
133 |
/* maxabs-type test statistic */ |
|
|
134 |
case MAXABS: |
|
|
135 |
ans = C_maxabsConditionalPvalue(tstat, |
|
|
136 |
REAL(GET_SLOT(linexpcov, PL2_covarianceSym)), |
|
|
137 |
pq, maxpts, releps, abseps, &tol); |
|
|
138 |
break; |
|
|
139 |
/* quadform-type test statistic */ |
|
|
140 |
case QUADFORM: |
|
|
141 |
/* var = 0 => rank = 0 */ |
|
|
142 |
if (REAL(GET_SLOT(linexpcov, PL2_rankSym))[0] > 0.5) |
|
|
143 |
ans = C_quadformConditionalPvalue(tstat, |
|
|
144 |
REAL(GET_SLOT(linexpcov, PL2_rankSym))[0]); |
|
|
145 |
break; |
|
|
146 |
default: error("C_ConditionalPvalue: undefined value for type argument"); |
|
|
147 |
} |
|
|
148 |
return(ans); |
|
|
149 |
} |
|
|
150 |
|
|
|
151 |
|
|
|
152 |
/** |
|
|
153 |
extract the (first) response variable from a learning sample |
|
|
154 |
*\param learnsample an object of class `LearningSample' |
|
|
155 |
*/ |
|
|
156 |
|
|
|
157 |
SEXP R_get_response(SEXP learnsample) { |
|
|
158 |
return(VECTOR_ELT(GET_SLOT(GET_SLOT(learnsample, PL2_responsesSym), |
|
|
159 |
PL2_variablesSym), 0)); |
|
|
160 |
} |
|
|
161 |
|
|
|
162 |
|
|
|
163 |
/** |
|
|
164 |
change the values of the response variable in a learning sample |
|
|
165 |
*\param learnsample an object of class `LearningSample' |
|
|
166 |
*\param y a REAL with new values |
|
|
167 |
*/ |
|
|
168 |
|
|
|
169 |
void R_set_response(SEXP learnsample, SEXP y) { |
|
|
170 |
|
|
|
171 |
double *v, *t, *j, *dy, *p; |
|
|
172 |
int i, n; |
|
|
173 |
|
|
|
174 |
n = LENGTH(y); |
|
|
175 |
dy = REAL(y); |
|
|
176 |
|
|
|
177 |
if (LENGTH(R_get_response(learnsample)) != n) |
|
|
178 |
error("lengths of arguments don't match"); |
|
|
179 |
|
|
|
180 |
v = REAL(VECTOR_ELT(GET_SLOT(GET_SLOT(learnsample, PL2_responsesSym), |
|
|
181 |
PL2_variablesSym), 0)); |
|
|
182 |
t = REAL(VECTOR_ELT(GET_SLOT(GET_SLOT(learnsample, PL2_responsesSym), |
|
|
183 |
PL2_transformationsSym), 0)); |
|
|
184 |
j = REAL(get_test_trafo(GET_SLOT(learnsample, PL2_responsesSym))); |
|
|
185 |
p = REAL(get_predict_trafo(GET_SLOT(learnsample, PL2_responsesSym))); |
|
|
186 |
|
|
|
187 |
for (i = 0; i < n; i++) { |
|
|
188 |
v[i] = dy[i]; |
|
|
189 |
t[i] = dy[i]; |
|
|
190 |
j[i] = dy[i]; |
|
|
191 |
p[i] = dy[i]; |
|
|
192 |
} |
|
|
193 |
} |