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.0.0
head repository: stefanocoretta/tidymv
Choose a Head Repository
Nothing to show
compare: v1.1.0
  • 5 commits
  • 9 files changed
  • 0 comments
  • 1 contributor
Showing with 274 additions and 14 deletions.
  1. +1 −0 .gitignore
  2. +6 −2 DESCRIPTION
  3. +10 −1 NEWS.md
  4. +29 −9 R/functions.R
  5. +7 −1 README.md
  6. +3 −1 man/plot_gamsd.Rd
  7. +34 −0 vignettes/plot-gamms.R
  8. +58 −0 vignettes/plot-gamms.Rmd
  9. +126 −0 vignettes/plot-gamms.html
@@ -3,3 +3,4 @@
.RData
.Ruserdata
*.Rproj
inst/doc
@@ -1,7 +1,7 @@
Package: tidymv
Type: Package
Title: Tidy Model Visualisation
Version: 1.0.0
Version: 1.1.0
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
@@ -10,8 +10,12 @@ License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.0.1
Imports: cowplot,
Imports: tidyverse,
cowplot,
dplyr,
itsadug,
ggplot2,
magrittr
Suggests: knitr,
rmarkdown
VignetteBuilder: knitr
11 NEWS.md
@@ -1,5 +1,13 @@
# Change Log

## [1.1.0] - 2017-10-31
### Added
- black and white option for `plot_gamsd()`

### Changed
- `gam` model in the vignette now has a reference smooth `s(x2)`
- updated vignette

## [1.0.0] - 2017-10-21
### Added
- documentation of package
@@ -15,4 +23,5 @@
- `create_start_event` function
- `plot_gamsd` function

[1.0.0]: https://github.com/stefanocoretta/tidymv/compare/v0.1.0...v1.0.0
[1.1.0]: https://github.com/stefanocoretta/tidymv/compare/v1.0.0...v1.1.0
[1.0.0]: https://github.com/stefanocoretta/tidymv/compare/v0.1.0...v1.0.0
@@ -29,10 +29,11 @@ create_event_start <- function(tibble, event.col) {
#' @param view The predictor determining the time series.
#' @param comparison The levels for the comparison as a named list.
#' @param conditions The values to use for other predictors as a named list.
#' @param bw Whether to plot in black and white (the default is \code{FALSE}).
#'
#' @importFrom magrittr "%>%"
#' @export
plot_gamsd <- function(model, view, comparison, conditions = NULL) {
plot_gamsd <- function(model, view, comparison, conditions = NULL, bw = FALSE) {
diff.df <- itsadug::plot_diff(
model,
view = view,
@@ -65,8 +66,9 @@ plot_gamsd <- function(model, view, comparison, conditions = NULL) {

annotate <- ggplot2::annotate(
"rect",
xmin=sig.diff$start, xmax=sig.diff$end,
ymin=-Inf, ymax=Inf, alpha=0.1, fill="red"
xmin = sig.diff$start, xmax = sig.diff$end,
ymin = -Inf, ymax = Inf, alpha = 0.1,
fill = ifelse(bw == FALSE, "red", "black")
)

is.sig <- is.null(sig.diff) == FALSE
@@ -79,13 +81,25 @@ plot_gamsd <- function(model, view, comparison, conditions = NULL) {
ggplot2::geom_ribbon(
ggplot2::aes(ymin = ymin.sm,
ymax = ymax.sm,
fill = smooth.df[[comp.column]]),
group = smooth.df[[comp.column]]
),
alpha = 0.2,
colour = "NA"
) +
ggplot2::geom_line(
ggplot2::aes(colour = smooth.df[[comp.column]])
) +
{if (bw == FALSE) {
ggplot2::aes(fill = smooth.df[[comp.column]])}
} +
{if (bw == FALSE) {
ggplot2::geom_line(
ggplot2::aes(colour = smooth.df[[comp.column]])
)
}
else {
ggplot2::geom_line(
ggplot2::aes(linetype = smooth.df[[comp.column]])
)
}
} +
ggplot2::theme_bw() +
ggplot2::theme(
axis.title.x = ggplot2::element_blank(),
@@ -94,8 +108,14 @@ plot_gamsd <- function(model, view, comparison, conditions = NULL) {
legend.position = "top"
) +
ggplot2::xlim(min(diff.df[[view]]), max(diff.df[[view]])) +
ggplot2::scale_colour_discrete(name = names(comparison)) +
ggplot2::scale_fill_discrete(name = names(comparison))
{if (bw == FALSE) {
ggplot2::scale_colour_discrete(name = names(comparison))
}
else {
ggplot2::scale_linetype_discrete(name = names(comparison))
}
} +
{if (bw == FALSE) {ggplot2::scale_fill_discrete(name = names(comparison))}}

ymin.di <- diff.df$est - diff.df$CI
ymax.di <- diff.df$est + diff.df$CI
@@ -1 +1,7 @@
# `tidymv`
# `tidymv`: Tidy Model Visualisation

This is the repository of the `R` package `tidymv`. This package provides functions for the visualisation of GAM(M)s using tidy tools from the `tidyverse`. `tidymv` is based on the `itsadug` package, and indeed it uses some of its functions under the hood.

## Installation

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

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

@@ -0,0 +1,34 @@
## ----setup, echo=FALSE, include=FALSE------------------------------------
knitr::opts_chunk$set(out.width = "300px", fig.align = "center", dpi = 300)
library(tidyverse)
theme_set(theme_bw())
library(itsadug)
library(tidymv)

## ----gam-----------------------------------------------------------------
set.seed(10)
data <- gamSim(4)
model <- gam(
y ~
fac +
s(x2) +
s(x2, by = fac) +
s(x0),
data = data
)

## ----plot-1-2------------------------------------------------------------
plot_gamsd(
model = model,
view = "x2",
comparison = list(fac = c("1", "2"))
)

## ----plot-1-3------------------------------------------------------------
plot_gamsd(
model = model,
view = "x2",
comparison = list(fac = c("1", "3")),
bw = TRUE
)

@@ -0,0 +1,58 @@
---
title: "Plot GAM(M)s reference and difference smooths"
author: "Stefano Coretta"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Vignette Title}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---

```{r setup, echo=FALSE, include=FALSE}
knitr::opts_chunk$set(out.width = "300px", fig.align = "center", dpi = 300)
library(tidyverse)
theme_set(theme_bw())
library(itsadug)
library(tidymv)
```

Reference and difference smooths of factor predictos from a `gam` model can be plotted with `plot_gamsd()`.
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)`.

```{r gam}
set.seed(10)
data <- gamSim(4)
model <- gam(
y ~
fac +
s(x2) +
s(x2, by = fac) +
s(x0),
data = data
)
```

We can now plot the estimated smooths for the two levels of `fac`, and the difference smooth.
It is only possible to compare two levels at once, so here we plot `1` and `2`.
Portions of the estimated smooths that are significantly different are indicated by a shaded red area both in the estimated smooths panel (top) and the difference smooth panel (bottom).
Significance is based on the difference smooth: at any point, if the confidence interval of the difference smooth does not include 0, the difference between the estimated smooths is significant.

```{r plot-1-2}
plot_gamsd(
model = model,
view = "x2",
comparison = list(fac = c("1", "2"))
)
```

Here we compare `1` with `3`, and we plot in black and white (gray scale).

```{r plot-1-3}
plot_gamsd(
model = model,
view = "x2",
comparison = list(fac = c("1", "3")),
bw = TRUE
)
```

Large diffs are not rendered by default.

No commit comments for this range

You can’t perform that action at this time.