问题
I am trying to run an R application, but I receive the following first error :
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 0 (non-NA) cases Calls: lm -> lm.fit
The code which generates the error is :
pppb = lm(Exchange.rate.change ~ Inflation.difference)
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)
This is the second error I get :
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 `
回答1:
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()
is generally not recommended; instead, use the data=
argument
Developed[Developed==1] = "Developed"
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()
ing multiple times will make things even more confusing (this is the source of the warnings you get)
developed = newppp[Developed==1,]
This is where things go wrong. The current copy of Developed
in your workspace is
[1] "Developed" "Developed" "Developed" "Developed" "0"
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)
来源:https://stackoverflow.com/questions/61963964/error-in-lm-fitx-y-offset-offset-singular-ok-singular-ok-0-non