model_display: Display a fitted curve

Description Usage Arguments Details Value Examples

Description

Draw a plot after fitting a two-dimensional model.

Usage

1
model_display(df, ..., digits = 2)

Arguments

df

A grouped data frame, created with model_cleanly_groupwise

...

List of name-value pairs in the form aesthetic = variable describing which variables in the layer data should be mapped to which aesthetics used by the paired geom/stat. The expression variable is evaluated within the layer data, so there is no need to refer to the original dataset (i.e., use ggplot(df, aes(variable)) instead of ggplot(df, aes(df$variable))). The names for x and y aesthetics are typically omitted because they are so common; all other aesthetics must be named.

digits

Integer indicating the number of significant digits to be used when fitted parameters are substituted in the model formula. Negative values are allowed; see signif(...).

Details

The parameters of the model, usually accessible via the .$tidy column after model_cleanly_groupwise has been called, are substituted in the fitted formula and can be accessed as .$sub.formula.

Value

A ggplot that can be further customized.

Examples

 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
library(dplyr)
library(ggplot2)

# create some noisy data for fitting a linear model
x <- seq(1, 10)
y1 <- 3 * x + 5 + rnorm(10)
y2 <- 1 * x + 1 + rnorm(10)

my_data <- bind_rows(data1 = tibble(x, y = y1),
                     data2 = tibble(x, y = y2), .id = "dataset") %>%
           group_by(dataset)

my_data %>%
  model_cleanly_groupwise(lm, formula = y ~ x) %>%
  model_display(color = dataset)

## positioning of per-facet text labels in ggplot is inherently
## difficult; the visual of the example could be improved

my_data %>%
  model_cleanly_groupwise(lm, formula = y ~ x) %>%
  model_display() +
  # add substituted formula
  geom_text(aes(x = -Inf, y = Inf, label = sub.formula),
    hjust = 0, vjust = 1) +
  # add R square
  geom_text(data = . %>% tidyr::unnest(glance),
    aes(x = Inf, y = Inf, label = paste("R^2 =", signif(r.squared, 3))),
    hjust = 1, vjust = 1) +
  # show as facet
  facet_wrap(vars(dataset))

benjbuch/summerr documentation built on July 11, 2021, 9:40 a.m.