【问题标题】:R Warning: newdata' had 15 rows but variables found have 22 rows [duplicate]R警告:newdata'有15行但发现的变量有22行[重复]
【发布时间】:2016-12-05 16:20:31
【问题描述】:

我在这里阅读了一些关于此的答案,但恐怕我无法找到答案。

我的 R 代码是:

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)

因此,我的训练数据和测试数据的列名完全相同。我仍然收到警告:

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

有人可以提出什么问题吗?另外,我想知道我的做法是否正确。

此外,我们将不胜感激有关如何计算模型准确性的一些指示。谢谢!

【问题讨论】:

  • 首选lm(avg_impressions ~ poly(ad_position, 12), data = colors_train_agg)
  • 问题是关于行不一致,如果您提供几个维度会有所帮助。 lapply(list(colors_test, colors_train, colors_train_agg, colors_test_agg), dim)
  • 你能提供数据吗?
  • 使用lm_colors &lt;- lm(avg_impressions ~ poly(ad_position, 13), data=colors_train_agg) 有效。谢谢@joel.wilson。谢谢大家的时间。
  • 虽然我们解决了您的问题,但一直有发布一个comp[lete问题(包括示例小数据)供我们处理的习惯

标签: r prediction non-linear-regression


【解决方案1】:

解决方案:

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

原因: 你可以自己比较model.matrix() 是如何生成矩阵来对predict() 中的数据进行评分的。所以当我们通过model(df$var1~df$var2)model.matrix() 寻找df$var1df$var2 来生成矩阵——但这有训练数据的维度(df)。在modelnewdata 中有不同名称的问题

执行以下步骤(如果您有兴趣了解原因):

model1 <- lm(var1~var2, data = df)
model2 <- lm(df$var1~df$var2)
debug(predict)
predict(model1, newdata = df1)
predict(model2, newdata = df1) 

【讨论】:

    猜你喜欢
    • 2017-01-04
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    相关资源
    最近更新 更多