Make confidence interval appear in brackets

1.3k views Asked by At

I have got a function in R:

myfun=function(data,conf.level){
  n11=data$ev.trt
  n12=data$n.trt-data$ev.trt
  n21=data$ev.ctrl
  n22=data$n.ctrl-data$ev.ctrl
  Author=data$Author
  z = qnorm(1 - (1-conf.level)/2)
  odds=(n11*n22)/(n12*n21)
  logodds=log(odds)
  varodds=(1/n11)+(1/n12)+(1/n21)+(1/n22)
  sdodds=sqrt(varodds)
  logCIlowerodds=logodds-(z*sdodds)
  logCIupperodds=logodds+(z*sdodds)
  CIlowerodds=exp(logCIlowerodds)
  CIupperodds=exp(logCIupperodds)
  CIodds=cbind(CIlowerodds, CIupperodds)
  print(CIodds)
}

When I run this, say:

myfun(epidural,0.95)

I get:

      CIlowerodds CIupperodds
[1,]  0.30008732    2.661063
[2,]  0.55185470    1.493418
[3,]  0.04713221   14.389324
[4,]  0.68714690    1.111550
[5,]  0.57123095    1.021207
[6,]  0.09259817    1.159933

What I want instead is that for [1,] I get a confidence interval with brackets and a comma in between, looking something like:

 (0.3000, 2.661)

Any ideas how I can do this?

Thank you

1

There are 1 answers

2
Rentrop On

Add the following code before you print the Result but after the cbind

to save the result and then print the vector

CIodds <- apply(CIodds,1,function(x){
  paste0("(",paste(x, collapse=", "),")")
})

to just print the result use (no print needed after this)

cat(apply(CIodds,1,function(x){
  paste0("(",paste(x, collapse=", "),") \n")
}))

Result:

(0.30008732, 2.661063)   
(0.55185470, 1.493418)