00001
00009 #include "party.h"
00010
00011
00021 void C_TeststatPvalue(const SEXP linexpcov, const SEXP varctrl,
00022 double *ans_teststat, double *ans_pvalue) {
00023
00024 double releps, abseps, tol;
00025 int maxpts;
00026
00027 maxpts = get_maxpts(varctrl);
00028 tol = get_tol(varctrl);
00029 abseps = get_abseps(varctrl);
00030 releps = get_releps(varctrl);
00031
00032
00033 ans_teststat[0] = C_TestStatistic(linexpcov, get_teststat(varctrl),
00034 get_tol(varctrl));
00035
00036
00037 if (get_pvalue(varctrl))
00038 ans_pvalue[0] = C_ConditionalPvalue(ans_teststat[0], linexpcov,
00039 get_teststat(varctrl),
00040 tol, &maxpts, &releps, &abseps);
00041 else
00042 ans_pvalue[0] = 1.0;
00043 }
00044
00053 void C_TeststatCriterion(const SEXP linexpcov, const SEXP varctrl,
00054 double *ans_teststat, double *ans_criterion) {
00055
00056 C_TeststatPvalue(linexpcov, varctrl, ans_teststat, ans_criterion);
00057
00058
00059
00060 if (get_pvalue(varctrl))
00061 ans_criterion[0] = 1 - ans_criterion[0];
00062 else
00063 ans_criterion[0] = ans_teststat[0];
00064
00065 }
00066
00067
00078 void C_IndependenceTest(const SEXP x, const SEXP y, const SEXP weights,
00079 SEXP linexpcov, SEXP varctrl,
00080 SEXP ans) {
00081
00082
00083
00084
00085 C_LinStatExpCov(REAL(x), ncol(x), REAL(y), ncol(y),
00086 REAL(weights), nrow(x), 1,
00087 GET_SLOT(linexpcov, PL2_expcovinfSym), linexpcov);
00088
00089
00090 if (get_teststat(varctrl) == 2)
00091 C_LinStatExpCovMPinv(linexpcov, get_tol(varctrl));
00092 C_TeststatPvalue(linexpcov, varctrl, &REAL(ans)[0], &REAL(ans)[1]);
00093 }
00094
00095
00105 SEXP R_IndependenceTest(SEXP x, SEXP y, SEXP weights, SEXP linexpcov, SEXP varctrl) {
00106
00107 SEXP ans;
00108
00109 PROTECT(ans = allocVector(REALSXP, 2));
00110 C_IndependenceTest(x, y, weights, linexpcov, varctrl, ans);
00111 UNPROTECT(1);
00112 return(ans);
00113 }
00114
00115
00130 void C_GlobalTest(const SEXP learnsample, const SEXP weights,
00131 SEXP fitmem, const SEXP varctrl,
00132 const SEXP gtctrl, const double minsplit,
00133 double *ans_teststat, double *ans_criterion, int depth) {
00134
00135 int ninputs, nobs, j, i, k, RECALC = 1, type;
00136 SEXP responses, inputs, y, x, xmem, expcovinf;
00137 SEXP thiswhichNA, Smtry;
00138 double *thisweights, *dweights, *pvaltmp, stweights = 0.0;
00139 int *ithiswhichNA, RANDOM, mtry, *randomvar, *index;
00140 int *dontuse, *dontusetmp;
00141
00142 ninputs = get_ninputs(learnsample);
00143 nobs = get_nobs(learnsample);
00144 responses = GET_SLOT(learnsample, PL2_responsesSym);
00145 inputs = GET_SLOT(learnsample, PL2_inputsSym);
00146 dweights = REAL(weights);
00147
00148
00149 y = get_test_trafo(responses);
00150
00151 expcovinf = GET_SLOT(fitmem, PL2_expcovinfSym);
00152 C_ExpectCovarInfluence(REAL(y), ncol(y), REAL(weights),
00153 nobs, expcovinf);
00154
00155 if (REAL(GET_SLOT(expcovinf, PL2_sumweightsSym))[0] < minsplit) {
00156 for (j = 0; j < ninputs; j++) {
00157 ans_teststat[j] = 0.0;
00158 ans_criterion[j] = 0.0;
00159 }
00160 } else {
00161
00162 dontuse = INTEGER(get_dontuse(fitmem));
00163 dontusetmp = INTEGER(get_dontusetmp(fitmem));
00164
00165 for (j = 0; j < ninputs; j++) dontusetmp[j] = !dontuse[j];
00166
00167
00168 RANDOM = get_randomsplits(gtctrl);
00169 Smtry = get_mtry(gtctrl);
00170 if (LENGTH(Smtry) == 1) {
00171 mtry = INTEGER(Smtry)[0];
00172 } else {
00173
00174 depth = (depth <= LENGTH(Smtry)) ? depth : LENGTH(Smtry);
00175 mtry = INTEGER(get_mtry(gtctrl))[depth - 1];
00176 }
00177 if (RANDOM & (mtry > ninputs)) {
00178 warning("mtry is larger than ninputs, using mtry = inputs");
00179 mtry = ninputs;
00180 RANDOM = 0;
00181 }
00182 if (RANDOM) {
00183 index = Calloc(ninputs, int);
00184 randomvar = Calloc(mtry, int);
00185 C_SampleNoReplace(index, ninputs, mtry, randomvar);
00186 j = 0;
00187 for (k = 0; k < mtry; k++) {
00188 j = randomvar[k];
00189 while(dontuse[j] && j < ninputs) j++;
00190 if (j == ninputs)
00191 error("not enough variables to sample from");
00192 dontusetmp[j] = 0;
00193 }
00194 Free(index);
00195 Free(randomvar);
00196 }
00197
00198 for (j = 1; j <= ninputs; j++) {
00199
00200 if ((RANDOM && dontusetmp[j - 1]) || dontuse[j - 1]) {
00201 ans_teststat[j - 1] = 0.0;
00202 ans_criterion[j - 1] = 0.0;
00203 continue;
00204 }
00205
00206 x = get_transformation(inputs, j);
00207
00208 xmem = get_varmemory(fitmem, j);
00209 if (!has_missings(inputs, j)) {
00210 C_LinStatExpCov(REAL(x), ncol(x), REAL(y), ncol(y),
00211 REAL(weights), nrow(x), !RECALC, expcovinf,
00212 xmem);
00213 } else {
00214 thisweights = C_tempweights(j, weights, fitmem, inputs);
00215
00216
00217
00218
00219
00220
00221 stweights = 0.0;
00222 for (i = 0; i < nobs; i++) stweights += thisweights[i];
00223 if (stweights < minsplit) {
00224 ans_teststat[j - 1] = 0.0;
00225 ans_criterion[j - 1] = 0.0;
00226 continue;
00227 }
00228
00229 C_LinStatExpCov(REAL(x), ncol(x), REAL(y), ncol(y),
00230 thisweights, nrow(x), RECALC,
00231 GET_SLOT(xmem, PL2_expcovinfSym),
00232 xmem);
00233 }
00234
00235 if (get_teststat(varctrl) == 2)
00236 C_LinStatExpCovMPinv(xmem, get_tol(varctrl));
00237 C_TeststatCriterion(xmem, varctrl, &ans_teststat[j - 1],
00238 &ans_criterion[j - 1]);
00239 }
00240
00241 type = get_testtype(gtctrl);
00242 switch(type) {
00243
00244 case BONFERRONI:
00245 for (j = 0; j < ninputs; j++)
00246 ans_criterion[j] = R_pow_di(ans_criterion[j], ninputs);
00247 break;
00248
00249 case MONTECARLO:
00250 pvaltmp = Calloc(ninputs, double);
00251 C_MonteCarlo(ans_criterion, learnsample, weights, fitmem,
00252 varctrl, gtctrl, pvaltmp);
00253 for (j = 0; j < ninputs; j++)
00254 ans_criterion[j] = 1 - pvaltmp[j];
00255 Free(pvaltmp);
00256 break;
00257
00258 case AGGREGATED:
00259 error("C_GlobalTest: aggregated global test not yet implemented");
00260 break;
00261
00262 case UNIVARIATE: break;
00263 case TESTSTATISTIC: break;
00264 default: error("C_GlobalTest: undefined value for type argument");
00265 break;
00266 }
00267 }
00268 }
00269
00270
00280 SEXP R_GlobalTest(SEXP learnsample, SEXP weights, SEXP fitmem,
00281 SEXP varctrl, SEXP gtctrl) {
00282
00283 SEXP ans, teststat, criterion;
00284
00285 GetRNGstate();
00286
00287 PROTECT(ans = allocVector(VECSXP, 2));
00288 SET_VECTOR_ELT(ans, 0,
00289 teststat = allocVector(REALSXP, get_ninputs(learnsample)));
00290 SET_VECTOR_ELT(ans, 1,
00291 criterion = allocVector(REALSXP, get_ninputs(learnsample)));
00292
00293 C_GlobalTest(learnsample, weights, fitmem, varctrl, gtctrl, 0,
00294 REAL(teststat), REAL(criterion), 1);
00295
00296 PutRNGstate();
00297
00298 UNPROTECT(1);
00299 return(ans);
00300 }