Pk.yd
Pk.yd

Reputation: 311

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

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.

Upvotes: 28

Views: 116633

Answers (11)

Eldorado
Eldorado

Reputation: 87

This is what helped in my case. I parsed the data that already exclude NAs and INFs.

lm(y ~ x, data = data[(y != Inf & is.na(y) == FALSE)])

Upvotes: 0

Andrew
Andrew

Reputation: 21

I solved this type of problem by resetting my options. options(na.action="na.exclude") or options(na.action="na.omit")

I checked my settings and had previously changed the option to "na.pass" which didn't drop my y observations with NAs (where y~x).

Upvotes: 2

Adrian Cadena
Adrian Cadena

Reputation: 11

Make sure you don't have any 0 in your dependent variable.

Upvotes: -1

Mihir Gada
Mihir Gada

Reputation: 1

Another thing to watch out for is using functions like log() or sin() make your x's and y's inf. eg. log 0 = 0 or sin(pi) = 0.

Upvotes: 0

moodymudskipper
moodymudskipper

Reputation: 47300

I got this error when I inverted the arguments when calling reformulate and use the formula in my lm call without checking, so I had the wrong predictor and response variable.

Upvotes: 0

Scott Kaiser
Scott Kaiser

Reputation: 317

I encountered this error when my equivalent of col2 was an integer64 rather than an integer and when using natural and polynomial splines, splines::bs and splines:ns for example:

m.fit <- lm(col1 ~ ns(col2))
m.fit <- lm(col1 ~ bs(col2, degree = 3))

Converting to a standard integer worked for me:

m.fit <- lm(col1 ~ ns(as.integer(col2)))
m.fit <- lm(col1 ~ bs(as.integer(col2), degree = 3))

Upvotes: 0

arredond
arredond

Reputation: 580

I just suffered another possibility, after all posible na.omit and na.exclude checks.

I was taking something like:

lm(log(x) ~ log(y), data = ...)

Without noticing that, for some values in my dataset, x or y could be zero: log(0) = -Inf

So just another thing to watch out for!

Upvotes: 8

algarecu
algarecu

Reputation: 416

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.

Upvotes: 10

d2a2d
d2a2d

Reputation: 1196

I just encountered the same problem. get the finite elements using

finiteElements = which(is.finite(col3*col4))
finiteData = data[finiteElements,]
lm(col2~col3*col4,na.action=na.exclude,data=finiteData)

Upvotes: 0

slammaster
slammaster

Reputation: 855

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!

Upvotes: 39

user2475878
user2475878

Reputation: 19

Try changing the type of col2 (and all other variables)

col2 <- as.integer(col2)

Upvotes: 1

Related Questions