Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MislavSag opened this issue Jun 7, 2021 · 12 comments

Comments

@MislavSag
Copy link

I am getting ollowing error:

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

It seems the error appears if data in lm function contains only NA. And that happens when, for example, event.window is exactly equal to number of non-NA elements before event date.

@ajaynshah
Copy link
Member

ajaynshah commented Jun 7, 2021 via email

@MislavSag
Copy link
Author

Here is the sample code:

returns_sample <- structure(c(Inf, -0.012987012987013, 0, -0.0105263684210527, 
                              0.0106383516296995, 0.001579, 0.00683126606246631, -0.0229645615866387, 
                              0.00534193404604344, 0.0180658873538788, 0.000521920668058451, 
                              -0.00625983307250921, 0.0188976912807188, -0.00206074188562611, 
                              -0.0196180165401127, 0.0684571353343866, 0.0418924572748911, 
                              0.0331126316514962, 0.00915755494505488, -0.0331216863374915, 
                              -0.00234626946721117, 0.00329256820319834, 0.010314064697609, 
                              -0.0464037144502883, 0.0180049157180009, 0.0482791586998086, 
                              -0.00775193798449603, 0.00643382352941169, 0.000913242009132453, 
                              -0.00547449817518253, -0.0137614226496066, -0.00232553488372089, 
                              -0.0093240555093681, 0.0103528941176472, -0.0186306017061296, 
                              0.039392501186521, -0.0319634246575342, -0.0320755173549284, 
                              -0.013157894736842, 0.037037037037037, 0.0361904761904763, 0.0261948529411764, 
                              -0.0192566054635021, -0.0196347488584474, 0.0218911048854729, 
                              0.021877802102197, 0.00624438001784111, 0.0243795223572483, -4.32713093134751e-08, 
                              -0.0380787537862397, 0.00629779577148004, -0.0201162270898525, 
                              0.0287408759124088, -0.0186252771618626, 0.0546769091730683, 
                              -0.0347043701799485, -0.00443852640923215, -0.0352206865804725, 
                              0.0235674676524953, -0.0261851015801353, 0.0176170607324988, 
                              -0.00911161731207288, 0.0101149425287357, -0.0213928083750569, 
                              0.0437209302325583, 0.0204991087344026, -0.0157205240174673, 
                              -0.0585625554569654, 0.0367577756833177, 0.01, -0.0063006300630063, 
                              -0.00769927536231874, 0, 0.00958466453674123, -0.0271247739602171, 
                              0.0125464684014869, 0.0247820100963745, 0.0223914017017466, 0.0494962768287341, 
                              0.00667779632721199, -0.0178275290215588, 0.0295483326298016, 
                              -0.0127101271012711, 0.0191029900332227, -0.0358598207008964, 
                              0.00549450549450547, -0.00210172341319881, -0.0016849199663016, 
                              0.105063291139241, -0.00572737686139757, 0.000768049155145833, 
                              0.0118956254796625, -0.00644671975730005, -0.016412213740458, 
                              0.000388048117966777, -0.00581846392552376, 0.019508388607101, 
                              -0.00114810562571743, -0.00383141762452111, -0.0111538461538461, 
                              -0.0175029171528588, 0.010688836104513, -0.0172346259302781, 
                              0.00518134715025909, -0.00911974623314826, 0.000400160064025767, 
                              -0.00800000000000001, 0.00766129032258056), .Dim = c(108L, 1L
                              ), .Dimnames = list(NULL, "ATC"), index = structure(c(18662, 
                                                                                    18663, 18666, 18667, 18668, 18669, 18670, 18674, 18675, 18676, 
                                                                                    18677, 18680, 18681, 18682, 18683, 18684, 18687, 18688, 18689, 
                                                                                    18690, 18691, 18694, 18695, 18696, 18697, 18698, 18701, 18702, 
                                                                                    18703, 18704, 18705, 18708, 18709, 18710, 18711, 18712, 18715, 
                                                                                    18716, 18717, 18718, 18722, 18723, 18724, 18725, 18726, 18729, 
                                                                                    18730, 18731, 18732, 18733, 18736, 18737, 18738, 18739, 18740, 
                                                                                    18743, 18744, 18745, 18746, 18747, 18750, 18751, 18752, 18753, 
                                                                                    18754, 18757, 18758, 18759, 18760, 18761, 18764, 18765, 18766, 
                                                                                    18767, 18768, 18771, 18772, 18773, 18774, 18775, 18779, 18780, 
                                                                                    18781, 18782, 18785, 18786, 18787, 18788, 18789, 18792, 18793, 
                                                                                    18794, 18795, 18796, 18799, 18800, 18801, 18802, 18803, 18806, 
                                                                                    18807, 18808, 18809, 18810, 18814, 18815, 18816, 18817), class = "Date"), class = "zoo")

events_sample <- structure(list(when = structure(c(18663, 18690, 18751, 18751), class = "Date"), 
                                name = c("ATC", "ATC", "ATC", "ATC")), class = "data.frame", row.names = c(NA, 
                                                                                                           -4L))

es <- eventstudy(firm.returns = returns_sample,
                 event.list = events_sample,
                 event.window = event_horizon,
                 type = "marketModel",
                 to.remap = FALSE,
                 inference = FALSE,
                 model.args = list(
                   market.returns = as.zoo(market_returns)
                 )
)

@vimsaa
Copy link
Contributor

vimsaa commented Jul 12, 2021

The code does not run for me. Please define event_horizon and provide a MWE to replicate the error.

@MislavSag
Copy link
Author

@vimsaa ,

Please try with this code, after defining event_sample:

library(quantmod)
market_returns <- quantmod::getSymbols("SPY")
market_returns <- SPY$SPY.Adjusted / lag(SPY$SPY.Adjusted) - 1
es <- eventstudy(firm.returns = returns_sample,
                 event.list = events_sample,
                 event.window = 30,
                 type = "marketModel",
                 to.remap = FALSE,
                 inference = FALSE,
                 model.args = list(
                   market.returns = as.zoo(market_returns)
                 )
)

@vimsaa
Copy link
Contributor

vimsaa commented Jul 14, 2021

I am unable to replicate the error you show above. I used the code you shared (appended below) and I get the following error:

"Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
NA/NaN/Inf in 'y'"

This is because lm.fit does not like NA/Inf in the data. In the next rollout of the package, we will internally drop entries that contain "Inf". A single line of code before calling eventstudy() currently fixes this problem:

returns_sample <- returns_sample[is.finite(returns_sample$ATC),drop=FALSE] ## <-- This is all is needed.

Hope this helps,
Vimal

library(eventstudies)
library(quantmod)

returns_sample <- structure(c(Inf, -0.012987012987013, 0, -0.0105263684210527,
0.0106383516296995, 0.001579, 0.00683126606246631, -0.0229645615866387,
0.00534193404604344, 0.0180658873538788, 0.000521920668058451,
-0.00625983307250921, 0.0188976912807188, -0.00206074188562611,
-0.0196180165401127, 0.0684571353343866, 0.0418924572748911,
0.0331126316514962, 0.00915755494505488, -0.0331216863374915,
-0.00234626946721117, 0.00329256820319834, 0.010314064697609,
-0.0464037144502883, 0.0180049157180009, 0.0482791586998086,
-0.00775193798449603, 0.00643382352941169, 0.000913242009132453,
-0.00547449817518253, -0.0137614226496066, -0.00232553488372089,
-0.0093240555093681, 0.0103528941176472, -0.0186306017061296,
0.039392501186521, -0.0319634246575342, -0.0320755173549284,
-0.013157894736842, 0.037037037037037, 0.0361904761904763, 0.0261948529411764,
-0.0192566054635021, -0.0196347488584474, 0.0218911048854729,
0.021877802102197, 0.00624438001784111, 0.0243795223572483, -4.32713093134751e-08,
-0.0380787537862397, 0.00629779577148004, -0.0201162270898525,
0.0287408759124088, -0.0186252771618626, 0.0546769091730683,
-0.0347043701799485, -0.00443852640923215, -0.0352206865804725,
0.0235674676524953, -0.0261851015801353, 0.0176170607324988,
-0.00911161731207288, 0.0101149425287357, -0.0213928083750569,
0.0437209302325583, 0.0204991087344026, -0.0157205240174673,
-0.0585625554569654, 0.0367577756833177, 0.01, -0.0063006300630063,
-0.00769927536231874, 0, 0.00958466453674123, -0.0271247739602171,
0.0125464684014869, 0.0247820100963745, 0.0223914017017466, 0.0494962768287341,
0.00667779632721199, -0.0178275290215588, 0.0295483326298016,
-0.0127101271012711, 0.0191029900332227, -0.0358598207008964,
0.00549450549450547, -0.00210172341319881, -0.0016849199663016,
0.105063291139241, -0.00572737686139757, 0.000768049155145833,
0.0118956254796625, -0.00644671975730005, -0.016412213740458,
0.000388048117966777, -0.00581846392552376, 0.019508388607101,
-0.00114810562571743, -0.00383141762452111, -0.0111538461538461,
-0.0175029171528588, 0.010688836104513, -0.0172346259302781,
0.00518134715025909, -0.00911974623314826, 0.000400160064025767,
-0.00800000000000001, 0.00766129032258056), .Dim = c(108L, 1L
), .Dimnames = list(NULL, "ATC"), index = structure(c(18662,
18663, 18666, 18667, 18668, 18669, 18670, 18674, 18675, 18676,
18677, 18680, 18681, 18682, 18683, 18684, 18687, 18688, 18689,
18690, 18691, 18694, 18695, 18696, 18697, 18698, 18701, 18702,
18703, 18704, 18705, 18708, 18709, 18710, 18711, 18712, 18715,
18716, 18717, 18718, 18722, 18723, 18724, 18725, 18726, 18729,
18730, 18731, 18732, 18733, 18736, 18737, 18738, 18739, 18740,
18743, 18744, 18745, 18746, 18747, 18750, 18751, 18752, 18753,
18754, 18757, 18758, 18759, 18760, 18761, 18764, 18765, 18766,
18767, 18768, 18771, 18772, 18773, 18774, 18775, 18779, 18780,
18781, 18782, 18785, 18786, 18787, 18788, 18789, 18792, 18793,
18794, 18795, 18796, 18799, 18800, 18801, 18802, 18803, 18806,
18807, 18808, 18809, 18810, 18814, 18815, 18816, 18817), class = "Date"), class = "zoo")

events_sample <- structure(list(when = structure(c(18663, 18690, 18751, 18751), class = "Date"),
name = c("ATC", "ATC", "ATC", "ATC")), class = "data.frame", row.names = c(NA,
-4L))

market_returns <- quantmod::getSymbols("SPY")
market_returns <- SPY$SPY.Adjusted / lag(SPY$SPY.Adjusted) - 1
market_returns <- na.omit(market_returns)

es <- eventstudy(firm.returns = returns_sample ,
event.list = events_sample,
event.window = 30,
type = "marketModel",
to.remap = FALSE,
inference = FALSE,
model.args = list(
market.returns = as.zoo(market_returns)
)
)

@MislavSag
Copy link
Author

I was looking for Inf in View mode, but didn't see it.

Thanks for explanation.

@MislavSag
Copy link
Author

Just want to add that I get this error sometimes even if there are no Inf or NaN values in firm returns.

@vimsaa
Copy link
Contributor

vimsaa commented Jul 19, 2021

Thank you for getting back in touch! Please provide an MWE to replicate the error so that we can improve the package!
best, Vimal

@MislavSag
Copy link
Author

I figure out the problem. Market returns and firm returns were not aligned. For example, if market returns start in 1993 and firm returns span some data from 1992, it can return above error.

@ajaynshah
Copy link
Member

ajaynshah commented Jul 19, 2021 via email

@chiraganand
Copy link
Member

Is there some way in which we can do better error detection & handling?

We can detect Inf in data and inform the caller.

I figure out the problem. Market returns and firm returns were not aligned. For example, if market returns start in 1993 and firm returns span some data from 1992, it can return above error.

Ideally, the package function should take care of this scenario because it does an inner join (via merge.zoo/merge.xts) of both data sets before pushing the data to lm. A replicatable example should help to find the exact problem.

@snvv
Copy link

snvv commented Jun 23, 2022

Hello
I can reproduce the problem and I think one possible solution is to use lm(y ~ x, na.action=na.exclude)
Regards
snvv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants