R Error in lm.fit(x, y, offset, singular.ok , …) : 0 (non-NA) cases (2 Examples)

 

In this article, I’ll illustrate how to debug the “Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) : 0 (non-NA) cases” in the R programming language.

The table of content is structured like this:

Let’s get started…

 

Example Data

Let’s first construct some example data:

set.seed(9364593)                     # Create example data
data <- data.frame(y = rnorm(100), 
                   x1 = rnorm(100),
                   x2 = NA)
head(data)                            # Head of example data

 

table 1 data frame r error lm fit zero non na cases

 

Have a look at the previous table. It reveals that our example data is constructed of 100 rows and three columns.

 

Example 1: Reproduce the Error in lm.fit – 0 (non-NA) cases

The following R syntax illustrates how to replicate the “Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) : 0 (non-NA) cases” in the R programming language.

Let’s assume that we want to estimate a linear regression model using the lm function in R. Then, we might try to use the following R syntax:

lm(y ~ ., data)                       # Estimate model based on entire data set
# Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
#   0 (non-NA) cases

Unfortunately, the RStudio console returns the error message “0 (non-NA) cases”.

The reason for this is that one (or multiple) of our data frame columns contains only NA values. The lm function (and other modelling functions as well) can not handle such only NA predictors.

So how can we solve this problem?

 

Example 2: Fix the Error in lm.fit – 0 (non-NA) cases

The following R code explains how to deal with the “Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) : 0 (non-NA) cases”.

To avoid this message, we have to remove all independent variables that contain only missing values from our model.

In the following R code, I’m explicitly specifying that I want to use only the column x1 as predictor variable:

lm(y ~ x1, data)                      # Estimate model based on subset
# Call:
# lm(formula = y ~ x1, data = data)
# 
# Coefficients:
# (Intercept)           x1  
#    -0.15534      0.06922

As you can see, the lm function has returned a valid output without any error messages.

 

Video, Further Resources & Summary

Do you want to learn more about errors? Then I recommend watching the following video of my YouTube channel. In the video, I’m explaining the R programming code of this article:

 

The YouTube video will be added soon.

 

Furthermore, you might want to read the other posts on this homepage.

 

At this point you should know how to handle the “Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) : 0 (non-NA) cases” in R. If you have any additional comments and/or questions, let me know in the comments section.

 

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