-
Notifications
You must be signed in to change notification settings - Fork 1
/
Example_GradientCOBRA.Rmd
80 lines (58 loc) · 2.23 KB
/
Example_GradientCOBRA.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
---
title: "Example"
author: "Sothea HAS"
date: "2023-06-22"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
if(!('pacman' %in% installed.packages()[,1])){
install.packages('pacman')
}
pacman::p_load(MASS)
pacman::p_load(devtools)
```
## Loading the source codes with `devtools`
```{r}
devtools::source_url("https://raw.githubusercontent.com/hassothea/AggregationMethods/main/GradientCOBRARegressor.R")
```
### 1. Boston housing
```{r}
Boston |> head()
X <- Boston[c("crim", "indus", "nox", "rm", "age", "dis", "rad", "tax", "ptratio", "black", "lstat")]
y <- Boston$medv
n <- length(y)
train <- logical(length = n)
train[sample(n, floor(0.8*n))] <- TRUE
```
Using the aggregation method:
```{r}
reg <- GradientCOBRARegressor(train_design = X[train,],
train_response = y[train],
test_design = X[!train,])
```
```{r}
# Relative Root Mean Squared Error
reg$fitted_aggregate |> map(.f = ~ sqrt(mean((y[!train] - .x)^2))/mean(y[!train]))
```
You can also use more than one kernel function at a time, with other options of basic estimators and learning process as follow:
```{r}
reg1 <- GradientCOBRARegressor(train_design = X[train,],
train_response = y[train],
test_design = X[!train,],
kernels = c('naive', 'gaussian'),
optimizeMethod = c("grid", "grad"),
machines = c('knn', 'rf', 'xgb', 'ridge', 'lasso'),
setBasicMachineParam = setBasicParameter(k = 5:10,
ntree = 300,
nrounds_xgb = c(300, 500)),
setGradParam = setGradParameter(rate = 0.01),
setGridParam = setGridParameter(figure = FALSE))
```
```{r}
# Relative Root Mean Squared Error
reg1$fitted_aggregate |> map(.f = ~ sqrt(mean((y[!train] - .x)^2))/mean(y[!train]))
```
For how to you the function, see: [https://hassothea.github.io/files/CodesPhD/GradientCOBRARegressor.html](https://hassothea.github.io/files/CodesPhD/GradientCOBRARegressor.html).