Skip to contents

Plot Model Predictions and Data in One Plot

Usage

plotCombined(prediction, ...)

# S3 method for class 'prdlist'
plot(
  x,
  data = NULL,
  ...,
  scales = "free",
  facet = c("wrap", "grid", "wrap_plain"),
  transform = NULL
)

# S3 method for class 'prdlist'
plotCombined(
  prediction,
  data = NULL,
  ...,
  scales = "free",
  facet = c("wrap", "grid", "wrap_plain"),
  transform = NULL,
  aesthetics = NULL,
  errfn = NULL
)

# S3 method for class 'prdframe'
plot(
  x,
  data = NULL,
  ...,
  scales = "free",
  facet = c("wrap", "grid", "wrap_plain"),
  transform = NULL
)

Arguments

prediction

A prdlist, the output of a prediction function such as those generated by Xs().

...

Logical expressions to subset the plotted data.

x

object of class prdlist or prdframe.

data

A datalist, i.e. a named list of data frames with columns name, time, value and sigma.

scales

The scales argument of ggplot2::facet_wrap() or ggplot2::facet_grid(): "free" (default), "fixed", "free_x" or "free_y".

facet

"wrap" (default), "grid" or "wrap_plain". "wrap_plain" gives one panel per combination of name and condition.

transform

Named list of transformations of the states, see coordTransform().

aesthetics

Named list of aesthetic mappings given as character, e.g. list(linetype = "name"). With data, they can also refer to columns of the condition grid of data.

errfn

Optional obsfn, the error model. The sigma it predicts is drawn as a band around the prediction. Default NULL.

Value

A ggplot object.

Details

The plotted data frame has columns time, value, sigma, name and condition.

Examples

# \donttest{
## Observation function and one parameter transformation per condition
fn <- eqnvec(
  sine = "1 + sin(6.28*omega*time)",
  cosine = "cos(6.28*omega*time)"
)
g <- Y(fn, parameters = "omega", outdir = tempdir())
#> Overwriting: /tmp/RtmpnLJd0N/obsfn.cpp
x <- Xt()
trafo <- lapply(1:3, function(i) eqnvec(omega = paste0("omega_", i)))
names(trafo) <- paste0("frequency_", 1:3)
p <- P(trafo, outdir = tempdir())
compile(g, p, output = "plotting_example")
#> using C++ compiler: g++ [-O2 -DNDEBUG -w -fPIC]

## Evaluate prediction
times <- seq(0, 1, .01)
pars <- structure(seq(1, 2, length.out = 3), names = getParameters(p))
prediction <- (g*x*p)(times, pars)

## Plot prediction
plotPrediction(prediction)

plotPrediction(prediction, scales = "fixed")

plotPrediction(prediction, facet = "grid")

plotPrediction(prediction, scales = "fixed",
               transform = list(sine = "x^2", cosine = "x - 1"))


## Simulate data
set.seed(1)
dataset <- as.data.frame(prediction)
dataset <- dataset[seq(1, nrow(dataset), 5), ]
dataset$value <- dataset$value + rnorm(nrow(dataset), 0, .1)
dataset$sigma <- 0.1
data <- as.datalist(dataset, splitBy = "condition")

## Plot data, and data with prediction
plotData(data)

plotCombined(prediction, data)

plotCombined(prediction, data, time <= 0.5 & condition == "frequency_1")

plotCombined(prediction, data, time <= 0.5 & condition != "frequency_1",
             facet = "grid")

plotCombined(prediction, data, aesthetics = list(linetype = "condition"))

# }