Search code examples
rtreelinear-regressionrweka

Obtaining the Linear Regression Model at each Leaf for M5P model


I am trying to figure how to get the linear model at each leaf of a tree generated by M5P method in RWeka library in R as an output to text file so that I can write a separate look up calculator program (say in Excel for non-R Users).

I am using

library (RWeka)
model = M5P (response ~ predictorA+predictorB, data=train).

I can get the tree output as model$classifier in a matrix. This works great thanks to This post

If I give the command:

model

R prints the model$classifier (the tree structure), followed by the LM at each leaf, I want to extract the coefficients of LM at each leaf.


Solution

  • Using the following code: I am able to get the LM coefficients out of R.

    library(rJava)      
    ModelTree=as.matrix(scan(text=.jcall(model$classifier, "S","toString") ,sep="\n", what="") )[-c(1:2, 6), ,drop=FALSE]