Apply a grouped model back to data

I approach such models

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 the models look 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 could just apply the β€œdo” again, but I don’t see it ... Alternatively, I can do something in accordance with

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

But I'm not sure about the syntax.

+4
r dplyr
Jun 22 '14 at 23:21
source share
2 answers

Here is the dplyr method to get 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 this in one step if you create a prediction 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(.)) 
+7
Jun 24 '14 at 0:33
source share

Not sure if there is a question here, but I think the data.table package data.table especially effective 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 part by = .EACHI )

+4
Jun 22 '14 at 23:35
source share



All Articles