Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

The following is my data-frame:

structure(list(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 

13, 14, 15), var1 = c(1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 

1, 1), var2 = c(0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1), 

    var3 = c(1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1), var4 = c(0, 

    0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1), outcome = c(1, 

    1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1)), row.names = c(NA, 

-15L), class = c("tbl_df", "tbl", "data.frame"))

I just want to have a code that will help me to calculate all possible odds ratio with the help of chi square, with 95% CI and p values, between all columns and the column outcome. I don't know how to do that, kindly help me...

1 Answer

0 votes
by (108k points)

The below code will help you to perform the provided tasks as described in your question but 3/4 give errors.

library(epitools)

cols <- grep("var", names(df1), value = TRUE)

res_list <- lapply(cols, function(v){

  tbl <- table(df1[, c(v, "outcome")])

  tryCatch(oddsratio(x = tbl), error = function(e) e)

})

ok <- !sapply(res_list, inherits, "error")

res_list[ok]

The following are some of the errors:

simpleError in uniroot(function(or) { 1 - midp(a1, a0, b1, b0, or) - alpha/2}, interval = interval): f() values at end points not of opposite sign

which can be handled with the following:

res_list[!ok]

If you are a beginner and want to know more about R, then do checkout the following R programming tutorial that will help you in understanding the basics of R 

Browse Categories

...