1

I have read few answers on this here but I am afraid I have not been able to figure out an answer.

My R code is:

colors <- bmw[bmw$Channel=="Colors" & bmw$Hour=20,]
colors_test <- tail(colors, 89)
colors_train <- head(colors, 810)

colors_train_agg <- aggregate(colors_train$Impressions, list(colors_train$`Position of Ad in Break`), FUN=mean, na.rm=TRUE)
colnames(colors_train_agg) <- c("ad_position", "avg_impressions")
lm_colors <- lm(colors_train_agg$avg_impressions ~ poly(colors_train_agg$ad_position, 12))
 summary(lm_colors)

colors_test_agg <- aggregate(colors_test$Impressions, list(colors_test$`Position of Ad in Break`), FUN=mean, na.rm=TRUE)
colnames(colors_test_agg) <- c("ad_position", "avg_impressions")
new.df <- data.frame(colors_test_agg$ad_position)
colnames(new.df) <- c("ad_position")
colors_test_test <- predict(lm_colors, newdata=new.df)

So I have exactly the same column names for both training and test data. I still get the warning:

Warning message: 'newdata' had 15 rows but variables found have 22 rows

Can some one suggest what is wrong? Also, I want to know if I am even doing it the right way.

Also, some pointers on how to calculate accuracy of the model will be greatly appreciated. Thanks!

kskp
  • 692
  • 5
  • 11
  • 23
  • 3
    prefer `lm(avg_impressions ~ poly(ad_position, 12), data = colors_train_agg)` – joel.wilson Dec 05 '16 at 16:22
  • Being as the question is about row inconsistency it would help if you provided a few dimensions. `lapply(list(colors_test, colors_train, colors_train_agg, colors_test_agg), dim)` – Pierre L Dec 05 '16 at 16:26
  • Can you provide the data? – Hack-R Dec 05 '16 at 16:28
  • Using `lm_colors <- lm(avg_impressions ~ poly(ad_position, 13), data=colors_train_agg)` worked. Thanks @joel.wilson. Thank you all for your time. – kskp Dec 05 '16 at 16:36
  • though we solved your problem, alwas have an habit of posting a comp[lete question ( includes a sample small data) for us to work on – joel.wilson Dec 05 '16 at 16:43
  • Sure @joel.wilson Sorry about the inconvenience caused. – kskp Dec 05 '16 at 16:59

1 Answers1

7

solution :

lm_colors <- lm(avg_impressions ~ poly(ad_position, 13), data=colors_train_agg)

Reason : you can compare yourselves how model.matrix() generates the matrix to score the data inside predict(). So when we pass model(df$var1~df$var2), model.matrix() looks for df$var1 and df$var2 to generate the matrix- but this has dimensions of training data (df). Problem of having different names in the model and in newdata

go through below steps( if you are interested in knowing the cause) :

model1 <- lm(var1~var2, data = df)
model2 <- lm(df$var1~df$var2)
debug(predict)
predict(model1, newdata = df1)
predict(model2, newdata = df1) 
joel.wilson
  • 8,243
  • 5
  • 28
  • 48