new small function, and new forest plot function

This commit is contained in:
agdamsbo 2019-12-03 14:27:00 +01:00
parent 1ba7567814
commit f4216a43b6
8 changed files with 164 additions and 5 deletions

View File

@ -1,6 +1,6 @@
Package: daDoctoR Package: daDoctoR
Title: Functions For Health Research Title: Functions For Health Research
Version: 0.19.5 Version: 0.19.7
Year: 2019 Year: 2019
Author: Andreas Gammelgaard Damsbo <agdamsbo@pm.me> Author: Andreas Gammelgaard Damsbo <agdamsbo@pm.me>
Maintainer: Andreas Gammelgaard Damsbo <agdamsbo@pm.me> Maintainer: Andreas Gammelgaard Damsbo <agdamsbo@pm.me>

View File

@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand # Generated by roxygen2: do not edit by hand
export(age_calc) export(age_calc)
export(biv_olr_plot_col)
export(calculate_overlap) export(calculate_overlap)
export(col_fact) export(col_fact)
export(col_num) export(col_num)
@ -16,6 +17,7 @@ export(hwe_app)
export(hwe_geno) export(hwe_geno)
export(hwe_sum) export(hwe_sum)
export(plot_ord_odds) export(plot_ord_odds)
export(plot_ord_odds2)
export(rep_biv) export(rep_biv)
export(rep_epi_tests) export(rep_epi_tests)
export(rep_glm) export(rep_glm)

28
R/biv_olr_plot_col.R Normal file
View File

@ -0,0 +1,28 @@
#' For collection of datapoints for bivariate ordinal logistic regression plotting.
#'
#' Use with plot_ord_odds(), model="df".
#' @param meas outcome meassure variable name or response in data-data.frame as a string. Should be factor, preferably ordered.
#' @param vars variables to compare against. As vector of columnnames.
#' @param data dataframe of data.
#' @keywords olr
#' @export
biv_olr_plot_col<-function(meas,vars,data){
d <- data
x <- data.frame(d[, c(ad)])
y <- d[, c(meas)]
dt <- cbind(y, x)
odds<-c(matrix(ncol = 3))
nms<-c("or","lo","hi")
for (i in 1:ncol(x)) {
dat <- data.frame(y = y, x[, i])
m <- polr(y ~ ., data = dat, Hess = TRUE)
mat<-suppressMessages(matrix(c(exp(coef(m)), exp(confint(m))),ncol=3,byrow=FALSE))
colnames(mat)<-nms
odd <- data.frame(mat)
odds<-rbind(odds,odd)
}
return(odds[-1,])
}

View File

@ -8,11 +8,11 @@
#' @param hori labels the horizontal axis (this i the y axis as the plot is rotated) #' @param hori labels the horizontal axis (this i the y axis as the plot is rotated)
#' @param vert labels the horizontal axis (this i the x axis as the plot is rotated) #' @param vert labels the horizontal axis (this i the x axis as the plot is rotated)
#' @param short flag to half number of ticks on horizontal axis. #' @param short flag to half number of ticks on horizontal axis.
#' @param input can be either "model", which is a olr model (polr()), or "df", which is a dataframe whith three columns for OR, lower CI and upper CI- #' @param input can be either "model", which is a olr model (polr()), or "df", which is a dataframe whith three columns for OR, lower CI and upper CI.
#' @keywords forestplot #' @keywords forestplot
#' @export #' @export
plot_ord_odds<-function(x, title = NULL,dec=3,lbls=NULL,hori="OR (95 % CI)",vert="Variables",short=FALSE,input="model"){ plot_ord_odds<-function(x, title = NULL,dec=3,lbls=NULL,hori="OR (95 % CI)",vert="Variables",short=FALSE,input=c("model","df")){
require(ggplot2) require(ggplot2)

75
R/plot_ord_odds2.R Normal file
View File

@ -0,0 +1,75 @@
#' Forrest plot from ordinal logistic regression, version2.
#'
#' Heavily inspired by https://www.r-bloggers.com/plotting-odds-ratios-aka-a-forrestplot-with-ggplot2/
#' @param meas outcome meassure variable name or response in data-data.frame as a string. Should be factor, preferably ordered.
#' @param vars variables to compare against. As vector of columnnames.
#' @param data dataframe of data.
#' @param title plot title
#' @param dec decimals for labels
#' @param lbls labels for variable names. Carefull, as the right order is not checked automatically!
#' @param hori labels the horizontal axis (this i the y axis as the plot is rotated)
#' @param vert labels the horizontal axis (this i the x axis as the plot is rotated)
#' @param short flag to half number of ticks on horizontal axis.
#' @param analysis can be either "biv", or "multi", for creation of forest plot from either bivariate (unadjusted) or multivariate (adjusted) ordinal logistic regression.
#' @keywords forestplot
#' @export
plot_ord_odds2<-function(meas,vars,data, title = NULL,dec=3,lbls=NULL,hori="OR (95 % CI)",vert="Variables",short=FALSE,analysis=c("biv","multi")){
require(ggplot2)
d <- data
x <- data.frame(d[, c(ad)])
y <- d[, c(meas)]
if (analysis=="biv"){
dt <- cbind(y, x)
odds<-c(matrix(ncol = 3))
nms<-c("or","lo","hi")
for (i in 1:ncol(x)) {
dat <- data.frame(y = y, x[, i])
m <- polr(y ~ ., data = dat, Hess = TRUE)
mat<-suppressMessages(matrix(c(exp(coef(m)), exp(confint(m))),ncol=3,byrow=FALSE))
colnames(mat)<-nms
odd <- data.frame(mat)
odds<-rbind(odds,odd)
}
}
if (analysis=="multi"){
m<-polr(y~.,data = dta2,Hess = TRUE)
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)){
odds$vars<-paste0(lbls," \n",paste0(rodds$or," [",rodds$lo,":",rodds$up,"]"))
}
else {
odds$vars<-paste0(row.names(odds)," \n",paste0(rodds$or," [",rodds$lo,":",rodds$up,"]"))
}
ticks<-c(seq(0, 1, by =.1), seq(1, 10, by =1), seq(10, 100, by =10))
if (short==TRUE){
ticks<-ticks[seq(1, length(ticks), 2)]
}
else {ticks<-ticks}
odds$ord<-c(nrow(odds):1)
ggplot(odds, aes(y= or, x = reorder(vars,ord))) +
geom_point() +
geom_errorbar(aes(ymin=lo, ymax=up), width=.2) +
scale_y_log10(breaks=ticks, labels = ticks) +
geom_hline(yintercept = 1, linetype=2) +
coord_flip() +
labs(title = title, x = vert, y = hori) +
theme_bw()
}

19
man/biv_olr_plot_col.Rd Normal file
View File

@ -0,0 +1,19 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/biv_olr_plot_col.R
\name{biv_olr_plot_col}
\alias{biv_olr_plot_col}
\title{For collection of datapoints for bivariate ordinal logistic regression plotting.}
\usage{
biv_olr_plot_col(meas, vars, data)
}
\arguments{
\item{meas}{outcome meassure variable name or response in data-data.frame as a string. Should be factor, preferably ordered.}
\item{vars}{variables to compare against. As vector of columnnames.}
\item{data}{dataframe of data.}
}
\description{
Use with plot_ord_odds(), model="df".
}
\keyword{olr}

View File

@ -6,7 +6,7 @@
\usage{ \usage{
plot_ord_odds(x, title = NULL, dec = 3, lbls = NULL, plot_ord_odds(x, title = NULL, dec = 3, lbls = NULL,
hori = "OR (95 \% CI)", vert = "Variables", short = FALSE, hori = "OR (95 \% CI)", vert = "Variables", short = FALSE,
input = "model") input = c("model", "df"))
} }
\arguments{ \arguments{
\item{x}{input data.} \item{x}{input data.}
@ -23,7 +23,7 @@ plot_ord_odds(x, title = NULL, dec = 3, lbls = NULL,
\item{short}{flag to half number of ticks on horizontal axis.} \item{short}{flag to half number of ticks on horizontal axis.}
\item{input}{can be either "model", which is a olr model (polr()), or "df", which is a dataframe whith three columns for OR, lower CI and upper CI-} \item{input}{can be either "model", which is a olr model (polr()), or "df", which is a dataframe whith three columns for OR, lower CI and upper CI.}
} }
\description{ \description{
Heavily inspired by https://www.r-bloggers.com/plotting-odds-ratios-aka-a-forrestplot-with-ggplot2/ Heavily inspired by https://www.r-bloggers.com/plotting-odds-ratios-aka-a-forrestplot-with-ggplot2/

35
man/plot_ord_odds2.Rd Normal file
View File

@ -0,0 +1,35 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/plot_ord_odds2.R
\name{plot_ord_odds2}
\alias{plot_ord_odds2}
\title{Forrest plot from ordinal logistic regression, version2.}
\usage{
plot_ord_odds2(meas, vars, data, title = NULL, dec = 3, lbls = NULL,
hori = "OR (95 \% CI)", vert = "Variables", short = FALSE,
analysis = c("biv", "multi"))
}
\arguments{
\item{meas}{outcome meassure variable name or response in data-data.frame as a string. Should be factor, preferably ordered.}
\item{vars}{variables to compare against. As vector of columnnames.}
\item{data}{dataframe of data.}
\item{title}{plot title}
\item{dec}{decimals for labels}
\item{lbls}{labels for variable names. Carefull, as the right order is not checked automatically!}
\item{hori}{labels the horizontal axis (this i the y axis as the plot is rotated)}
\item{vert}{labels the horizontal axis (this i the x axis as the plot is rotated)}
\item{short}{flag to half number of ticks on horizontal axis.}
\item{analysis}{can be either "biv", or "multi", for creation of forest plot from either bivariate (unadjusted) or multivariate (adjusted) ordinal logistic regression.}
}
\description{
Heavily inspired by https://www.r-bloggers.com/plotting-odds-ratios-aka-a-forrestplot-with-ggplot2/
}
\keyword{forestplot}