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.4.0
head repository: stefanocoretta/tidymv
Choose a Head Repository
Nothing to show
compare: v1.5.0
  • 14 commits
  • 9 files changed
  • 0 comments
  • 1 contributor
Showing with 181 additions and 31 deletions.
  1. +3 −3 DESCRIPTION
  2. +1 −0 NAMESPACE
  3. +12 −0 NEWS.md
  4. +109 −17 R/functions.R
  5. +1 −1 README.md
  6. +24 −0 man/plot_difference.Rd
  7. +1 −1 man/plot_smooths.Rd
  8. +3 −2 vignettes/plot-gamms.Rmd
  9. +27 −7 vignettes/plot-smooths.Rmd
@@ -1,16 +1,16 @@
Package: tidymv
Type: Package
Title: Tidy Model Visualisation
Version: 1.4.0
Date: 2018-07-03
Version: 1.5.0
Date: 2018-08-11
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
tools from the tidyverse.
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.0.1
RoxygenNote: 6.1.0
Imports: cowplot,
dplyr,
ggplot2,
@@ -2,6 +2,7 @@

export(create_event_start)
export(get_gam_predictions)
export(plot_difference)
export(plot_gamsd)
export(plot_smooths)
importFrom(magrittr,"%>%")
12 NEWS.md
@@ -1,5 +1,16 @@
# Change Log

## [1.5.0] - 2018-08-11
### Added
- `plot_difference()` to plot difference smooths
- support for plotting single smooth

### Changed
- deprecated message with `plot_gamsd()` now mentions `plot_difference()`

### Fixed
- vignettes titles and plots

## [1.4.0] - 2018-07-03
### Added
- `split` argument for separating columns (useful for interactions)
@@ -63,6 +74,7 @@
- `create_start_event` function
- `plot_gamsd` function

[1.5.0]: https://github.com/stefanocoretta/tidymv/compare/v1.4.0...v1.5.0
[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
@@ -69,8 +69,12 @@ get_gam_predictions <- function(model, time_series, series_length = 25, conditio
fitted$`(AR.start)` <- NULL
}

fitted_series <- fitted %>%
unique()
if (ncol(fitted) > 0) {
fitted_series <- fitted %>%
unique()
} else {
fitted_series <- fitted
}

fitted_series <- fitted_series %>%
dplyr::mutate(
@@ -81,6 +85,11 @@ get_gam_predictions <- function(model, time_series, series_length = 25, conditio
) %>%
tidyr::unnest(!!time_series_q)

if (ncol(fitted_series) > 0) {
fitted_series <- fitted_series %>%
unique()
}

if (exclude_random) {
if (rlang::is_empty(random_effects)) {
exclude_random_effects <- as.null()
@@ -195,10 +204,13 @@ 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, exclude_terms = NULL, series_length = 25, split = NULL, sep = "\\.") {
plot_smooths <- function(model, time_series, comparison = NULL, 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)
if (comparison_q == dplyr::quo(NULL)) {
comparison_q <- NULL
}
if (facet_terms_q == dplyr::quo(NULL)) {
facet_terms_q <- NULL
}
@@ -212,20 +224,38 @@ plot_smooths <- function(model, time_series, comparison, facet_terms = NULL, con
dplyr::quo_name(time_series_q), dplyr::quo_name(outcome_q)
)
) +
ggplot2::geom_ribbon(
ggplot2::aes_string(
ymin = "CI_lower",
ymax = "CI_upper",
fill = dplyr::quo_name(comparison_q)
),
alpha = 0.2
) +
ggplot2::geom_path(
ggplot2::aes_string(
colour = dplyr::quo_name(comparison_q),
linetype = dplyr::quo_name(comparison_q)
{if (!is.null(comparison_q)) {
ggplot2::geom_ribbon(
ggplot2::aes_string(
ymin = "CI_lower",
ymax = "CI_upper",
fill = dplyr::quo_name(comparison_q)
),
alpha = 0.2
)
) +
}} +
{if (is.null(comparison_q)) {
ggplot2::geom_ribbon(
ggplot2::aes_string(
ymin = "CI_lower",
ymax = "CI_upper"
),
alpha = 0.2
)
}} +
{if (!is.null(comparison_q)) {
ggplot2::geom_path(
ggplot2::aes_string(
colour = dplyr::quo_name(comparison_q),
linetype = dplyr::quo_name(comparison_q)
)
)
}} +
{if (is.null(comparison_q)) {
ggplot2::geom_path(
ggplot2::aes_string()
)
}} +
{if (!is.null(facet_terms_q)) {
ggplot2::facet_wrap(facet_terms_q)
}}
@@ -250,7 +280,7 @@ plot_smooths <- function(model, time_series, comparison, facet_terms = NULL, con
#' @importFrom magrittr "%>%"
#' @export
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")
.Deprecated("plot_smooth", msg = "'plot_gamsd' is deprecated and will be removed, use 'plot_smooths()' and 'plot_difference'.\n")

diff.df <- itsadug::plot_diff(
model,
@@ -362,3 +392,65 @@ plot_gamsd <- function(model, view, comparison, conditions = NULL, rm_re = FALSE

cowplot::plot_grid(smooth.plot, diff.plot, align = "v", nrow = 2, rel_heights = c(2/3, 1/3))
}

#' Plot difference smooth from a GAM.
#'
#' It plots the difference smooth from a \link[mgcv]{gam} or \link[mgcv]{bam}.
#' Significant differences are marked with red areas.
#'
#' @inheritParams get_gam_predictions
#' @param time_series An unquoted expression indicating the model term that defines the time series.
#' @param difference A named list with the levels to compute the difference of.
#' @param conditions A named list specifying the levels to plot from the model terms not among \code{time_series} or \code{difference}. Notice the difference with \link[tidymv]{plot_smooths}, which uses \link[rlang]{quos}.
#'
#' @export
plot_difference <- function(model, time_series, difference, conditions = NULL, series_length = 25) {
time_series_q <- dplyr::enquo(time_series)
time_series_chr <- quo_name(time_series_q)

fitted <- model$model

time_series_min <- dplyr::select(fitted, !!time_series_q) %>% min()
time_series_max <- dplyr::select(fitted, !!time_series_q) %>% max()

conditions <- c(conditions, rlang::ll(!!time_series_chr := seq(time_series_min, time_series_max, length.out = series_length)))

diff <- itsadug::get_difference(model, difference, cond = conditions, print.summary = FALSE) %>%
dplyr::mutate(
CI_upper = difference + CI,
CI_lower = difference - CI
)

sig_diff <- itsadug::find_difference(
diff$difference, diff$CI, diff[[time_series_chr]]
)

annotate <- ggplot2::annotate(
"rect",
xmin = sig_diff$start, xmax = sig_diff$end,
ymin = -Inf, ymax = Inf, alpha = 0.1,
fill = "red"
)

is_sig <- is.null(sig_diff) == FALSE

diff_plot <- diff %>%
ggplot2::ggplot(
ggplot2::aes_string(
dplyr::quo_name(time_series_q), "difference"
)
) +
{if (is_sig) {annotate}} +
ggplot2::geom_ribbon(
ggplot2::aes_string(
ymin = "CI_lower",
ymax = "CI_upper"
),
alpha = 0.2
) +
ggplot2::geom_path(
) +
geom_hline(yintercept = 0)

return(diff_plot)
}
@@ -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.4.0", 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.5.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.

@@ -1,10 +1,10 @@
---
title: "Plot GAM(M)s reference and difference smooths"
title: "Plot GAM(M)s reference and difference smooths (deprecated)"
author: "Stefano Coretta"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Vignette Title}
%\VignetteIndexEntry{Plot GAM(M)s reference and difference smooths (deprecated)}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
@@ -20,6 +20,7 @@ library(tidymv)
The estimated smooths and the difference smooth of factor predictors from a `gam` model can be plotted with `plot_gamsd()`.
This function has now been deprecated and will be removed in future versions.
This vignette is kept for historical reasons.
Please check `vignette("plot-smooths", package = "tidymv")`.

To illustrate how to use `plot_gamsd()`, 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)`.

@@ -4,7 +4,7 @@ author: "Stefano Coretta"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Vignette Title}
%\VignetteIndexEntry{Plotting GAMs}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
@@ -23,6 +23,8 @@ library(tidymv)
data(simdat)
```

## Plotting smooths

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)`.

```{r gam}
@@ -50,6 +52,23 @@ plot_smooths(
theme(legend.position = "top")
```

## Plotting a single smooth

It is also possible to plot a single smooth.

```{r gam-2}
model_2 <- gam(
y ~
s(x0),
data = data
)
plot_smooths(
model = model_2,
time_series = x0
)
```

## Plotting interactions

It is very likely that the model will contain interactions.
@@ -66,9 +85,7 @@ simdata <- simdat %>%
model_inter <- bam(
Y ~
Group +
# s(Time, by = Group) +
# s(Time, by = Condition) +
GroupCondition +
s(Time, by = GroupCondition),
data = simdata
)
@@ -81,7 +98,8 @@ plot_smooths(
comparison = Group,
facet_terms = Condition,
split = list(GroupCondition = c("Group", "Condition"))
)
) +
theme(legend.position = "top")
```

To plot just one or some of the facets, you should use the `conditions` argument.
@@ -95,7 +113,8 @@ plot_smooths(
facet_terms = Condition,
conditions = quos(Condition == -1),
split = list(GroupCondition = c("Group", "Condition"))
)
) +
theme(legend.position = "top")
```

```{r plot-interactions-3}
@@ -106,5 +125,6 @@ plot_smooths(
facet_terms = Condition,
conditions = quos(Condition %in% c(-1, 3)),
split = list(GroupCondition = c("Group", "Condition"))
)
) +
theme(legend.position = "top")
```

No commit comments for this range

You can’t perform that action at this time.