Created
July 14, 2021 12:40
-
-
Save thengl/02272afaff6de55d2198a9dfd84c09dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(mlr) | |
## Synthetic data ---- | |
set.seed(200) | |
n = 100 | |
x <- 1:n | |
y <- x + rnorm(n = 50, mean = 15, sd = 15) | |
dat <- data.frame(x,y) | |
## newdata: | |
newdata <- data.frame( | |
x = -100:200 | |
) | |
## Fir Ensemble ML using mlr ---- | |
SL.library = c("regr.ranger", "regr.glm", "regr.gamboost", "regr.ksvm") | |
lrns <- lapply(SL.library, mlr::makeLearner) | |
tsk <- mlr::makeRegrTask(data = dat, target = "y") | |
init.m <- mlr::makeStackedLearner(lrns, method = "stack.cv", super.learner = "regr.lm", resampling=mlr::makeResampleDesc(method = "CV")) | |
eml = train(init.m, tsk) | |
summary(eml$learner.model$super.model$learner.model) | |
# Residuals: | |
# Min 1Q Median 3Q Max | |
# -25.8665 -7.1403 0.0125 6.9210 27.7502 | |
# | |
# Coefficients: | |
# Estimate Std. Error t value Pr(>|t|) | |
# (Intercept) -2.3088 2.8341 -0.815 0.4173 | |
# regr.ranger -0.4184 0.2201 -1.901 0.0603 . | |
# regr.glm 4.7697 0.9230 5.168 1.31e-06 *** | |
# regr.gamboost -4.1556 1.0181 -4.082 9.31e-05 *** | |
# regr.ksvm 0.8295 0.3419 2.426 0.0171 * | |
# --- | |
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 | |
# | |
# Residual standard error: 10.89 on 95 degrees of freedom | |
# Multiple R-squared: 0.8796, Adjusted R-squared: 0.8745 | |
# F-statistic: 173.5 on 4 and 95 DF, p-value: < 2.2e-16 | |
## GLM is the best model, RF and kSVM are on the edge |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment