具有部分残差的效果图:为什么xlevels评估为100个值?

时间:2017-03-11 17:17:27

标签: r plot effects interaction

John Fox和合作者在effects CRAN软件包中的函数xlevels中对参数Effect的帮助说:

(...) If partial residuals are
computed, then the focal predictor that is to appear on the
horizontal axis of an effect plot is evaluated at 100 equally
spaced values along its full range, and, by default, other
numeric predictors are evaluated at the quantiles specified
in the ‘quantiles’ argument, unless their values are given
explicitly in ‘xlevels’.

我不明白: 1)当焦点预测变量是连续变量和离散变量对时xlevels的相关性是什么(我原以为它不相关); 2)在大多数情况下,当条件图的数量约为2到4时,为什么xlevels对于连续预测器对的值为100; 3)xlevels如何影响横坐标上的焦点预测器(似乎它没有)。

1)如果模型仅包含连续预测变量和因子,则xlevels似乎不起作用,并且通过评估预测变量的实际值来获得部分残差(这是我期望的) ;在这种情况下,我觉得上面的文字不适用。以下代码将Effect图与所获得的部分残差进行比较"手工"

     library(effects)
     library(ggplot2)

     x11()
     x11()

     set.seed(123)
     n <- 6  ## with small n it is easier to compare the plots
     x1 <- rnorm(n, mean = 15)
     xf <- rep(c(0, 1), c(n/2, n - n/2))
     dd <- data.frame(y = x1 + x1 * 4 * xf + rnorm(n, 0, sd = 0.2),
                     x1 = x1, xf = factor(xf))
     mi1 <- lm(y ~ x1 * xf, data = dd)


     plot(Effect(c("x1", "xf"), mi1, partial.residuals = TRUE), nrow = 1)

     plot(Effect(c("x1", "xf"), mi1, partial.residuals = TRUE,
            xlevels = list(x1 = c(1, 9, 37)))) 

     ############# "By hand", step by step
     r1 <- resid(mi1)
     ## Individual beta_j * x_j terms to add
     add_x1 <- x1 * coefficients(mi1)["x1"]
     add_x1_f1 <- model.matrix(mi1)[, "x1:xf1"] * coefficients(mi1)["x1:xf1"]

     ## Partial residuals
     partial_residuals <- r1 + add_x1 + add_x1_f1

     ## For convenience for ggplot2
     pd <- data.frame(x1 = dd$x1, xf = dd$xf,
                    partial_residuals = partial_residuals,
                   fitted = fitted(mi1))

     dev.set(dev.next())
     ## Identical to the Effect plot.
     ggplot(data = pd, aes(x = x1, y = partial_residuals)) +
           geom_smooth(method = "lm") + geom_point() +
           facet_wrap( ~ xf, nrow = 1)

2和3)。如果模型包含两个连续的预测变量,我认为通过修改xlevels我们可以更改面板的数量,但我不明白为什么默认值是在100个值处进行评估。如果我理解正确的话,那里正在实施的是John Fox和Sanford Weisberg所讨论的一个想法,例如,Visualizing Lack of Fit in Complex Regression Models: Adding Partial Residuals to Effect Displays。例如,在幻灯片34上,我们读取&#34;为了获得部分残差,我们将除了定义水平轴的焦点预测器之外的效果中的预测变量舍入到切片值。&#34;我理解这一点,但切片不会影响水平轴上的焦点预测器(只有调节面板中的焦点预测器)。下面的代码再次与&#34; hand&#34;获得的部分残差进行比较。通过手动切割其中一个预测变量,并且还显示在水平轴上更改焦点预测变量的xlevels似乎没有区别,重要的是切换另一个预测变量:

     set.seed(123)
     n <- 12  ## with small n easier to see
     x1 <- rnorm(n, mean = 15)
     x2 <- rep(c(1, 2, 8, 9), length.out = n) ## Simpler if few values
     dd2 <- data.frame(y = x1 + 2 * x1 * x2 + rnorm(n),
                       x1 = x1, x2 = x2)
     mi12 <- lm(y ~ x1 * x2, data = dd2)
     summary(mi12)


     ## Residuals + Individual beta_j * x_j terms to add
     tt <- c("x1", "x1:x2")
     pr2 <- resid(mi12) + model.matrix(mi12)[, tt] %*% coefficients(mi12)[tt]

     ## Partial residuals with slicing of x2:
     ## Evaluate x2 only at 1 and 9; all values 1 and 2 are set to 1
     ## and all 8 and 9 are set to 9. I think this is similar to
     ## "fitted <- y[good][closest(trans(x.fit), x[good])]"
     ## in line 317 in plot-methods.R?

     x22 <- dd2$x2
     x22[x22 == 2] <- 1
     x22[x22 == 8] <- 9

     mm2 <- cbind(x1 = dd2$x1, "x1:x2" = dd2$x1 * x22)
     pr2b <- resid(mi12) + mm2 %*% coefficients(mi12)[tt]

     ## For convenience for ggplot2
     pd2 <- data.frame(x1 = dd2$x1,
                       x2 = dd2$x2,
                       x22 = x22,
                       pr2 = pr2,
                       pr2b = pr2b)


     plot(Effect(c("x1", "x2"), mi12, partial.residuals = TRUE))
     dev.set(dev.next())
     ggplot(data = pd2, aes(x = x1, y = pr2)) +
         geom_smooth(method = "lm") + geom_point() +
         facet_wrap( ~ factor(x2, levels = c(8, 9, 1, 2)), nrow = 2)


     ## Only at two values of x2
     plot(Effect(c("x1", "x2"), mi12, partial.residuals = TRUE,
                 xlevels = list(x2 = c(1, 9), x1 = 5)))
     dev.set(dev.next())
     ggplot(data = pd2, aes(x = x1, y = pr2b)) +
         geom_smooth(method = "lm") + geom_point() +
         facet_wrap( ~ factor(x22), nrow = 1)

     ## other values for x1 make no difference
     plot(Effect(c("x1", "x2"), mi12, partial.residuals = TRUE,
                     xlevels = list(x2 = c(1, 9), x1 = 1)))

如上例所示,查看代码,文件plot-methods.R的第317行(函数plot.eff),我们看到fitted <- y[good][closest(trans(x.fit), x[good])]。更改xlevels对示例1中x的大小没有影响,只有当我们更改xlevels x2时才会在示例2-3中产生影响。这似乎与&#34;将条件面板中的预测器设置为一组切片值(如果它是连续的) - 不需要离散预测器&#34;。

1 个答案:

答案 0 :(得分:0)

John Fox提供了答案:

当数字预测器被“切片”时,这是在a处完成的 由于每个切片对应于相对较少数量的值 显示屏中的面板。使用100个值作为数字预测变量 这不是切片 - 也就是说,预测器出现在 图的水平轴 - 允许我们计算拟合值 在相对较少的点数,然后使用 最接近的一个,加快计算速度。