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

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

Introduction of Example Data

data(iris)                        # Constructing example data
iris_new <- iris
iris_new$Species <- NA
head(iris_new)
#   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1          5.1         3.5          1.4         0.2      NA
# 2          4.9         3.0          1.4         0.2      NA
# 3          4.7         3.2          1.3         0.2      NA
# 4          4.6         3.1          1.5         0.2      NA
# 5          5.0         3.6          1.4         0.2      NA
# 6          5.4         3.9          1.7         0.4      NA

Example 1: Replicating the Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) : 0 (non-NA) cases

lm(Sepal.Length ~ ., iris_new)    # lm estimation leads to error
# Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
#   0 (non-NA) cases

Example 2: Solving the Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) : 0 (non-NA) cases

lm(Sepal.Length ~                 # Exclude only-NA column from model
     Sepal.Width +
     Petal.Length +
     Petal.Width,
   iris_new)
# Call:
# lm(formula = Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, 
#     data = iris_new)
# 
# Coefficients:
#  (Intercept)   Sepal.Width  Petal.Length   Petal.Width  
#       1.8560        0.6508        0.7091       -0.5565

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.
You need to agree with the terms to proceed

Menu
Top