0
votes

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
I think the error message says that you do not have any data to fit the model on (or all are NA). Could you post a reproducible example with the data you use?Dominik S. Meier
is any of your columns all NAs? can do dput(head(Exchange.rate.change,5)) and dput(head(Inflation.difference,5)) and paste the output as part of your post?StupidWolf
dput(head(Exchange.rate.change,5)) - c(-3.187, 1.4781, 0.0395, -1.6416, -20.6329) dput(head(Inflation.difference,5)) - c(-1.2351, 1.5508, 1.0371, 0.0461, -18.4126) So they don't seem to be NA.Sam Stewart
Here is a part of data I am using : 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+000Sam Stewart
In order to make your question reproducible and thus answerable, we need minimal, self-contained code and data so that we are able to reproduce your problem on our machine. Please follow these simple guidelines: stackoverflow.com/questions/5963269/…jay.sf

1 Answers

1
votes

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)