Home > OS >  write a r function to run regression
write a r function to run regression

Time:06-13

I wanted to write a R function to run regression and generate tables. Here is the function i had so far, only including the regression part here.

library(geepack)
id<-rep(c(1:100),times=3)
df1<-data.frame(id)
df1$var1 <- sample(500:1000, length(df1$id))
df1$conf1 <- sample(500:1000, length(df1$id))
df1$conf2 <- sample(500:1000, length(df1$id))
df1$outcome <- sample(0:1, length(df1$id), replace=TRUE)

getReg<-function(varname, outcomename, confounderlist,  pat_id, data){
data.sub <- na.omit(subset(data, select = c(outcomename, varname, confounderlist, pat_id)))
  formula1<-paste(outcomename,"~",varname,  " ",
                  paste(confounderlist, collapse=" "), ",", paste("id", "=", pat_id))
  fit1<-geeglm(formula1, data=data.sub, binomial(link="logit"))
}
getReg(varname="var1", outcomename="outcome",  confounderlist=c("conf1", "conf2"), pat_id="id", data=df1)

I got an error saying " Error in str2lang(x) : :1:30: unexpected ',' 1: outcome ~ var1 conf1 conf2 , ^ " Can anyone let me know what's wrong with my code? Thanks a lot!

CodePudding user response:

The id parameter needs to be passed to the id parameter in geeglm, rather than contained in the formula. I also found that I needed to unquote the formula:

library(geepack)
id <- rep(c(1:100), times = 3)
df1 <- data.frame(id)
df1$var1 <- sample(500:1000, length(df1$id))
df1$conf1 <- sample(500:1000, length(df1$id))
df1$conf2 <- sample(500:1000, length(df1$id))
df1$outcome <- sample(0:1, length(df1$id), replace = TRUE)

getReg <- function(varname, outcomename, confounderlist, pat_id, data) {
  data.sub <- na.omit(
    subset(
      data, 
      select = c(outcomename, varname, confounderlist, pat_id)
      )
    )
  formula1 <- as.formula(
    paste(
      outcomename, "~", 
      varname,  " ",
      paste(confounderlist, collapse = "   ")
      )
    )
  fit1 <- geeglm(
    formula1, 
    data = data.sub, 
    id = data.sub[[pat_id]], 
    binomial(link = "logit")
    )
}
getReg(
  varname = "var1", 
  outcomename = "outcome",  
  confounderlist = c("conf1", "conf2"), 
  pat_id = "id", 
  data = df1
  )
  • Related