Learn R Programming

ggvis (version 0.3.0.1)

compute_smooth: Smooth data with a model.

Description

Fit a 1d model, then compute predictions and (optionally) standard errors over an evenly spaced grid.

Usage

compute_smooth(x, formula, ..., method = NULL, se = FALSE, level = 0.95,
  n = 80L)

Arguments

x
Dataset-like object to smooth. Built-in methods for data frames, grouped data frames and ggvis visualisations.
method
Model fitting function to use - it must support R's standard modelling interface, taking a formula and data frame as input, and returning predictions with predict. If not supplied, will use
formula
Formula passed to modelling function. Can use any variables from data.
se
include standard errors in output? Requires appropriate method of predict_grid, since the interface for returning predictions with standard errors is not consistent acrossing modelling frameworks.
level
the confidence level of the standard errors.
n
the number of grid points to use in the prediction
...
arguments passed on to method function

Value

  • A data frame with columns:
  • resp_regularly spaced grid of n locations
  • pred_predicted value from smooth
  • pred_lwr_ and pred_upr_upper and lower bounds of confidence interval (if se = TRUE)
  • pred_se_the standard error (width of the confidence interval) (if se = TRUE)

Examples

Run this code
mtcars %>% compute_smooth(mpg ~ wt, n = 10)
mtcars %>% compute_smooth(mpg ~ wt, n = 10, se = TRUE)
mtcars %>% group_by(cyl) %>% compute_smooth(mpg ~ wt, n = 10)

# Override method to suppress message or change approach
mtcars %>% compute_smooth(mpg ~ wt, n = 10, method = "loess")
mtcars %>% compute_smooth(mpg ~ wt, n = 10, method = "lm")

# Plot the results
mtcars %>% compute_smooth(mpg ~ wt) %>% ggvis(~pred_, ~resp_) %>% layer_paths()
mtcars %>% ggvis() %>% compute_smooth(mpg ~ wt) %>% layer_paths(~pred_, ~resp_)

Run the code above in your browser using DataLab