1

When I run the code below in the R console, I get the following error in line 10:

"Error in lda.default(x, grouping, ...) : variables 5 6 appear to be constant within groups"

However the rest of the code is still processed and the data is plotted. But when I integrate this code into my shiny app, the plot panel shows the error message and nothing else.

Is there a way to get around this?

Input:

require(MASS)
require(ggplot2)
require(scales)
require(gridExtra)

x = 'Species'
ex = iris[, x]

lda <- lda(ex ~ ., iris)

prop.lda = lda$svd^2/sum(lda$svd^2)

plda <- predict(object = lda, newdata = iris)

dataset = data.frame(colAndShape = iris[,"Species"], lda = plda$x)

p1 <- ggplot(dataset) + geom_point(aes(lda.LD1, lda.LD2, colour = colAndShape, shape = colAndShape), size = 2.5) + 
    labs(x = paste("LD1 (", percent(prop.lda[1]), ")", sep=""),
    y = paste("LD2 (", percent(prop.lda[2]), ")", sep=""))

grid.arrange(p1)

Console output:

> require(MASS)
> require(ggplot2)
> require(scales)
> require(gridExtra)
> 
> x = 'Species'
> 
> ex = iris[, x]
> 
> lda <- lda(ex ~ ., iris)
Error in lda.default(x, grouping, ...) : 
  variables 5 6 appear to be constant within groups
> 
> prop.lda = lda$svd^2/sum(lda$svd^2)
> 
> plda <- predict(object = lda,
+                 newdata = iris)
> 
> dataset = data.frame(colAndShape = iris[,"Species"], lda = plda$x)
> 
> p1 <- ggplot(dataset) + geom_point(aes(lda.LD1, lda.LD2, colour = colAndShape, shape = colAndShape), size = 2.5) + 
+   labs(x = paste("LD1 (", percent(prop.lda[1]), ")", sep=""),
+        y = paste("LD2 (", percent(prop.lda[2]), ")", sep=""))
>  
> grid.arrange(p1)
Anuraag
  • 11
  • 3

1 Answers1

1

You have an error in your lda call. Instead of providing a variable name in the formula, you're providing a factor vector. You should construct the formula from the variables using paste or sprintf and as.formula.

x = 'Species'
lda <- lda(as.formula(paste(x, ".", sep = "~")), iris)

prop.lda = lda$svd^2/sum(lda$svd^2)

plda <- predict(object = lda, newdata = iris)

dataset = data.frame(colAndShape = iris[,"Species"], lda = plda$x)

p1 <- ggplot(dataset) + geom_point(aes(lda.LD1, lda.LD2, colour = colAndShape, shape = colAndShape), size = 2.5) + 
    labs(x = paste("LD1 (", percent(prop.lda[1]), ")", sep=""),
    y = paste("LD2 (", percent(prop.lda[2]), ")", sep=""))

p1  #grid.arrange(p1) isn't needed in this example.

enter image description here

Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
Jake Kaupp
  • 7,892
  • 2
  • 26
  • 36
  • the line `ex = iris[, x]` is not required – Sandipan Dey Feb 10 '17 at 17:37
  • I didn't remove it as it might be downstream in OPs application, not part of their MWE. Same reason why I commented out the `grid.arrange()` part, as it might be used in other places – Jake Kaupp Feb 10 '17 at 17:38
  • Now I'm getting a different error and the app is still behaving in the same way. "Error in eval(expr, envir, enclos) : object 'setosa' not found" – Anuraag Feb 10 '17 at 19:31
  • Different error = New Question. And you might want to consider including the full code for the app. – Jake Kaupp Feb 10 '17 at 19:32