'"grouping factor must have exactly 2 levels"

Hi y'all I'm fairly new to R and I'm supposed to calculate F statistic for this table

effect of total length vs swim speed

The code I have inputted is as follows:

# F-test
res.ftest <- var.test(TotalLength ~ SwimSpeed , data = my_data)
res.ftest

I know I have more than two levels from the other posts I have read online, but I am not sure what to change to get the outcome I want.

r


Solution 1:[1]

FIRST AND FOREMOST...If you invoke

?var.test()

you will note that the S3 version you called assumes lhs is numeric and rhs is a 2-level factor.

As for the rest, while I don't know the words to your specific work/school assignment here, the words shouldn't be "calculate an F-test", exactly. They should be "analyze these data appropriately". While there are a number of routes you could take, this is normally seen as a regression problem, NOT a problem of trying to compare two variances/complete a 1-way ANOVA which is what var.test() is designed to do. (Reading the documentation at, for example, https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/var.test should make this clear and is something you should always do when invoking R procedures.)

Using a subset of your data (please do this yourself for stack helpers next time rather than make someone here do it for you)...

df <- data.frame(
                 ID = 1:4,
                 TL = c(27.1,29.0,33.0,29.3),
                 SS = c(86.6,62.4,63.8,62.3)
                 )  

cor.test(df$TL,df$SS) # reports t statistic
# or
summary(lm(df$TL ~ df$SS)) # reports F statistic

Note that F is simply t^2 here in the 2 variable case.


Lastly, I should add it is remotely, vaguely possible the assignment is to check if the variances of the 2 distributions are equal even though I can see no reason why anyone would want to know considering they are 2 different measures on two different underlying scales measuring 2 different things. However,

var.test(df$TL, df$SS) 

will return a "result" should you take the assignment to mean compare the observed variances.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1