From af0c04e5ce8ec0030c3ce0342a4733a59cb7ccdf Mon Sep 17 00:00:00 2001 From: agdamsbo Date: Thu, 4 Oct 2018 10:10:35 +0200 Subject: [PATCH] universalising.. --- R/rep_glm.R | 23 +++++++++-------------- man/rep_glm.Rd | 8 ++++++-- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/R/rep_glm.R b/R/rep_glm.R index 4978c85..a06090e 100644 --- a/R/rep_glm.R +++ b/R/rep_glm.R @@ -1,15 +1,17 @@ #' A repeated 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 y Effect meassure. +#' @param meas Effect meassure. Input as c() of columnnames, use dput(). #' @param vars variables in model. Input as c() of columnnames, use dput(). #' @param string variables to test. Input as c() of columnnames, use dput(). +#' @param ci flag to get results as OR with 95% confidence interval. +#' @param data data frame to pull variables from. #' @keywords logistic regression #' @export #' @examples #' rep_glm() -rep_glm<-function(y,vars,string,ci=FALSE,data){ +rep_glm<-function(meas,vars,string,ci=FALSE,data){ ## x is data.frame of predictors, y is vector of an aoutcome as a factor ## output is returned as coefficient, or if or=TRUE as OR with 95 % CI. ## @@ -17,8 +19,9 @@ rep_glm<-function(y,vars,string,ci=FALSE,data){ require(dplyr) d<-data - x<-select(d,one_of(c(string))) - v<-select(d,one_of(c(vars))) + x<-data.frame(d[,c(string)]) + v<-data.frame(d[,c(vars)]) + y<-d[,c(meas)] dt<-cbind(y,v) m1<-length(coef(glm(y~.,family = binomial(),data = dt))) @@ -30,30 +33,22 @@ rep_glm<-function(y,vars,string,ci=FALSE,data){ names(df)<-c("pred","or_ci","pv") for(i in 1:ncol(x)){ - dat<-cbind(dt,x[,i]) - m<-glm(y~.,family = binomial(),data=dat) 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,")") - pv<-round(tidy(m)$p.value[-c(1:m1)],3) - x1<-x[,i] if (is.factor(x1)){ - pred<-paste(names(x)[i],levels(x1)[-1],sep = "_") - } + pred<-paste(names(x)[i],levels(x1)[-1],sep = "_")} else {pred<-names(x)[i]} - df<-rbind(df,cbind(pred,or_ci,pv)) - - }} + df<-rbind(df,cbind(pred,or_ci,pv))}} if (ci==FALSE){ diff --git a/man/rep_glm.Rd b/man/rep_glm.Rd index 291c196..aacf28d 100644 --- a/man/rep_glm.Rd +++ b/man/rep_glm.Rd @@ -4,14 +4,18 @@ \alias{rep_glm} \title{A repeated logistic regression function} \usage{ -rep_glm(y, vars, string, ci = FALSE, data) +rep_glm(meas, vars, string, ci = FALSE, data) } \arguments{ -\item{y}{Effect meassure.} +\item{meas}{Effect meassure. Input as c() of columnnames, use dput().} \item{vars}{variables in model. Input as c() of columnnames, use dput().} \item{string}{variables to test. Input as c() of columnnames, use dput().} + +\item{ci}{flag to get results as OR with 95% confidence interval.} + +\item{data}{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.