Two new functions: rep_olr and plot_ord_odds

This commit is contained in:
agdamsbo 2018-10-09 14:03:56 +02:00
parent a8afb7df46
commit 800b4e8199
7 changed files with 198 additions and 5 deletions

View File

@ -9,7 +9,8 @@ Description: Tools for Danish health research. A collection of
Depends: R (>= 3.4.4) Depends: R (>= 3.4.4)
Imports: broom, Imports: broom,
dplyr, dplyr,
epiR epiR,
ggplot2
License: GPL (>= 2) License: GPL (>= 2)
Encoding: UTF-8 Encoding: UTF-8
LazyData: true LazyData: true

View File

@ -10,10 +10,12 @@ export(dob_extract_cpr)
export(hwe_allele) export(hwe_allele)
export(hwe_geno) export(hwe_geno)
export(hwe_sum) export(hwe_sum)
export(plot_ord_odds)
export(rep_biv) export(rep_biv)
export(rep_epi_tests) export(rep_epi_tests)
export(rep_glm) export(rep_glm)
export(rep_lm) export(rep_lm)
export(rep_olr)
export(rep_reg_cie) export(rep_reg_cie)
export(strobe_diff_bygroup) export(strobe_diff_bygroup)
export(strobe_diff_byvar) export(strobe_diff_byvar)

39
R/plot_ord_odds.R Normal file
View File

@ -0,0 +1,39 @@
#' Forrest plot from ordinal logistic regression
#'
#' Heavily inspired by https://www.r-bloggers.com/plotting-odds-ratios-aka-a-forrestplot-with-ggplot2/
#' @param x ordinal logistic regression model.
#' @param title plot title
#' @param dec decimals for labels
#' @param lbls labels for variable names. Carefull, as the right order is not checked automatically!
#' @keywords forestplot
#' @export
#' @examples
#' plot_ord_odds()
plot_ord_odds<-function(x, title = NULL,dec=3,lbls=NULL){
require(ggplot2)
odds<-data.frame(cbind(exp(coef(x)), exp(confint(x))))
names(odds)<-c("or", "lo", "up")
rodds<-round(odds,digits = dec)
if (!is.null(lbls)){
odds$vars<-paste0(v.names," \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(.1, 1, by =.1), seq(0, 10, by =1), seq(10, 100, by =10))
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 = "Variables", y = "OR (95 % CI)") +
theme_bw()
}

View File

@ -81,11 +81,10 @@ rep_glm<-function(meas,vars,string,ci=FALSE,data){
}} }}
pa<-as.numeric(df[,3]) pa<-as.numeric(df[,"pv"])
t <- ifelse(pa<=0.1,"include","drop")
pa<-ifelse(pa<0.001,"<0.001",pa) pa<-ifelse(pa<0.001,"<0.001",pa)
t <- ifelse(pa<=0.1|pa=="<0.001","include","drop")
pa <- ifelse(pa<=0.05|pa=="<0.001",paste0("*",pa), pa <- ifelse(pa<=0.05|pa=="<0.001",paste0("*",pa),
ifelse(pa>0.05&pa<=0.1,paste0(".",pa),pa)) ifelse(pa>0.05&pa<=0.1,paste0(".",pa),pa))

99
R/rep_olr.R Normal file
View File

@ -0,0 +1,99 @@
#' A repeated ordinal logistic regression function
#'
#' @description For bivariate analyses. The confint() function is rather slow, causing the whole function to hang when including many predictors and calculating the ORs with CI.
#' @param meas Effect meassure. Input as c() of columnnames, use dput().
#' @param vars variables in model. Input as c() of columnnames, use dput().
#' @param str variables to test. Input as c() of columnnames, use dput().
#' @param ci flag to get results as OR with 95% confidence interval.
#' @param dta data frame to pull variables from.
#' @keywords olr ordinal logistic regression
#' @export
#' @examples
#' rep_olr()
rep_olr<-function(meas,vars,string,ci=FALSE,data){
require(broom)
require(MASS)
d<-data
x<-data.frame(d[,c(string)])
v<-data.frame(d[,c(vars)])
names(v)<-c(vars)
y<-d[,c(meas)]
dt<-cbind(y,v)
m1<-length(coef(polr(y~.,data = dt,Hess=TRUE)))
if (!is.factor(y)){stop("y should be a factor!")}
if (ci==TRUE){
df<-data.frame(matrix(ncol = 3))
names(df)<-c("pred","or_ci","pv")
for(i in 1:ncol(x)){
dat<-cbind(dt,x[,i])
m<-polr(y~.,data=dat,Hess=TRUE)
ctable <- coef(summary(m))
l<-suppressMessages(round(exp(confint(m))[-c(1:m1),1],2))
u<-suppressMessages(round(exp(confint(m))[-c(1:m1),2],2))
or<-round(exp(coef(m))[-c(1:m1)],2)
or_ci<-paste0(or," (",l," to ",u,")")
p <- (pnorm(abs(ctable[, "t value"]), lower.tail = FALSE) * 2)[1:length(coef(m))]
pv<-round(p[-c(1:m1)],3)
x1<-x[,i]
if (is.factor(x1)){
pred<-paste(names(x)[i],levels(x1)[-1],sep = "_")}
else {pred<-names(x)[i]}
df<-rbind(df,cbind(pred,or_ci,pv))
}}
if (ci==FALSE){
df<-data.frame(matrix(ncol = 3))
names(df)<-c("pred","b","pv")
for(i in 1:ncol(x)){
dat<-cbind(dt,x[,i])
m<-polr(y~.,data=dat,Hess=TRUE)
b<-round(coef(m)[-c(1:m1)],2)
p <- (pnorm(abs(ctable[, "t value"]), lower.tail = FALSE) * 2)[1:length(coef(m))]
pv<-round(p[-c(1:m1)],3)
x1<-x[,i]
if (is.factor(x1)){
pred<-paste(names(x)[i],levels(x1)[-1],sep = "_")
}
else {pred<-names(x)[i]}
df<-rbind(df,cbind(pred,b,pv))
}}
pa<-as.numeric(df[,c("pv")])
t <- ifelse(pa<=0.1,"include","drop")
pa<-ifelse(pa<0.001,"<0.001",pa)
pa <- ifelse(pa<=0.05|pa=="<0.001",paste0("*",pa),
ifelse(pa>0.05&pa<=0.1,paste0(".",pa),pa))
r<-data.frame(df[,1:2],pa,t)[-1,]
return(r)
}

24
man/plot_ord_odds.Rd Normal file
View File

@ -0,0 +1,24 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/plot_ord_odds.R
\name{plot_ord_odds}
\alias{plot_ord_odds}
\title{Forrest plot from ordinal logistic regression}
\usage{
plot_ord_odds(x, title = NULL, dec = 3, lbls = NULL)
}
\arguments{
\item{x}{ordinal logistic regression model.}
\item{title}{plot title}
\item{dec}{decimals for labels}
\item{lbls}{labels for variable names. Carefull, as the right order is not checked automatically!}
}
\description{
Heavily inspired by https://www.r-bloggers.com/plotting-odds-ratios-aka-a-forrestplot-with-ggplot2/
}
\examples{
plot_ord_odds()
}
\keyword{forestplot}

29
man/rep_olr.Rd Normal file
View File

@ -0,0 +1,29 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/rep_olr.R
\name{rep_olr}
\alias{rep_olr}
\title{A repeated ordinal logistic regression function}
\usage{
rep_olr(meas, vars, string, ci = FALSE, data)
}
\arguments{
\item{meas}{Effect meassure. Input as c() of columnnames, use dput().}
\item{vars}{variables in model. Input as c() of columnnames, use dput().}
\item{ci}{flag to get results as OR with 95% confidence interval.}
\item{str}{variables to test. Input as c() of columnnames, use dput().}
\item{dta}{data frame to pull variables from.}
}
\description{
For bivariate analyses. The confint() function is rather slow, causing the whole function to hang when including many predictors and calculating the ORs with CI.
}
\examples{
rep_olr()
}
\keyword{logistic}
\keyword{olr}
\keyword{ordinal}
\keyword{regression}