Reputation: 61
I am running following code
class(TrainSet$volume)
that gives me [1] "numeric"
Then I run
model1 <- randomForest(TrainSet$volume ~ ., data = TrainSet, importance = TRUE)
it gave me
Error in randomForest.default(m, y, ...) : NA/NaN/Inf in foreign function call (arg 1)
What could be the reason? Thanks
Upvotes: 1
Views: 623
Reputation: 131
Besides missing data or infinities as Peter_Evan suggested, another possibility is a character variable in TrainSet. Therefore, run the following four lines:
any(is.na(TrainSet))
any(apply(TrainSet, 2, is.infinite))
any(apply(TrainSet, 2, is.nan))
any(is.character(TrainSet))
If any return TRUE, you have your problem.
Upvotes: 1
Reputation: 947
It is hard to know for sure without more information about your data, but as the error suggests you seem to have one of those values (NA/NaN/Inf) somewhere in your data frame. Perhaps inf
as NA
tends to throw a different error. We can recreate your error below:
library(randomForest)
#setting data
data(iris)
#making an infinite value
iris[1,1] <- Inf
#grab row
iris[is.infinite(iris$Sepal.Length),]
#output
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1 Inf 3.5 1.4 0.2 setosa
#checking data type
is.numeric(iris$Sepal.Length) #TRUE
#reproducing error
iris.rf <- randomForest(iris$Sepal.Width ~ ., data=iris, importance=TRUE)
#output
Error in randomForest.default(m, y, ...) :
NA/NaN/Inf in foreign function call (arg 1)
As to where or why this is in your data is unclear (again, need to see the data to make this call). A common way inf
is created is by a mistake in per-processing that introduces a confused calculation, like dividing be zero.
is.infinite(pi / 0)
#output
# [1] TRUE
Scanning for infinites or NA (with is.infinite
or is.na
) and reviewing any changes you made to your data seems like a good place to start.
Upvotes: 2