From fa76288a0ae8beba4834e3cf26fff10f14917295 Mon Sep 17 00:00:00 2001 From: agdamsbo Date: Fri, 12 Oct 2018 11:26:20 +0200 Subject: [PATCH] Updated counting and added flag --- DESCRIPTION | 2 +- R/strobe_pred.R | 76 +++++++++++++++++++++++++++++++--------------- man/strobe_pred.Rd | 4 ++- 3 files changed, 55 insertions(+), 27 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index e0de7f3..a16ba37 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: daDoctoR Type: Package Title: FUNCTIONS FOR HEALTH RESEARCH -Version: 0.1.0.9008 +Version: 0.1.0.9009 Author@R: c(person("Andreas", "Gammelgaard Damsbo", email = "agdamsbo@pm.me", role = c("cre", "aut"))) Maintainer: Andreas Gammelgaard Damsbo Description: I am a Danish medical doctor involved in neuropsychiatric research. diff --git a/R/strobe_pred.R b/R/strobe_pred.R index ab9fc67..dfaef58 100644 --- a/R/strobe_pred.R +++ b/R/strobe_pred.R @@ -5,12 +5,13 @@ #' @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. +#' @param n.by.adj flag to indicate wether to count number of patients in adjusted model or overall. #' @keywords logistic #' @export #' @examples #' strobe_pred() -strobe_pred<-function(meas,adj,data,dec=2){ +strobe_pred<-function(meas,adj,data,dec=2,n.by.adj=FALSE){ ## Ønskeliste: ## ## - Sum af alle, der indgår (Overall N) @@ -58,7 +59,7 @@ strobe_pred<-function(meas,adj,data,dec=2){ dat<-data.frame(m=m,ads) ma <- glm(m ~ .,family = binomial(), data = dat) - + miss<-length(ma$na.action) actable <- coef(summary(ma)) pa <- actable[,4] @@ -74,32 +75,54 @@ strobe_pred<-function(meas,adj,data,dec=2){ aup<-aci[,2] aor_ci<-paste0(aco," (",alo," to ",aup,")") - dat2<-dat[,-1] # names(dat2)<-c(var,names(ads)) + nq<-c() - for (i in 1:ncol(dat2)){ - if (is.factor(dat2[,i])){ - vec<-dat2[,i] - ns<-names(dat2)[i] - for (r in 1:length(levels(vec))){ - vr<-levels(vec)[r] - dr<-vec[vec==vr&!is.na(vec)] - n<-as.numeric(length(dr)) - nall<-as.numeric(nrow(dat[!is.na(dat2[,c(ns)]),])) - nl<-paste0(ns,levels(vec)[r]) - pro<-round(n/nall*100,0) - rt<-paste0(n," (",pro,"%)") + if (n.by.adj==TRUE){ + dat2<-ma$model[,-1] + for (i in 1:ncol(dat2)){ + if (is.factor(dat2[,i])){ + vec<-dat2[,i] + ns<-names(dat2)[i] + for (r in 1:length(levels(vec))){ + vr<-levels(vec)[r] + n<-as.numeric(length(vec[vec==vr&!is.na(vec)])) + nall<-as.numeric(length(dat2[,c(ns)])) + nl<-paste0(ns,levels(vec)[r]) + pro<-round(n/nall*100,0) + rt<-paste0(n," (",pro,"%)") + nq<-rbind(nq,cbind(nl,rt)) + }} + if (!is.factor(dat2[,i])){ + num<-dat2[,i] + nl<-names(dat2)[i] + rt<-as.numeric(length(dat2[,c(nl)])) nq<-rbind(nq,cbind(nl,rt)) - } - } - if (!is.factor(dat2[,i])){ - num<-dat2[,i] - nl<-names(dat2)[i] - rt<-as.numeric(nrow(dat[!is.na(dat2[,c(nl)]),])) - nq<-rbind(nq,cbind(nl,rt)) - } - } + }}} + + else { + dat2<-dat[,-1] + for (i in 1:ncol(dat2)){ + if (is.factor(dat2[,i])){ + vec<-dat2[,i] + ns<-names(dat2)[i] + for (r in 1:length(levels(vec))){ + vr<-levels(vec)[r] + n<-as.numeric(length(vec[vec==vr&!is.na(vec)])) + nall<-as.numeric(length(dat[,c(ns)])) + nl<-paste0(ns,levels(vec)[r]) + pro<-round(n/nall*100,0) + rt<-paste0(n," (",pro,"%)") + nq<-rbind(nq,cbind(nl,rt)) + }} + if (!is.factor(dat2[,i])){ + num<-dat2[,i] + nl<-names(dat2)[i] + rt<-as.numeric(length(dat[,c(nl)])) + nq<-rbind(nq,cbind(nl,rt)) + }}} + rnames<-c() @@ -132,5 +155,8 @@ strobe_pred<-function(meas,adj,data,dec=2){ names(ref)<-c("Variable","N","Crude OR (95 % CI)","Mutually adjusted OR (95 % CI)") - return(ref) + ls<-list(tbl=ref,miss) + names(ls)<-c("Printable table","Deleted due to missingness") + + return(ls) } diff --git a/man/strobe_pred.Rd b/man/strobe_pred.Rd index e8d2f2b..1c72dcf 100644 --- a/man/strobe_pred.Rd +++ b/man/strobe_pred.Rd @@ -4,7 +4,7 @@ \alias{strobe_pred} \title{Logistic regression of predictors according to STROBE} \usage{ -strobe_pred(meas, adj, data, dec = 2) +strobe_pred(meas, adj, data, dec = 2, n.by.adj = FALSE) } \arguments{ \item{meas}{binary outcome meassure variable, column name in data.frame as a string. Can be numeric or factor. Result is calculated accordingly.} @@ -14,6 +14,8 @@ strobe_pred(meas, adj, data, dec = 2) \item{data}{dataframe of data.} \item{dec}{decimals for results, standard is set to 2. Mean and sd is dec-1.} + +\item{n.by.adj}{flag to indicate wether to count number of patients in adjusted model or overall.} } \description{ Printable table of logistic regression analysis according to STROBE.