Search code examples
rinterpolationggtern

ggtern + geom_interpolate_tern + expand.formula with unexpected output


This is a followup from my previous question,

So given the following data,

> 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
>

I am trying to reproduce a chart from this article (private access). The article claims to use a special cubic model

However, when I try to use the notation value ~ (x + y + z)^3 -1, I get

object 'z' not found

Which I assume is because z is linearly dependent on x and y.

When I try to recreate the special cubic model with only x and y, I tried using the cubicS and quad functions with 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)
      > 

However, geom_interpolate_tern will say I am using too many predictors,

    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"
      ) 

Output:

    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 

Solution

  • By default, the interpolation method is 'loess', to maintain consistent with the default smoothing method in ggplot2 for things like geom_smooth(...). This error is being thrown, due to the predictors limit for loess regression.

    No matter, this is easily fixed, specify method = lm instead. I have added coloured points to see how the model fits with respect to your data.

    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"
    )
    

    Output