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

后端 未结 10 1177
天涯浪人
天涯浪人 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:27

    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.

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

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

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

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

    col2 <- as.integer(col2)
    
    0 讨论(0)
  • 2020-12-03 10:34

    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).

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

    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!

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

    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.

    0 讨论(0)
提交回复
热议问题