用D来求回归多项式的导数时出错。

时间:2021-02-10 20:23:34

I have points on a 2D graph. I want to find the best 3rd polynomial that fits this model and get its first derivative. But I can't get D function working. Here is simple example:

二维图上有点。我想找到最适合这个模型的三次多项式并得到它的一阶导数。但是我不能让D函数工作。这是简单的例子:

a <- 0:10
b <- c(2, 4, 5, 8, 9, 12, 15, 16, 18, 19, 20)
plot(a, b)
m1 <- lm(b ~ a + I(a ^ 2) + I(a ^ 3))
s <- coef(m1)

## try to get 1st derivative of the regression polynomial
D(expression(s[1] + s[2] * a + (a ^ 2) * s[3] + (a ^ 3) * s[4]), "a")

Error in D(expression(s[1] + s[2] * a + (a^2) * s[3] + (a^3) * s[4]), :

错误在D(表达式(s[1]+[2]* +(^ 2)* s[3]+(^ 3)* s[4]),:

Function '[' is not in the derivatives table

函数'['不在微分表中

I want to avoid computing numerical derivative by differencing. Thanks for help!

我要避免用不同的方法计算数字导数。谢谢你的帮助!

1 个解决方案

#1


2  

The error message you see "Function '[' is not in the derivatives table" is because D can only recognize a certain set of functions for symbolic operations. You can find them in ?D:

您所看到的“函数”(“不在衍生表”中)的错误消息是由于D只能识别一组用于符号操作的函数。你能在里面找到他们吗?

The internal code knows about the arithmetic operators ‘+’, ‘-’,
‘*’, ‘/’ and ‘^’, and the single-variable functions ‘exp’, ‘log’,
‘sin’, ‘cos’, ‘tan’, ‘sinh’, ‘cosh’, ‘sqrt’, ‘pnorm’, ‘dnorm’,
‘asin’, ‘acos’, ‘atan’, ‘gamma’, ‘lgamma’, ‘digamma’ and
‘trigamma’, as well as ‘psigamma’ for one or two arguments (but
derivative only with respect to the first).  (Note that only the
standard normal distribution is considered.)

While the "[" is actually a function in R (read ?Extract or ?"[").

而"["实际上是R中的一个函数(读取?提取还是?")

To demonstrate the similar behaviour, consider:

要演示类似的行为,请考虑:

s <- function (x) x

D(expression(s(x) + x ^ 2), name = "x")
# Error in D(expression(s(x) + x^2), name = "x") : 
#  Function 's' is not in the derivatives table

Here, even though s has been defined as a simple function, D can do nothing with it.

在这里,即使s被定义为一个简单的函数,D也不能做任何事情。

Your problem has been solved by my recent answers for Function for derivatives of polynomials of arbitrary order (symbolic method preferred). Three methods are provided in three of my answers, none of which are based on numerical derivatives. I personally prefer to the one using outer (the only answer with LaTeX math formula), as for polynomials everything is exact.

你的问题已经被我最近的关于任意阶多项式导数函数的答案解决了(符号方法优先)。我的回答中提供了三种方法,没有一种是基于数值导数的。我个人更喜欢使用outer(乳胶数学公式的唯一答案),至于多项式,一切都是准确的。

To use that solution, use the function g there, and specify argument x by values where you want to evaluate the derivative (say 0:10), and pc by your polynomial regression coefficients s. By default, nderiv = 0L so the polynomial itself is returned as if predict.lm(m1, newdata = list(a = 0:10)) were called. But once nderiv is specified, you get exact derivatives of your regression curve.

要使用这个解决方案,可以使用函数g,并通过值来指定参数x,在这里,你需要用多项式回归系数来计算导数值(比如0:10)和pc,默认情况下,nderiv = 0L,所以多项式本身就会像预测一样返回。lm(m1, newdata = list(a = 0:10))被调用。一旦指定了nderiv,就会得到回归曲线的准确导数。

a <- 0:10
b <- c(2, 4, 5, 8, 9, 12, 15, 16, 18, 19, 20)
plot(a, b)
m1 <- lm(b ~ a + I(a ^ 2) + I(a ^ 3))
s <- coef(m1)
#(Intercept)           a      I(a^2)      I(a^3) 
# 2.16083916  1.17055167  0.26223776 -0.02020202 

## first derivative at your data points
g(0:10, s, nderiv = 1)
# [1] 1.1705517 1.6344211 1.9770785 2.1985237 2.2987568 2.2777778 2.1355866
# [8] 1.8721834 1.4875680 0.9817405 0.3547009

Other remark: It is suggested that you use poly(a, degree = 3, raw = TRUE) rather than I(). They do the same here, but poly is more concise, and make it easier if you want interaction, like in How to write interactions in regressions in R?

其他说明:建议您使用poly(a, degree = 3, raw = TRUE)而不是I()。它们在这里也一样,但是poly更简洁,如果你想要交互的话,它更简单,比如如何用R表示回归?

#1


2  

The error message you see "Function '[' is not in the derivatives table" is because D can only recognize a certain set of functions for symbolic operations. You can find them in ?D:

您所看到的“函数”(“不在衍生表”中)的错误消息是由于D只能识别一组用于符号操作的函数。你能在里面找到他们吗?

The internal code knows about the arithmetic operators ‘+’, ‘-’,
‘*’, ‘/’ and ‘^’, and the single-variable functions ‘exp’, ‘log’,
‘sin’, ‘cos’, ‘tan’, ‘sinh’, ‘cosh’, ‘sqrt’, ‘pnorm’, ‘dnorm’,
‘asin’, ‘acos’, ‘atan’, ‘gamma’, ‘lgamma’, ‘digamma’ and
‘trigamma’, as well as ‘psigamma’ for one or two arguments (but
derivative only with respect to the first).  (Note that only the
standard normal distribution is considered.)

While the "[" is actually a function in R (read ?Extract or ?"[").

而"["实际上是R中的一个函数(读取?提取还是?")

To demonstrate the similar behaviour, consider:

要演示类似的行为,请考虑:

s <- function (x) x

D(expression(s(x) + x ^ 2), name = "x")
# Error in D(expression(s(x) + x^2), name = "x") : 
#  Function 's' is not in the derivatives table

Here, even though s has been defined as a simple function, D can do nothing with it.

在这里,即使s被定义为一个简单的函数,D也不能做任何事情。

Your problem has been solved by my recent answers for Function for derivatives of polynomials of arbitrary order (symbolic method preferred). Three methods are provided in three of my answers, none of which are based on numerical derivatives. I personally prefer to the one using outer (the only answer with LaTeX math formula), as for polynomials everything is exact.

你的问题已经被我最近的关于任意阶多项式导数函数的答案解决了(符号方法优先)。我的回答中提供了三种方法,没有一种是基于数值导数的。我个人更喜欢使用outer(乳胶数学公式的唯一答案),至于多项式,一切都是准确的。

To use that solution, use the function g there, and specify argument x by values where you want to evaluate the derivative (say 0:10), and pc by your polynomial regression coefficients s. By default, nderiv = 0L so the polynomial itself is returned as if predict.lm(m1, newdata = list(a = 0:10)) were called. But once nderiv is specified, you get exact derivatives of your regression curve.

要使用这个解决方案,可以使用函数g,并通过值来指定参数x,在这里,你需要用多项式回归系数来计算导数值(比如0:10)和pc,默认情况下,nderiv = 0L,所以多项式本身就会像预测一样返回。lm(m1, newdata = list(a = 0:10))被调用。一旦指定了nderiv,就会得到回归曲线的准确导数。

a <- 0:10
b <- c(2, 4, 5, 8, 9, 12, 15, 16, 18, 19, 20)
plot(a, b)
m1 <- lm(b ~ a + I(a ^ 2) + I(a ^ 3))
s <- coef(m1)
#(Intercept)           a      I(a^2)      I(a^3) 
# 2.16083916  1.17055167  0.26223776 -0.02020202 

## first derivative at your data points
g(0:10, s, nderiv = 1)
# [1] 1.1705517 1.6344211 1.9770785 2.1985237 2.2987568 2.2777778 2.1355866
# [8] 1.8721834 1.4875680 0.9817405 0.3547009

Other remark: It is suggested that you use poly(a, degree = 3, raw = TRUE) rather than I(). They do the same here, but poly is more concise, and make it easier if you want interaction, like in How to write interactions in regressions in R?

其他说明:建议您使用poly(a, degree = 3, raw = TRUE)而不是I()。它们在这里也一样,但是poly更简洁,如果你想要交互的话,它更简单,比如如何用R表示回归?