Advanced neuralGAM visualization with ggplot2 library

# S3 method for class 'neuralGAM'
autoplot(object, select, xlab = NULL, ylab = NULL, ...)

Arguments

object

a fitted neuralGAM object as produced by neuralGAM().

select

selects the term to be plotted.

xlab

A title for the x axis.

ylab

A title for the y axis.

...

other graphics parameters to pass on to plotting commands. See details for ggplot2::geom_line options

Value

A ggplot object, so you can use common features from the ggplot2 package to manipulate the plot.

Author

Ines Ortega-Fernandez, Marta Sestelo.

Examples

# \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"
autoplot(ngam, select="x1")


# add custom title
autoplot(ngam, select="x1") + ggplot2::ggtitle("Main Title")

# add labels
autoplot(ngam, select="x1") + ggplot2::xlab("test") + ggplot2::ylab("my y lab")

# plot multiple terms:
plots <- lapply(c("x1", "x2", "x3"), function(x) autoplot(ngam, select = x))
gridExtra::grid.arrange(grobs = plots, ncol = 3, nrow = 1)

# }