# 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 |

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

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 |

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 |

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.

- Help – Error in if (NA) { : missing value where TRUE/FALSE needed
- Dealing with Warnings & Errors in R (Example Codes)
- The R Programming Language

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.

## 10 Comments. Leave new

how to handle when I still get this error despite having independent variables which have no NAs whatsoever?

Hi Rakshathi,

Could you illustrate how your data looks like and share your code?

Regards

Joachim

I want to make a linear reg with y, x1 and x2

with m1 <- lm(data=data1, y ~ x1, na.action = "na.exclude") I get a result

with m2 <- lm(data=data1, y ~ x2, na.action = "na.exclude") I get a result

with m3 <- lm(data=data1, y ~ x1, x2, na.action = "na.exclude") I get Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) :

0 (non-NA) cases

What happens here?

both x1 and x2 contain some NA, but only 20 of 140 observations

Hey,

It seems like the specification of your formula is incorrect. You would have to use a + sign between x1 and x2 in your third example:

Does this fix your error?

Regards,

Joachim

Hello!

I get the same error when trying to perform a two-ways anova.

My script:

res.aov <- anova_test(

data = Apportal, dv = score, wid = record_id,

within = c(time, visit))

Error message: Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) :

0 (non-NA) cases

Output: get_anova_table(res.aov)

ANOVA Table (type III tests)

Effect DFn DFd F p p<.05 ges

1 visit 1 29 1.68e-30 1 1.7e-33

I have NO missing values in any of my variables.

Whay am I doing wrong?

Thank you,

Bea

Hey Bea,

This is difficult to evaluate without seeing your data. Could you share (a subset of) the data here, or send it to joachim@statisticsglobe.com ?

Regards,

Joachim

Hi! I actually have the same problem as Bea, only with a Repeated Measures ANOVA. I have no missing values in my data

res.aov <- anova_test(

data = dat, dv = e.coli, wid = id,

within = c(site, time)

)

get_anova_table(res.aov)

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, …) :

0 (non-NA) cases

and I don't even get an output…

id site time weight e.coli

1 p t1 0,2297 96968256,53

2 ww t1 0,2354 4575876,75

3 p t1 0,1959 35296621,32

I would appreciate any help.

Thanks! Lisa

Hey Lisa,

Apologies for the late reply, I just got back from vacation. Do you still need help with this question?

Regards,

Joachim

I’m having the exact same issue as Lisa, no NAs in my data frame but this error appears when I try to run a repeated measures ANOVA in R using the same method Lisa mentioned.

Hey Jess,

Unfortunately, I have no experience with this error message when using ANOVA myself. However, I found this thread on Stack Overflow, which seems to explain the problem.

I’m quoting from the answer of StupidWolf in this thread:

“For repeated measure anova, you need complete observations for each time point, before and after treatment”Is this maybe the reason for your error?

Regards,

Joachim