Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linear model function lm() error: NA/NaN/Inf in foreign function call (arg 1)

Tags:

r

nan

lm

kaggle

Say I have data.frame a

I use

m.fit <- lm(col2 ~ col3 * col4, na.action = na.exclude) 

col2 has some NA values, col3 and col4 have values less than 1.

I keep getting

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :  NA/NaN/Inf in foreign function call (arg 1) 

I've checked the mailing list and it appears that it is because of the NAs in col2 but I tried using na.action=na.exclude/omit/pass but none of them seem to work. I've tested lm again on first 10 entries, definitely not because of the NAs. Problem with this warning is every google results seem to be pointing at NA.

Did I misinterpret the error or am I using lm wrongly?

Data is at kaggle. I'm modelling MonthlyIncome data using linear regression (as I couldn't get a certain glm family to work). I've created my own variables to use but if you try to model MonthlyIncome with variables already present it fails.

like image 982
Pk.yd Avatar asked Dec 07 '11 13:12

Pk.yd


People also ask

What does error in LM fit X Y offset offset singular OK singular OK na NaN inf in Y mean?

Example 2: Wrong Target Variable in Linear Regression Model Another reason why the error message “Error in lm. fit(x, y, offset = offset, singular. ok = singular. ok, …) : NA/NaN/Inf in 'x'” occurs is that the target and predictor variables in the lm() function are not specified properly.

What does error in LM fit mean in R?

This error occurs when you attempt to use the lm() function to fit a linear regression model in R, but either the predictor or response variable contains NaN or Inf values.

What does LM () do in R?

The lm() function is used to fit linear models to data frames in the R Language. It can be used to carry out regression, single stratum analysis of variance, and analysis of covariance to predict the value corresponding to data that is not in the data frame.

What is the general format of the LM () function?

The lm() function Note that the formula argument follows a specific format. For simple linear regression, this is “YVAR ~ XVAR” where YVAR is the dependent, or predicted, variable and XVAR is the independent, or predictor, variable.


2 Answers

I know this thread is really old, but the answers don't seem complete, and I just ran into the same problem.

The problem I was having was because the NA columns also had NaN and Inf. Remove those and try it again. Specifically:

col2[which(is.nan(col2))] = NA col2[which(col2==Inf)] = NA 

Hope that helps your 18 month old question!

like image 73
slammaster Avatar answered Sep 21 '22 09:09

slammaster


You should have a read the book A Beginner’s Guide to R for a complete explanation on this. Specifically, it mentions the following error:

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok,...): NA/NaN/Inf in foreign function call (arg 4)

The solution is to add a small constant value to the Intensity data, for example, 1. Note that there is an on-going discussion in the statistical community concerning adding a small value. Be that as it may, you cannot use the log of zero when doing calculations in R.

like image 33
algarecu Avatar answered Sep 21 '22 09:09

algarecu