lm.fit(x,y,offset = offset,singular.ok = singular.ok,...)中的错误:0(非NA)情况下调用:lm->适应 [英] Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases Calls: lm -> lm.fit

查看:270
本文介绍了lm.fit(x,y,offset = offset,singular.ok = singular.ok,...)中的错误:0(非NA)情况下调用:lm->适应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行R应用程序,但是收到以下 first 错误:

I am trying to run an R application, but I receive the following first error :

lm.fit(x,y,offset = offset,singular.ok = singular.ok,...)中的错误:0(非NA)案例通话:lm-> lm.fit

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases Calls: lm -> lm.fit

产生错误的代码是:

pppb = lm(Exchange.rate.change ~ Inflation.difference)

我是R的新手,我真的很难发现错误,因此对它的帮助非常感谢.这是一个最小的数据集:

I am new to R and is really hard for me to find the mistake, so any help it is really appreciated. This is a minimal data set:

Country Inflation.difference    Exchange.rate.change    Developed
Australia   -1.235100000e+000   -3.187000000e+000   1.000000000e+000
Austria 1.550800000e+000    1.478100000e+000    1.000000000e+000
Belgium 1.037100000e+000    3.950000000e-002    1.000000000e+000
Canada  4.610000000e-002    -1.641600000e+000   1.000000000e+000
Chile   -1.841260000e+001   -2.063290000e+001   0.000000000e+000

这是重现该错误所需的最低限度可运行代码:

This is the minimal runnable code necessary to reproduce the error :

ppp = read.table("test.dat",sep="\t", header=TRUE, row.names=NULL)
attach(ppp)
Developed[Developed==1] = "Developed"
newppp = ppp[ppp$Country!="Brazil",]
attach(newppp)
developed = newppp[Developed==1,]
attach(developed)
pppb = lm(Exchange.rate.change ~ Inflation.difference)

这是我收到的第二错误:

以下对象被 .GlobalEnv屏蔽:发达ppp屏蔽了以下对象:国家,发达地区,汇率变动,通货膨胀率差异以下对象被 .GlobalEnv掩盖:发达newppp屏蔽了以下对象:国家,发达地区,汇率变动,通货膨胀率差异ppp屏蔽了以下对象:国家,发达地区,汇率变化,通货膨胀差异
lm.fit(x,y,offset = offset,singular.ok = singular.ok,...)中的错误:0(非NA)案例通话:lm-> lm.fit执行停止`

The following object is masked by .GlobalEnv: Developed The following objects are masked from ppp: Country, Developed, Exchange.rate.change, Inflation.difference The following object is masked by .GlobalEnv: Developed The following objects are masked from newppp: Country, Developed, Exchange.rate.change, Inflation.difference The following objects are masked from ppp: Country, Developed, Exchange.rate.change, Inflation.difference
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases Calls: lm -> lm.fit Execution halted `

推荐答案

tl; dr (如果您这样做的话)

tl;dr if you just do

lm(Exchange.rate.change ~ Inflation.difference, data =ppp,
      subset=Developed==1)

在读取数据后(没有其他任何代码),它似乎可以正常工作.

immediately after reading the data (without any of the other code) it seems to work fine.

或者,如果您想对数据进行子集化

Or, if you want to subset the data you could do

developed <- ppp[ppp$Developed==1, ] 
## or developed <- subset(ppp, Developed == 1)
lm(Exchange.rate.change ~ Inflation.difference, data = developed)


attach(ppp)

通常不推荐

attach();而是使用 data = 参数

Developed[Developed==1] = "Developed"

这很奇怪(我认为这不会影响以后的结果);它将数字矢量转换为字符(因此内容为已开发"或"0")

This is weird (and doesn't affect the later results, I think); it converts the numeric vector to character (so the contents are either "Developed" or "0")

newppp = ppp[ppp$Country!="Brazil",]

巴西实际上不在您显示给我们的数据集中,因此在这种特殊情况下它什么也没做

Brazil isn't actually in the data set you showed us, so this doesn't do anything in this particular case

attach(newppp)

attach()多次将使事情更加混乱(这是收到警告的原因)

attach()ing multiple times will make things even more confusing (this is the source of the warnings you get)

developed = newppp[Developed==1,]

这是出问题的地方.您工作区中 Developed 的当前副本是

This is where things go wrong. The current copy of Developed in your workspace is

[1] "Developed" "Developed" "Developed" "Developed" "0"        

因为您之前的声明.这些值都不等于1,因此 developed 现在为空(零行).

because of your previous statement. None of these values is equal to 1, so developed is now empty (zero rows).

attach(developed)
pppb = lm(Exchange.rate.change ~ Inflation.difference)

这篇关于lm.fit(x,y,offset = offset,singular.ok = singular.ok,...)中的错误:0(非NA)情况下调用:lm-&gt;适应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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