GLM:警告信息:’newdata’ 有 16623 行,但找到的变量有 22488 行

我已经广泛搜索了论坛,找到了许多类似的文章,但没有一个能解决我的问题。

现在,我求助于你们。

我的数据类似于这样:

ontime currency incoterms price month1      USD      FOB       234.2    011      CAD      FOB        92.4    010      USD      DAP       238.9    020      EUR      FOB       100      031      CNY      DAP       739.8    04

我使用了以下代码:

g = df$ontime      #binarya = df$currency    #Stringb = df$INCOTERMS   #Stringc = df$price       #floatf = df$month       #stringmod1 <- glm(g~a+b+c,family=binomial(link="logit"), data=df[f=="01",])pred_ontime1 <- predict(mod1,df[f%in%c("02","03","04"),],type="response")

我的目标是测试我在01月训练的模型,在02月、03月和04月的数据上进行测试。

然而,我的结果是这样的:

Warning message:'newdata' had 16623 rows but variables found have 22488 rows

我尝试过在01月训练,并在01月、02月、03月和04月上测试,这样没有给我错误信息,但似乎不合适在训练集中包含测试数据。

数值16623当然是02月、03月和04月行数的总和,而22488是01月、02月、03月和04月行数的总和。

我该怎么办?


回答:

尝试运行模型时不先将每一列保存到向量中。我认为predict()无法识别它正在使用的变量名与建模时相同。

mod1 <- glm(ontime ~ currency + INCOTERMS + price, family = binomial(link = "logit"), data = df[df$month == "01",])pred_ontime1 <- predict(mod1,df[df$month %in% c("02","03","04"),], type = "response")

看看这样是否有效。


对于任何感兴趣的人,这里是一个可复现的例子:

df <- read.table(textConnection("ontime currency incoterms price month0      USD      DAP       234.2    01                          1      CAD      FOB        92.4    01                          0      USD      DAP       238.9    02                          0      USD      FOB       100      03                          1      CAD      DAP       739.8    04"), header = TRUE)mod1 <- glm(ontime ~ currency + incoterms + price, family = binomial(link = "logit"), data = df[df$month == 1,])pred_ontime1 <- predict(mod1, df[df$month %in% c(2:4),], type = "response")pred_ontime1           3            4            5 5.826215e-11 5.826215e-11 1.000000e+00 

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注