Skip to content

Commit

Permalink
Bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ewenharrison committed Nov 18, 2018
1 parent a428e25 commit df0d4bc
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 112 deletions.
206 changes: 111 additions & 95 deletions R/coefficient_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,27 @@
#' (must be numeric/continuous).
#' @param explanatory Character vector of any length: name(s) of explanatory
#' variables.
#' @param random_effect Character vector of length 1, name of random effect
#' variable.
#' @param factorlist Option to provide output directly from
#' \code{\link{summary_factorlist}()}.
#' @param lmfit Option to provide output directly from \code{\link{lmmulti}()}
#' and \code{\link{lmmixed}()}.
#' @param confint_type For for \code{lmer} models, one of \code{c("default",
#' "Wald", "profile", "boot")} Note "default" == "Wald".
#' @param breaks Manually specify x-axis breaks in format \code{c(0.1, 1, 10)}.
#' @param column_space Adjust table column spacing.
#' @param dependent_label Main label for plot.
#' @param prefix Plots are titled by default with the dependent variable. This adds text before that label.
#' @param suffix Plots are titled with the dependent variable. This adds text after that label.
#' @param prefix Plots are titled by default with the dependent variable. This
#' adds text before that label.
#' @param suffix Plots are titled with the dependent variable. This adds text
#' after that label.
#' @param table_text_size Alter font size of table text.
#' @param title_text_size Alter font size of title text.
#' @param plot_opts A list of arguments to be appended to the ggplot call by "+".
#' @param table_opts A list of arguments to be appended to the ggplot table call by "+".
#' @param plot_opts A list of arguments to be appended to the ggplot call by
#' "+".
#' @param table_opts A list of arguments to be appended to the ggplot table call
#' by "+".
#' @param ... Other parameters.
#' @return Returns a table and plot produced in \code{ggplot2}.
#'
Expand All @@ -41,96 +49,104 @@
#'
#' @import ggplot2

coefficient_plot = function(.data, dependent, explanatory, factorlist=NULL, lmfit=NULL,
breaks=NULL, column_space=c(-0.5, -0.1, 0.5),
dependent_label = NULL,
prefix = "", suffix = ": Coefficient, 95% CI, p-value)",
table_text_size = 5,
title_text_size = 18,
plot_opts = NULL, table_opts = NULL, ...){
coefficient_plot = function(.data, dependent, explanatory, random_effect = NULL,
factorlist=NULL, lmfit=NULL,
confint_type = "default",
breaks=NULL, column_space=c(-0.5, -0.1, 0.5),
dependent_label = NULL,
prefix = "", suffix = ": Coefficient, 95% CI, p-value)",
table_text_size = 5,
title_text_size = 18,
plot_opts = NULL, table_opts = NULL, ...){

requireNamespace("ggplot2")

# Generate or format factorlist object
if(!is.null(factorlist)){
if(is.null(factorlist$Total)) stop("summary_factorlist function must include total_col=TRUE")
if(is.null(factorlist$fit_id)) stop("summary_factorlist function must include fit_id=TRUE")
}

if(is.null(factorlist)){
factorlist = summary_factorlist(.data, dependent, explanatory, total_col=TRUE, fit_id=TRUE)
}

if(is.null(breaks)){
breaks = scales::pretty_breaks()
}

# Generate or format lm
if(is.null(lmfit) && is.null(random_effect)){
lmfit = lmmulti(.data, dependent, explanatory)
lmfit_df_c = fit2df(lmfit, condense = TRUE, estimate_suffix = " (multivariable)",
confint_type = confint_type, ...)
} else if(is.null(lmfit) && !is.null(random_effect)){
lmfit = lmmixed(.data, dependent, explanatory, random_effect)
lmfit_df_c = fit2df(lmfit, condense = TRUE, estimate_suffix = " (multilevel)",
confint_type = confint_type, ...)
}

lmfit_df = fit2df(lmfit, condense = FALSE, confint_type = confint_type, ...)

requireNamespace("ggplot2")

# Generate or format factorlist object
if(!is.null(factorlist)){
if(is.null(factorlist$Total)) stop("summary_factorlist function must include total_col=TRUE")
if(is.null(factorlist$fit_id)) stop("summary_factorlist function must include fit_id=TRUE")
}

if(is.null(factorlist)){
factorlist = summary_factorlist(.data, dependent, explanatory, total_col=TRUE, fit_id=TRUE)
}

# Generate or format lm
if(is.null(lmfit)){
lmfit = lmmulti(.data, dependent, explanatory)
}
lmfit_df_c = fit2df(lmfit, condense = TRUE, ...)
lmfit_df = fit2df(lmfit, condense = FALSE, ...)

if(is.null(breaks)){
breaks = scales::pretty_breaks()
}
# Merge
df.out = finalfit_merge(factorlist, lmfit_df_c)
names(df.out)[which(names(df.out) %in% "Coefficient")] = "Coefficient (multivariate)"
df.out = finalfit_merge(df.out, lmfit_df, ref_symbol = "1.0")

# Fill in total for continuous variables (NA by default)
df.out$Total[is.na(df.out$Total)] = dim(.data)[1]

# Remove unwanted lines, where there are more variables in model than wish to display.
# These not named in factorlist, creating this problem. Interactions don't show on plot.
if (any(
is.na(df.out$label)
)
){
remove_rows = which(is.na(df.out$label)) # This row doesn't work when is.na == FALSE, hence if()
df.out = df.out[-remove_rows,]
} else {
df.out
}

# Fix order
df.out$levels = as.character(df.out$levels)
df.out$fit_id = factor(df.out$fit_id, levels = df.out$fit_id[order(-df.out$index)])

# Plot
g1 = ggplot(df.out, aes(x = as.numeric(Coefficient), xmin = as.numeric(L95), xmax = as.numeric(U95),
y = fit_id))+
geom_point(aes(size = Total), shape=22, fill="darkblue")+
geom_errorbarh(height=0.2) +
geom_vline(xintercept = 1, linetype = "longdash", colour = "black")+
scale_x_continuous(breaks= breaks)+
xlab("Coefficient (95% CI)")+
theme_classic(14)+
theme(axis.title.x = element_text(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.line.y = element_blank(),
axis.ticks.y = element_blank(),
legend.position="none")

t1 = ggplot(df.out, aes(x = as.numeric(Coefficient), y = fit_id))+
annotate("text", x = column_space[1], y = df.out$fit_id, label=df.out[,2], hjust=0, size=table_text_size)+
annotate("text", x = column_space[2], y = df.out$fit_id, label=df.out[,3], hjust=1, size=table_text_size)+
annotate("text", x = column_space[3], y = df.out$fit_id, label=df.out[,7], hjust=1, size=table_text_size)+
theme_classic(14)+
theme(axis.title.x = element_text(colour = "white"),
axis.text.x = element_text(colour = "white"),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
line = element_blank())

# Add optional arguments
g1 = g1 + plot_opts
t1 = t1 + table_opts

# Add dependent name label
title = plot_title(.data, dependent, dependent_label = dependent_label, prefix = prefix, suffix = suffix)

gridExtra::grid.arrange(t1, g1, ncol=2, widths = c(3,2),
top=grid::textGrob(title, x=0.02, y=0.2,
gp=grid::gpar(fontsize=title_text_size), just="left"))
# Merge
df.out = finalfit_merge(factorlist, lmfit_df_c)
df.out = finalfit_merge(df.out, lmfit_df, ref_symbol = "0")

# Fill in total for continuous variables (NA by default)
df.out$Total[is.na(df.out$Total)] = dim(.data)[1]

# Remove unwanted lines, where there are more variables in model than wish to display.
# These not named in factorlist, creating this problem. Interactions don't show on plot.
if (any(
is.na(df.out$label)
)
){
remove_rows = which(is.na(df.out$label)) # This row doesn't work when is.na == FALSE, hence if()
df.out = df.out[-remove_rows,]
} else {
df.out
}

# Fix order
df.out$levels = as.character(df.out$levels)
df.out$fit_id = factor(df.out$fit_id, levels = df.out$fit_id[order(-df.out$index)])

# Plot
g1 = ggplot(df.out, aes(x = as.numeric(Coefficient), xmin = as.numeric(L95), xmax = as.numeric(U95),
y = fit_id))+
geom_point(aes(size = Total), shape=22, fill="darkblue")+
geom_errorbarh(height=0.2) +
geom_vline(xintercept = 0, linetype = "longdash", colour = "black")+
scale_x_continuous(breaks= breaks)+
xlab("Coefficient (95% CI)")+
theme_classic(14)+
theme(axis.title.x = element_text(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.line.y = element_blank(),
axis.ticks.y = element_blank(),
legend.position="none")

t1 = ggplot(df.out, aes(x = as.numeric(Coefficient), y = fit_id))+
annotate("text", x = column_space[1], y = df.out$fit_id, label=df.out[,2], hjust=0, size=table_text_size)+
annotate("text", x = column_space[2], y = df.out$fit_id, label=df.out[,3], hjust=1, size=table_text_size)+
annotate("text", x = column_space[3], y = df.out$fit_id, label=df.out[,7], hjust=1, size=table_text_size)+
theme_classic(14)+
theme(axis.title.x = element_text(colour = "white"),
axis.text.x = element_text(colour = "white"),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
line = element_blank())

# Add optional arguments
g1 = g1 + plot_opts
t1 = t1 + table_opts

# Add dependent name label
title = plot_title(.data, dependent, dependent_label = dependent_label, prefix = prefix, suffix = suffix)

gridExtra::grid.arrange(t1, g1, ncol=2, widths = c(3,2),
top=grid::textGrob(title, x=0.02, y=0.2,
gp=grid::gpar(fontsize=title_text_size), just="left"))
}
2 changes: 1 addition & 1 deletion R/fit2df.R
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ fit2df.glmlist <- function(.data, condense=TRUE, metrics=FALSE, remove_intercept

fit2df.lmerMod = function(.data, condense=TRUE, metrics=FALSE, remove_intercept=TRUE,
explanatory_name = "explanatory",
estimate_name = "OR",
estimate_name = "Coefficient",
estimate_suffix = "",
p_name = "p",
digits=c(2,2,3),
Expand Down
15 changes: 10 additions & 5 deletions R/or_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
#' @param glmfit Option to provide output directly from \code{\link{glmmulti}()}
#' and \code{\link{glmmixed}()}.
#' @param confint_type One of \code{c("profile", "default")} for GLM models or
#' \code{c("default", "Wald", "profile", "boot")} for \code{glmer/lmer}
#' models. Note "default" == "Wald". Not implemented for \code{lm, coxph or
#' coxphlist}.
#' \code{c("default", "Wald", "profile", "boot")} for \code{glmer}
#' models. Note "default" == "Wald".
#' @param breaks Manually specify x-axis breaks in format \code{c(0.1, 1, 10)}.
#' @param column_space Adjust table column spacing.
#' @param dependent_label Main label for plot.
Expand Down Expand Up @@ -56,7 +55,7 @@

or_plot = function(.data, dependent, explanatory, random_effect=NULL,
factorlist=NULL, glmfit=NULL,
confint_type = "profile",
confint_type = NULL,
breaks=NULL, column_space=c(-0.5, 0, 0.5),
dependent_label = NULL,
prefix = "", suffix = ": OR (95% CI, p-value)",
Expand All @@ -80,6 +79,13 @@ or_plot = function(.data, dependent, explanatory, random_effect=NULL,
breaks = scales::pretty_breaks()
}

# Confidence intervals, default to "profile" for glm and "Wald" for glmer
if(is.null(confint_type) && is.null(random_effect)){
confint_type = "profile"
} else if(is.null(confint_type) && !is.null(random_effect)){
confint_type == "default"
}

# Generate or format glm
if(is.null(glmfit) && is.null(random_effect)){
glmfit = glmmulti(.data, dependent, explanatory)
Expand All @@ -101,7 +107,6 @@ or_plot = function(.data, dependent, explanatory, random_effect=NULL,
df.out$Total[is.na(df.out$Total)] = dim(.data)[1]

# Remove unwanted lines, where there are more variables in model than wish to display.
# Note merge function in summarizer merge is now `all` rather than `all.x` as wish to preserve interactions
# These not named in factorlist, creating this problem. Interactions don't show on plot.
if (any(
is.na(df.out$label)
Expand Down
23 changes: 17 additions & 6 deletions man/coefficient_plot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/fit2df.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions man/or_plot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit df0d4bc

Please sign in to comment.