I wrote a function to test if a mean is within a confidence interval, and wanted to test out this function using many sets of data.
checkInt <- function(x, mean=20){
lower <- t.test(x)$conf.int[1]
upper <- t.test(x)$conf.int[2]
Within_interval <- ifelse(between(mean,lower,upper),1 , 0)
width <- upper - lower
return(matrix(Within_interval,width))
}
So I create a set of random exp data and wanted to apply my function on each row of this data, but keeps giving me weird results
many_set <- t((replicate(5, rexp(n=300, rate=1/20))))
t(apply(many_set , 1 , checkInt))
CodePudding user response:
First, as far as I know, you can't return a matrix
in your apply function checkInt
, since your typeof(many_set) == 'double'
. So, you need to calculate the width
value outside, since it doesn't depend on the mean, and return a scalar variable.
Second, if you want to specify a matrix via apply
to retrieve , one of the options would be to return a function within a function.
library(dplyr)
checkInt <- function(lower, upper){
return(function(mean = 20) {
Within_interval <- ifelse(between(mean,lower,upper), 1, 0)
return(Within_interval)
})
}
replicate <- replicate(5, rexp(n=300, rate=1/20))
many_set <- t((replicate(5, rexp(n=300, rate=1/20))))
lower <- t.test(many_set)$conf.int[1]
upper <- t.test(many_set)$conf.int[2]
width <- upper - lower
t(apply(many_set , 1 , checkInt(lower, upper)))
CodePudding user response:
Do what @dcarlson suggested in the comment and use c()
rather than matrix()
. Moreover you can avoid the ifelse
by transforming boolean of between
as as.integer()
.
checkInt <- function(x, meanv=20){
lower <- t.test(x)$conf.int[1]
upper <- t.test(x)$conf.int[2]
Within_interval <- as.integer(data.table::between(meanv, lower, upper))
width <- upper - lower
return(c(Within_interval, width))
}
set.seed(42)
many_set <- t(replicate(5, rexp(n=300, rate=1/20)))
t(apply(many_set, 1, checkInt))
# [,1] [,2]
# [1,] 0 5.333380
# [2,] 1 4.444028
# [3,] 1 4.995460
# [4,] 1 4.294835
# [5,] 1 4.369884