R/delta_method.R

Defines functions delta_ses

Documented in delta_ses

#' Get standard errors using delta method approximation
#'
#' @param formula a list of RHS-only formula(s)
#' @param mean estimated mean of X
#' @param cov  estimated variance-covariance matrix of X
#'
#' @return SEs vector
#' @keywords internal
#'
#' @importFrom stats deriv
#'
delta_ses <- function(formula, mean, cov) {
  cov <- as.matrix(cov)
  formula <- as.list(formula)

  syms <- paste0("x", seq_along(mean))
  for (i in seq_along(mean)) assign(syms[i], mean[i])

  gdashmu <- t(sapply(formula, function(x) {
    as.numeric(attr(eval(deriv(x, syms)), "gradient"))
  }))

  new.covar <- gdashmu %*% cov %*% t(gdashmu)
  sqrt(diag(new.covar))
}

Try the ShinyItemAnalysis package in your browser

Any scripts or data that you put into this service are public.

ShinyItemAnalysis documentation built on May 31, 2023, 7:08 p.m.