#Install last ve
#library(devtools)
#install_github("acca3003/ngboostR")

library(ngboostR) # R implementation for NGBoost
library(Metrics) # Métrics
library(MASS) # boston houses dataset
library(caret)


data(Boston)
set.seed(999)
trainIndex <- createDataPartition(Boston$medv, p = .8, 
                                  list = FALSE, 
                                  times = 1)

X_train = Boston[trainIndex,1:13]
Y_train = Boston[trainIndex,14]

X_val = Boston[-trainIndex,1:13]
Y_val = Boston[-trainIndex,14]



# Create the regressor object
# reg_ngboost <- create_regressor() # Default parameters
reg_ngboost <- create_regressor(Dist=Normal(),
                                Base=DecisionTreeRegressor(
                                  criterion="mae",
                                  min_samples_split=2,
                                  min_samples_leaf=1,
                                  min_weight_fraction_leaf=0.0,
                                  max_depth=5,
                                  splitter="best",
                                  random_state=NULL),
                                natural_gradient=TRUE,
                                n_estimators=as.integer(600),
                                learning_rate=0.002,
                                minibatch_frac=0.8,
                                col_sample=0.9,
                                verbose=TRUE,
                                verbose_eval=as.integer(50),
                                tol=1e-5)
# Train with the boston data
fit_regressor(reg_ngboost, X_train, Y_train, X_val, Y_val)

# Predict the price
predictions <- predict_regressor(reg_ngboost, X_val)
Metrics::rmse(Y_val,predictions)

# Predict the price as a distribution
predictions_dist <- predict_regressor_dist(reg_ngboost, X_val)
predictions_dist


acca3003/ngboostR documentation built on Dec. 18, 2021, 10:21 p.m.