R: Create a model of a data set and compute predictions.

compute_model_prediction {ggvis} | R Documentation

## Create a model of a data set and compute predictions.

### Description

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

### Usage

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

    compute_smooth(x, formula, ..., span = 0.75, se = FALSE)

### Arguments

`x` | Dataset-like object to model and predict. Built-in methods for data frames, grouped data frames and ggvis visualisations.
`formula` | Formula passed to modelling function. Can use any variables from data.
`...` | arguments passed on to `model` function
`model` | 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 `loess` for <= 1000 points, otherwise it will use `gam`. Other modelling functions that will work include `lm`, `glm` and `rlm`.
`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
`domain` | If `NULL` (the default), the domain of the predicted values will be the same as the domain of the prediction variable in the data. It can also be a two-element numeric vector specifying the min and max.
`method` | Deprecated. Please use `model` instead.
`span` | Smoothing span used for loess model.

### Details

`compute_model_prediction` fits a model to the data and makes predictions with it. `compute_smooth` is a special case of model predictions where the model is a smooth loess curve whose smoothness is controlled by the `span` parameter.

### Value

A data frame with columns:

``resp_`` | regularly spaced grid of `n` locations
``pred_`` | predicted value from model
``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

    # Use a small value of n for these examples
    mtcars %>% compute_model_prediction(mpg ~ wt, n = 10)
    mtcars %>% compute_model_prediction(mpg ~ wt, n = 10, se = TRUE)
    mtcars %>% group_by(cyl) %>% compute_model_prediction(mpg ~ wt, n = 10)

    # compute_smooth defaults to loess
    mtcars %>% compute_smooth(mpg ~ wt)

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

    # Set the domain manually
    mtcars %>%
      compute_model_prediction(mpg ~ wt, n = 20, model = "lm", domain = c(0, 8))

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

[Package ggvis version 0.4.3 Index]