-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
94 lines (70 loc) · 2.22 KB
/
README.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
---
output: github_document
bibliography: inst/REFERENCES.bib
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# Extremal Random Forests
<!-- badges: start -->
[![R build status](https://github.com/nicolagnecco/erf/workflows/R-CMD-check/badge.svg)](https://github.com/nicolagnecco/erf/actions)
<!-- badges: end -->
The package `erf` implements the extremal random forests (ERF), an algorithm to
predict extreme conditional quantiles in large dimensions. For more details see @merg2020 [https://arxiv.org/abs/2201.12865].
## Installation
<!-- You can install the released version of erf from [CRAN](https://CRAN.R-project.org) with: -->
``` r
install.packages("erf")
```
You can install the development version from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("nicolagnecco/erf")
```
## Example
This basic example shows how to fit and predict conditional quantiles with `erf`.
```{r example1, message=FALSE}
library(erf)
library(ggplot2)
library(dplyr)
# Function to model scale
scale_step <- function(X) {
## numeric_vecotr -> numeric_vector
## produce scale function: scale(X) = step function
sigma_x <- 1 + 1 * (X > 0)
return(sigma_x)
}
# Generate data
set.seed(42)
n <- 2000
p <- 10
X <- matrix(runif(n * p, min = -1, max = 1), n, p)
Y <- scale_step(X[, 1]) * rnorm(n)
# Fit ERF
fit_erf <- erf(X, Y, intermediate_quantile = 0.8)
# Predict ERF
quantiles <- c(0.9, 0.99)
pred_erf <- predict(fit_erf, newdata = X, quantiles = quantiles)
true_quantiles <- matrix(rep(qnorm(quantiles), n),
ncol = length(quantiles),
byrow = TRUE) * scale_step(X[, 1])
# Plot results
my_palette <- list(
"red" = "#D55E00",
"blue" = "#0072B2"
)
ggplot() +
geom_point(aes(x = X[, 1], y = Y), alpha = .5, col = "grey") +
geom_point(aes(x = X[, 1], y = pred_erf[, 2]), alpha = .5,
col = my_palette$blue) +
geom_line(aes(x = X[, 1], y = true_quantiles[, 2]), col = my_palette$red,
linetype = "dashed", size = 1) +
theme_bw()
```
## References