as.forest: Coerce objects to forest class

View source: R/as.R

as.forestR Documentation

Coerce objects to forest class

Description

Coerce vectors or data frames to objects for use by plot.forest.

Usage

as.forest(x, ...)

## Default S3 method:
as.forest(
  x,
  lower,
  upper,
  p.value,
  labels,
  N,
  P,
  digits = 2L,
  text.estimate = NULL,
  text.pvalue = NULL,
  text.x = NULL,
  text.low = NULL,
  text.high = NULL,
  ...
)

## S3 method for class 'data.frame'
as.forest(x, labels = NULL, N = NULL, P = NULL, ...)

Arguments

x

the model estimates

...

ignored

lower, upper

the lower and upper confidence intervals for x

p.value

p-values for each estimate

labels

the row labels for each x

N, P

sample size and percentages for each x

digits

the number of digits past the decimal point to keep

Examples

x <- as.forest(
  x = 1:5, lower = 1:5 - 0.5, upper = 1:5 + 0.5, p.value = runif(5),
  labels = paste('var', 1:5), N = 1:5 * 10, P = 1:5 / 10
)
x
plot(x)

## flag reference rows
x <- as.forest(
  x = 1:5, lower = 1:5 - 0.5, upper = 1:5 + 0.5, p.value = runif(5),
  text.estimate = replace(1:5, 4, 'Reference'),
  labels = paste('var', 1:5), N = 1:5 * 10, P = 1:5 / 10
)
x
plot(x)

## with headers
x <- c(NA, 1, 2, NA, 1, 2, 3)
x <- as.forest(
  x = x, lower = x - 1, upper = x + 1, p.value = replace(runif(7), is.na(x), NA),
  labels = ifelse(is.na(x), 'header', paste0('   ', x)), N = x * 10, P = x / 10
)
plot(x, show_conf = TRUE)


## example forest plot
dat <- data.frame(matrix(rnorm(100 * 8), 100))
fit <- glm(I(X1 > 0.5) ~ ., dat, family = 'binomial')
forest(fit)

## compare with as.forest
s <- summary(fit)$coefficients[-1L, ]
x <- data.frame(exp(s[, 1]), exp(confint(fit))[-1, ], s[, 4])
f <- as.forest(
  x, labels = gsub('X', 'var ', names(dat)[-1]),
  N = colSums(dat[, -1] > 0.5), P = lengths(dat)[-1] / 100
)
plot(f, col.rows = c('grey95', 'none'))


raredd/forest documentation built on Feb. 19, 2024, 9:22 p.m.