2019-11-12 14:12:27 +01:00
|
|
|
#' Print ordinal logistic regression results according to STROBE
|
2018-10-10 09:02:22 +02:00
|
|
|
#'
|
2019-11-12 14:12:27 +01:00
|
|
|
#' Printable table of ordinal logistic regression analysis oaccording to STROBE. Uses polr() funtion of the MASS-package.
|
2018-10-10 09:02:22 +02:00
|
|
|
#' @param meas outcome meassure variable name in data-data.frame as a string. Can be numeric or factor. Result is calculated accordingly.
|
2018-10-24 12:22:54 +02:00
|
|
|
#' @param vars variables to compare against. As vector of columnnames.
|
2018-10-10 09:02:22 +02:00
|
|
|
#' @param data dataframe of data.
|
|
|
|
#' @param dec decimals for results, standard is set to 2. Mean and sd is dec-1.
|
2018-10-11 15:34:44 +02:00
|
|
|
#' @keywords olr
|
2018-10-10 09:02:22 +02:00
|
|
|
#' @export
|
|
|
|
|
2018-10-24 12:22:54 +02:00
|
|
|
strobe_olr<-function(meas,vars,data,dec=2){
|
|
|
|
|
2018-10-10 09:02:22 +02:00
|
|
|
require(MASS)
|
|
|
|
require(dplyr)
|
|
|
|
|
|
|
|
d<-data
|
|
|
|
m<-d[,c(meas)]
|
2018-10-24 12:22:54 +02:00
|
|
|
v<-d[,c(vars)]
|
2018-10-10 09:02:22 +02:00
|
|
|
|
2018-10-11 08:42:14 +02:00
|
|
|
dat<-data.frame(m,v)
|
|
|
|
|
2018-10-10 09:02:22 +02:00
|
|
|
ma <- polr(m ~ ., data = dat, Hess=TRUE)
|
|
|
|
|
|
|
|
actable <- coef(summary(ma))
|
|
|
|
pa <- pnorm(abs(actable[, "t value"]), lower.tail = FALSE) * 2
|
|
|
|
pa<-ifelse(pa<0.001,"<0.001",round(pa,3))
|
|
|
|
pa <- ifelse(pa<=0.05|pa=="<0.001",paste0("*",pa),
|
|
|
|
ifelse(pa>0.05&pa<=0.1,paste0(".",pa),pa))
|
|
|
|
|
|
|
|
apv<-pa[1:length(coef(ma))]
|
|
|
|
|
|
|
|
aco<-round(exp(coef(ma)),dec)
|
|
|
|
aci<-round(exp(confint(ma)),dec)
|
|
|
|
alo<-aci[,1]
|
|
|
|
aup<-aci[,2]
|
|
|
|
aor_ci<-paste0(aco," (",alo," to ",aup,")")
|
|
|
|
|
2018-10-24 12:22:54 +02:00
|
|
|
dat2<-ma$model[,-1]
|
2018-10-10 09:02:22 +02:00
|
|
|
# names(dat2)<-c(var,names(ads))
|
|
|
|
nq<-c()
|
|
|
|
|
|
|
|
for (i in 1:ncol(dat2)){
|
|
|
|
if (is.factor(dat2[,i])){
|
2018-10-24 12:22:54 +02:00
|
|
|
vec<-dat2[,i]
|
2018-10-10 09:02:22 +02:00
|
|
|
ns<-names(dat2)[i]
|
|
|
|
for (r in 1:length(levels(vec))){
|
|
|
|
vr<-levels(vec)[r]
|
2018-10-24 12:00:53 +02:00
|
|
|
dr<-vec[vec==vr]
|
2018-10-10 09:02:22 +02:00
|
|
|
n<-as.numeric(length(dr))
|
2018-10-24 12:22:54 +02:00
|
|
|
nall<-as.numeric(nrow(dat2))
|
2018-10-10 09:02:22 +02:00
|
|
|
nl<-paste0(ns,levels(vec)[r])
|
2018-10-11 17:11:35 +02:00
|
|
|
pro<-round(n/nall*100,0)
|
|
|
|
rt<-paste0(n," (",pro,"%)")
|
|
|
|
nq<-rbind(nq,cbind(nl,rt))
|
2018-10-10 09:02:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!is.factor(dat2[,i])){
|
|
|
|
num<-dat2[,i]
|
|
|
|
ns<-names(dat2)[i]
|
2018-10-24 12:22:54 +02:00
|
|
|
n<-as.numeric(nrow(dat2))
|
|
|
|
nall<-as.numeric(nrow(dat2))
|
2018-10-11 17:11:35 +02:00
|
|
|
pro<-round(n/nall*100,0)
|
|
|
|
rt<-paste0(n," (",pro,"%)")
|
|
|
|
nq<-rbind(nq,cbind(ns,rt))
|
2018-10-10 09:02:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rnames<-c()
|
|
|
|
|
|
|
|
for (i in 1:ncol(dat2)){
|
|
|
|
if (is.factor(dat2[,i])){
|
|
|
|
rnames<-c(rnames,names(dat2)[i],paste0(names(dat2)[i],levels(dat2[,i])))
|
|
|
|
}
|
|
|
|
if (!is.factor(dat2[,i])){
|
|
|
|
rnames<-c(rnames,paste0(names(dat2)[i],".all"),names(dat2)[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res<-cbind(aor_ci,apv)
|
|
|
|
rest<-data.frame(names=row.names(res),res,stringsAsFactors = F)
|
|
|
|
|
2018-10-11 17:11:35 +02:00
|
|
|
numb<-data.frame(names=nq[,c("nl")],N=nq[,c("rt")],stringsAsFactors = F)
|
2018-10-10 09:02:22 +02:00
|
|
|
namt<-data.frame(names=rnames,stringsAsFactors = F)
|
|
|
|
|
|
|
|
coll<-left_join(left_join(namt,numb,by="names"),rest,by="names")
|
|
|
|
|
2018-10-24 12:22:54 +02:00
|
|
|
df<-data.frame(coll)
|
2018-10-10 09:02:22 +02:00
|
|
|
|
2018-10-24 12:22:54 +02:00
|
|
|
names(df)<-c("Variable","N","OR (95 % CI)","p value")
|
2018-10-10 09:02:22 +02:00
|
|
|
|
2018-10-24 12:22:54 +02:00
|
|
|
return(df)
|
2018-10-10 09:02:22 +02:00
|
|
|
}
|