Skip to content

Commit c0ebe96

Browse files
authored
adds test for quantile regression (#3774)
* adds test for quantile regression * restyled
1 parent 0253123 commit c0ebe96

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

tests/testthat/Rplot001.png

8.67 KB
Loading

tests/testthat/test-geom-quantile.R

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
context("geom-quantile")
2+
3+
test_that("geom_quantile matches quantile regression", {
4+
set.seed(6531)
5+
x <- rnorm(10)
6+
df <- tibble::tibble(
7+
x = x,
8+
y = x^2 + 0.5 * rnorm(10)
9+
)
10+
11+
ps <- ggplot(df, aes(x, y)) + geom_quantile()
12+
13+
quants <- c(0.25, 0.5, 0.75)
14+
15+
pred_rq <- predict(
16+
quantreg::rq(y ~ x,
17+
tau = quants,
18+
data = df
19+
),
20+
tibble::tibble(
21+
x = seq(min(x), max(x), length = 100)
22+
)
23+
)
24+
25+
pred_rq <- cbind(seq(min(x), max(x), length = 100), pred_rq)
26+
colnames(pred_rq) <- c("x", paste("Q", quants * 100, sep = "_"))
27+
28+
ggplot_data <- tibble::as_tibble(layer_data(ps))
29+
30+
pred_rq_test_25 <- pred_rq[, c("x", "Q_25")]
31+
colnames(pred_rq_test_25) <- c("x", "y")
32+
33+
expect_equal(
34+
ggplot_data[ggplot_data$quantile == 0.25, c("x", "y")],
35+
pred_rq_test_25
36+
)
37+
38+
pred_rq_test_50 <- pred_rq[, c("x", "Q_50")]
39+
colnames(pred_rq_test_50) <- c("x", "y")
40+
41+
expect_equal(
42+
ggplot_data[ggplot_data$quantile == 0.5, c("x", "y")],
43+
pred_rq_test_50
44+
)
45+
46+
pred_rq_test_75 <- pred_rq[, c("x", "Q_75")]
47+
colnames(pred_rq_test_75) <- c("x", "y")
48+
49+
expect_equal(
50+
ggplot_data[ggplot_data$quantile == 0.75, c("x", "y")],
51+
pred_rq_test_75
52+
)
53+
})

0 commit comments

Comments
 (0)