I have a dataset/dataframe in which I have calculated the daily log returns of five thousand companies and these companies are as column as well. I want carry out ADF test on this dataframe. I have found how to estimate ADF test on vector but could not find how to calculate it on dataframe or matrix structure. Additionally how can I leave out the date column when estimating ADF test on the companies.enter image description here

The picture illustrates some portion of my dataset. The code I ran and error I received are as follows

adf.test(logs, alternative = c("stationary", "explosive"),
     k = trunc((length(1)-1)^(1/3)))

Error in adf.test(logs, alternative = c("stationary", "explosive"), k = trunc((length(1) -  :  x is not a vector or univariate time series
  • $\begingroup$ I did not downvote your question but I can understand why somebody did: Please show your code and pinpoint exactly to the parts where you encounter problems. $\endgroup$ – vonjd Dec 30 '15 at 10:54
  • 1
    $\begingroup$ I hope this additions to the question helps to understand what I'm trying to estimate. $\endgroup$ – Aquarius Dec 30 '15 at 11:07

It is not clear from the post if you are querying for the mechanics/code for looping over the series or the appropriate critical values. I here make a comment on the latter.

One of the main pitfalls when testing multiple hypotheses is the fact that a certain percentage would fail under the null (as this xkcd strip nicely illustrates https://xkcd.com/882/ ).

If you run a DF test on 10,000 stocks you would expect 500 to show up as mean reverting at 5% confidence, even if they are all independent random walks. One needs to account for this feature by lowering the confidence level.

If your tests are independent, then you could have a Bonferroni-type adjustment of the confidence level to incorporate myltiple testing (https://en.m.wikipedia.org/wiki/Bonferroni_correction). But stocks are not independent, and therefore your t-statistics are not independent neither. To account for their correlation I would use bootstrapped critical values on the vector of t-stats.


There a to ways that you can performe the ADF test to a data frame, first write a loop for applying the test to all the columns or use the apply function to your data. For leaving out the first column just create an other data frame like this: da=yourDataName[,-1]. the code for the ADF would be something like apply(da,2,adfTest,lags=0,type="c"). The 2 is saying that the function adfTest should be apply to the columns, the adfTest is from the package fUnitRoots, lags=0 so it does not perform the test lagging the series and type="c" so it includes a constant. I don't like the test from timeSeries package because it will lag the series automatically so you will get "always" a stationary series.

  • $\begingroup$ I ran the test according to the code you specifiied but I recieved error Error in res.sum$coefficients[coefNum, 1] : subscript out of bounds $\endgroup$ – Aquarius Jan 4 '16 at 11:46
  • $\begingroup$ that's odd because that error means that you are trying to apply the function to a column that does not exists. The code works just fine in mi computer but i am just using a data frame of 5 columns. Sorry but i think I can't help you with that problem $\endgroup$ – Alejandro Andrade Jan 4 '16 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.