【发布时间】:2014-08-12 19:21:21
【问题描述】:
假设您正在研究一个回归模型和至少一个预测变量 通过样条估计,例如,
library(splines)
data(diamonds, package = "ggplot2")
fit <- lm(price ~ bs(depth, degree = 5) + bs(carat, knots = c(2, 3)) * color,
data = diamonds)
上述拟合仅用于说明目的,没有任何有意义的理由 存在。
现在,让我们保持相同的基本公式,但更改两者的结位置 深度和克拉。更新需要以动态方式进行,以便 可能是更大的 MCMC 方法的一部分(结的数量和结位置 由可逆跳跃或出生/死亡步骤确定)。
我很清楚 update 和 update.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 在 bs 或
ns 动态调用?
【问题讨论】: