New function strobe_diff_twodim

This commit is contained in:
agdamsbo 2018-10-10 11:49:52 +02:00
parent 45f734631d
commit 2907028382
4 changed files with 135 additions and 1 deletions

View File

@ -19,4 +19,5 @@ export(rep_olr)
export(rep_reg_cie)
export(strobe_diff_bygroup)
export(strobe_diff_byvar)
export(strobe_diff_twodim)
export(strobe_olr)

105
R/strobe_diff_twodim.R Normal file
View File

@ -0,0 +1,105 @@
#' Print regression results according to STROBE
#'
#' Printable table of regression analysis by group for meas. Detects wether to perform logistic or linear regression.
#' @param meas outcome meassure variable name in data-data.frame as a string. Can be numeric or factor. Result is calculated accordingly.
#' @param groups groups to compare, as string.
#' @param adj variables to adjust for, as string.
#' @param data dataframe of data.
#' @param dec decimals for results, standard is set to 2. Mean and sd is dec-1.
#' @keywords cpr
#' @export
#' @examples
#' strobe_diff_byvar()
strobe_diff_twodim<-function(meas,group,adj,data,dec=2){
## meas: sdmt
## var: rtreat
## group: genotype
## for dichotome exposure variable (var)
d<-data
m<-d[,c(meas)]
g<-d[,c(group)]
ads<-d[,c(adj)]
dat<-data.frame(m,g,ads)
df<-data.frame(grp=c(group,as.character(levels(g))))
if(!is.factor(m)){
mod<-lm(m~g,data=dat)
ci<-confint(mod)
co<-round(coef(mod)[-1],dec)
lo<-round(ci[-1,1],dec)
up<-round(ci[-1,2],dec)
or_ci<-c("0",paste0(co," (",lo," to ",up,")"))
amod<-lm(m~.,data=dat)
aci<-confint(amod)
aco<-round(coef(amod)[2:length(levels(g))],dec)
alo<-round(aci[2:length(levels(g)),1],dec)
aup<-round(aci[2:length(levels(g)),2],dec)
aor_ci<-c("0",paste0(aco," (",alo," to ",aup,")"))
nr<-c()
for (r in 1:length(levels(g))){
vr<-levels(dat$g)[r]
dr<-dat[dat$g==vr,]
n<-as.numeric(nrow(dr[!is.na(dr$m),]))
mean<-round(mean(dr$m,na.rm = TRUE),dec-1)
sd<-round(sd(dr$m,na.rm = TRUE),dec-1)
ms<-paste0(mean," (",sd,")")
nr<-c(nr,n,ms)
}
irl<-rbind(matrix(NA,ncol=4),cbind(matrix(nr,ncol=2,byrow = TRUE),cbind(or_ci,aor_ci)))
colnames(irl)<-c("N","Mean (SD)","Difference","Adjusted Difference")
df<-cbind(df,irl)
ls<-list(linear.regression=df)
}
if(is.factor(m)){
mod<-glm(m~g,family=binomial(),data=di)
ci<-confint(mod)
co<-round(coef(mod)[-1],dec)
lo<-round(ci[-1,1],dec)
up<-round(ci[-1,2],dec)
or_ci<-c("0",paste0(co," (",lo," to ",up,")"))
amod<-glm(m~.,family=binomial(),data=di)
aci<-confint(amod)
aco<-round(coef(amod)[2:length(levels(g))],dec)
alo<-round(aci[2:length(levels(g)),1],dec)
aup<-round(aci[2:length(levels(g)),2],dec)
aor_ci<-c("0",paste0(aco," (",alo," to ",aup,")"))
nr<-c()
for (r in 1:length(levels(g))){
vr<-levels(dat$g)[r]
dr<-dat[dat$g==vr,]
n<-as.numeric(nrow(dr[!is.na(dr$m),]))
nl<-levels(m)[2]
out<-nrow(dr[dr$m==nl&!is.na(dr$m),])
pro<-round(out/n*100,0)
rt<-paste0(out," (",pro,"%)")
nr<-c(nr,n,rt)
}
irl<-rbind(matrix(NA,ncol=4),cbind(matrix(nr,ncol=2,byrow = TRUE),cbind(or_ci,aor_ci)))
colnames(irl)<-c("N",paste0("N.",nl),"OR","Adjusted OR")
df<-cbind(df,irl)
ls<-list(logistic.regression=df)
}
ls$adjustments<-dput(names(ads))
return(ls)
}

View File

@ -4,7 +4,7 @@
\alias{plot_ord_odds}
\title{Forrest plot from ordinal logistic regression}
\usage{
plot_ord_odds(x, title = NULL, dec = 3, lbls = NULL)
plot_ord_odds(x, title = NULL, dec = 3, lbls = NULL, short = FALSE)
}
\arguments{
\item{x}{ordinal logistic regression model.}
@ -14,6 +14,8 @@ plot_ord_odds(x, title = NULL, dec = 3, lbls = NULL)
\item{dec}{decimals for labels}
\item{lbls}{labels for variable names. Carefull, as the right order is not checked automatically!}
\item{short}{flag to half number of ticks on horizontal axis.}
}
\description{
Heavily inspired by https://www.r-bloggers.com/plotting-odds-ratios-aka-a-forrestplot-with-ggplot2/

26
man/strobe_diff_twodim.Rd Normal file
View File

@ -0,0 +1,26 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/strobe_diff_twodim.R
\name{strobe_diff_twodim}
\alias{strobe_diff_twodim}
\title{Print regression results according to STROBE}
\usage{
strobe_diff_twodim(meas, group, adj, data, dec = 2)
}
\arguments{
\item{meas}{outcome meassure variable name in data-data.frame as a string. Can be numeric or factor. Result is calculated accordingly.}
\item{adj}{variables to adjust for, as string.}
\item{data}{dataframe of data.}
\item{dec}{decimals for results, standard is set to 2. Mean and sd is dec-1.}
\item{groups}{groups to compare, as string.}
}
\description{
Printable table of regression analysis by group for meas. Detects wether to perform logistic or linear regression.
}
\examples{
strobe_diff_byvar()
}
\keyword{cpr}