I'm trying to learn the R's modeling framework tidymodels. After creating the model and specifying the package (engine) I want to use for my model, I now try to use some specific functions that are inside of the engine I chose. In this case it is the randomForest package and the varImpPlot() function that I'm trying to use. However, this error shows up when I try to execute it where it says that in order to use the function the object has to be a randomForest object. Well, this is obvious, but my question would be, is there some way to translate the parsnip object to the object of the engine I chose, or some way to use these functions inside of the package I have chosen? Thanks for help!
model_rand_forest <- rand_forest() %>%
set_engine("randomForest") %>%
set_mode("regression") %>%
translate()
training_workflow <- workflow() %>%
add_recipe(recipe) %>%
add_model(model_rand_forest)
training_workflow_fit <- training_workflow %>% fit(data = train)
training_workflow_fit %>% varImpPlot()
training_workflow_fit %>% varImpPlot()
Error in varImpPlot(.) :
This function only works for objects of class `randomForest'
You can extract the randomForest object from the workflow by using $fit$fit$fit. In your example, this should work
training_workflow_fit$fit$fit$fit %>% varImpPlot()
Or you could use the below syntax, which may be more neat
training_workflow_fit %>%
chuck("fit") %>%
chuck("fit") %>%
chuck("fit") %>%
varImpPlot()
Related
I am using a dataset from an online practice tutorial and the code can be found at the bottom of Page 4 (https://tomhouslay.files.wordpress.com/2017/02/indivvar_mv_tutorial_asreml.pdf)
In the tutorial, they get the function to work using the code listed below, but in my R session, I get an error that says:
No tidy method for objects of class lmerMod.
I have tried using the package "parsnip" as well as restarting my R session and I have tried requiring broom as suggested in other answers to similar questions.
The haggis practice csv file can be downloaded from here: https://figshare.com/articles/Haggis_data_behavioural_syndromes/4702540
library(asreml)
library(nadiv)
library(tidytext)
library(tidyverse)
library(broom)
require(broom)
library(lme4)
library(data.table)
library(parsnip)
HData<- read_csv("haggis practice.csv")
lmer_b <- lmer(boldness ~ scale(assay_rep, scale=FALSE) +
scale(body_size) +
(1|ID),
data = HData)
plot(lmer_b)
qqnorm(residuals(lmer_b))
hist(residuals(lmer_b))
summary(lmer_b)
rep_bold <- tidy(lmer_b, effects = "ran_pars", scales = "vcov") %>%
select(group, estimate) %>%
spread(group, estimate) %>%
mutate(repeatability = ID/(ID + Residual))
Providing an answer (from the comments).
The tidy methods for multilevel/mixed-type models (e.g. from lme4, brms, MCMCglmm, ...) were moved to broom.mixed. You can either install/load the broom.mixed package, or use the broomExtra package, which is a "meta-package" that looks for methods in both broom and broom.mixed ...
I am trying to create a smooth spline for each sample in a grouped dataframe. For this I am using a nest and map approach and mgcv gam (following this example https://smu095.github.io/2019/02/16/2019-02-16-tidytuesday-fitting-multiple-time-series-models-using-purrr/).
After running the gam I would like to use broom::augment to extract the fitted data and calculate confidence intervals.
This code works using broom 0.5.6 but throws an error using the new broom 0.7 version. broom::tidy and broom:glance still work with this format but augment stops with "Error: Problem with mutate() input augment_spline.
x object 'year' not found"
Example code below
library(tidyverse)
library(dslabs)
#Use the gapminder dataset that comes with dslabs as an example
glimpse(gapminder)
gapminder_nest <- gapminder %>%
group_by(country) %>%
nest()%>%
mutate(splined =map(data, ~mgcv::gam(population ~ s(year, k=5, bs="tp"), data=.x))) %>%
mutate(augment_spline= map(splined, broom::augment))%>%
unnest(augment_spline)%>%
dplyr::select(country, population,.fitted,.se.fit)
Same code runs if using broom 0.5.6
devtools::install_version("broom", version = "0.5.6", repos = "http://cran.us.r-project.org")
All online tutorials I could find present similar code that doesn't seem to work using broom 0.7
In the newer version I think it also needs data in newdata argument. You can pass the data as separate argument with map2.
library(tidyverse)
library(dslabs)
gapminder_nest <- gapminder %>%
group_by(country) %>%
nest()%>%
mutate(splined = map(data, ~mgcv::gam(population ~ s(year, k=5, bs="tp"), data=.x))) %>%
mutate(augment_spline = map2(splined, data, ~broom::augment(.x, newdata = .y))) %>%
unnest(augment_spline)
Although this works but this doesn't return all the columns as the 0.5.6 version of broom does i.e se.fit,.resid,.hat, .sigma and .cooksd.This only returns .fitted column.
I am using a dataset from an online practice tutorial and the code can be found at the bottom of Page 4 (https://tomhouslay.files.wordpress.com/2017/02/indivvar_mv_tutorial_asreml.pdf)
In the tutorial, they get the function to work using the code listed below, but in my R session, I get an error that says:
No tidy method for objects of class lmerMod.
I have tried using the package "parsnip" as well as restarting my R session and I have tried requiring broom as suggested in other answers to similar questions.
The haggis practice csv file can be downloaded from here: https://figshare.com/articles/Haggis_data_behavioural_syndromes/4702540
library(asreml)
library(nadiv)
library(tidytext)
library(tidyverse)
library(broom)
require(broom)
library(lme4)
library(data.table)
library(parsnip)
HData<- read_csv("haggis practice.csv")
lmer_b <- lmer(boldness ~ scale(assay_rep, scale=FALSE) +
scale(body_size) +
(1|ID),
data = HData)
plot(lmer_b)
qqnorm(residuals(lmer_b))
hist(residuals(lmer_b))
summary(lmer_b)
rep_bold <- tidy(lmer_b, effects = "ran_pars", scales = "vcov") %>%
select(group, estimate) %>%
spread(group, estimate) %>%
mutate(repeatability = ID/(ID + Residual))
Providing an answer (from the comments).
The tidy methods for multilevel/mixed-type models (e.g. from lme4, brms, MCMCglmm, ...) were moved to broom.mixed. You can either install/load the broom.mixed package, or use the broomExtra package, which is a "meta-package" that looks for methods in both broom and broom.mixed ...
I want to normalize only an arbitrary selection of variables using step_normalize from the recipe package (tidymodels). Unfortunately I can't find a selection function that seems to work within the step_normalize which selects a list of variables:
library(tidymodels)
iris %>%
recipe(Species ~ .) %>%
step_normalize(vars_select(Sepal.Length, Petal.Length)) %>%
prep()
I get this error message:
Error: Not all functions are allowed in step function selectors (e.g. `vars_select`). See ?selections.
step_normalize does not support this select helper function, this works:
iris %>%
recipe(Species ~ .) %>%
step_normalize(Sepal.Length, Petal.Length) %>%
prep()
See ?selections for supported selector functions.
I'm trying to run regressions within a nested data frame as described here. For my purposes, I'm using felm from the lfe package because I have many levels of fixed effects.
If I re-do the example in the link above using felm instead of lm, it works for the most part until I try to use broom::augment.
library(tidyverse)
library(broom)
library(gapminder)
library(lfe)
by_country <- gapminder %>%
group_by(continent, country) %>%
nest()
country_felm <- function(data){
felm(lifeExp ~ year, data = data)
}
by_country <- by_country %>%
mutate(model = purrr::map(data, country_felm)
)
Everything works up to this point except that I had to use a function instead of a formula in purrr::map in the last line of code, possibly another felm quirk.
Now if I try to use broom to extract the model output, it works for glance and tidy, but not for augment.
by_country %>% unnest(model %>% purrr::map(broom::glance))
by_country %>% unnest(model %>% purrr::map(broom::tidy))
by_country %>% unnest(model %>% purrr::map(broom::augment))
Trying to use augment results in the following error message:
Error in mutate_impl(.data, dots) :
argument must be coercible to non-negative integer
In addition: Warning message:
In seq_len(nrow(x)) : first element used of 'length.out' argument
It looks like augment is having trouble finding the data for the data argument, which is generally the dataset used for fitting.
The problem is easier to see if working with a single one of these models rather than all of them at once.
This doesn't work, with your given error:
augment(by_country$model[[1]])
But explicitly passing the data to the data argument does:
augment(by_country$model[[1]], data = by_country$data[[1]])
A work-around is therefore to pass the dataset to augment as the second argument. This can be done via purrr:map2, looping through both the model and data columns at the same time.
by_country %>%
unnest(model %>% purrr::map2(., data, augment))