Skip to content

Commit

Permalink
filling untested code
Browse files Browse the repository at this point in the history
  • Loading branch information
bcjaeger committed Oct 8, 2023
1 parent 68dbd4b commit 6ff8cae
Show file tree
Hide file tree
Showing 13 changed files with 300 additions and 189 deletions.
20 changes: 10 additions & 10 deletions R/collapse_misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ data_impute <- function(data, cols, values){
}


data_impute_nocheck <- function(data, cols, values){

for(col in cols)
data <- collapse::replace_NA(data,
cols = col,
value = values[[col]])

data

}
# data_impute_nocheck <- function(data, cols, values){
#
# for(col in cols)
# data <- collapse::replace_NA(data,
# cols = col,
# value = values[[col]])
#
# data
#
# }


2 changes: 0 additions & 2 deletions R/infer.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ infer_pred_horizon <- function(object, pred_type, pred_horizon){

# throw error if pred_type requires pred_horizon
if(is.null(pred_horizon)){

stop("pred_horizon was not specified and could not be found in object.",
call. = FALSE)

}


Expand Down
2 changes: 2 additions & 0 deletions src/Forest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ void Forest::init(std::unique_ptr<Data> input_data,
// oobag denominator tracks the number of times an obs is oobag
oobag_denom.zeros(data->get_n_rows());

// # nocov start
if(verbosity > 1){

Rcout << "------------ input data dimensions ------------" << std::endl;
Expand All @@ -100,6 +101,7 @@ void Forest::init(std::unique_ptr<Data> input_data,
Rcout << std::endl;

}
// # nocov end

}

Expand Down
58 changes: 47 additions & 11 deletions src/Tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,12 @@
}

if(verbosity > 4){

// # nocov start
mat x_print = x_inbag.rows(rows_node);
Rcout << " -- Column " << j << " was sampled but ";
Rcout << "its unique values are " << unique(x_print.col(j));
Rcout << std::endl;

// # nocov end
}

return(false);
Expand Down Expand Up @@ -385,10 +385,12 @@
if(n_obs >= leaf_min_obs) {

if(verbosity > 3){
// # nocov start
Rcout << std::endl;
Rcout << " -- lower cutpoint: " << lincomb(*it) << std::endl;
Rcout << " - n_obs, left node: " << n_obs << std::endl;
Rcout << std::endl;
// # nocov end
}

break;
Expand All @@ -404,7 +406,9 @@
if(it == lincomb_sort.end()-1) {

if(verbosity > 3){
// # nocov start
Rcout << " -- Could not find a valid cut-point" << std::endl;
// # nocov end
}

return;
Expand Down Expand Up @@ -437,10 +441,12 @@
--it;

if(verbosity > 3){
// # nocov start
Rcout << std::endl;
Rcout << " -- upper cutpoint: " << lincomb(*it) << std::endl;
Rcout << " - n_obs, right node: " << n_obs << std::endl;
Rcout << std::endl;
// # nocov end
}

break;
Expand All @@ -459,7 +465,9 @@
if(j > k){

if(verbosity > 2) {
// # nocov start
Rcout << " -- Could not find valid cut-points" << std::endl;
// # nocov end
}

return;
Expand Down Expand Up @@ -502,7 +510,9 @@
double stat, stat_best = 0;

if(verbosity > 3){
// # nocov start
Rcout << " -- cutpoint (score)" << std::endl;
// # nocov end
}

for(it = cuts_sampled.begin(); it != cuts_sampled.end(); ++it){
Expand All @@ -517,21 +527,25 @@
it_start = *it;

if(verbosity > 3){
// # nocov start
Rcout << " --- ";
Rcout << lincomb.at(lincomb_sort(*it));
Rcout << " (" << stat << "), ";
Rcout << "N = " << sum(g_node % w_node) << " moving right";
Rcout << std::endl;
// # nocov end
}

}

if(verbosity > 3){
// # nocov start
Rcout << std::endl;
Rcout << " -- best stat: " << stat_best;
Rcout << ", min to split: " << split_min_stat;
Rcout << std::endl;
Rcout << std::endl;
// # nocov end
}

// do not split if best stat < minimum stat
Expand Down Expand Up @@ -587,10 +601,12 @@
void Tree::sprout_leaf(uword node_id){

if(verbosity > 2){
// # nocov start
Rcout << "-- sprouting node " << node_id << " into a leaf";
Rcout << " (N = " << sum(w_node) << ")";
Rcout << std::endl;
Rcout << std::endl;
// # nocov end
}

leaf_summary[node_id] = mean(y_node.col(0));
Expand Down Expand Up @@ -648,11 +664,13 @@
double accuracy_normal = compute_prediction_accuracy(pred_values);

if(verbosity > 1){
// # nocov start
Rcout << " -- prediction accuracy before noising: ";
Rcout << accuracy_normal << std::endl;
Rcout << " -- mean leaf pred: ";
Rcout << mean(conv_to<vec>::from(pred_leaf));
Rcout << std::endl << std::endl;
// # nocov end
}

random_number_generator.seed(seed);
Expand Down Expand Up @@ -691,11 +709,13 @@
double accuracy_permuted = compute_prediction_accuracy(pred_values);

if(verbosity > 3){
// # nocov start
Rcout << " -- prediction accuracy after noising " << pred_col << ": ";
Rcout << accuracy_permuted << std::endl;
Rcout << " - mean leaf pred: ";
Rcout << mean(conv_to<vec>::from(pred_leaf));
Rcout << std::endl << std::endl;
// # nocov end
}

double accuracy_difference = accuracy_normal - accuracy_permuted;
Expand Down Expand Up @@ -733,7 +753,7 @@
this->max_nodes = (2 * max_leaves) - 1;

if(verbosity > 2){

// # nocov start
Rcout << "- N obs inbag: " << n_obs_inbag;
Rcout << std::endl;
Rcout << "- N row inbag: " << n_rows_inbag;
Expand All @@ -743,8 +763,7 @@
Rcout << "- max leaves: " << max_leaves;
Rcout << std::endl;
Rcout << std::endl;


// # nocov end
}

// reserve memory for outputs (likely more than we need)
Expand Down Expand Up @@ -798,12 +817,13 @@
n_retry++;

if(verbosity > 3){

// # nocov start
Rcout << "-- attempting to split node " << *node;
Rcout << " (N = " << sum(w_node) << ",";
Rcout << " try number " << n_retry << ")";
Rcout << std::endl;
Rcout << std::endl;
// # nocov end
}

sample_cols();
Expand All @@ -813,7 +833,9 @@
x_node = x_inbag(rows_node, cols_node);

if(verbosity > 3) {
// # nocov start
print_uvec(cols_node, "columns sampled (showing up to 5)", 5);
// # nocov end
}

// beta holds estimates (first item) and variance (second)
Expand Down Expand Up @@ -892,7 +914,9 @@
vec beta_est = beta.unsafe_col(0);

if(verbosity > 3) {
// # nocov start
print_vec(beta_est, "linear combo weights (showing up to 5)", 5);
// # nocov end
}

bool beta_all_zeros = find(beta_est != 0).is_empty();
Expand All @@ -908,10 +932,10 @@
find_all_cuts();

if(verbosity > 3 && cuts_all.is_empty()){

// # nocov start
Rcout << " -- no cutpoints identified";
Rcout << std::endl;

// # nocov end
}

// empty cuts_all => no valid cutpoints => make leaf or retry
Expand All @@ -930,7 +954,9 @@
// 2. the method used for lincombs allows it

if(verbosity > 3){
// # nocov start
Rcout << " -- p-values:" << std::endl;
// # nocov end
}

vec beta_var = beta.unsafe_col(1);
Expand All @@ -946,15 +972,15 @@
pvalue = R::pchisq(pow(beta_est[i],2)/beta_var[i], 1, false, false);

if(verbosity > 3){

// # nocov start
Rcout << " --- column " << cols_node[i] << ": ";
Rcout << pvalue;
if(pvalue < 0.05) Rcout << "*";
if(pvalue < 0.01) Rcout << "*";
if(pvalue < 0.001) Rcout << "*";
if(pvalue < vi_max_pvalue) Rcout << " [+1 to VI numerator]";
Rcout << std::endl;

// # nocov end
}

if(pvalue < vi_max_pvalue){ (*vi_numer)[cols_node[i]]++; }
Expand All @@ -963,7 +989,11 @@

}

if(verbosity > 3){ Rcout << std::endl; }
if(verbosity > 3){
// # nocov start
Rcout << std::endl;
// # nocov end
}

}

Expand All @@ -981,11 +1011,13 @@
node_assignments.elem(rows_node) = node_left + g_node;

if(verbosity > 2){
// # nocov start
Rcout << "-- node " << *node << " was split into ";
Rcout << "node " << node_left << " (left) and ";
Rcout << node_left+1 << " (right)";
Rcout << std::endl;
Rcout << std::endl;
// # nocov end
}

nodes_queued.push_back(node_left);
Expand Down Expand Up @@ -1035,7 +1067,9 @@
if(coef_values.size() == 0) return;

if(verbosity > 2){
// # nocov start
Rcout << " -- computing leaf predictions" << std::endl;
// # nocov end
}

uvec obs_in_node;
Expand Down Expand Up @@ -1080,12 +1114,14 @@
}

if(verbosity > 4){
// # nocov start
uvec in_left = find(pred_leaf == child_left[i]);
uvec in_right = find(pred_leaf == child_left[i]+1);
Rcout << "No. to node " << child_left[i] << ": ";
Rcout << in_left.size() << "; " << std::endl;
Rcout << "No. to node " << child_left[i]+1 << ": ";
Rcout << in_right.size() << std::endl << std::endl;
// # nocov end
}

}
Expand Down
Loading

0 comments on commit 6ff8cae

Please sign in to comment.