Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty plots when model has 2 or more factor variables #39

Closed
sklayn opened this issue Apr 5, 2019 · 2 comments
Closed

Empty plots when model has 2 or more factor variables #39

sklayn opened this issue Apr 5, 2019 · 2 comments
Assignees
Labels
bug

Comments

@sklayn
Copy link

@sklayn sklayn commented Apr 5, 2019

This happened to me recently, when I tried to recreate a plot from a previous project (worked perfectly about a month ago). When a model has 2 or more factor variables, an extra empty plot is drawn between them.
As far as I can tell, it's a problem with the number of parametric term elements returned - there is an extra NULL element in the final plot list to draw, which causes the empty plot..
More empty plots are added for each additional factor variable in the model - 1 between factor1 and factor2, 2 between factor2 and factor3, and I suppose it would go on.

Example below.

library(mgcv)
#> Loading required package: nlme
#> This is mgcv 1.8-28. For overview type 'help("mgcv-package")'.
library(gratia)

## simulate some data
dat <- gamSim(4, n = 300)
#> Factor `by' variable example

## make a second factor variable
dat$fac2 <- dat$fac

## GAM with 2 factors and 2 numeric terms
mod.2factors <- gam(y ~ s(x0) + s(x1) + factor(fac) + factor(fac2), data = dat, family = gaussian(link = "identity"))

draw(mod.2factors)

### doesn't happen when there is only one factor
mod.1factor <- gam(y ~ s(x0) + s(x1) + s(x2) + factor(fac), data = dat, family = gaussian(link = "identity"))

draw(mod.1factor)

### the problem seems to be compounded (more extra empty plots) for each additional factor term
dat$fac3 <- dat$fac
mod.3factors <- gam(y ~ s(x0) + s(x1) + factor(fac) + factor(fac2) + factor(fac3), data = dat, family = gaussian(link = "identity"))

draw(mod.3factors)

Created on 2019-04-05 by the reprex package (v0.2.1)

Session info:
R version 3.5.3 (2019-03-11)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Linux Mint 19

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8
[4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=bg_BG LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=bg_BG LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=bg_BG LC_IDENTIFICATION=C

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] reprex_0.2.1 gratia_0.2-8 mgcv_1.8-28 nlme_3.1-137

loaded via a namespace (and not attached):
[1] Rcpp_1.0.1 pillar_1.3.1 compiler_3.5.3 plyr_1.8.4 tools_3.5.3
[6] digest_0.6.18 evaluate_0.13 tibble_2.1.1 gtable_0.3.0 lattice_0.20-38
[11] pkgconfig_2.0.2 rlang_0.3.3 Matrix_1.2-17 rstudioapi_0.10 yaml_2.2.0
[16] mvtnorm_1.0-10 xfun_0.6 knitr_1.22 dplyr_0.8.0.1 fs_1.2.7
[21] grid_3.5.3 cowplot_0.9.4 tidyselect_0.2.5 glue_1.3.1 R6_2.4.0
[26] processx_3.3.0 rmarkdown_1.12 callr_3.2.0 whisker_0.3-2 ggplot2_3.1.0
[31] purrr_0.3.2 tidyr_0.8.3 clipr_0.5.0 magrittr_1.5 ps_1.3.0
[36] htmltools_0.3.6 scales_1.0.0 splines_3.5.3 assertthat_0.2.1 colorspace_1.4-1
[41] labeling_0.3 lazyeval_0.2.2 munsell_0.5.0 crayon_1.3.4

@gavinsimpson gavinsimpson self-assigned this Aug 21, 2019
@gavinsimpson gavinsimpson added the bug label Aug 21, 2019
@gavinsimpson
Copy link
Owner

@gavinsimpson gavinsimpson commented Aug 21, 2019

Thanks for reporting this and sorry it's taken me a while to get around to looking at this.

This is due to some silly logic adding the parametric plots to the list of plots to draw. I'm testing a fix now and should have something committed to the master branch shortly.

gavinsimpson added a commit that referenced this issue Aug 21, 2019
@sklayn
Copy link
Author

@sklayn sklayn commented Sep 10, 2019

Thank you, it's perfect! Sorry it took me so long to reply - and thanks for the great package!

gavinsimpson added a commit that referenced this issue Sep 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.