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
00129 void C_GlobalTest(const SEXP learnsample, const SEXP weights,
00130 SEXP fitmem, const SEXP varctrl,
00131 const SEXP gtctrl, const double minsplit,
00132 double *ans_teststat, double *ans_criterion) {
00133
00134 int ninputs, nobs, j, i, k, RECALC = 1, type;
00135 SEXP responses, inputs, y, x, xmem, expcovinf;
00136 SEXP thiswhichNA;
00137 double *thisweights, *dweights, *pvaltmp, stweights = 0.0;
00138 int *ithiswhichNA, RANDOM, mtry, *randomvar, *index;
00139 int *dontuse, *dontusetmp;
00140
00141 ninputs = get_ninputs(learnsample);
00142 nobs = get_nobs(learnsample);
00143 responses = GET_SLOT(learnsample, PL2_responsesSym);
00144 inputs = GET_SLOT(learnsample, PL2_inputsSym);
00145 dweights = REAL(weights);
00146
00147 y = get_transformation(responses, 1);
00148
00149 expcovinf = GET_SLOT(fitmem, PL2_expcovinfSym);
00150 C_ExpectCovarInfluence(REAL(y), ncol(y), REAL(weights),
00151 nobs, expcovinf);
00152
00153 if (REAL(GET_SLOT(expcovinf, PL2_sumweightsSym))[0] < minsplit) {
00154 for (j = 0; j < ninputs; j++) {
00155 ans_teststat[j] = 0.0;
00156 ans_criterion[j] = 0.0;
00157 }
00158 } else {
00159
00160 dontuse = INTEGER(get_dontuse(fitmem));
00161 dontusetmp = INTEGER(get_dontusetmp(fitmem));
00162
00163 for (j = 0; j < ninputs; j++) dontusetmp[j] = !dontuse[j];
00164
00165
00166 RANDOM = get_randomsplits(gtctrl);
00167 mtry = get_mtry(gtctrl);
00168 if (RANDOM & (mtry > ninputs)) {
00169 warning("mtry is larger than ninputs, using mtry = inputs");
00170 mtry = ninputs;
00171 RANDOM = 0;
00172 }
00173 if (RANDOM) {
00174 index = Calloc(ninputs, int);
00175 randomvar = Calloc(mtry, int);
00176 C_SampleNoReplace(index, ninputs, mtry, randomvar);
00177 j = 0;
00178 for (k = 0; k < mtry; k++) {
00179 j = randomvar[k];
00180 while(dontuse[j] && j < ninputs) j++;
00181 if (j == ninputs)
00182 error("not enough variables to sample from");
00183 dontusetmp[j] = 0;
00184 }
00185 Free(index);
00186 Free(randomvar);
00187 }
00188
00189 for (j = 1; j <= ninputs; j++) {
00190
00191 if ((RANDOM && dontusetmp[j - 1]) || dontuse[j - 1]) {
00192 ans_teststat[j - 1] = 0.0;
00193 ans_criterion[j - 1] = 0.0;
00194 continue;
00195 }
00196
00197 x = get_transformation(inputs, j);
00198
00199 xmem = get_varmemory(fitmem, j);
00200 if (!has_missings(inputs, j)) {
00201 C_LinStatExpCov(REAL(x), ncol(x), REAL(y), ncol(y),
00202 REAL(weights), nrow(x), !RECALC, expcovinf,
00203 xmem);
00204 } else {
00205 thisweights = REAL(get_weights(fitmem, j));
00206 thiswhichNA = get_missings(inputs, j);
00207 ithiswhichNA = INTEGER(thiswhichNA);
00208 for (i = 0; i < nobs; i++) thisweights[i] = dweights[i];
00209 for (k = 0; k < LENGTH(thiswhichNA); k++)
00210 thisweights[ithiswhichNA[k] - 1] = 0.0;
00211
00212
00213
00214
00215
00216
00217 stweights = 0.0;
00218 for (i = 0; i < nobs; i++) stweights += thisweights[i];
00219 if (stweights < minsplit) {
00220 ans_teststat[j - 1] = 0.0;
00221 ans_criterion[j - 1] = 0.0;
00222 continue;
00223 }
00224
00225 C_LinStatExpCov(REAL(x), ncol(x), REAL(y), ncol(y),
00226 thisweights, nrow(x), RECALC,
00227 GET_SLOT(xmem, PL2_expcovinfSym),
00228 xmem);
00229 }
00230
00231 if (get_teststat(varctrl) == 2)
00232 C_LinStatExpCovMPinv(xmem, get_tol(varctrl));
00233 C_TeststatCriterion(xmem, varctrl, &ans_teststat[j - 1],
00234 &ans_criterion[j - 1]);
00235 }
00236
00237 type = get_testtype(gtctrl);
00238 switch(type) {
00239
00240 case BONFERRONI:
00241 for (j = 0; j < ninputs; j++)
00242 ans_criterion[j] = R_pow_di(ans_criterion[j], ninputs);
00243 break;
00244
00245 case MONTECARLO:
00246 pvaltmp = Calloc(ninputs, double);
00247 C_MonteCarlo(ans_criterion, learnsample, weights, fitmem,
00248 varctrl, gtctrl, pvaltmp);
00249 for (j = 0; j < ninputs; j++)
00250 ans_criterion[j] = 1 - pvaltmp[j];
00251 Free(pvaltmp);
00252 break;
00253
00254 case AGGREGATED:
00255 error("C_GlobalTest: aggregated global test not yet implemented");
00256 break;
00257
00258 case UNIVARIATE: break;
00259 case TESTSTATISTIC: break;
00260 default: error("C_GlobalTest: undefined value for type argument");
00261 break;
00262 }
00263 }
00264 }
00265
00266
00276 SEXP R_GlobalTest(SEXP learnsample, SEXP weights, SEXP fitmem,
00277 SEXP varctrl, SEXP gtctrl) {
00278
00279 SEXP ans, teststat, criterion;
00280
00281 GetRNGstate();
00282
00283 PROTECT(ans = allocVector(VECSXP, 2));
00284 SET_VECTOR_ELT(ans, 0,
00285 teststat = allocVector(REALSXP, get_ninputs(learnsample)));
00286 SET_VECTOR_ELT(ans, 1,
00287 criterion = allocVector(REALSXP, get_ninputs(learnsample)));
00288
00289 C_GlobalTest(learnsample, weights, fitmem, varctrl, gtctrl, 0,
00290 REAL(teststat), REAL(criterion));
00291
00292 PutRNGstate();
00293
00294 UNPROTECT(1);
00295 return(ans);
00296 }