GLM:警告消息:'newdata' 有 16623 行,但找到的变量有 22488 行
GLM: Warning message: 'newdata' had 16623 rows but variables found have 22488 rows
我在论坛上四处搜寻,找到了很多这样的文章,但是,none 解决了我的问题。
现在,我转向你。
我有类似的数据:
ontime currency incoterms price month
1 USD FOB 234.2 01
1 CAD FOB 92.4 01
0 USD DAP 238.9 02
0 EUR FOB 100 03
1 CNY DAP 739.8 04
我这个代码:
g = df$ontime #binary
a = df$currency #String
b = df$INCOTERMS #String
c = df$price #float
f = df$month #string
mod1 <- 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 month
0 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
这里我生成了一些看起来像你的问题的伪数据作为数据框 df
:
currency <- c('USD','CAD','CAD','EUR','CNY','USD','EUR','CNY')
incoterms <- c('FOB','FOB','DAP','DAP','FOB','DAP','FOB','DAP')
month <- c('01','01','01','01','01','02','03','04')
df <- data.frame(currency, incoterms, month)
df <- rbind(df,df,df,df)
df$price <- rnorm(nrow(df), 200, 50)
df$ontime <- rbinom(nrow(df), 1, 0.5)
然后我继续装mod1
。重要的是,我没有将每个预测变量定义为向量,我只是从数据框中按名称提取它们,该数据框已被子集化为仅包含第一个月。
mod1 <- glm(ontime ~ currency + incoterms + price, data = df[month == '01',])
以下预测函数现在可以正常运行:
pred <- predict(mod1, df[month %in% c('02','03','04'),], type = 'response')
我在论坛上四处搜寻,找到了很多这样的文章,但是,none 解决了我的问题。
现在,我转向你。
我有类似的数据:
ontime currency incoterms price month
1 USD FOB 234.2 01
1 CAD FOB 92.4 01
0 USD DAP 238.9 02
0 EUR FOB 100 03
1 CNY DAP 739.8 04
我这个代码:
g = df$ontime #binary
a = df$currency #String
b = df$INCOTERMS #String
c = df$price #float
f = df$month #string
mod1 <- 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 month
0 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
这里我生成了一些看起来像你的问题的伪数据作为数据框 df
:
currency <- c('USD','CAD','CAD','EUR','CNY','USD','EUR','CNY')
incoterms <- c('FOB','FOB','DAP','DAP','FOB','DAP','FOB','DAP')
month <- c('01','01','01','01','01','02','03','04')
df <- data.frame(currency, incoterms, month)
df <- rbind(df,df,df,df)
df$price <- rnorm(nrow(df), 200, 50)
df$ontime <- rbinom(nrow(df), 1, 0.5)
然后我继续装mod1
。重要的是,我没有将每个预测变量定义为向量,我只是从数据框中按名称提取它们,该数据框已被子集化为仅包含第一个月。
mod1 <- glm(ontime ~ currency + incoterms + price, data = df[month == '01',])
以下预测函数现在可以正常运行:
pred <- predict(mod1, df[month %in% c('02','03','04'),], type = 'response')