Skip to contents

Generate regular data over the covariates of a smooth

Usage

smooth_data(
  model,
  id,
  n = 100,
  n_2d = NULL,
  n_3d = NULL,
  n_4d = NULL,
  offset = NULL,
  include_all = FALSE,
  var_order = NULL,
  envir = NULL,
  back_transform = FALSE
)

Arguments

model

a fitted model

id

the number ID of the smooth within model to process.

n

numeric; the number of new observations to generate.

n_2d

numeric; the number of points along each of the first two axes of a smooth surface, including surface panels of higher-dimensional smooths. The default, NULL, uses n instead. Factor levels are retained, and curves with only one continuous covariate use n.

n_3d

numeric; the number of new observations to generate for the third dimension of a 3D smooth. For factor conditioning dimensions, retain at most this many levels, taking the first in fitted order.

n_4d

numeric; the number of new observations to generate for the dimensions higher than 2 (!) of a kD smooth (k >= 4). For example, if the smooth is a 4D smooth, each of dimensions 3 and 4 will get n_4d new observations. For factor conditioning dimensions, retain at most this many levels per dimension, taking the first in fitted order.

offset

numeric; value of the model offset to use.

include_all

logical; include all covariates involved in the smooth? if FALSE, only the covariates involved in the smooth will be included in the returned data frame. If TRUE, a representative value will be included for all other covariates in the model that aren't actually used in the smooth. This can be useful if you want to pass the returned data frame on to mgcv::PredictMat().

var_order

character; the order in which the terms in the smooth should be processed. Only useful for tensor products with at least one 2d marginal smooth.

envir

an optional environment supplying functions and constants used in model expressions. The available model formula environment is used when NULL. Covariate observations should be supplied in data.

back_transform

logical; generate the grid on original covariate scales? The default, FALSE, uses evenly spaced model coordinates. With TRUE, recover raw fitting rows and generate original-scale coordinates, then evaluate the model expressions forwards. Each axis must depend on one distinct original model covariate. Both raw and evaluated columns are returned, with auxiliary .gratia_raw_* columns for transformed axes. Missing raw inputs or unavailable functions produce an error. Numeric by multipliers are one on automatic grids, including when a multiplier shares an original axis name; raw axis coordinates are retained separately. If the multiplier is itself an untransformed grid axis, supply prediction data to smooth_estimates() instead. Typical values added by include_all = TRUE to model-scale grids do not provide raw coordinates for transformed axes.

Details

Spline-on-the-sphere grids use evenly spaced observed-range latitude and longitude values. Plotting constructs geographic cell edges separately, without moving these evaluation coordinates.

Examples

load_mgcv()
df <- data_sim("eg1", seed = 42)
m <- bam(y ~ s(x0) + s(x1) + s(x2) + s(x3), data = df)

# generate data over range of x1 for smooth s(x1)
smooth_data(m, id = 2)
#> # A tibble: 100 x 1
#>           x1
#>        <dbl>
#>  1 0.0004050
#>  2 0.01046  
#>  3 0.02052  
#>  4 0.03057  
#>  5 0.04063  
#>  6 0.05069  
#>  7 0.06074  
#>  8 0.07080  
#>  9 0.08086  
#> 10 0.09091  
#> # i 90 more rows

# generate data over range of x1 for smooth s(x1), with typical value for
# other covariates in the model
smooth_data(m, id = 2, include_all = TRUE)
#> # A tibble: 100 x 4
#>           x1     x0     x2     x3
#>        <dbl>  <dbl>  <dbl>  <dbl>
#>  1 0.0004050 0.4883 0.4708 0.4879
#>  2 0.01046   0.4883 0.4708 0.4879
#>  3 0.02052   0.4883 0.4708 0.4879
#>  4 0.03057   0.4883 0.4708 0.4879
#>  5 0.04063   0.4883 0.4708 0.4879
#>  6 0.05069   0.4883 0.4708 0.4879
#>  7 0.06074   0.4883 0.4708 0.4879
#>  8 0.07080   0.4883 0.4708 0.4879
#>  9 0.08086   0.4883 0.4708 0.4879
#> 10 0.09091   0.4883 0.4708 0.4879
#> # i 90 more rows