R 中的 RandomForest 报告对象中缺少值,但向量中的 NA 为零
RandomForest in R reports missing values in object, but vector has zero NAs in it
我试图在 R 中使用 randomForest 包,但我遇到了一个问题,R 告诉我响应向量中缺少数据。
> rf_blackcomb_earlyGame <- randomForest(max_cohort ~ ., data=blackcomb_earlyGame[-c(1,2), ])
Error in na.fail.default(list(max_cohort = c(47, 25, 20, 37, 1, 0, 23, :
missing values in object
指定的错误已经很清楚了。我以前也遇到过,以前确实有漏数据,这次没有漏数据
> class(blackcomb_earlyGame$max_cohort)
[1] "numeric"
> which(is.na(blackcomb_earlyGame$max_cohort))
integer(0)
我尝试使用 na.roughfix 看看是否有帮助,但我收到以下错误。
Error in na.roughfix.data.frame(list(max_cohort = c(47, 25, 20, 37, 1, :
na.roughfix only works for numeric or factor
我检查了每个向量以确保其中 none 个包含任何 NA,并且 none 个包含。
有人有什么建议吗?
也许有 Inf
或 -Inf
值?
is.na(c(1, NA, Inf, NaN, -Inf))
#[1] FALSE TRUE FALSE TRUE FALSE
is.finite(c(1, NA, Inf, NaN, -Inf))
#[1] TRUE FALSE FALSE FALSE FALSE
randomForest
可能会因数种不同类型的数据问题而失败。缺失值(NA
)、NaN
、Inf
或 -Inf
的值以及未转换为因子的字符类型都会失败,并出现各种错误消息.
我们可以在下面看到每个问题生成的错误消息的一些示例:
my.df <- data.frame(a = 1:26, b=letters, c=(1:26)+rnorm(26))
rf <- randomForest(a ~ ., data=my.df)
# this works without issues, because b=letters is cast into a factor variable by default
my.df$d <- LETTERS # Now we add a character column
rf <- randomForest(a ~ ., data=my.df)
# Error in randomForest.default(m, y, ...) :
# NA/NaN/Inf in foreign function call (arg 1)
# In addition: Warning message:
# In data.matrix(x) : NAs introduced by coercion
rf <- randomForest(d ~ ., data=my.df)
# Error in y - ymean : non-numeric argument to binary operator
# In addition: Warning message:
# In mean.default(y) : argument is not numeric or logical: returning NA
my.df$d <- c(NA, rnorm(25))
rf <- randomForest(a ~ ., data=my.df)
rf <- randomForest(d ~ ., data=my.df)
# Error in na.fail.default(list(a = 1:26, b = 1:26, c = c(3.14586293058335, :
# missing values in object
my.df$d <- c(Inf, rnorm(25))
rf <- randomForest(a ~ ., data=my.df)
rf <- randomForest(d ~ ., data=my.df)
# Error in randomForest.default(m, y, ...) :
# NA/NaN/Inf in foreign function call (arg 1)
有趣的是,您收到的错误消息是由数据框中的 character
类型引起的(参见 ),这是我在有数字列时看到的错误NA
。这表明可能存在 (1) 来自不同版本 randomForest
的错误差异或 (2) 错误消息以更复杂的方式取决于数据结构。无论哪种方式,对于收到此类错误的任何人的建议是查找上面列出的数据的所有可能问题,以便追查原因。
我试图在 R 中使用 randomForest 包,但我遇到了一个问题,R 告诉我响应向量中缺少数据。
> rf_blackcomb_earlyGame <- randomForest(max_cohort ~ ., data=blackcomb_earlyGame[-c(1,2), ])
Error in na.fail.default(list(max_cohort = c(47, 25, 20, 37, 1, 0, 23, :
missing values in object
指定的错误已经很清楚了。我以前也遇到过,以前确实有漏数据,这次没有漏数据
> class(blackcomb_earlyGame$max_cohort)
[1] "numeric"
> which(is.na(blackcomb_earlyGame$max_cohort))
integer(0)
我尝试使用 na.roughfix 看看是否有帮助,但我收到以下错误。
Error in na.roughfix.data.frame(list(max_cohort = c(47, 25, 20, 37, 1, :
na.roughfix only works for numeric or factor
我检查了每个向量以确保其中 none 个包含任何 NA,并且 none 个包含。
有人有什么建议吗?
也许有 Inf
或 -Inf
值?
is.na(c(1, NA, Inf, NaN, -Inf))
#[1] FALSE TRUE FALSE TRUE FALSE
is.finite(c(1, NA, Inf, NaN, -Inf))
#[1] TRUE FALSE FALSE FALSE FALSE
randomForest
可能会因数种不同类型的数据问题而失败。缺失值(NA
)、NaN
、Inf
或 -Inf
的值以及未转换为因子的字符类型都会失败,并出现各种错误消息.
我们可以在下面看到每个问题生成的错误消息的一些示例:
my.df <- data.frame(a = 1:26, b=letters, c=(1:26)+rnorm(26))
rf <- randomForest(a ~ ., data=my.df)
# this works without issues, because b=letters is cast into a factor variable by default
my.df$d <- LETTERS # Now we add a character column
rf <- randomForest(a ~ ., data=my.df)
# Error in randomForest.default(m, y, ...) :
# NA/NaN/Inf in foreign function call (arg 1)
# In addition: Warning message:
# In data.matrix(x) : NAs introduced by coercion
rf <- randomForest(d ~ ., data=my.df)
# Error in y - ymean : non-numeric argument to binary operator
# In addition: Warning message:
# In mean.default(y) : argument is not numeric or logical: returning NA
my.df$d <- c(NA, rnorm(25))
rf <- randomForest(a ~ ., data=my.df)
rf <- randomForest(d ~ ., data=my.df)
# Error in na.fail.default(list(a = 1:26, b = 1:26, c = c(3.14586293058335, :
# missing values in object
my.df$d <- c(Inf, rnorm(25))
rf <- randomForest(a ~ ., data=my.df)
rf <- randomForest(d ~ ., data=my.df)
# Error in randomForest.default(m, y, ...) :
# NA/NaN/Inf in foreign function call (arg 1)
有趣的是,您收到的错误消息是由数据框中的 character
类型引起的(参见 NA
。这表明可能存在 (1) 来自不同版本 randomForest
的错误差异或 (2) 错误消息以更复杂的方式取决于数据结构。无论哪种方式,对于收到此类错误的任何人的建议是查找上面列出的数据的所有可能问题,以便追查原因。