2022-10-11 08:25:39 +02:00
|
|
|
utils::globalVariables(c("vname"))
|
|
|
|
#' Confidence interval plot with point estimate
|
|
|
|
#'
|
2023-01-11 12:54:08 +01:00
|
|
|
#' Horizontal forest plot of point estimate with confidence intervals.
|
|
|
|
#' Includes dichotomous or olr, depending on number of levels in "x".
|
2022-10-11 08:25:39 +02:00
|
|
|
#' Title and axis labels can be added to the ggplot afterwards.
|
|
|
|
#'
|
|
|
|
#' @param ds data set
|
|
|
|
#' @param x text string of main exposure variable
|
|
|
|
#' @param y text string of outcome variable
|
|
|
|
#' @param vars variables for multivariate analysis.
|
|
|
|
#' @param dec Decimals in labels
|
|
|
|
#' @param lbls Labels for variable names
|
|
|
|
#' @param title Plot title. Can be specified later.
|
|
|
|
#'
|
|
|
|
#' @return ggplot element
|
|
|
|
#' @export
|
|
|
|
#'
|
|
|
|
#' @import ggplot2
|
|
|
|
#' @importFrom MASS polr
|
2023-01-11 12:54:08 +01:00
|
|
|
#' @importFrom stats as.formula coef confint lm quantile reorder binomial glm
|
2022-10-11 08:25:39 +02:00
|
|
|
#'
|
|
|
|
#' @examples
|
|
|
|
#' data(talos)
|
|
|
|
#' talos[,"mrs_1"]<-factor(talos[,"mrs_1"],ordered=TRUE)
|
|
|
|
#' ci_plot(ds = talos, x = "rtreat", y = "mrs_1", vars = c("hypertension","diabetes"))
|
2023-01-04 14:33:48 +01:00
|
|
|
ci_plot<- function(ds, x, y, vars=NULL, dec=3, lbls=NULL, title=NULL){
|
2022-10-11 08:25:39 +02:00
|
|
|
|
2023-01-11 12:54:08 +01:00
|
|
|
if (!is.factor(ds[,y])) stop("Outcome has to be factor")
|
2022-10-11 08:25:39 +02:00
|
|
|
|
2023-01-04 14:33:48 +01:00
|
|
|
# Formula
|
|
|
|
ci_form <- as.formula(paste0(y,"~",x,"+."))
|
|
|
|
|
|
|
|
# Ordinal logistic regression for non-dichotomous factors
|
2022-10-11 08:25:39 +02:00
|
|
|
if (length(levels(ds[,y])) > 2){
|
2023-01-11 12:54:08 +01:00
|
|
|
m <- MASS::polr(formula = ci_form, data=ds[,unique(c(x, y, vars))],
|
|
|
|
Hess=TRUE, method="logistic")
|
2022-10-11 08:25:39 +02:00
|
|
|
if (is.null(title)) title <- "Ordinal logistic regression"
|
|
|
|
}
|
|
|
|
|
2023-01-04 14:33:48 +01:00
|
|
|
# Binary logistic regression for dichotomous factors
|
2022-10-11 08:25:39 +02:00
|
|
|
if (length(levels(ds[,y])) == 2){
|
2023-01-11 12:54:08 +01:00
|
|
|
m <- glm(formula = ci_form, data=ds[unique(c(x, y, vars))],
|
|
|
|
family=binomial())
|
2022-10-11 08:25:39 +02:00
|
|
|
if (is.null(title)) title <- "Binary logistic regression"
|
|
|
|
}
|
|
|
|
|
|
|
|
odds <- data.frame(cbind(exp(coef(m)), exp(confint(m))))
|
|
|
|
|
|
|
|
names(odds)<-c("or", "lo", "up")
|
|
|
|
rodds<-round(odds, digits = dec)
|
|
|
|
|
|
|
|
if (is.null(lbls)){
|
2023-01-11 12:54:08 +01:00
|
|
|
odds$vname<-paste0(row.names(odds)," \n",
|
|
|
|
paste0(rodds$or,"
|
|
|
|
[",rodds$lo,":",rodds$up,"]"))
|
2022-10-11 08:25:39 +02:00
|
|
|
} else {
|
2023-01-11 12:54:08 +01:00
|
|
|
odds$vname<-paste0(lbls," \n",paste0(rodds$or,
|
|
|
|
" [",rodds$lo,":",rodds$up,"]"))
|
2022-10-11 08:25:39 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-01-11 12:54:08 +01:00
|
|
|
odds$ord<-rev(seq_len(nrow(odds)))
|
2022-10-11 08:25:39 +02:00
|
|
|
|
2023-01-11 12:54:08 +01:00
|
|
|
ggplot2::ggplot(data = odds,
|
|
|
|
mapping = ggplot2::aes(y = or, x = reorder(vname,ord))) +
|
2022-10-11 08:25:39 +02:00
|
|
|
ggplot2::geom_point() +
|
2023-01-11 12:54:08 +01:00
|
|
|
ggplot2::geom_errorbar(mapping = ggplot2::aes(ymin=lo, ymax=up),
|
|
|
|
width = 0.2) +
|
2022-10-11 08:25:39 +02:00
|
|
|
ggplot2::scale_y_log10() +
|
|
|
|
ggplot2::geom_hline(yintercept = 1, linetype=2) +
|
|
|
|
ggplot2::labs(title=title) +
|
|
|
|
ggplot2::coord_flip()
|
|
|
|
}
|
|
|
|
|
2023-01-04 14:33:48 +01:00
|
|
|
|