假设您正在研究一个回归模型,并且至少有一个预测变量是通过样条估计的,例如,
library(splines)
data(diamonds, package = "ggplot2")
fit <- lm(price ~ bs(depth, degree = 5) + bs(carat, knots = c(2, 3)) * color,
data = diamonds)
上述拟合仅用于说明目的,没有任何有意义的理由。
现在,让我们保持相同的基本公式,但更改深度和克拉的结位置。更新需要以动态方式进行,以便它可以成为更大 MCMC 方法的一部分(结的数量和结位置由可逆跳跃或生/死步骤确定)。
我很清楚update
andupdate.formula
调用,但我不相信这些工具会有所帮助。下面的伪代码应该说明我计划开发的函数的行为。
foo <- function(formula, data) {
# Original Model matrix, the formula will be of the form:
Xmat_orig <- model.matrix(formula, data)
# some fancy method for selecting new knot locations here
# lots of cool R code....
# pseudo code for the 'new knots'. In the example formula above var1 would be
# depth and var2 would be carat. The number of elements in this list would be
# dependent on the formula passed into foo.
new_knots <- list(k1 = knot_locations_for_var1,
k2 = knot_locations_for_var2)
# updated model matrix:
# pseudo code for that the new model matrix call would look like.
Xmat_new <-
model.matrix(y ~ bs(var1, degree = 5, knots = new_knots$k1) + bs(var2, knots = new_knots$k2) * color,
data = data)
return(Xmat_new)
}
有人可以建议一种knots
在 abs
或
ns
动态调用中修改调用的方法吗?