线性模型函数lm()错误:外部函数调用(arg 1)中的NA/NaN/Inf [英] Linear model function lm() error: NA/NaN/Inf in foreign function call (arg 1)

查看:395
本文介绍了线性模型函数lm()错误:外部函数调用(arg 1)中的NA/NaN/Inf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有data.frame a

Say I have data.frame a

我使用

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

col2具有一些NA值,col3col4具有小于1的值.

col2 has some NA values, col3 and col4 have values less than 1.

我不断得到

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
NA/NaN/Inf in foreign function call (arg 1)

我已经检查了邮件列表,看来这是由于col2中的NA引起的,但是我尝试使用na.action=na.exclude/omit/pass,但它们似乎都不起作用.我已经在前10个条目中再次测试了lm,绝对不是因为NA.此警告的问题是,每个Google搜索结果似乎都指向NA.

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.

我误解了错误还是我错误地使用了lm?

Did I misinterpret the error or am I using lm wrongly?

数据位于 kaggle .我正在使用线性回归对 MonthlyIncome 数据进行建模(因为我无法使用某个特定的glm系列).我已经创建了自己的变量以供使用,但是如果您尝试使用已经存在的变量对 MonthlyIncome 进行建模,则会失败.

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.

推荐答案

我知道此线程确实很旧,但是答案似乎并不完整,我遇到了同样的问题.

I know this thread is really old, but the answers don't seem complete, and I just ran into the same problem.

我遇到的问题是因为NA列还具有NaN和Inf.删除那些,然后再试一次.具体来说:

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

希望能帮助您解决18个月的问题!

Hope that helps your 18 month old question!

这篇关于线性模型函数lm()错误:外部函数调用(arg 1)中的NA/NaN/Inf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆