Home > Software design >  Change confidence level in t test (inside of by()) in R
Change confidence level in t test (inside of by()) in R

Time:10-16

I need to change the value of confidence level in t.test in by()

res<-by(HUS[,1], HUS$air.conditioning, FUN=t.test)

Where i can provide conf.level?

CodePudding user response:

Use anonymous/lamdba function (function(x)

res <- by(HUS[,1], HUS$air.conditioning, FUN = 
         function(x) t.test(x, conf.level = 0.90))

Or specify the named argument

res <- by(HUS[,1], HUS$air.conditioning, FUN=t.test, conf.level = 0.90)

NOTE: Specifying the named argument may not work in all the functions e.g. ave


Check with reproducible example

res1 <- by(mtcars[, "mpg"], mtcars$cyl, FUN = t.test, conf.level = 0.90)
res2 <- by(mtcars[, "mpg"], mtcars$cyl, FUN = function(x) t.test(x,  conf.level = 0.90))
> res1 <- lapply(res1, function(x) {x$data.name <- "x"; x})
> all.equal(res1, res2, check.attributes = FALSE)
[1] TRUE
  • Related