R package to parse CART objects to linear models
library(devtools)
install_github("ian-frankenburg/treeLM")
library(treeLM)
library(rpart)
library(partykit)
# fit classification tree using rpart
rpart_fit <- rpart(Kyphosis ~ Age + Number + Start, data = kyphosis)
# design matrix
X_rpart <- treelm(rpart_fit)
# fit classification tree using conditional inference trees
ctree_fit <- ctree(Kyphosis ~ Age + Number + Start, data = kyphosis)
# design matrix
X_ctree <- treelm(ctree_fit)
library(mlbench)
library(rpart)
data("BostonHousing")
# fit rpart model to Boston Housing data
fit_rpart <- rpart(medv ~ ., data = BostonHousing, cp = 0.001)
# get design matrix
X <- as.data.frame(treelm(fit_rpart))
y <- BostonHousing$medv
# fit linear model using design matrix from treelm function
fit_lm = lm(y~.,cbind(y,X))
# compare predictions from rpart and linear model
all.equal(predict(fit_rpart,newdata = BostonHousing),predict(fit_lm,newdata = X))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.