Skip to contents

Prepare a data slice through model covariates

Usage

data_slice(object, ...)

# S3 method for default
data_slice(object, ...)

# S3 method for data.frame
data_slice(object, ...)

# S3 method for gam
data_slice(object, ..., data = NULL, envir = NULL)

# S3 method for gamm
data_slice(object, ...)

# S3 method for list
data_slice(object, ...)

# S3 method for scam
data_slice(object, ...)

Arguments

object

an R model object.

...

<dynamic-dots> User supplied variables defining the data slice. Arguments passed via ... need to named

data

an alternative data frame of values containing all the variables needed to fit the model. If NULL, the default, the data used to fit the model will be recovered using model.frame. User-supplied expressions passed in ... will be evaluated in data.

envir

the environment within which to recreate the data used to fit object.

Examples

# \dontshow{
op <- options(pillar.sigfig = 3)
# }
load_mgcv()

# simulate some Gaussian data
df <- data_sim("eg1", n = 50, seed = 2)

# fit a GAM with 1 smooth and 1 linear term
m <- gam(y ~ s(x2, k = 7) + x1, data = df, method = "REML")

# Want to predict over f(x2) while holding `x1` at some value.
# Default will use the observation closest to the median for unspecified
# variables.
ds <- data_slice(m, x2 = evenly(x2, n = 50))
ds
#> # A tibble: 50 x 2
#>        x2    x1
#>     <dbl> <dbl>
#>  1 0.0228 0.403
#>  2 0.0424 0.403
#>  3 0.0619 0.403
#>  4 0.0815 0.403
#>  5 0.101  0.403
#>  6 0.121  0.403
#>  7 0.140  0.403
#>  8 0.160  0.403
#>  9 0.179  0.403
#> 10 0.199  0.403
#> # i 40 more rows

# for full control, specify the values you want
ds <- data_slice(m, x2 = evenly(x2, n = 50), x1 = 0.3)

# or provide an expression (function call) which will be evaluated in the
# data frame passed to `data` or `model.frame(object)`
ds <- data_slice(m, x2 = evenly(x2, n = 50), x1 = mean(x1))