Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. library(dplyr)
  2. library(tidyr)
  3. library(purrr)
  4. library(broom)
  5. library(ggplot2)
  6.  
  7. f1 = hwy ~ cyl
  8. f2 = hwy ~ displ
  9. f3 = hwy ~ cyl + displ
  10.  
  11. lin_mod = function(formula) {
  12. function(data) {
  13. lm(formula, data = data)
  14. }
  15. }
  16.  
  17. mpg %>%
  18. group_by(manufacturer) %>%
  19. nest() %>%
  20. mutate(model = map(data, lin_mod(f1)),
  21. aug = map(model, augment),
  22. res = map(aug, ".resid"))
  23.  
  24. mpg %>%
  25. group_by(manufacturer) %>%
  26. nest() %>%
  27. mutate(model1 = map(data, lin_mod(f1)),
  28. aug1 = map(model1, augment),
  29. res1 = map(aug1, ".resid"),
  30. model2 = map(data, lin_mod(f2)),
  31. aug2 = map(model2, augment),
  32. res2 = map(aug2, ".resid"),
  33. model3 = map(data, lin_mod(f3)),
  34. aug3 = map(model3, augment),
  35. res3 = map(aug3, ".resid"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement