Skip to content

lm_robust does not work with ordered factors as clusters #421

@pekofsky

Description

@pekofsky

Example 1

When running the code below:

data("ChickWeight")
lm_robust(weight ~ 0 + Diet + Time:Diet, data = ChickWeight, clusters = Chick)

I get the error:

Error in !(class(ret[["cluster"]]) %in% c("factor", "integer")) && !is.null(ret[["cluster"]]) : 'length = 2' in coercion to 'logical(1)'

I believe this is a bug relating to ChickWeight$Chick being an ordered factor. When running the following code instead, lm_robust() works as expected:

data("ChickWeight")
ChickWeight$Chick <- factor(ChickWeight$Chick, ordered = FALSE)
lm_robust(weight ~ 0 + Diet + Time:Diet, data = ChickWeight, clusters = Chick)

Example 2

To prove that this bug is related to clusters being an ordered factor, I tried again with mtcars. The following code, where clusters is not an ordered factor, runs as expected:

data("mtcars")
lm_robust(mpg ~ wt + hp, data = mtcars, clusters = cyl)

When I try again, but with cyl being an ordered factor, I get the same error:

data("mtcars")
mtcars$cyl <- factor(mtcars$cyl, ordered = TRUE)
lm_robust(mpg ~ wt + hp, data = mtcars, clusters = cyl)

Error in !(class(ret[["cluster"]]) %in% c("factor", "integer")) && !is.null(ret[["cluster"]]) : 'length = 2' in coercion to 'logical(1)'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions