I am trying to build a basic regression Tree in R FROM SCRATCH (I know of rpart, tree, RandomForest,etc.). But it is just something that I would want to code myself for my culture.
In terms of pseudo-code it should look something like that:
while(!stopping_condition){
for (leaf in all_the_leaves_in_that_layer){
min_over_s_and_j=min_over_s_for_variable_j=criteria(first_s,first_v)
for (v in variables){
for (s in possible_split_values_for_variable_v){
if (criteria(s,v)<min_over_s_for_variable_j){
min_over_s_for_variable_j<-criteria(s,v)}}
if (min_over_s_for_variable_j<min_over_s_and_j){
min_over_s_and_j<-min_over_s_for_variable_j}}
tree=split_tree(tree,chosen_j,chosen_s)
}}
But I am at a loss regarding what structure to use for the tree and how to deal with all the leaves. My first thought was to have a tree as a list of list of list etc. but to access a specific layer of nodes I don't know what command to use ( I want to be able to prune it myself afterwards). Do you think it is worth implementing a new class in R ? Can I use something already created (for the class) ?
Also do you think the structure of my code is correct or could it be improved ?
I am also at a loss on how the splits should be for a continuous variables I know that you have to sort the values in the variable you want to split but afterwards do you create 2*(n-2)+2 splits by using each value as a threshold the two extremes giving birth to only two thresholds but each one in the middle creating two splits (the point corresponding to the value either left or right of the split). Or do you take the middle of each segment ? I dont know if I am being clear enough.
Do you know if R is a good choice of language to do it ?
Do you know some good step-by-step tutorials ?(I also accept them in C++,Java or Python)