2

I'm getting the message Error: invalid model formula in ExtractVars while trying to fit linear models using tidyr version 0.4.0. I have run this exact code without error in a previous session, but have changed some library info, so I'm wondering if that is perhaps the issue.

Here's the code I'm trying to run:

library(gapminder)
library(dplyr)
library(tidyr)
library(ggplot2)
library(purrr)
library(broom)

# create one dataframe for each country
by_country <- gapminder %>%
  group_by(continent, country) %>%
  nest()

# fit a linear model to each country with purrr, save as new var 'model'
by_country <- by_country %>% 
  mutate(model = purrr::map(data, ~ lm(lifeExp ~ year, data = .)))

Here's my session info:

R version 3.2.2 (2015-08-14) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 7 x64 (build 7601) Service Pack 1

locale: [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C LC_TIME=English_United States.1252

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] dplyr_0.4.3 broom_0.4.0 purrr_0.2.0 tidyr_0.4.0 gapminder_0.2.0 lubridate_1.3.3
[7] stringr_1.0.0 ggplot2_1.0.1 plyr_1.8.3 reshape_0.8.5 ProjectTemplate_0.6

loaded via a namespace (and not attached): [1] Rcpp_0.12.3 magrittr_1.5 MASS_7.3-43 mnormt_1.5-3 munsell_0.4.2 lattice_0.20-33 colorspace_1.2-6 R6_2.1.2
[9] tools_3.2.2 parallel_3.2.2 grid_3.2.2 nlme_3.1-121 gtable_0.1.2 psych_1.5.8 DBI_0.3.1 lazyeval_0.1.10 [17] digest_0.6.8 assertthat_0.1 reshape2_1.4.1 memoise_1.0.0 labeling_0.3 stringi_1.0-1 scales_0.3.0 proto_0.3-10

I'm using ProjectTemplate, which automatically loads these libraries before running the above code (I think): reshape, plyr, ggplot2, stringr, lubridate, gapminder, tidyr, purrr, broom, dplyr. I realize there is some duplication, but don't think it would cause the code not to run? Any insight is super appreciated!

c. garrett
  • 69
  • 2
  • 4
  • Works for me. It's the example from the RStudio blog? Try restarting and just loading the necessary packages only... – Stephen Henderson Feb 09 '16 at 21:32
  • @StephenHenderson Exactly, the example from the RStudio blog. I tried restarting and loading only `projecttemplate`, `ggplot2`, `lubridate`, `gapminder`, `tidyr`, `purrr`, `broom`, and `dplyr` (in that order), but still get the same error. – c. garrett Feb 09 '16 at 22:48
  • try without `projecttemplate` and maybe not `purrr` as you are importing `purrr::map` if it still doesn't work ... maybe file a bug report. – Stephen Henderson Feb 09 '16 at 22:52
  • @HubertL hmm... the `nest()` statement works fine for me, I just run into issues in the next step when trying to build the models. Are you using version 0.4.0 of `tidyr`? – c. garrett Feb 09 '16 at 22:53
  • @StephenHenderson okay, loaded libraries without `projecttemplate`. tried in this order: `gapminder`, `dplyr`, `magrittr`, `tidyr`, `ggplot2`, `purrr`, `broom`, and it works. When tried in this order `ggplot2`, `lubridate`, `gapminder`, `tidyr`, `purrr`, `broom`, `dplyr`, I get the original error. I don't see any masked objects from the version that produces the error that seem related. – c. garrett Feb 09 '16 at 23:05
  • OK, it works for me. It's their example. If you are all updated and you're sure you haven't any odd typo then file a bug report. – Stephen Henderson Feb 09 '16 at 23:14
  • @HubertL You will need 0.4 as shown here: http://blog.rstudio.org/2016/02/02/tidyr-0-4-0/ – Stephen Henderson Feb 09 '16 at 23:15
  • Right, it works well with 0.4.1 – HubertL Feb 09 '16 at 23:16
  • @StephenHenderson do both versions of loading in the libraries work for you, or just the first? Also, RE: filing a bug report, would it fall under purrr instead of tidyr? I've never filed one before. – c. garrett Feb 09 '16 at 23:21
  • @HubertL Thanks for trying it out. I think it's an issue of the order I'm loading libraries. If you load the libraries in this order: `ggplot2`, `lubridate`, `gapminder`, `tidyr`, `purrr`, `broom`, `dplyr`, does the rest of the code work for you? – c. garrett Feb 09 '16 at 23:26
  • Yes I reproduce when I load in that order, I get a warning `Attachement du package : ‘dplyr’ The following object is masked from ‘package:purrr’: order_by The following objects are masked from ‘package:lubridate’: intersect, setdiff, union The following objects are masked from ‘package:stats’: filter, lag The following objects are masked from ‘package:base’: intersect, setdiff, setequal, union` – HubertL Feb 09 '16 at 23:29
  • But it works if I don't load `lubridate` – HubertL Feb 09 '16 at 23:31
  • @StephenHenderson This seems to be a bug, If I load `dplyr`, `tidyr`, `purrr` and run `mtcars %>% group_by(cyl) %>% nest() %>% mutate(mode = map(data, ~ lm(mpg ~ wt, data = .)))` it works fine, if I load `ggplot2` as well, I get a very similar error message `Error in eval(expr, envir, enclos) : invalid term in model formula` – luffe Mar 01 '16 at 14:28

0 Answers0