Error - No tidy method for objects of class lmerMod - r

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 ...

Related

Using package specific functions with parsnip

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()

Using nest and map in combination with mgcv gam and broom augment

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.

R Error when I use mice::pool function: "Error: No tidy method for objects of class lmerMod" [duplicate]

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 ...

Unnest nested tidydrc models

Problem
I've been using a tidy wrapper for the drc package—tidydrc— to build growth curves which produces a tidy version of the normal output (best for ggplot). However, due to the inherit nesting of the models, I can't run simple drc functions since the models are nested inside a dataframe. I've attached code that mirrors drc and tidydrc package below.
Goal
To compare information criteria from multiple model fits for the tidydrc output using the drc function mselect()—ultimately to efficiently select the best fitting model.
Ideal Result (works with drc)
library(tidydrc) # To load the Puromycin data
library(drc)
model_1 <- drm(rate ~ conc, state, data = Puromycin, fct = MM.3())
mselect(model_1, list(LL.3(), LL.5(), W1.3(), W1.4(), W2.4(), baro5()))
# DESIRED OUTPUT SIMILAR TO THIS
logLik IC Lack of fit Res var
MM.3 -78.10685 170.2137 0.9779485 70.54874 # Best fitting model
LL.3 -78.52648 171.0530 0.9491058 73.17059
W1.3 -79.22592 172.4518 0.8763679 77.75903
W2.4 -77.87330 173.7466 0.9315559 78.34783
W1.4 -78.16193 174.3239 0.8862192 80.33907
LL.5 -77.53835 177.0767 0.7936113 87.80627
baro5 -78.00206 178.0041 0.6357592 91.41919
Not Working Example with tidydrc
library(tidyverse) # tidydrc utilizes tidyverse functions
model_2 <- tidydrc_model(data = Puromycin, conc, rate, state, model = MM.3())
summary(model_2)
Error: summary.vctrs_list_of() not implemented.
Now, I can manually tease apart the list of models in the dataframe model_2 but can't seem to figure out the correct apply statements (it's a mess) to get this working.
Progress Thus Far
These both produce the same error, so at least I've subsetted a level down but now I'm stuck and pretty sure this is not the ideal solution.
mselect(model_2$drmod, list(LL.3(), LL.5(), W1.3(), W1.4(), W2.4(), baro5()))
model_2_sub <- model_2$drmod # Manually subset the drmod column
apply(model_2_sub, 2, mselect(list(LL.3(), LL.5(), W1.3(), W1.4(), W2.4(), baro5())))
Error in UseMethod("logLik") :
no applicable method for 'logLik' applied to an object of class "list"
I've even tried the tidyverse function unnest() to no avail
model_2_unnest <- model_2 %>% unnest_longer(drmod, indices_include = FALSE)

Error message when using broom to get coefficients from glmmTMB zero inflation models

Using the Owls data and the glmmTMB package, I want to visually compare the regression coefficients from different zero-Inflated models that differ in the family used (ZIPOISS, ZINB1, ZINB2) and with/out the offset (logBroodSize).
However my first problem is to get the coefficients. The tidy function from package broom should provide you with the coefficients to plot them later with ggplot, but I get the following error when I try to get them:
modList= list(zipoiss, zinb1, zinb2, zinb1_bs, zinb2_bs)
coefs= ldply(modList,tidy,effect="fixed",conf.int=TRUE,
.id="model") %>%
mutate(term=abbfun(term)) %>%
select(model,term,estimate,conf.low,conf.high) %>%
filter(!term %in% c("Intercept","Intercept.1","NCalls","zi_NCalls"))
Error in as.data.frame.default(x) :
cannot coerce class ""glmmTMB"" to a data.frame
Also: Warning message:
In tidy.default(X[[i]], ...) :
No method for tidying an S3 object of class glmmTMB , using as.data.frame
Any idea of what could be wrong? I was already told that not having a right version of broom could be the reason, however I have had installed the right version of it... Code for a reproducible example is provided next:
# Packages and dataset
library(glmmTMB)
library(broom) # devtools::install_github("bbolker/broom")
library(plyr)
library(dplyr)
data(Owls,package="glmmTMB")
Owls = plyr::rename(Owls, c(SiblingNegotiation="NCalls"))
Owls = transform(Owls,
ArrivalTime=scale(ArrivalTime, center=TRUE, scale=FALSE),
obs=factor(seq(nrow(Owls))))
# Models
zipoiss<-glmmTMB(NCalls~(FoodTreatment+ArrivalTime)*SexParent+
offset(logBroodSize)+(1|Nest),
data=Owls,
ziformula = ~ 1,
family="poisson")
zinb2<- glmmTMB(NCalls~(FoodTreatment+ArrivalTime)*SexParent+
offset(logBroodSize)+(1|Nest),
data=Owls,
ziformula = ~ 1,
family="nbinom2")
zinb1 <- glmmTMB(NCalls~(FoodTreatment+ArrivalTime)*SexParent+
offset(logBroodSize)+(1|Nest),
data=Owls,
ziformula = ~ 1,
family="nbinom1")
zinb1_bs<- glmmTMB(NCalls~(FoodTreatment+ArrivalTime)*SexParent+
BroodSize+(1|Nest),
data=Owls,
ziformula = ~ 1,
family="nbinom1")
zinb2_bs<- glmmTMB(NCalls~(FoodTreatment+ArrivalTime)*SexParent+
BroodSize+(1|Nest),
data=Owls,
ziformula = ~ 1,family="nbinom2")
# Get coefficients ("coefs" does not work yet...)
modList = list(zipoiss, zinb1, zinb2, zinb1_bs, zinb2_bs)
coefs = ldply(modList,tidy,effect="fixed",conf.int=TRUE,
.id="model") %>%
mutate(term=abbfun(term)) %>%
select(model,term,estimate,conf.low,conf.high) %>%
filter(!term %in% c("Intercept","Intercept.1","NCalls","zi_NCalls"))
This now works (as of today) with the new/under-development broom.mixed package, e.g.
devtools::install_github("bbolker/broom.mixed")
Hopefully this will be on CRAN sometime soon, but it's only medium priority
for me, and I don't want to release it to CRAN until it's at least 90% baked. Pull requests welcome!
The last step changed a little bit (for one thing, I don't seem to have abbfun):
modList = lme4:::namedList(zipoiss, zinb1, zinb2, zinb1_bs, zinb2_bs)
coefs = ldply(modList,tidy,effect="fixed",conf.int=TRUE,
.id="model") %>%
select(model,term,component,estimate,conf.low,conf.high) %>%
filter(!term %in% c("(Intercept)","NCalls"))
Caveat: The development of these tools for glmmTMB models is pretty new/experimental; you should (1) sanity-check your results carefully and (2) report an issue if something doesn't work as expected.

Resources