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

Pk.yd picture Pk.yd · Dec 7, 2011 · Viewed 97.3k times · Source

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.

Answer

slammaster picture slammaster · Sep 5, 2013

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!