Return predictions from a GAM model generated with mgcv. The output can be plotted with plot()
.
Usage
predict_gam(
model,
length_out = 10,
values = NULL,
series = NULL,
exclude_terms = NULL,
ci_z = 1.96,
tran_fun = NULL,
separate = NULL,
sep_by = "\\."
)
Arguments
- model
A
gam
orbam
model object.- length_out
An integer indicating how many values to use along the numeric variables for predicting the response (the default is
10
).- values
User supplied values for specific variables as a named list.
- series
A string specifying the variable that corresponds to the series to be plotted on the $x$-axis. If a string is given, the other numeric variables in the model are set to their mean value, unless specific values are given in
values
. If a character vector of two strings is given, the two variables will be taken as the elements of a tensor product smooth. This allows the user to plot 2D raster plots.- exclude_terms
Terms to be excluded from the prediction. Term names should be given as they appear in the model summary (for example,
"s(x0,x1)"
).- ci_z
The z-value for calculating the CIs (the default is
1.96
for 95 percent CI).- tran_fun
Function to use for transforming the predicted values and CIs.
- separate
Names list of factor interaction variables to be separated.
- sep_by
Character to separate by (the default is
\\.
).
Examples
library(mgcv)
set.seed(10)
sim_data_1 <- gamSim(1, n = 200, scale = 2)
#> Gu & Wahba 4 term additive model
model <- gam(y ~ x0 + s(I(x1^2)) + s(x2) + offset(x3), data = sim_data_1)
predict_gam(model)
#> Warning: Could not recover model data from environment. Please make sure your
#> data is available in your workspace.
#> Trying to retrieve data from the model frame now.
#> # A tibble: 1,331 × 8
#> x0 x1 x2 x3 y se lower_ci[,1] upper_ci[,1]
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 0.0144 0.0000639 0.00217 0 -0.247 0.874 -1.96 1.47
#> 2 0.112 0.0000639 0.00217 0 -0.151 0.853 -1.82 1.52
#> 3 0.209 0.0000639 0.00217 0 -0.0548 0.837 -1.69 1.58
#> 4 0.306 0.0000639 0.00217 0 0.0414 0.824 -1.57 1.66
#> 5 0.403 0.0000639 0.00217 0 0.138 0.815 -1.46 1.73
#> 6 0.501 0.0000639 0.00217 0 0.234 0.810 -1.35 1.82
#> 7 0.598 0.0000639 0.00217 0 0.330 0.810 -1.26 1.92
#> 8 0.695 0.0000639 0.00217 0 0.426 0.815 -1.17 2.02
#> 9 0.792 0.0000639 0.00217 0 0.522 0.823 -1.09 2.14
#> 10 0.890 0.0000639 0.00217 0 0.619 0.836 -1.02 2.26
#> # ℹ 1,321 more rows
predict_gam(model, values = list(x0 = mean(sim_data_1$x0)))
#> Warning: Could not recover model data from environment. Please make sure your
#> data is available in your workspace.
#> Trying to retrieve data from the model frame now.
#> # A tibble: 121 × 8
#> x0 x1 x2 x3 y se lower_ci[,1] upper_ci[,1]
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 0.474 0.0000639 0.00217 0 0.207 0.811 -1.38 1.80
#> 2 0.474 0.0998 0.00217 0 0.249 0.808 -1.33 1.83
#> 3 0.474 0.200 0.00217 0 0.375 0.799 -1.19 1.94
#> 4 0.474 0.299 0.00217 0 0.584 0.790 -0.963 2.13
#> 5 0.474 0.399 0.00217 0 0.876 0.786 -0.664 2.42
#> 6 0.474 0.499 0.00217 0 1.26 0.787 -0.282 2.80
#> 7 0.474 0.598 0.00217 0 1.78 0.790 0.233 3.33
#> 8 0.474 0.698 0.00217 0 2.48 0.790 0.932 4.03
#> 9 0.474 0.798 0.00217 0 3.38 0.795 1.82 4.94
#> 10 0.474 0.898 0.00217 0 4.50 0.826 2.88 6.12
#> # ℹ 111 more rows
predict_gam(model, series = "x2")
#> Warning: Could not recover model data from environment. Please make sure your
#> data is available in your workspace.
#> Trying to retrieve data from the model frame now.
#> # A tibble: 11 × 8
#> x0 x1 x2 x3 y se lower_ci[,1] upper_ci[,1]
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 0.474 0.362 0.00217 0 0.757 0.787 -0.785 2.30
#> 2 0.474 0.362 0.102 0 7.36 0.478 6.42 8.30
#> 3 0.474 0.362 0.202 0 11.5 0.489 10.6 12.5
#> 4 0.474 0.362 0.301 0 10.4 0.452 9.51 11.3
#> 5 0.474 0.362 0.401 0 6.93 0.440 6.07 7.79
#> 6 0.474 0.362 0.501 0 6.53 0.517 5.52 7.55
#> 7 0.474 0.362 0.601 0 7.05 0.463 6.14 7.96
#> 8 0.474 0.362 0.701 0 5.79 0.467 4.88 6.71
#> 9 0.474 0.362 0.800 0 3.69 0.496 2.72 4.66
#> 10 0.474 0.362 0.900 0 2.44 0.458 1.54 3.33
#> 11 0.474 0.362 1.00 0 3.14 0.701 1.76 4.51
predict_gam(model, exclude_terms = "s(I(x1^2))")
#> Warning: Could not recover model data from environment. Please make sure your
#> data is available in your workspace.
#> Trying to retrieve data from the model frame now.
#> # A tibble: 1,331 × 8
#> x0 x1 x2 x3 y se lower_ci[,1] upper_ci[,1]
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 0.0144 0.0000639 0.00217 0 1.55 0.835 -0.0909 3.18
#> 2 0.112 0.0000639 0.00217 0 1.64 0.815 0.0450 3.24
#> 3 0.209 0.0000639 0.00217 0 1.74 0.798 0.173 3.30
#> 4 0.306 0.0000639 0.00217 0 1.83 0.786 0.293 3.38
#> 5 0.403 0.0000639 0.00217 0 1.93 0.778 0.405 3.46
#> 6 0.501 0.0000639 0.00217 0 2.03 0.775 0.508 3.55
#> 7 0.598 0.0000639 0.00217 0 2.12 0.776 0.602 3.64
#> 8 0.695 0.0000639 0.00217 0 2.22 0.782 0.687 3.75
#> 9 0.792 0.0000639 0.00217 0 2.32 0.792 0.763 3.87
#> 10 0.890 0.0000639 0.00217 0 2.41 0.806 0.831 3.99
#> # ℹ 1,321 more rows
# By-variables
sim_data_2 <- gamSim(4)
#> Factor `by' variable example
model_2 <- gam(y ~ s(x2, by = fac) + s(x0), data = sim_data_2)
predict_gam(model_2)
#> Warning: Could not recover model data from environment. Please make sure your
#> data is available in your workspace.
#> Trying to retrieve data from the model frame now.
#> # A tibble: 363 × 7
#> x2 fac x0 y se lower_ci[,1] upper_ci[,1]
#> <dbl> <fct> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 0.00161 2 0.00130 -0.789 0.609 -1.98 0.405
#> 2 0.101 2 0.00130 -0.612 0.417 -1.43 0.206
#> 3 0.201 2 0.00130 -0.407 0.352 -1.10 0.283
#> 4 0.300 2 0.00130 -0.0976 0.352 -0.788 0.592
#> 5 0.399 2 0.00130 0.335 0.363 -0.377 1.05
#> 6 0.499 2 0.00130 0.861 0.371 0.133 1.59
#> 7 0.598 2 0.00130 1.49 0.372 0.761 2.22
#> 8 0.698 2 0.00130 2.24 0.370 1.52 2.97
#> 9 0.797 2 0.00130 3.12 0.384 2.36 3.87
#> 10 0.897 2 0.00130 4.10 0.463 3.19 5.01
#> # ℹ 353 more rows
# Poisson data
sim_data_3 <- sim_data_2
sim_data_3$y <- round(sim_data_2$y) + 20
model_3 <- gam(y ~ s(x2, by = fac), data = sim_data_3, family = poisson)
predict_gam(model_3, length_out = 50)
#> Warning: Could not recover model data from environment. Please make sure your
#> data is available in your workspace.
#> Trying to retrieve data from the model frame now.
#> # A tibble: 153 × 6
#> x2 fac y se lower_ci[,1] upper_ci[,1]
#> <dbl> <fct> <dbl> <dbl> <dbl> <dbl>
#> 1 0.00161 2 2.94 0.0382 2.86 3.01
#> 2 0.0215 2 2.94 0.0358 2.87 3.01
#> 3 0.0414 2 2.95 0.0336 2.88 3.01
#> 4 0.0613 2 2.95 0.0314 2.89 3.01
#> 5 0.0812 2 2.95 0.0295 2.90 3.01
#> 6 0.101 2 2.96 0.0276 2.90 3.01
#> 7 0.121 2 2.96 0.0260 2.91 3.01
#> 8 0.141 2 2.97 0.0245 2.92 3.01
#> 9 0.161 2 2.97 0.0231 2.92 3.01
#> 10 0.181 2 2.97 0.0220 2.93 3.02
#> # ℹ 143 more rows
predict_gam(model_3, length_out = 50, tran_fun = exp)
#> Warning: Could not recover model data from environment. Please make sure your
#> data is available in your workspace.
#> Trying to retrieve data from the model frame now.
#> # A tibble: 153 × 6
#> x2 fac y[,1] se lower_ci[,1] upper_ci[,1]
#> <dbl> <fct> <dbl> <dbl> <dbl> <dbl>
#> 1 0.00161 2 18.9 0.0382 17.5 20.4
#> 2 0.0215 2 19.0 0.0358 17.7 20.3
#> 3 0.0414 2 19.0 0.0336 17.8 20.3
#> 4 0.0613 2 19.1 0.0314 18.0 20.3
#> 5 0.0812 2 19.2 0.0295 18.1 20.3
#> 6 0.101 2 19.3 0.0276 18.2 20.3
#> 7 0.121 2 19.3 0.0260 18.4 20.3
#> 8 0.141 2 19.4 0.0245 18.5 20.4
#> 9 0.161 2 19.5 0.0231 18.6 20.4
#> 10 0.181 2 19.6 0.0220 18.7 20.4
#> # ℹ 143 more rows
# Bivariate smooths
model_4 <- gam(y ~ te(x1, x2), data = sim_data_1)
predict_gam(model_4)
#> Warning: Could not recover model data from environment. Please make sure your
#> data is available in your workspace.
#> Trying to retrieve data from the model frame now.
#> # A tibble: 121 × 6
#> x1 x2 y se lower_ci[,1] upper_ci[,1]
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 0.00799 0.00217 4.24 1.62 1.06 7.42
#> 2 0.107 0.00217 3.81 1.21 1.44 6.17
#> 3 0.206 0.00217 3.45 1.03 1.43 5.47
#> 4 0.305 0.00217 3.25 0.996 1.30 5.21
#> 5 0.404 0.00217 3.24 0.997 1.29 5.20
#> 6 0.503 0.00217 3.40 1.01 1.42 5.38
#> 7 0.602 0.00217 3.71 0.958 1.83 5.58
#> 8 0.701 0.00217 4.17 0.916 2.38 5.97
#> 9 0.801 0.00217 4.85 1.03 2.83 6.88
#> 10 0.900 0.00217 5.74 1.39 3.00 8.47
#> # ℹ 111 more rows