0

this question is a follow up from function doesn't work in R ("argument is not numeric or logical")

I fixed the code for the 'calcBetweenGroupsVariance' function outlined in this textbook by using the aforementioned thread. I am now onto the next stage, where you need to calculate the separation achieved by a variable. This is the code:

calcSeparations <- function(variables,groupvariable) {
  # find out how many variables we have
  variables <- as.data.frame(variables)
  numvariables <- length(variables)
  # find the variable names
  variablenames <- colnames(variables)
  # calculate the separation for each variable
  for (i in 1:numvariables)
  {
    variablei <- variables[i]
    variablename <- variablenames[i]
    Vw <- calcWithinGroupsVariance(variablei, groupvariable)
    Vb <- calcBetweenGroupsVariance (variablei, groupvariable)
    sep <- Vb/Vw
    print(paste("variable",variablename,"Vw=",Vw,"Vb=",Vb,"separation=",sep))
  }
}
calcSeparations(wine[2:14],wine[1])

It comes up with the same error again - that the data is not numeric, even though it is. Any help would be appreciated. Please let me know if I have not provided enough information - I am new to stackoverflow. Thanks!

Limey
  • 10,234
  • 2
  • 12
  • 32
  • It would be helpful to see your input data. Please post the output from `dput(wine)` or `dput(head(wine))` if the former is too long. Also, I doubt we need to see all 14 variables in the data frame to sort this out. Can you confirm you see the same issue with, say, `calcSeparations(wine[2:4],wine[1])`: in which case, we only need to see the first four columns of `wine`. If you DONT see the problem with the smaller data set, then you definitely have a problem with (some of) your data. Oh, and welcome to SO. – Limey Jul 07 '20 at 11:20
  • Hello @Limey and thank you for your speedy response. I've posted the output from my 'frog' data rather than the example: > dput(head(frog)) structure(list(fam = c(28L, 26L, 22L, 30L, 45L, 31L), clade = c(4L, 3L, 3L, 3L, 3L, 4L), LM = c(4L, 3L, 3L, 3L, 4L, 3L), hab = c(3L, 1L, 1L, 1L, 3L, 1L), subhab = c(6L, 10L, 6L, 1L, 3L, 1L), body = c(0.883, 0.516, 0.82, 0.679, 1.03, 1.388), vert = c(0.865, 0.437, 0.674, 0.627, 1.149, 1.355), pelv = c(0.805, 0.498, 0.814, 0.696, 0.999, 1.379)), row.names = c(NA, 6L), class = "data.frame") etc (it wouldn't let me post the entire output) – Dr Blue Jul 07 '20 at 11:36
  • > calcSeparations(frog[6:8],frog[3]) [1] "variable body Vw= NA Vb= NaN separation= NaN" [1] "variable vert Vw= NA Vb= NaN separation= NaN" [1] "variable pelv Vw= NA Vb= NaN separation= NaN" > – Dr Blue Jul 07 '20 at 11:38
  • An example of the expected output for body would be Vw (within groups variance) should be 0.3554355 and Vb (between groups) should be 0.7355689, and the separation should be Vb/Vw – Dr Blue Jul 07 '20 at 11:40

0 Answers0