Learn R Programming

gratia (version 0.8.1)

add_fitted.gam: Add fitted values from a GAM to a data frame

Description

Add fitted values from a GAM to a data frame

Usage

# S3 method for gam
add_fitted(data, model, value = ".value", type = "response", prefix = ".", ...)

Value

A data frame (tibble) formed from data and predictions from model.

Arguments

data

a data frame containing values for the variables used to fit the model. Passed to stats::predict() as newdata.

model

a fitted model for which a stats::predict() method is available. S3 method dispatch is performed on the model argument.

value

character; the name of the variable in which model predictions will be stored.

type

character; the type of predictions to return. See mgcv::predict.gam() for options.

prefix

character; string to prepend to names of predicted values when type is "terms", "iterms", "lpmatrix". These prediction types result in a matrix of values being returned. prefix will be prepended to each of the names of columns returned by such prediction types.

...

additional arguments passed to mgcv::predict.gam().

Examples

Run this code

load_mgcv()
# \dontshow{
op <- options(pillar.sigfig = 3, cli.unicode = FALSE)
# }
df <- data_sim("eg1", seed = 1)
df <- df[, c("y","x0","x1","x2","x3")]
m <-  gam(y ~ s(x0) + s(x1) + s(x2) + s(x3), data = df, method = "REML")

##
add_fitted(df, m)

## with type = "terms" or "iterms"
add_fitted(df, m, type = "terms")
options(op)

Run the code above in your browser using DataLab