deriv 位於 stats 包(package)。 說明
通過符號和算法計算簡單表達式的導數。
用法
    D (expr, name)
 deriv(expr, ...)
deriv3(expr, ...)
 ## Default S3 method:
deriv(expr, namevec, function.arg = NULL, tag = ".expr",
       hessian = FALSE, ...)
 ## S3 method for class 'formula'
deriv(expr, namevec, function.arg = NULL, tag = ".expr",
       hessian = FALSE, ...)
## Default S3 method:
deriv3(expr, namevec, function.arg = NULL, tag = ".expr",
       hessian = TRUE, ...)
## S3 method for class 'formula'
deriv3(expr, namevec, function.arg = NULL, tag = ".expr",
       hessian = TRUE, ...)
參數
| expr | 
 | 
| name,namevec | 字符向量,給出將計算導數的變量名稱(僅適用於  | 
| function.arg | 如果指定且非  | 
| tag | 特點;用於結果中本地創建的變量的前綴。轉換為本機編碼時不得超過 60 字節。 | 
| hessian | 一個邏輯值,指示是否應計算二階導數並將其合並到返回值中。 | 
| ... | 傳入或傳出方法的參數。 | 
細節
D 以其同名的 S 為模型,用於獲取簡單的符號導數。
deriv 是一個通用函數,具有默認值和 formula 方法。它返回 call,用於同時計算 expr 及其(偏)導數。它使用所謂的算法導數。如果 function.arg 是一個函數,則其參數可以具有默認值,請參閱下麵的 fx 示例。
目前, deriv.formula 隻是在提取 ~ 右側的表達式後調用 deriv.default 。
deriv3 及其方法與 deriv 及其方法等效,隻是 hessian 對於 deriv3 默認為 TRUE 。
內部代碼知道算術運算符+,-,*,/和^和 single-variable 函數exp,log,sin,cos,tan,sinh,cosh,sqrt,pnorm,dnorm,asin,acos,atan,gamma,lgamma,digamma和trigamma, 也psigamma對於一個或兩個參數(但僅針對第一個參數進行派生)。 (請注意,僅考慮標準正態分布。)
自從R3.4.0、single-variable函數log1p,expm1,log2,log10,cospi,sinpi,tanpi,factorial, 和lfactorial也受到支持。
值
D 返回一個調用,因此可以輕鬆迭代以獲得更高的導數。
deriv 和 deriv3 通常返回一個 expression 對象,其求值返回帶有包含梯度矩陣的 "gradient" 屬性的函數值。如果 hessian 是 TRUE,則計算還會返回包含 Hessian 數組的 "hessian" 屬性。
如果 function.arg 不是 NULL ,則 deriv 和 deriv3 返回帶有這些參數的函數而不是表達式。
例子
## formula argument :
dx2x <- deriv(~ x^2, "x") ; dx2x
## Not run: expression({
         .value <- x^2
         .grad <- array(0, c(length(.value), 1), list(NULL, c("x")))
         .grad[, "x"] <- 2 * x
         attr(.value, "gradient") <- .grad
         .value
})
## End(Not run)
mode(dx2x)
x <- -1:2
eval(dx2x)
## Something 'tougher':
trig.exp <- expression(sin(cos(x + y^2)))
( D.sc <- D(trig.exp, "x") )
all.equal(D(trig.exp[[1]], "x"), D.sc)
( dxy <- deriv(trig.exp, c("x", "y")) )
y <- 1
eval(dxy)
eval(D.sc)
## function returned:
deriv((y ~ sin(cos(x) * y)), c("x","y"), function.arg = TRUE)
## function with defaulted arguments:
(fx <- deriv(y ~ b0 + b1 * 2^(-x/th), c("b0", "b1", "th"),
             function(b0, b1, th, x = 1:7){} ) )
fx(2, 3, 4)
## First derivative
D(expression(x^2), "x")
stopifnot(D(as.name("x"), "x") == 1)
## Higher derivatives
deriv3(y ~ b0 + b1 * 2^(-x/th), c("b0", "b1", "th"),
     c("b0", "b1", "th", "x") )
## Higher derivatives:
DD <- function(expr, name, order = 1) {
   if(order < 1) stop("'order' must be >= 1")
   if(order == 1) D(expr, name)
   else DD(D(expr, name), name, order - 1)
}
DD(expression(sin(x^2)), "x", 3)
## showing the limits of the internal "simplify()" :
## Not run: 
-sin(x^2) * (2 * x) * 2 + ((cos(x^2) * (2 * x) * (2 * x) + sin(x^2) *
    2) * (2 * x) + sin(x^2) * (2 * x) * 2)
## End(Not run)
## New (R 3.4.0, 2017):
D(quote(log1p(x^2)), "x") ## log1p(x) = log(1 + x)
stopifnot(identical(
       D(quote(log1p(x^2)), "x"),
       D(quote(log(1+x^2)), "x")))
D(quote(expm1(x^2)), "x") ## expm1(x) = exp(x) - 1
stopifnot(identical(
       D(quote(expm1(x^2)), "x") -> Dex1,
       D(quote(exp(x^2)-1), "x")),
       identical(Dex1, quote(exp(x^2) * (2 * x))))
D(quote(sinpi(x^2)), "x") ## sinpi(x) = sin(pi*x)
D(quote(cospi(x^2)), "x") ## cospi(x) = cos(pi*x)
D(quote(tanpi(x^2)), "x") ## tanpi(x) = tan(pi*x)
stopifnot(identical(D(quote(log2 (x^2)), "x"),
                    quote(2 * x/(x^2 * log(2)))),
          identical(D(quote(log10(x^2)), "x"),
                    quote(2 * x/(x^2 * log(10)))))
參考
Griewank, A. and Corliss, G. F. (1991) Automatic Differentiation of Algorithms: Theory, Implementation, and Application. SIAM proceedings, Philadelphia.
Bates, D. M. and Chambers, J. M. (1992) Nonlinear models. Chapter 10 of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.
也可以看看
相關用法
- R decompose 移動平均線的經典季節性分解
- R dendrapply 將函數應用於樹狀圖的所有節點
- R dendrogram 一般樹結構
- R deviance 模型偏差
- R density 核密度估計
- R delete.response 修改術語對象
- R df.residual 剩餘自由度
- R dummy.coef 提取原始編碼中的係數
- R dist 距離矩陣計算
- R diffinv 離散積分:差分的逆
- R stlmethods STL 對象的方法
- R medpolish 矩陣的中值波蘭(穩健雙向分解)
- R naprint 調整缺失值
- R summary.nls 總結非線性最小二乘模型擬合
- R summary.manova 多元方差分析的匯總方法
- R formula 模型公式
- R nls.control 控製 nls 中的迭代
- R aggregate 計算數據子集的匯總統計
- R kruskal.test Kruskal-Wallis 秩和檢驗
- R quade.test 四方測試
- R plot.stepfun 繪製階躍函數
- R alias 查找模型中的別名(依賴項)
- R qqnorm 分位數-分位數圖
- R eff.aovlist 多層方差分析的計算效率
- R pairwise.t.test 成對 t 檢驗
注:本文由純淨天空篩選整理自R-devel大神的英文原創作品 Symbolic and Algorithmic Derivatives of Simple Expressions。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
