Joehat
Joehat

Reputation: 1129

R: Error transforming object of class lm into data frame when using tidy()

I am trying to convert my lm model into a data frame in order to select the features with p-values above 0.05.

# Creating data frame from model's output
# library(permutations)
tm <- tidy(ols_reg)

# visualise dataframe of the model
# (using non scientific notation of numbers)
options(scipen = 999)
tm

tm$term[tm$p.value < 0.05]

but I get the error: Error in as.word(x) : can only coerce numeric objects to word

Why is this happening and how could I fix it?

P.S.: I can't find the original source of code. Please edit the question if you do.

Upvotes: 0

Views: 486

Answers (1)

CSJCampbell
CSJCampbell

Reputation: 2115

As the error message says, permutations::tidy is for working with objects of class word. I suspect you are looking for broom::tidy?

library(broom)
ols_reg <- lm(mpg ~ ., data = mtcars)
tm <- broom::tidy(ols_reg)
tm$term[tm$p.value < 0.1]
# [1] "wt"

Packages you import with library are added to your search path.

search()
# [1] ".GlobalEnv"           "package:permutations"
# [3] "package:broom"        "package:tidyr"
# ...

When two (or more) functions have the same name, R will use the object found closest to the start of the search path. Packages get attached at position two, then pushed down as you attach more packages. broom will be masked by permutations if it is attached first. The :: operator allows you to specify the package namespace in which to find the function.

Upvotes: 3

Related Questions