Home > Blockchain >  How to I change confidence interval display format
How to I change confidence interval display format

Time:10-01

I'm looking to change the format of 95% CI from (0.1 -- 0.6) to (0.1 to 0.6) or (0.1, 0.6).

Using epikit::unite_ci() function

enter image description here

CodePudding user response:

In general, you can substitute text strings with gsub:

gsub(" --", ",",x = "(0.1 -- 0.6)")
gsub("to", ",",x = "(0.1 -- 0.6)")

If you want a more appropriate answer, please provide a small reproducible example.

CodePudding user response:

I suggest to modify the fmt_ci function of epikit as follows:

library(epikit)

fmt_ci <- function (e = numeric(), l = numeric(), u = numeric(), digits = 2, 
    percent = TRUE) {
    stopifnot(is.numeric(e), is.numeric(l), is.numeric(u), is.numeric(digits))
    # Below the modified row
    msg <- "%s (CI %.2f to %.2f)"
    msg <- gsub("2", digits, msg)
    fun <- if (percent) 
        match.fun(scales::percent)
    else match.fun(scales::number)
    e <- fun(e, scale = 1, accuracy = 1/(10^digits), big.mark = ",")
    sprintf(msg, e, l, u)
}
# Replace fmt_ci in epikit with the above modified function
assignInNamespace("fmt_ci", fmt_ci, pos="package:epikit")

Running the code:

fit <- lm(100/mpg ~ disp   hp   wt   am, data = mtcars)
df  <- data.frame(v = names(coef(fit)), e = coef(fit), confint(fit), row.names = NULL)
names(df) <- c("variable", "estimate", "lower", "upper")
print(df)
out <- unite_ci(df, "slope (CI)", estimate, lower, upper, m100 = FALSE, percent = FALSE)
print(out)

now you get:

     variable           slope (CI)
1 (Intercept) 0.74 (-0.77 to 2.26)
2        disp 0.00 (-0.00 to 0.01)
3          hp 0.01 (-0.00 to 0.01)
4          wt  1.00 (0.38 to 1.62)
5          am 0.16 (-0.61 to 0.93)
  •  Tags:  
  • r
  • Related