plot.qgcompemmfit: Default plotting method for a qgcompfit object

View source: R/base_plot.R

plot.qgcompemmfitR Documentation

Default plotting method for a qgcompfit object

Description

Plot a quantile g-computation object. For qgcomp.noboot, this function will create a butterfly plot of weights. For qgcomp.boot, this function will create a box plot with smoothed line overlaying that represents a non-parametric fit of a model to the expected outcomes in the population at each quantile of the joint exposures (e.g. '1' represents 'at the first quantile for every exposure')

Usage

## S3 method for class 'qgcompemmfit'
plot(x, emmval = 0, suppressprint = FALSE, ...)

Arguments

x

"qgcompfit" object from qgcomp.noboot, qgcomp.boot, qgcomp.cox.noboot, qgcomp.cox.boot, qgcomp.zi.noboot or qgcomp.zi.boot functions

emmval

fixed value for effect measure modifier at which pointwise comparisons are calculated

suppressprint

If TRUE, suppresses the plot, rather than printing it by default (it can be saved as a ggplot2 object (or list of ggplot2 objects if x is from a zero- inflated model) and used programmatically) (default = FALSE)

...

unused

Value

If suppressprint=FALSE, then this function prints a plot specific to a "qgcompemmfit" object. If no bootstrapping is used, it will print a butterfly plot of the weights at the specified value of the modifier (set via emmval parameter) If bootstrapping is used, it will print a joint regression line for all exposures at the specified value of the modifier (set via emmval parameter)

If suppressprint=TRUE, then this function returns a "gg" (regression line) or "gtable" (butterfly plot) object (from ggplot2 package or gtable/grid packages), which can be used to print a ggplot figure and modify either of the above figures (see example below)

See Also

qgcomp.noboot, qgcomp.boot, and qgcomp

Examples

set.seed(50)
# linear model, binary modifier
dat <- data.frame(y=runif(50), x1=runif(50), x2=runif(50),
z=rbinom(50,1,0.5), r=rbinom(50,1,0.5))
(qfit <- qgcomp.emm.noboot(f=y ~ z + x1 + x2, emmvar="z",
expnms = c('x1', 'x2'), data=dat, q=2, family=gaussian()))
plot(qfit, emmval = 1)
#
library(ggplot2)

# example with bootstrapping
dat2 <- data.frame(y=runif(50), x1=runif(50), x2=runif(50),
z=sample(0:2, 50,replace=TRUE), r=rbinom(50,1,0.5))
dat2$z = as.factor(dat2$z)
(qfit4 <- qgcomp.emm.boot(f=y ~ z + x1 + x2, emmvar="z",
                          degree = 1, B = 20,
                         expnms = c('x1', 'x2'), data=dat2, q=4, family=gaussian()))
plot(qfit4)
pp0 = plot(qfit4, emmval=0, suppressprint=TRUE)
pp1 = plot(qfit4, emmval=1, suppressprint=TRUE)
pp2 = plot(qfit4, emmval=2, suppressprint=TRUE)
pp1 + theme_linedraw() # can use with other ggplot functions

# overlay (fussy to work with)
ppom <- ggplot_build(pp0 + pp1[2] + pp2[[2]] + scale_color_discrete(guide="none"))
ppom$data[[1]]$colour <- ppom$data[[2]]$colour <- "gray40" # emmval = 0 -> dark gray
ppom$data[[3]]$colour <- ppom$data[[4]]$colour <- "gray80" # emmval = 1 -> light gray
ppom$data[[5]]$colour <- ppom$data[[6]]$colour <- "black" # emmval = 2 -> black
xincrement = 0.025
ppom$data[[1]]$x <- ppom$data[[2]]$x <- ppom$data[[1]]$x - xincrement
ppom$data[[2]]$xmin <- ppom$data[[2]]$xmin - xincrement
ppom$data[[2]]$xmax <- ppom$data[[2]]$xmax - xincrement
ppom$data[[5]]$x <- ppom$data[[6]]$x <- ppom$data[[5]]$x + xincrement
ppom$data[[6]]$xmin <- ppom$data[[6]]$xmin + xincrement
ppom$data[[6]]$xmax <- ppom$data[[6]]$xmax + xincrement
plot(ggplot_gtable(ppom))

## Not run: 
library(gtable) # may need to separately install gtable
# example with no bootstrapping, adding object from bootstrapped fit
pp2 <- plot(qfit, emmval = 1, suppressprint=TRUE)
grid.draw(pp2)
# insert row on top that is 1/2 height of existing plot
pp2b = gtable::gtable_add_rows(pp2, heights=unit(0.5, 'null') ,pos = 0)
# add plot to that row
pp3 = gtable::gtable_add_grob(pp2b, ggplot2::ggplotGrob(pp), t=1,l=1,r=2)
grid.draw(pp3)

## End(Not run)

qgcompint documentation built on March 22, 2022, 5:06 p.m.