从正交多项式的理论表达式推导预测最大值的位置
我在poly 的文档中引用了 Kennedy 和 Gentle (1982) 的“统计计算”一书,现在分享我关于正交多项式计算的发现,以及我们如何使用它们来找到最大预测值的位置 x。
书中介绍的正交多项式(第 343-4 页)是一元的(即最高阶系数始终为 1),并通过以下递归过程获得:
其中 q 是所考虑的正交多项式的数量。
请注意以下上述术语的关系与poly的文档:
- 您问题的摘录中出现的 “三项递归” 是第三个表达式的 RHS,它正好包含三个项。
- 第三个表达式中的 rho(j+1) 系数称为“居中常数”。
- 第三个表达式中的 gamma(j) 系数在文档中没有名称,但它们与 “归一化常数” 直接相关,如下所示.
作为参考,这里我贴上poly文档的“价值”部分的相关摘录:
一个矩阵,其行对应于 x 中的点,列对应于度数,属性“degree”指定列的度数,(除非 raw = TRUE)“coefs”包含用于构造的居中和归一化常数正交多项式
回到递归,我们可以从第三个表达式,通过对 p(j+1) w.r.t 施加正交性条件。 p(j) 和 p(j-1)。
(需要注意的是,正交性条件不是积分,而是n个观察到的x个点的总和,因此多项式系数取决于在数据上!--例如,对于 Tchebyshev 正交多项式,情况并非如此)。
参数表达式变为:
对于回归中使用的 1 阶和 2 阶多项式,我们得到以下表达式,这些表达式已经用 R 代码编写:
# First we define the number of observations in the data
n = length(x)
# For p1(x):
# p1(x) = (x - rho1) p0(x) (since p_{-1}(x) = 0)
rho1 = mean(x)
# For p2(x)
# p2(x) = (x - rho2) p1(x) - gamma1
gamma1 = var(x) * (n-1)/n
rho2 = sum( x * (x - mean(x))^2 ) / (n*gamma1)
我们得到的:
> c(rho1, rho2, gamma1)
[1] 100.50 100.50 3333.25
注意poly(x,2)的coefs属性是:
> attr(poly(x,2), "coefs")
$alpha
[1] 100.5 100.5
$norm2
[1] 1 200 666650 1777555560
其中$alpha 包含 居中常数,即 rho 值(与我们的一致——顺便说一句,当x 的分布是对称的 对于任何 q!(观察和证明),并且$norm2 包含 归一化常数(在这种情况下为p(-1,x)、p(0,x)、p(1,x)和p(2 ,x)),也就是将递归公式中的多项式归一化的常数c(j)——通过将它们除以sqrt(c(j)) em>--,使生成的多项式 r(j,x) 满足 sum_over_i{ r(j,x_i)^2 } = 1; 注意r(j,x)是存储在poly()返回的对象中的多项式。
从上面已经给出的表达式,我们观察到 gamma(j) 恰好是两个连续归一化常数的比率,即: gamma(j) = c(j) / c (j-1).
我们可以通过计算检查我们的gamma1值是否与这个比率一致:
gamma1 == attr(poly(x,2), "coefs")$norm2[3] / attr(poly(x,2), "coefs")$norm2[2]
返回TRUE。
回到找到模型预测值的最大值的问题,我们可以:
-
将预测值表示为 r(1,x) 和 r(2,x) 以及逻辑回归系数的函数,即:
pred(x) = beta0 + beta1 * r(1,x) + beta2 * r(2,x)
-
导出表达式 w.r.t. x,将其设置为 0 并求解 x。
在 R 代码中:
# Get the normalization constants alpha(j) to obtain r(j,x) from p(j,x) as
# r(j,x) = p(j,x) / sqrt( norm(j) ) = p(j,x) / alpha(j)
alpha1 = sqrt( attr(poly(x,2), "coefs")$norm2[3] )
alpha2 = sqrt( attr(poly(x,2), "coefs")$norm2[4] )
# Get the logistic regression coefficients (beta1 and beta2)
coef1 = as.numeric( model$coeff["poly(x, 2)1"] )
coef2 = as.numeric( model$coeff["poly(x, 2)2"] )
# Compute the x at which the maximum occurs from the expression that is obtained
# by deriving the predicted expression pred(x) = beta0 + beta1*r(1,x) + beta2*r(2,x)
# w.r.t. x and setting the derivative to 0.
xmax = ( alpha2^-1 * coef2 * (rho1 + rho2) - alpha1^-1 * coef1 ) / (2 * alpha2^-1 * coef2)
给出:
> xmax
[1] 97.501114
即与我的previous answer 中描述的其他“经验”方法获得的值相同。
从您提供的代码开始,获取预测值最大值的位置 x 的完整代码是:
# First we define the number of observations in the data
n = length(x)
# Parameters for p1(x):
# p1(x) = (x - rho1) p0(x) (since p_{-1}(x) = 0)
rho1 = mean(x)
# Parameters for p2(x)
# p2(x) = (x - rho2) p1(x) - gamma1
gamma1 = var(x) * (n-1)/n
rho2 = mean( x * (x - mean(x))^2 ) / gamma1
# Get the normalization constants alpha(j) to obtain r(j,x) from p(j,x) as
# r(j,x) = p(j,x) / sqrt( norm(j) ) = p(j,x) / alpha(j)
alpha1 = sqrt( attr(poly(x,2), "coefs")$norm2[3] )
alpha2 = sqrt( attr(poly(x,2), "coefs")$norm2[4] )
# Get the logistic regression coefficients (beta1 and beta2)
coef1 = as.numeric( model$coeff["poly(x, 2)1"] )
coef2 = as.numeric( model$coeff["poly(x, 2)2"] )
# Compute the x at which the maximum occurs from the expression that is obtained
# by deriving the predicted expression pred(x) = beta0 + beta1*r(1,x) + beta2*r(2,x)
# w.r.t. x and setting the derivative to 0.
( xmax = ( alpha2^-1 * coef2 * (rho1 + rho2) - alpha1^-1 * coef1 ) / (2 * alpha2^-1 * coef2) )