Title: | Tidy Prediction and Plotting of Generalised Additive Models |
---|---|
Description: | Provides functions that compute predictions from Generalised Additive Models (GAMs) fitted with 'mgcv' and return them as a tibble. These can be plotted with a generic plot()-method that uses 'ggplot2' or plotted as any other data frame. The main function is predict_gam(). |
Authors: | Stefano Coretta [aut, cre] |
Maintainer: | Stefano Coretta <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.0 |
Built: | 2024-12-18 23:50:44 UTC |
Source: | CRAN |
This data table contains counts of three type of gestures performed by 60 infants from Bengali, Chinese and British backgrounds.
gest
gest
A tibble with 540 observations and 5 variables:
Unique parent/infant dyad ID.
Cultural background of dyad.
Time point in infant months.
Type of gesture.
Number of gestures.
Get difference between two smooths
get_difference( model, series, compare, values = NULL, exclude_terms = NULL, length_out = 25, ci_z = 1.96 )
get_difference( model, series, compare, values = NULL, exclude_terms = NULL, length_out = 25, ci_z = 1.96 )
model |
A |
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 |
compare |
A named list of factor levels to compare. |
values |
User supplied values for specific variables as a named list. |
exclude_terms |
Terms to be excluded from the prediction. Term names
should be given as they appear in the model summary (for example,
|
length_out |
An integer indicating how many values to use along the
numeric variables for predicting the response (the default is |
ci_z |
The z-value for calculating the CIs (the default is |
A tibble with the difference smooth.
library(mgcv) set.seed(10) data <- gamSim(4) model <- gam(y ~ s(x2, by = fac) + s(x0), data = data) get_difference(model, "x2", list(fac = c("1", "2")))
library(mgcv) set.seed(10) data <- gamSim(4) model <- gam(y ~ s(x2, by = fac) + s(x0), data = data) get_difference(model, "x2", list(fac = c("1", "2")))
Plotting methods for tidygam
objects.
## S3 method for class 'tidygam' plot(x, series = NULL, comparison = NULL, raster_interp = FALSE, ...)
## S3 method for class 'tidygam' plot(x, series = NULL, comparison = NULL, raster_interp = FALSE, ...)
x |
A |
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 |
comparison |
Name of a categorical predictor to compare as a string. |
raster_interp |
Whether to linearly interpolate when plotting a tensor
product smooth/interaction. It makes sense only when |
... |
Arguments passed to |
A ggplot
object.
library(mgcv) set.seed(10) sim_data <- gamSim(4) model_1 <- gam(y ~ s(x2, by = fac) + s(x0), data = sim_data) preds_1 <- predict_gam(model_1, length_out = 50, exclude_terms = "s(x0)") plot(preds_1, "x2") preds_2 <- predict_gam(model_1, length_out = 100, values = list(x0 = 0)) plot(preds_2, "x2", "fac") library(ggplot2) plot(preds_2, "x2", "fac") + scale_fill_brewer(type = "qual") + scale_color_brewer(type = "qual") # Plotting tensor product smooths/interactions model_2 <- gam(y ~ te(x0, x2, by = fac), data = sim_data) preds_3 <- predict_gam(model_2) preds_3 %>% plot(series = c("x0", "x2"), comparison = "fac")
library(mgcv) set.seed(10) sim_data <- gamSim(4) model_1 <- gam(y ~ s(x2, by = fac) + s(x0), data = sim_data) preds_1 <- predict_gam(model_1, length_out = 50, exclude_terms = "s(x0)") plot(preds_1, "x2") preds_2 <- predict_gam(model_1, length_out = 100, values = list(x0 = 0)) plot(preds_2, "x2", "fac") library(ggplot2) plot(preds_2, "x2", "fac") + scale_fill_brewer(type = "qual") + scale_color_brewer(type = "qual") # Plotting tensor product smooths/interactions model_2 <- gam(y ~ te(x0, x2, by = fac), data = sim_data) preds_3 <- predict_gam(model_2) preds_3 %>% plot(series = c("x0", "x2"), comparison = "fac")
Plotting methods for tidygam.diff
objects.
## S3 method for class 'tidygam.diff' plot(x, ..., sig = TRUE, sig_col = "red", sig_alpha = 0.25)
## S3 method for class 'tidygam.diff' plot(x, ..., sig = TRUE, sig_col = "red", sig_alpha = 0.25)
x |
A |
... |
Arguments passed to |
sig |
Shade the interval(s) where the difference smooth does not include 0 (default is |
sig_col |
Colour for the shading (default is |
sig_alpha |
Alpha level for the shading (default is |
A ggplot
object.
library(mgcv) set.seed(10) data <- gamSim(4) model <- gam(y ~ s(x2, by = fac) + s(x0), data = data) model_diff <- get_difference(model, "x2", list(fac = c("1", "2"))) plot(model_diff)
library(mgcv) set.seed(10) data <- gamSim(4) model <- gam(y ~ s(x2, by = fac) + s(x0), data = data) model_diff <- get_difference(model, "x2", list(fac = c("1", "2"))) plot(model_diff)
Return predictions from a GAM model generated with mgcv. The output can be plotted with plot()
.
predict_gam( model, length_out = 10, values = NULL, series = NULL, exclude_terms = NULL, ci_z = 1.96, tran_fun = NULL, separate = NULL, sep_by = "\\." )
predict_gam( model, length_out = 10, values = NULL, series = NULL, exclude_terms = NULL, ci_z = 1.96, tran_fun = NULL, separate = NULL, sep_by = "\\." )
model |
A |
length_out |
An integer indicating how many values to use along the
numeric variables for predicting the response (the default is |
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 |
exclude_terms |
Terms to be excluded from the prediction. Term names
should be given as they appear in the model summary (for example,
|
ci_z |
The z-value for calculating the CIs (the default is |
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 |
A tibble with predictions.
library(mgcv) set.seed(10) sim_data_1 <- gamSim(1, n = 200, scale = 2) model <- gam(y ~ x0 + s(I(x1^2)) + s(x2) + offset(x3), data = sim_data_1) predict_gam(model) predict_gam(model, values = list(x0 = mean(sim_data_1$x0))) predict_gam(model, series = "x2") predict_gam(model, exclude_terms = "s(I(x1^2))") # By-variables sim_data_2 <- gamSim(4) model_2 <- gam(y ~ s(x2, by = fac) + s(x0), data = sim_data_2) predict_gam(model_2) # 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) predict_gam(model_3, length_out = 50, tran_fun = exp) # Bivariate smooths model_4 <- gam(y ~ te(x1, x2), data = sim_data_1) predict_gam(model_4)
library(mgcv) set.seed(10) sim_data_1 <- gamSim(1, n = 200, scale = 2) model <- gam(y ~ x0 + s(I(x1^2)) + s(x2) + offset(x3), data = sim_data_1) predict_gam(model) predict_gam(model, values = list(x0 = mean(sim_data_1$x0))) predict_gam(model, series = "x2") predict_gam(model, exclude_terms = "s(I(x1^2))") # By-variables sim_data_2 <- gamSim(4) model_2 <- gam(y ~ s(x2, by = fac) + s(x0), data = sim_data_2) predict_gam(model_2) # 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) predict_gam(model_3, length_out = 50, tran_fun = exp) # Bivariate smooths model_4 <- gam(y ~ te(x1, x2), data = sim_data_1) predict_gam(model_4)
This data table contains ERP amplitude data from 39 subjects listening to speech and music.
struct
struct
A tibble with 17160 observations and 6 variables:
Time from stimulus onset in milliseconds.
Electrode number.
Electrode voltage at time t.
Language vs music.
Structural type (grammatical vs ungrammatical).