> deriv(expression(log(sigma) - log(sigma^2 + (x - mu)^2)), "mu") expression({ .expr3 <- x - mu .expr5 <- sigma^2 + .expr3^2 .value <- log(sigma) - log(.expr5) .grad <- array(0, c(length(.value), 1), list(NULL, c("mu"))) .grad[, "mu"] <- 2 * .expr3/.expr5 attr(.value, "gradient") <- .grad .value }) > deriv(expression(sin(x)), "x") expression({ .value <- sin(x) .grad <- array(0, c(length(.value), 1), list(NULL, c("x"))) .grad[, "x"] <- cos(x) attr(.value, "gradient") <- .grad .value }) #### stupid !!!!! > deriv(expression(lgamma(x)), "x") Error in deriv.default(expression(lgamma(x)), "x") : Function `lgamma' is not in the derivatives table #### for those that know calculus, the derivative is the digamma function > epsilon <- 1e-9 > (lgamma(2 + epsilon) - lgamma(2)) / epsilon [1] 0.4227843 > digamma(2) [1] 0.4227843 > fred <- function(y) function(x) x + y > sally <- fred(10) > sally function(x) x + y > sally(2:4) [1] 12 13 14 #### comments added after class > environment(fun = sally) > get("y", environment(fun = sally)) [1] 10 > y Error: Object "y" not found