neuralGAM
objectR/predict.NeuralGAM.R
predict.NeuralGAM.Rd
Takes a fitted neuralGAM
object produced by
neuralGAM()
and produces predictions given a new set of values for the model covariates.
# S3 method for class 'neuralGAM'
predict(object, newdata = NULL, type = "link", terms = NULL, verbose = 1, ...)
a fitted `neuralGAM` object
A data frame or list containing the values of covariates at which predictions are required. If not provided, the function returns the predictions for the original training data.
when type="link"
(default), the linear
predictor is returned. When type="terms"
each component of the linear
predictor is returned separately on each column of a data.frame
. When
type="response"
predictions on the scale of the response are returned.
If type="terms"
, then only results for the terms named
in this list will be returned. If NULL
then no terms are excluded (default).
Verbosity mode (0 = silent, 1 = print messages). Defaults to 1.
Other options.
Predicted values according to type
parameter.
# \dontrun{
n <- 24500
seed <- 42
set.seed(seed)
x1 <- runif(n, -2.5, 2.5)
x2 <- runif(n, -2.5, 2.5)
x3 <- runif(n, -2.5, 2.5)
f1 <-x1**2
f2 <- 2*x2
f3 <- sin(x3)
f1 <- f1 - mean(f1)
f2 <- f2 - mean(f2)
f3 <- f3 - mean(f3)
eta0 <- 2 + f1 + f2 + f3
epsilon <- rnorm(n, 0.25)
y <- eta0 + epsilon
train <- data.frame(x1, x2, x3, y)
library(neuralGAM)
ngam <- neuralGAM(y ~ s(x1) + x2 + s(x3), data = train,
num_units = 1024, family = "gaussian",
activation = "relu",
learning_rate = 0.001, bf_threshold = 0.001,
max_iter_backfitting = 10, max_iter_ls = 10,
seed = seed
)
#> [1] "Initializing neuralGAM..."
#> [1] "BACKFITTING Iteration 1 - Current Err = 0.32048205436 BF Threshold = 0.001 Converged = FALSE"
#> [1] "BACKFITTING Iteration 2 - Current Err = 0.000623513488713123 BF Threshold = 0.001 Converged = TRUE"
n <- 5000
x1 <- runif(n, -2.5, 2.5)
x2 <- runif(n, -2.5, 2.5)
x3 <- runif(n, -2.5, 2.5)
test <- data.frame(x1, x2, x3)
# Obtain linear predictor
eta <- predict(ngam, test, type = "link")
# Obtain predicted response
yhat <- predict(ngam, test, type = "response")
# Obtain each component of the linear predictor
terms <- predict(ngam, test, type = "terms")
# Obtain only certain terms:
terms <- predict(ngam, test, type = "terms", terms = c("x1", "x2"))
# }