Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
base repository: stefanocoretta/tidymv
Choose a Base Repository
Nothing to show
base: v1.3.1
head repository: stefanocoretta/tidymv
Choose a Head Repository
Nothing to show
compare: v1.4.0
  • 18 commits
  • 9 files changed
  • 0 comments
  • 1 contributor
Showing with 154 additions and 18 deletions.
  1. +2 −1 DESCRIPTION
  2. +12 −0 NEWS.md
  3. +57 −12 R/functions.R
  4. +1 −1 README.md
  5. +8 −1 man/get_gam_predictions.Rd
  6. +3 −1 man/plot_gamsd.Rd
  7. +8 −1 man/plot_smooths.Rd
  8. +2 −1 vignettes/plot-gamms.Rmd
  9. +61 −0 vignettes/plot-smooths.Rmd
@@ -1,7 +1,8 @@
Package: tidymv
Type: Package
Title: Tidy Model Visualisation
Version: 1.3.1
Version: 1.4.0
Date: 2018-07-03
Authors@R: person("Stefano", "Coretta", email = paste0("stefano.coretta", "@", "gmail.com"),
role = c("aut", "cre"))
Description: This package provides functions for model visualisation using tidy
12 NEWS.md
@@ -1,5 +1,16 @@
# Change Log

## [1.4.0] - 2018-07-03
### Added
- `split` argument for separating columns (useful for interactions)
- how to plot interactions in vignette
- `exclude_terms` argument to exclude terms when predicting
- exclude `tensor.smooths` when plotting smooths
- experimental `ylim` argument to `plot_gamsd()`

### Fixed
- message in `plot_gamsd()` which said "will be deprecated"

## [1.3.1] - 2018-02-26
### Fixed
- warning in `get_gam_predictions()`
@@ -52,6 +63,7 @@
- `create_start_event` function
- `plot_gamsd` function

[1.4.0]: https://github.com/stefanocoretta/tidymv/compare/v1.3.1...v1.4.0
[1.3.1]: https://github.com/stefanocoretta/tidymv/compare/v1.3.0...v1.3.1
[1.3.0]: https://github.com/stefanocoretta/tidymv/compare/v1.2.0...v1.3.0
[1.2.0]: https://github.com/stefanocoretta/tidymv/compare/v1.1.0...v1.2.0
@@ -28,9 +28,12 @@ create_event_start <- function(tibble, event_col) {
#' @param series_length An integer indicating how many values along the time series to use for predicting the outcome term.
#' @param conditions A list of quosures with \link[rlang]{quos} specifying the levels to plot from the model terms.
#' @param exclude_random Whether to exclude random smooths (the default is \code{TRUE}).
#' @param exclude_terms Terms to be excluded from the prediction. Term names should be given as they appear in the model summary (for example, \code{"s(x0,x1)"}).
#' @param split Columns to separate as a named list.
#' @param sep Separator between columns (default is \code{"\\."}, which is the default with \code{}). If character, it is interpreted as a regular expression.
#'
#' @export
get_gam_predictions <- function(model, time_series, series_length = 25, conditions = NULL, exclude_random = TRUE) {
get_gam_predictions <- function(model, time_series, series_length = 25, conditions = NULL, exclude_random = TRUE, exclude_terms = NULL, split = NULL, sep = "\\.") {
time_series_q <- dplyr::enquo(time_series)
time_series_name <- dplyr::quo_name(time_series_q)
outcome_q <- model$formula[[2]]
@@ -88,19 +91,42 @@ get_gam_predictions <- function(model, time_series, series_length = 25, conditio
exclude_random_effects <- as.null()
}

# Exclude smooth terms which are not the time series to be plotted
# Exclude smooth terms which are not the time series to be plotted or tensor smooths
exclude_smooths <- as.null()
exclude_terms <- as.null()
excluded_terms <- as.null()
for (smooth in 1:length(model[["smooth"]])) {
smooth_term <- model[["smooth"]][[smooth]][["term"]][[1]]
if (smooth_term != time_series_name) {
exclude_terms <- c(exclude_terms, smooth_term)
excluded_terms <- c(excluded_terms, smooth_term)
smooth_label <- model[["smooth"]][[smooth]][["label"]]
exclude_smooths <- c(exclude_smooths, smooth_label)
}
smooth_class <- attr(model$smooth[[smooth]],"class")[1]
if (smooth_class == "tensor.smooth") {
smooth_term <- model[["smooth"]][[smooth]][["term"]][[2]]
excluded_terms <- c(excluded_terms, smooth_term)
smooth_label <- model[["smooth"]][[smooth]][["label"]]
exclude_smooths <- c(exclude_smooths, smooth_label)
}
}

excluded <- as.null()
if (!is.null(exclude_terms)) {
for (term in 1:length(exclude_terms)) {
for (label in 1:length(model[["smooth"]])) {
smooth_label <- model[["smooth"]][[label]][["label"]]
if (smooth_label == exclude_terms[term]) {
smooth_term <- model[["smooth"]][[label]][["term"]]
if (length(smooth_term) > 1) {
smooth_term_2 <- model[["smooth"]][[label]][["term"]][[2]]
excluded <- c(excluded, smooth_term_2)
}
}
}
}
}

exclude_these <- c(exclude_random_effects, exclude_smooths)
exclude_these <- c(exclude_random_effects, exclude_smooths, exclude_terms)

predicted <- stats::predict(
model,
@@ -121,16 +147,33 @@ get_gam_predictions <- function(model, time_series, series_length = 25, conditio

if (!is.null(exclude_random_effects)) {
predicted_tbl <- predicted_tbl %>%
dplyr::select(-(!!!rlang::syms(random_effects_terms))) %>%
dplyr::select(-dplyr::one_of(random_effects_terms)) %>%
unique()
}

if (!is.null(exclude_smooths)) {
predicted_tbl <- predicted_tbl %>%
dplyr::select(-(!!!rlang::syms(exclude_terms))) %>%
dplyr::select(-dplyr::one_of(excluded_terms)) %>%
unique()
}

if (!is.null(excluded)) {
predicted_tbl <- predicted_tbl %>%
dplyr::select(-dplyr::one_of(excluded)) %>%
unique()
}

if (!is.null(split)) {
for (i in 1:length(split)) {
predicted_tbl <- tidyr::separate(
data = predicted_tbl,
col = names(split)[i],
into = split[[i]],
sep = sep
)
}
}

if (!is.null(conditions)) {
predicted_tbl <- predicted_tbl %>%
dplyr::filter(!!!conditions)
@@ -152,7 +195,7 @@ get_gam_predictions <- function(model, time_series, series_length = 25, conditio
#' @importFrom rlang ":="
#' @importFrom stats "predict"
#' @export
plot_smooths <- function(model, time_series, comparison, facet_terms = NULL, conditions = NULL, exclude_random = TRUE, series_length = 25) {
plot_smooths <- function(model, time_series, comparison, facet_terms = NULL, conditions = NULL, exclude_random = TRUE, exclude_terms = NULL, series_length = 25, split = NULL, sep = "\\.") {
time_series_q <- dplyr::enquo(time_series)
comparison_q <- dplyr::enquo(comparison)
facet_terms_q <- dplyr::enquo(facet_terms)
@@ -161,7 +204,7 @@ plot_smooths <- function(model, time_series, comparison, facet_terms = NULL, con
}
outcome_q <- model$formula[[2]]

predicted_tbl <- get_gam_predictions(model, !!time_series_q, conditions, exclude_random = exclude_random, series_length = series_length)
predicted_tbl <- get_gam_predictions(model, !!time_series_q, conditions, exclude_random = exclude_random, exclude_terms = exclude_terms, series_length = series_length, split = split, sep = sep)

smooths_plot <- predicted_tbl %>%
ggplot2::ggplot(
@@ -202,11 +245,12 @@ plot_smooths <- function(model, time_series, comparison, facet_terms = NULL, con
#' @param conditions The values to use for other predictors as a named list.
#' @param rm_re Whether to remove random effects (the default is \code{FALSE}).
#' @param bw Whether to plot in black and white (the default is \code{FALSE}).
#' @param ylim Limits of the y-axis of the smooths panel.
#'
#' @importFrom magrittr "%>%"
#' @export
plot_gamsd <- function(model, view, comparison, conditions = NULL, rm_re = FALSE, bw = FALSE) {
.Deprecated("plot_smooth", msg = "'plot_gamsd' will be deprecated, use 'plot_smooths'. (Plotting with the difference smooth is not supported yet.)\n")
plot_gamsd <- function(model, view, comparison, conditions = NULL, rm_re = FALSE, bw = FALSE, ylim = NULL) {
.Deprecated("plot_smooth", msg = "'plot_gamsd' is deprecated and will be removed, use 'plot_smooths()'. (Plotting with the difference smooth is not supported yet.)\n")

diff.df <- itsadug::plot_diff(
model,
@@ -291,7 +335,8 @@ plot_gamsd <- function(model, view, comparison, conditions = NULL, rm_re = FALSE
ggplot2::scale_linetype_discrete(name = names(comparison))
}
} +
{if (bw == FALSE) {ggplot2::scale_fill_discrete(name = names(comparison))}}
{if (bw == FALSE) {ggplot2::scale_fill_discrete(name = names(comparison))}} +
{if (!is.null(ylim)) {ggplot2::ylim(ylim[1], ylim[2])}}

ymin.di <- diff.df$est - diff.df$CI
ymax.di <- diff.df$est + diff.df$CI
@@ -4,6 +4,6 @@ This is the repository of the `R` package `tidymv`. This package provides functi

## Installation

To install the package, use `devtools::install_github("stefanocoretta/tidymv@v1.3.1", build_vignettes = TRUE)`. To learn how to use the package, do `vignette("plot-smooths", package = "tidymv")` after the installation.
To install the package, use `devtools::install_github("stefanocoretta/tidymv@v1.4.0", build_vignettes = TRUE)`. To learn how to use the package, do `vignette("plot-smooths", package = "tidymv")` after the installation.

If you wish to install the development version, use `devtools::install_github("stefanocoretta/tidymv", build_vignettes = TRUE)`.

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

@@ -45,7 +45,8 @@ Significance is based on the difference smooth: at any point, if the confidence
plot_gamsd(
model = model,
view = "x2",
comparison = list(fac = c("1", "2"))
comparison = list(fac = c("1", "2")),
ylim = c(-5, 5)
)
```

@@ -17,8 +17,10 @@ knitr::opts_chunk$set(
)
library(ggplot2)
theme_set(theme_bw())
library(dplyr)
library(itsadug)
library(tidymv)
data(simdat)
```

To illustrate how to use `plot_smooths()`, let's first prepare some dummy data with a factor variable and run `gam()` on this data. The `gam` model includes a reference smooth `s(x2)`, a by-factor difference smooth `s(x2, by = fac)`, and a smooth `s(x0)`.
@@ -47,3 +49,62 @@ plot_smooths(
) +
theme(legend.position = "top")
```

## Plotting interactions

It is very likely that the model will contain interactions.
It is possible to plot models with interactions by specifying faceting with the `facet_terms` argument.

```{r interaction-data}
simdata <- simdat %>%
filter(
Subject %in% c("a01", "a08", "a15", "c01", "c08", "c15")
) %>%
mutate(
GroupCondition = interaction(Group, Condition)
)
model_inter <- bam(
Y ~
Group +
# s(Time, by = Group) +
# s(Time, by = Condition) +
s(Time, by = GroupCondition),
data = simdata
)
```

```{r plot-interactions}
plot_smooths(
model = model_inter,
time_series = Time,
comparison = Group,
facet_terms = Condition,
split = list(GroupCondition = c("Group", "Condition"))
)
```

To plot just one or some of the facets, you should use the `conditions` argument.
This argument takes a list of quosures qith `quos()`.

```{r plot-interactions-2}
plot_smooths(
model = model_inter,
time_series = Time,
comparison = Group,
facet_terms = Condition,
conditions = quos(Condition == -1),
split = list(GroupCondition = c("Group", "Condition"))
)
```

```{r plot-interactions-3}
plot_smooths(
model = model_inter,
time_series = Time,
comparison = Group,
facet_terms = Condition,
conditions = quos(Condition %in% c(-1, 3)),
split = list(GroupCondition = c("Group", "Condition"))
)
```

No commit comments for this range

You can’t perform that action at this time.