ggtern + geom_interpolate_tern + expand.formula 意外输出

ggtern + geom_interpolate_tern + expand.formula with unexpected output

这是

的跟进

所以给定以下数据,

> foo
Resp         A         B         C
1  1.629 0.3333333 0.3333333 0.3333333
2  1.734 0.1666667 0.6666667 0.1666667
3   1.957 0.0000000 1.0000000 0.0000000
4  1.778 1.0000000 0.0000000 0.0000000
5  1.682 0.6666667 0.1666667 0.1666667
6  1.407 0.1666667 0.1666667 0.6666667
7  1.589 0.0000000 0.5000000 0.5000000
8  1.251 0.0000000 0.0000000 1.0000000
9  1.774 0.5000000 0.5000000 0.0000000
10 1.940 0.5000000 0.0000000 0.5000000
>

我正在尝试从 this article (private access). The article claims to use a special cubic model

复制图表

但是,当我尝试使用符号 value ~ (x + y + z)^3 -1 时,我得到

object 'z' not found

我认为这是因为 z 线性依赖于 xy

当我尝试仅使用 xy 重新创建特殊立方模型时,我尝试将 cubicSquad 函数与 expand.formula 一起使用,

      > expand.formula(Resp ~ cubicS(A,B) + quad(A,B))
       Resp ~ (A + B)^3 + I(A * B * (A - B)) + (A + B)^2 + I(A^2) + I(B^2)
      > 

但是,geom_interpolate_tern 会说我使用了太多预测变量,

    foo <-
      structure(
        list(
          Resp = c(1.629, 1.734, 1.957, 1.778, 1.682, 1.407,
                   1.589, 1.251, 1.774, 1.94),
          A = c(0.3333333, 0.1666667, 0, 1,
                0.6666667, 0.1666667, 0, 0, 0.5, 0.5),
          B = c(0.3333333, 0.6666667,
                1, 0, 0.1666667, 0.1666667, 0.5, 0, 0.5, 0),
          C = c(0.3333333,
                0.1666667, 0, 0, 0.1666667, 0.6666667, 0.5, 1, 0, 0.5)
        ),
        .Names = c("Resp",
                   "A", "B", "C"),
        class = "data.frame",
        row.names = c("1", "2",
                      "3", "4", "5", "6", "7", "8", "9", "10")
      )

    ggtern(data=foo,aes(y = A,x = B,z = C)) +
      geom_interpolate_tern(
        data = foo,
        mapping = aes(
          value = Resp,color=..level..
        ),
        formula = expand.formula(value ~ cubicS(x,y) + quad(x,y)),
        base = "identity"
      ) 

输出:

    Warning messages:
      1: In structure(c(), class = c(class(x), class(y))) :
      Calling 'structure(NULL, *)' is deprecated, as NULL cannot have attributes.
    Consider 'structure(list(), *)' instead.
    2: Computation failed in `stat_interpolate_tern()`:
      only 1-4 predictors are allowed 

默认情况下,插值方法是'loess',以与ggplot2中的默认平滑方法保持一致,如geom_smooth(...)。由于黄土回归的预测变量限制,将引发此错误。

没关系,这很容易解决,请改为指定 method = lm。我添加了彩色点以查看模型如何适合您的数据。

ggtern(data=foo,aes(y = A,x = B,z = C)) +
  geom_point(aes(color=Resp)) + 
  geom_interpolate_tern(
    data = foo,
    mapping = aes(
      value = Resp,color=..level..
    ),
    method=lm,   # <<<<<< SPECIFY METHOD HERE <<<<<<<
    formula = expand.formula(value ~ cubicS(x,y) + quad(x,y)),
    base = "identity"
)