我正在为Kaggle竞赛(链接)开发一个CTR预测模型。我阅读了培训集中的前10万行数据,然后将其进一步拆分为80/20的火车/测试集。
ad_data <- read.csv("train", header = TRUE, stringsAsFactors = FALSE, nrows = 100000)
trainIndex <- createDataPartition(ad_data$click, p=0.8, list=FALSE, times=1)
ad_train <- ad_data[trainIndex,]
ad_test <- ad_data[-trainIndex,]
然后,我使用ad_train数据开发了GLM模型。
ad_glm_model <- glm(ad_train$clicks ~ ad_train$C1 + ad_train$site_category + ad_train$device_type, family = binomial(link = "logit"), data = ad_train)
但是,每当我试图使用预测函数来检查它在ad_test集中的性能时,我就会得到错误:
test_model <- predict(ad_glm_model, newdata = ad_test, type = "response")
Warning message:
'newdata' had 20000 rows but variables found have 80000 rows
怎么回事?如何在新数据上测试我的GLM模型?
编辑:它工作得很完美。只需要做这个调用,而不是:
ad_glm_model <- glm(clicks ~ C1 + site_category + device_type, family = binomial(link = "logit"), data = ad_train)
发布于 2017-02-20 15:53:48
这是因为在模型公式中包含了每个变量的数据帧的名称。相反,你的公式应该是:
glm(clicks ~ C1 + site_category + device_type, family = binomial(link = "logit"), data = ad_train)
正如第二环节在重复通知中所描述的那样:
这是一个在数据和新数据之间使用不同名称的问题,而不是在使用向量或数据文件之间的问题。 当您用lm函数拟合模型,然后使用“预测”进行预测时,“预测”尝试在您的新数据上找到相同的名称。在第一个例子中,名称x与mtcar$wt冲突,因此您将得到警告。
https://stackoverflow.com/questions/42355901
复制相似问题