universalising..

This commit is contained in:
agdamsbo 2018-10-04 10:10:35 +02:00
parent 39dc511e12
commit af0c04e5ce
2 changed files with 15 additions and 16 deletions

View File

@ -1,15 +1,17 @@
#' A repeated logistic regression function #' 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. #' @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 vars variables in model. Input as c() of columnnames, use dput().
#' @param string variables to test. 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 #' @keywords logistic regression
#' @export #' @export
#' @examples #' @examples
#' rep_glm() #' 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 ## 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. ## 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) require(dplyr)
d<-data d<-data
x<-select(d,one_of(c(string))) x<-data.frame(d[,c(string)])
v<-select(d,one_of(c(vars))) v<-data.frame(d[,c(vars)])
y<-d[,c(meas)]
dt<-cbind(y,v) dt<-cbind(y,v)
m1<-length(coef(glm(y~.,family = binomial(),data = dt))) 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") names(df)<-c("pred","or_ci","pv")
for(i in 1:ncol(x)){ for(i in 1:ncol(x)){
dat<-cbind(dt,x[,i]) dat<-cbind(dt,x[,i])
m<-glm(y~.,family = binomial(),data=dat) m<-glm(y~.,family = binomial(),data=dat)
l<-suppressMessages(round(exp(confint(m))[-c(1:m1),1],2)) l<-suppressMessages(round(exp(confint(m))[-c(1:m1),1],2))
u<-suppressMessages(round(exp(confint(m))[-c(1:m1),2],2)) u<-suppressMessages(round(exp(confint(m))[-c(1:m1),2],2))
or<-round(exp(coef(m))[-c(1:m1)],2) or<-round(exp(coef(m))[-c(1:m1)],2)
or_ci<-paste0(or," (",l," to ",u,")") or_ci<-paste0(or," (",l," to ",u,")")
pv<-round(tidy(m)$p.value[-c(1:m1)],3) pv<-round(tidy(m)$p.value[-c(1:m1)],3)
x1<-x[,i] x1<-x[,i]
if (is.factor(x1)){ 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]} else {pred<-names(x)[i]}
df<-rbind(df,cbind(pred,or_ci,pv)) df<-rbind(df,cbind(pred,or_ci,pv))}}
}}
if (ci==FALSE){ if (ci==FALSE){

View File

@ -4,14 +4,18 @@
\alias{rep_glm} \alias{rep_glm}
\title{A repeated logistic regression function} \title{A repeated logistic regression function}
\usage{ \usage{
rep_glm(y, vars, string, ci = FALSE, data) rep_glm(meas, vars, string, ci = FALSE, data)
} }
\arguments{ \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{vars}{variables in model. Input as c() of columnnames, use dput().}
\item{string}{variables to test. 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{ \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. 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.