Table of Contents
The error “Na/NaN/Inf in foreign function call” occurs when the randomForest.default() function is unable to perform calculations due to missing or invalid data values (such as Not a Number, Not a Number, or Infinity) within the foreign function call. This can happen when the input data contains null values or when there are issues with the data structure. It is important to address and fix these errors in order to successfully run the randomForest.default() function.
Fix: randomForest.default(m, y, …) : Na/NaN/Inf in foreign function call
One error you may encounter in R is:
Error in randomForest.default(m, y, ...) : NA/NaN/Inf in foreign function call (arg 1)
There are two reasons for why this error might occur:
- There are NA, NaN, or Inf values in the dataset
- One of the variables in the dataset is a character
The easiest way to fix this error is to remove rows with missing data and convert character variables to factor variables:
#remove rows with missing values df <- na.omit(df) #convert all character variables to factor variableslibrary(dplyr) df %>% mutate_if(is.character, as.factor)
This tutorial shares an example of how to fix this error in practice.
How to Reproduce the Error
Suppose we attempt to fit a random forest to the following data frame in R:
library(randomForest)
#create data frame
df <- data.frame(y <- c(30, 29, 30, 45, 23, 19, 9, 8, 11, 14),
x1 <- c('A', 'A', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C'),
x2 <- c(4, 4, 5, 7, 8, 7, 9, 6, 13, 15))
#attempt to fit random forest model
model <- randomForest(formula = y ~ ., data = df)
Error in randomForest.default(m, y, ...) :
NA/NaN/Inf in foreign function call (arg 1)We receive an error because x1 is a character variable in the data frame.
We can confirm this by using the str() function to view the structure of the data frame:
str(df)'data.frame': 10 obs. of 3 variables:
$ y....c.30..29..30..45 : num 30 29 30 45 23 19 9 8 11 14
$ x1....c..A....A....B....B.... : chr "A" "A" "B" "B"
$ x2....c.4..4..5..7.. : num 4 4 5 7 8 7 9 6 13 15How to Fix the Error
To fix this error, we can use the mutate_if() function from to convert each character column to a factor column:
library(dplyr)
#convert each character column to factor
df = df %>% mutate_if(is.character, as.factor)
#fit random forest model
model <- randomForest(formula = y ~ ., data = df)
#view summary of model
model
Call:
randomForest(formula = y ~ ., data = df)
Type of random forest: regression
Number of trees: 500
No. of variables tried at each split: 1
Mean of squared residuals: 65.0047
% Var explained: 48.64
We don’t receive any error this time because there are no longer any character variables in the data frame.
The following tutorials explain how to address other common errors in R:
Cite this article
stats writer (2024). “What does the error ‘Na/NaN/Inf in foreign function call’ mean in the context of the randomForest.default() function?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/what-does-the-error-na-nan-inf-in-foreign-function-call-mean-in-the-context-of-the-randomforest-default-function/
stats writer. "“What does the error ‘Na/NaN/Inf in foreign function call’ mean in the context of the randomForest.default() function?." PSYCHOLOGICAL SCALES, 5 May. 2024, https://scales.arabpsychology.com/stats/what-does-the-error-na-nan-inf-in-foreign-function-call-mean-in-the-context-of-the-randomforest-default-function/.
stats writer. "“What does the error ‘Na/NaN/Inf in foreign function call’ mean in the context of the randomForest.default() function?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/what-does-the-error-na-nan-inf-in-foreign-function-call-mean-in-the-context-of-the-randomforest-default-function/.
stats writer (2024) '“What does the error ‘Na/NaN/Inf in foreign function call’ mean in the context of the randomForest.default() function?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/what-does-the-error-na-nan-inf-in-foreign-function-call-mean-in-the-context-of-the-randomforest-default-function/.
[1] stats writer, "“What does the error ‘Na/NaN/Inf in foreign function call’ mean in the context of the randomForest.default() function?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. “What does the error ‘Na/NaN/Inf in foreign function call’ mean in the context of the randomForest.default() function?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
