R Error in lm.fit(x, y, offset, singular.ok, …) : NA/NaN/Inf in ‘x’ (2 Examples)

 

In this R tutorial you’ll learn how to deal with the “Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) : NA/NaN/Inf in ‘x'”.

The tutorial is structured as follows:

Let’s get started!

 

Example Data

The first step is to construct some data that we can use in the following examples:

set.seed(52389374)                                   # Create example data
data <- data.frame(y = rnorm(100),
                   x = c(NA, Inf, NaN, rnorm(97)))
head(data)                                           # Head of example data

 

table 1 data frame r error lm fit na nan inf programming language

 

As you can see based on Table 1, our example data is a data frame consisting of 100 rows and two columns.

 

Example 1: Reproduce the Error in lm.fit : NA/NaN/Inf

This example shows how to replicate the “Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) : NA/NaN/Inf in ‘x'” in the R programming language.

Let’s assume that we want to estimate a linear model based on our data. Then, we typically would apply the lm function as shown below:

lm(y ~ x, data)                                      # Try to apply lm function
# Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
#   NA/NaN/Inf in 'x'

Unfortunately, the RStudio console returns the message “Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) : NA/NaN/Inf in ‘x'”.

The reason for this is that our data contains NaN and Inf values. In contrast to NA values, these kinds of values can not be handled by the lm function.

So how can we solve this problem?

 

Example 2: Fix the Error in lm.fit : NA/NaN/Inf

In Example 2, I’ll explain how to handle the “Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) : NA/NaN/Inf in ‘x'”.

For this, we have to replace the NaN and Inf values in our data frame:

data_new <- data                                     # Duplicate data
data_new[is.na(data_new) | data_new == "Inf"] <- NA  # Replace NaN & Inf with NA

The previous R programming syntax has created a new data frame called data_new that does contain NA values instead of NaN and Inf.

Now, we can apply the lm function to this new data frame:

lm(y ~ x, data_new)                                  # Properly apply lm function
# Call:
# lm(formula = y ~ x, data = data_new)
# 
# Coefficients:
# (Intercept)            x  
#   -0.043774    -0.001974

Works fine!

 

Video & Further Resources

Do you need more information on the R programming codes of this tutorial? Then I recommend watching the following video of my YouTube channel. In the video, I show the R code of this article in RStudio.

 

The YouTube video will be added soon.

 

Furthermore, you may read the related tutorials which I have published on my website. You can find a selection of articles on topics such as vectors, coding errors, and missing data below:

 

In this R programming tutorial you have learned how to get rid of the “Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) : NA/NaN/Inf in ‘x'”. Let me know in the comments below, if you have further questions. Furthermore, please subscribe to my email newsletter in order to get updates on new tutorials.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top