简体   繁体   中英

Using dplyr rowwise to create multiple linear models

considering this post: https://www.tidyverse.org/blog/2020/06/dplyr-1-0-0/

I was trying to create multiple models for a data set, using multiple formulas. this example says:

library(dplyr, warn.conflicts = FALSE)

models <- tibble::tribble(
  ~model_name,    ~ formula,
  "length-width", Sepal.Length ~ Petal.Width + Petal.Length,
  "interaction",  Sepal.Length ~ Petal.Width * Petal.Length
)

iris %>% 
  nest_by(Species) %>% 
  left_join(models, by = character()) %>% 
  rowwise(Species, model_name) %>% 
  mutate(model = list(lm(formula, data = data))) %>% 
  summarise(broom::glance(model))

You can see rowwise function is used to get the answer but when i dont use this function, i still get the correct answer

iris %>%
  nest_by(Species) %>% 
  left_join(models, by = character()) %>% 
  mutate(model = list(lm(formula, data = data))) %>% 
  summarise(broom::tidy(model))

i only lost the "model_name" column, but considering that rowwise documentation says, this function is to compute , i dont get why is still computed this way, why this happens?

thanks in advance.

considering https://cran.r-project.org/web/packages/dplyr/vignettes/rowwise.html

You can optionally supply “identifier” variables in your call to rowwise(). These variables are preserved when you call summarise(), so they behave somewhat similarly to the grouping variables passed to group_by():

i didn't understand how identifiers works, so as far i get this "identifiers" (Species,model_name) doesn't affect how to compute a value, only the way your tibble is presented.

So if you have a rowwise tibble created by nest_by you dont need the rowwise() function to compute by row. So in my example, rowwise function only give you a extra column of information but linear model is still the same. this is just for a "elegant way", it doesn't change the way its computed.

Thanks to tmfmnk

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM