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 NA
s 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 NA
s. 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.
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.
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.
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.
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.
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With