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

后端 未结 10 1178
天涯浪人
天涯浪人 2020-12-03 09:49

Say I have data.frame a

I use

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

col2 has some <

相关标签:
10条回答
  • 2020-12-03 10:46

    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)
    
    0 讨论(0)
  • 2020-12-03 10:48

    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.

    0 讨论(0)
  • 2020-12-03 10:53

    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!

    0 讨论(0)
  • 2020-12-03 10:53

    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))
    
    0 讨论(0)
提交回复
热议问题