0

I am using dplyr + the broom package to run separate regression for each subgroup in my

my_data %>% group_by(my_groups) %>% 
    do(tidy(lm( attended_school ~ treated + I_post_treatmet, data=.) 
            ))

(the actuall data is of restricted access, can't share here)

I get the following error:

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  0 (non-NA) cases

If I understood correctly this means the regression could not be run for some subgroup, possibly due to all NAs in some variable.

How can I catch those errors so that in the end I receive a dataset containing the regression outputs for the subgroups in which the regressions ran sucessfully?

EDIT: tryed a syntax similar to the link suggested in the comments and got the same problem:

my_data %>% group_by(my_groups) %>% 
    do(aa=tryCatch(lm( attended_school ~ treated + I_post_treatmet, data=.) 
            ))

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases

LucasMation
  • 2,408
  • 2
  • 22
  • 45
  • [This](http://stackoverflow.com/questions/39044500/comparing-models-with-dplyr-and-broomglance-how-to-continue-if-error-is-produ) looks closely related – aosmith May 12 '17 at 17:30
  • I tryed that and several close variations. Nothing worked – LucasMation May 12 '17 at 17:57
  • 1
    Did you try the exact solution in the answer in the link? You haven't included an reproducible example so no one else can test if that answer works in your situation or not. – aosmith May 12 '17 at 19:29
  • You didn't include the second argument to `tryCatch` from that link, which is the relevant one. – David Robinson May 13 '17 at 02:20

0 Answers0