2

Hey I have following test data:

test = data.frame(Date = c(as.Date("2010-10-10"), as.Date("2010-10-10"), as.Date("2010-12-10"), as.Date("2010-12-10")), Rate = c(0.1, 0.15, 0.16, 0.2), FCF = c(100,200,150,250))

Now I want to group the data by Date, do linear regression FCF~Rate in each group and then perform these regression in each group to get Value of this regression for each date and Rate. I have following code:

output = test %>% group_by(Date) %>% do(mod = lm(FCF ~ Rate, data = .))
output = test %>% left_join(output, by = "Date")
output = output %>% ungroup() %>% mutate(Value = predict(mod, newdata = Rate))

everything works fine without the last line because mod is not a model but a list and I cant do prediction. What should I change?

EDIT: When I evaluate this code:

  output = test %>% group_by(Date) %>%
  do(mod = lm(FCF ~ Rate, data = .))
  output = test %>% left_join(output, by = "Date")
  output = output %>% ungroup() 

I get:

enter image description here

The question is how can I use models from mod column to calculate predicted value for Rates from column Rate.

  • 1
    Quick clarification question: Why are you performing the linear models on subsets of your data? Is this a repeated measures study? If that's the case, then perhaps what you are really after is a linear model with Rate nested within date, in which case you may want to consider using the lmer or lme4 packages for developing your model from the un-aggregated data. – Sean McKenzie May 05 '21 at 20:04

1 Answers1

3

Here is one way

library(dplyr)
library(purrr)
test %>%
   nest_by(Date) %>% 
   mutate(mod = list(lm(FCF ~ Rate, data = data))) %>%
   ungroup %>%
   mutate(out = map2(data, mod, 
       ~ predict(.y, newdata = data.frame(Rate = .x$Rate))))

-output

# A tibble: 2 x 4
#  Date                 data mod    out      
#  <date>     <list<tibble>> <list> <list>   
#1 2010-10-10        [2 × 2] <lm>   <dbl [2]>
#2 2010-12-10        [2 × 2] <lm>   <dbl [2]>

If we need the 'Pred' column as well

library(tidyr)
out <- test %>%
       nest_by(Date) %>% 
       mutate(mod = list(lm(FCF ~ Rate, data = data))) %>%
       ungroup %>% 
       mutate(data =  map2(data, mod, ~ .x %>%
                  mutate(Pred = predict(.y, newdata =  cur_data())))) %>%
       unnest(data)

 
akrun
  • 874,273
  • 37
  • 540
  • 662
  • It's not exactly what I want. I want to group data by `Date`, then create a linear model `FCF~Rate` for each `Date` (so 2 models: `mod_d1`, `mod_d2`) then for each date I want to use this model for the rate from the given day, and finally I want to get the same data.frame from which I started but with one more column with value from the model. – Johhn White May 05 '21 at 21:12
  • @JohhnWhite It is doing the grouping by Date. with `nest_by` – akrun May 05 '21 at 23:04
  • @JohnWhite Regarding the different you can extract the column 'mod'. It would be different for different dates – akrun May 05 '21 at 23:13