Dear R-users, I've been using R for a while and I am very satisfied! Unfortunately, I still have not figured out an efficient and general way to construct and use lags of time series, especially when I need to work with different packages. Let me give an example. I have two time series x and y and I want to estimate a variaty of distributed lags models and run different tests (autocorrelation, etc). It is obvious that I need to be able to lag x and y in a flexible way. So far, my temporary solution was to construct the lags manually (x1,..,xn and y1,..,yn) in a spreadsheet and import it to R, which is not very satisfactory because it does not allow for much flexibility. Is there a straighforward command which allows me to easily construct a lag when required and which allows me to, for example, use the lm() command to fit a dynamic model and the bgtest() command to perform the breusch-godfrey test on the same model? Is it adviseable to use time series objects which consist of many time series (like a dataframe) or is it better to have it contain only one time series? I would be grateful for any hints and links. Thx! Christian
What is the best way to lag a time series?
4 messages · Christian Schoder, Liviu Andronic, Patrick Burns +1 more
On Sun, Dec 26, 2010 at 8:49 AM, Christian Schoder
<schoc152 at newschool.edu> wrote:
Dear R-users, I've been using R for a while and I am very satisfied! Unfortunately, I still have not figured out an efficient and general way to construct and use lags of time series, especially when I need to work with different packages. Let me give an example. I have two time series x and y and I want to estimate a variaty of distributed lags models and run different tests (autocorrelation, etc). It is obvious that I need to be able to lag x and y in a flexible way. So far, my temporary solution was to construct the lags manually (x1,..,xn and y1,..,yn) in a spreadsheet and import it to R, which is not very satisfactory because it does not allow for much flexibility. Is there a straighforward command which allows me to easily construct a lag
Perhaps ?diff. Liviu
when required and which allows me to, for example, use the lm() command to fit a dynamic model and the bgtest() command to perform the breusch-godfrey test on the same model? Is it adviseable to use time series objects which consist of many time series (like a dataframe) or is it better to have it contain only one time series? I would be grateful for any hints and links. Thx! Christian
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Do you know how to read? http://www.alienetworks.com/srtest.cfm http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader Do you know how to write? http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail
First off, there are data manipulation techniques that will beat doing it in a spreadsheet. For example: head(x, -1) is lagged 1 relative to tail(x, -1) But I think you are really looking for 'Lag' in the 'quantmod' package.
On 26/12/2010 07:49, Christian Schoder wrote:
Dear R-users, I've been using R for a while and I am very satisfied! Unfortunately, I still have not figured out an efficient and general way to construct and use lags of time series, especially when I need to work with different packages. Let me give an example. I have two time series x and y and I want to estimate a variaty of distributed lags models and run different tests (autocorrelation, etc). It is obvious that I need to be able to lag x and y in a flexible way. So far, my temporary solution was to construct the lags manually (x1,..,xn and y1,..,yn) in a spreadsheet and import it to R, which is not very satisfactory because it does not allow for much flexibility. Is there a straighforward command which allows me to easily construct a lag when required and which allows me to, for example, use the lm() command to fit a dynamic model and the bgtest() command to perform the breusch-godfrey test on the same model? Is it adviseable to use time series objects which consist of many time series (like a dataframe) or is it better to have it contain only one time series? I would be grateful for any hints and links. Thx! Christian
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Patrick Burns pburns at pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno')
The correct answer to "How to lag..?" is almost certainly, "Don't." The functionality of numerous time series packages and functions take care of this automatically for you (using suitable data structures, probably). Rather than trying to reinvent wheels, it might be wiser to consult the Time Series Task View on Cran to see what's there first. Incidentally, my limited understanding is that modern time series methods tend to use more appropriately specified covariance structures (e.g. arima models) rather than the lagged models of e.g. classical econometrics. But on this, I would happily stand correction. -- Cheers, Bert
On Sun, Dec 26, 2010 at 12:21 AM, Liviu Andronic <landronimirc at gmail.com> wrote:
On Sun, Dec 26, 2010 at 8:49 AM, Christian Schoder <schoc152 at newschool.edu> wrote:
Dear R-users, I've been using R for a while and I am very satisfied! Unfortunately, I still have not figured out an efficient and general way to construct and use lags of time series, especially when I need to work with different packages. Let me give an example. I have two time series x and y and I want to estimate a variaty of distributed lags models and run different tests (autocorrelation, etc). It is obvious that I need to be able to lag x and y in a flexible way. So far, my temporary solution was to construct the lags manually (x1,..,xn and y1,..,yn) in a spreadsheet and import it to R, which is not very satisfactory because it does not allow for much flexibility. Is there a straighforward command which allows me to easily construct a lag
Perhaps ?diff. Liviu
when required and which allows me to, for example, use the lm() command to fit a dynamic model and the bgtest() command to perform the breusch-godfrey test on the same model? Is it adviseable to use time series objects which consist of many time series (like a dataframe) or is it better to have it contain only one time series? I would be grateful for any hints and links. Thx! Christian
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
-- Do you know how to read? http://www.alienetworks.com/srtest.cfm http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader Do you know how to write? http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Bert Gunter Genentech Nonclinical Biostatistics