predict.DDPstar: Predictions from fitted DDPstar models

View source: R/predict.DDPstar.R

predict.DDPstarR Documentation

Predictions from fitted DDPstar models

Description

Takes a fitted DDPstar object produced by DDPstar() and produces predictions for different functionals given a new set of values for the model covariates.

Usage

## S3 method for class 'DDPstar'
predict(object, what = NULL, newdata, reg.select = NULL, den.grid = NULL, 
  quant.probs = NULL, q.value = NULL, 
  parallel = c("no", "multicore", "snow"), ncpus = 1, cl = NULL, ...)

Arguments

object

An object of class DDPstar as produced by DDPstar().

what

Character vector with the functionals to be obtained: “regfun” (regression function); “varfun” (variance function); “quantfun” (condional quantiles); “denfun” (conditional densities); ; “probfun” (conditional probabilities, i.e., probabilities of the form P(Y \leq y^{*} \mid X = x)).

newdata

Data frame containing the values of the covariates at which predictions will be computed

reg.select

Numeric value containing the position of the covariate effect in the DDPstar formula for which predictions are required. If NULL (and "regfun" is in input argument what), the regression function (coditional mean) is computed.

den.grid

Numeric vector with the response variable values at which the conditional densities should be computed. Applicable and required if “denfun” is in argument what. If required and NULL, a sequence of 100 values between the minumum and the maximum of the observed responses is considered.

quant.probs

Numeric vector with the quantiles at which to obtain/predict the conditional quantiles. Applicable and required if “quantfun” is in argument what. If required and NULL, the deciles are computed.

q.value

Numeric vector with the response variable values at which to obtain/predict the conditional probabilities. Applicable and required if “probfun” is in argument what.

parallel

A characters string with the type of parallel operation: either "no" (default), "multicore" (not available on Windows) or "snow".

ncpus

An integer with the number of processes to be used in parallel operation. Defaults to 1.

cl

An object inheriting from class cluster (from the parallel package), specifying an optional parallel or snow cluster if parallel = "snow". If not supplied, a cluster on the local machine is created for the duration of the call.

...

Further arguments passed to or from other methods. Not yet implemented.

Value

A list with the following components (npred is the number of columns in newdata and nsave is the number of Gibbs sampler iterations saved)

regfun

Present if "regfun" is in input argument what. A matrix of dimension npred\timesnsave with the MCMC realisations of the regression function (conditional mean) or the desired component (if reg.select is used).

reg.select

Argument reg.select used in the call. Present if "regfun" is in input argument what.

varfun

Present if "varfun" is in input argument what. A matrix of dimension npred\timesnsave with the MCMC realisations of the conditional variance.

quantfun

Present if "quantfun" is in input argument what. An array of dimension npred\timesnsave\timesnq with the MCMC realisations of the conditional quantiles. Here nq is the length of argument quant.probs.

quant.probs

Argument quant.probs used in the call. Present if "quantfun" is in input argument what.

denfun

Present if "denfun" is in input argument what. An array of dimension nden\timesnpred\timesnsave with the MCMC realisations of the conditional densities. Here nden is the length of argument den.grid.

den.grid

Argument den.grid used in the call. Present if "denfun" is in input argument what.

probfun

Present if "probfun" is in input argument what. An array of dimension ny\timesnpred\timesnsave with the MCMC realisations of the conditional probabilities. Here ny is the length of argument q.value

q.value

Argument q.value used in the call. Present if "probfun" is in input argument what.

See Also

DDPstar

Examples

library(DDPstar)
data(dde)
dde$GAD <- dde$GAD/7 # GAD in weeks

set.seed(10) # For reproducibility
fit_dde <- DDPstar(formula = GAD ~ f(DDE, bdeg = 3, nseg = 20, pord = 2, atau = 1, btau = 0.005), 
 data = dde, mcmc = list(nburn = 20000, nsave = 15000, nskip = 1), 
  prior = list(a = 2, b = 0.5, aalpha = 2, balpha = 2, L = 20), 
  standardise = TRUE, compute.lpml = TRUE, compute.WAIC = TRUE)
 
# Data frame for predictions (regression, variance, quartiles and probabilities functions)
df_pred <- data.frame(DDE = seq(min(dde$DDE), max(dde$DDE), length = 50))

# Data frame for conditional densities
# Compute the densities for 4 different DDE values
df_pred_den <- data.frame(DDE = quantile(dde$DDE, c(0.1, 0.6, 0.9, 0.99)))
sequenceGAD <- seq(min(dde$GAD), max(dde$GAD), length = 100)
 
# Regression and variance function
  reg_var_DDE <- predict(fit_dde, newdata = df_pred, 
    what = c("regfun", "varfun"), 
    parallel = "multicore", 
    ncpus = 2)
    # Regression function
    reg_m  <- apply(reg_var_DDE$regfun, 1, median) 
    reg_ql <- apply(reg_var_DDE$regfun, 1, quantile, 0.025) 
    reg_qh <- apply(reg_var_DDE$regfun, 1, quantile, 0.975)

    plot(dde$DDE, dde$GAD, xlab = "DDE (mg/L)", 
      ylab = "Gestational age at delivery (in weeks)",
      main = "Regression function")
    lines(df_pred$DDE, reg_m, lwd = 2)
    lines(df_pred$DDE, reg_ql, lty = 2)
    lines(df_pred$DDE, reg_qh, lty = 2)

    # Variance function
    var_m  <- apply(reg_var_DDE$varfun, 1, median) 
    var_ql <- apply(reg_var_DDE$varfun, 1, quantile, 0.025) 
    var_qh <- apply(reg_var_DDE$varfun, 1, quantile, 0.975)

    plot(df_pred$DDE, var_m, type = "l", lwd = 2,
      xlab = "DDE (mg/L)", 
      ylab = "",
      main = "Variance function")
    lines(df_pred$DDE, var_ql, lty = 2)
    lines(df_pred$DDE, var_qh, lty = 2)

# Quartiles
  p <- c(0.25, 0.5, 0.75)
  quart_DDE <- predict(fit_dde, newdata = df_pred, 
    what = "quantfun", quant.probs = p, 
    parallel = "multicore", ncpus = 2)
  
  quart_m <- apply(quart_DDE$quantfun , c(1,3), median)
  quart_ql <- apply(quart_DDE$quantfun , c(1,3), quantile, 0.025)
  quart_qh <- apply(quart_DDE$quantfun , c(1,3), quantile, 0.975)
  
  plot(dde$DDE, dde$GAD, xlab = "DDE (mg/L)", 
      ylab = "Gestational age at delivery (in weeks)",
      main = "Conditional quartiles")
  
  # Q1    
  lines(df_pred$DDE, quart_m[,1], lwd = 2, col = 2)
  lines(df_pred$DDE, quart_ql[,1], lty = 2, col = 2)
  lines(df_pred$DDE, quart_qh[,1], lty = 2, col = 2)
  
  # Median
  lines(df_pred$DDE, quart_m[,2], lwd = 2)
  lines(df_pred$DDE, quart_ql[,2], lty = 2)
  lines(df_pred$DDE, quart_qh[,2], lty = 2)
  
  # Q3
  lines(df_pred$DDE, quart_m[,3], lwd = 2, col = 3)
  lines(df_pred$DDE, quart_ql[,3], lty = 2, col = 3)
  lines(df_pred$DDE, quart_qh[,3], lty = 2, col = 3)

# Conditional densities
  den_DDE <- predict(fit_dde, newdata = df_pred_den,
    what = "denfun", den.grid = sequenceGAD,
    parallel = "multicore", ncpus = 2)
  
  den_m  <- apply(den_DDE$denfun, c(1,2), median)
  den_ql <- apply(den_DDE$denfun, c(1,2), quantile, 0.025)
  den_qh <- apply(den_DDE$denfun, c(1,2), quantile, 0.975)
  
  op <- par(mfrow = c(2,2))
  plot(sequenceGAD, den_m[,1], type = "l", lwd = 2,
    xlab = "DDE (mg/L)",
    ylab = "",
    main = "DDE = 12.57 (10th percentile)")
  lines(sequenceGAD, den_ql[,1], lty = 2)
  lines(sequenceGAD, den_qh[,1], lty = 2)

  plot(sequenceGAD, den_m[,2], type = "l", lwd = 2,
  xlab = "DDE (mg/L)",
    ylab = "",
    main = "DDE = 28.44 (60th percentile)")
  lines(sequenceGAD, den_ql[,2], lty = 2)
  lines(sequenceGAD, den_qh[,2], lty = 2)

  plot(sequenceGAD, den_m[,3], type = "l", lwd = 2,
    xlab = "DDE (mg/L)",
    ylab = "",
    main = "DDE = 53.72 (90th percentile)")
  lines(sequenceGAD, den_ql[,3], lty = 2)
  lines(sequenceGAD, den_qh[,3], lty = 2)
  plot(sequenceGAD, den_m[,4], type = "l", lwd = 2,
    xlab = "DDE (mg/L)",
    ylab = "",
    main = "DDE = 105.47 (99th percentile)")
  lines(sequenceGAD, den_ql[,4], lty = 2)
  lines(sequenceGAD, den_qh[,4], lty = 2) 
  par(op)

# Conditional probabilities
  probs_DDE <- predict(fit_dde, newdata = df_pred, 
    what = "probfun", q.value = c(37), 
    parallel = "multicore", ncpus = 2)
  
  prob_m  <- apply(probs_DDE$probfun, c(1,2), median)
  prob_ql <- apply(probs_DDE$probfun, c(1,2), quantile, 0.025)
  prob_qh <- apply(probs_DDE$probfun, c(1,2), quantile, 0.975)
 
  plot(df_pred$DDE, prob_m, type = "l", lwd = 2, ylim = c(0, 1),
      xlab = "DDE (mg/L)", 
      ylab = "P(GAD < 37 | DDE)",
      main = "Conditional Probabilities (premature delivery)")
  lines(df_pred$DDE, prob_ql, lty = 2)
  lines(df_pred$DDE, prob_qh, lty = 2)




DDPstar documentation built on April 3, 2025, 8:46 p.m.