example: expectation_data(D, function(x) (x-colMeans(D)) %*% t(x-colMeans(D))) computes the covariance of the data D, except the matrix structure is lost (it's just a vector, which can be coerced back to a matrix if needed).

expectation_data(
  data,
  g = function(x) x,
  ...,
  compute_stats = TRUE,
  alpha = 0.05
)

Arguments

data

a matrix of data

g

a function to apply to each row of the data

...

additional arguments to pass to g

compute_stats

whether to compute CIs for the expectations

alpha

the confidence level for the confidence interval for each component of the expectation (if compute_stats is TRUE)

Value

if compute_stats is TRUE, then a list with the following components: value - The estimate of the expectation ci - The confidence intervals for each component of the expectation n - The number of samples otherwise, just the value of the expectation.

Examples

set.seed(42)
data <- matrix(rnorm(200), ncol = 2)
# sample mean with confidence interval
expectation_data(data)
#> $value
#> [1]  0.03251482 -0.08748371
#> 
#> $ci
#>            [,1]       [,2]
#> [1,] -0.1715874 0.23661703
#> [2,] -0.2646985 0.08973105
#> 
#> $n
#> [1] 100
#> 

# just the point estimate, no CI
expectation_data(data, compute_stats = FALSE)
#> [1]  0.03251482 -0.08748371

# expectation of a function of the data (row-wise)
expectation_data(data, g = function(x) sum(x^2))
#> $value
#> [1] 1.891645
#> 
#> $ci
#> [1] 1.472095 2.311195
#> 
#> $n
#> [1] 100
#>