Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply grouped model back onto data

Tags:

r

dplyr

I fit models like so

groupedTrainingSet = group_by(trainingSet, geo);
models = do(groupedTrainingSet, mod = lm(revenue ~ julian, data=.))

grouptedTestSet = group_by(testSet, geo);
// TODO: apply model back to test set

Where models looks like

 geo     mod
1   APAC <S3:lm>
2  LATAM <S3:lm>
3     ME <S3:lm>
7    ROW <S3:lm>
4     WE <S3:lm>
5     NA <S3:lm>

I think I should be able to just apply 'do' again but I'm not seeing it...Alternatively I can do something along the lines of

apply(trainingData, fitted =
    predict(select(models, geo==geo)$mod, .));

But I'm not sure about the syntax there.

like image 222
ygr Avatar asked Jun 22 '14 23:06

ygr


2 Answers

Here is a dplyr method of obtaining a similar answer, following the approach used by @Mike.Gahan :

library(dplyr) 

iris.models <- iris %>%
  group_by(Species) %>%
  do(mod = lm(Sepal.Length ~ Sepal.Width, data = .))

iris %>% 
  tbl_df %>%
  left_join(iris.models) %>%
  rowwise %>%
  mutate(Sepal.Length_pred = predict(mod,
                                    newdata = list("Sepal.Width" = Sepal.Width)))

alternatively you can do it in one step if you create a predicting function:

m <- function(df) {
  mod <- lm(Sepal.Length ~ Sepal.Width, data = df)
  pred <- predict(mod,newdata = df["Sepal.Width"])
  data.frame(df,pred)
}

iris %>%
  group_by(Species) %>%
  do(m(.))
like image 134
AndrewMacDonald Avatar answered Nov 11 '22 21:11

AndrewMacDonald


Not sure there is a question here, but I think the data.table package is especially efficient here.

#Load data.table package
require(data.table)
iris <- data.table(iris)

#Make a model for each species group
iris.models <- iris[, list(Model = list(lm(Sepal.Length ~ Sepal.Width))),
                      keyby = Species]

#Make predictions on dataset
setkey(iris, Species)
iris[iris.models, prediction := predict(i.Model[[1]], .SD), by = .EACHI]

(for data.table version <= 1.9.2 omit the by = .EACHI part)

like image 4
Mike.Gahan Avatar answered Nov 11 '22 22:11

Mike.Gahan