Dear all, I am having difficulties using the seemingly-simple function lag. I have a dataframe with several weather variables (maxitemp, windspeed, rainfall etc), and the response variable (admissions). The dataset is fairly large (1530 observations). I simply want to model the response against a lag of a couple of the explanatory variables, say maxitemp and rainfall. I would like to look at lagtimes of 4 days, i.e. 4 observations. My dataframe is called allengland. I thought I would be able to do something like: model1 <- lm(admissions~lag(maxitemp,4),data=allengland) but when I compare this to a lag of 0 I have exactly the same output, so I am clearly doing something wrong. Could someone please point me in the right direction? The example in the help for lag is not very detailed for what I need to do. Thanks in advance, Robin Williams Met Office summer intern - Health Forecasting robin.williams@metoffice.gov.uk [[alternative HTML version deleted]]
You want to apply lag to time series objects and its likely that you don't have that. The dyn (and also dynlm) packages have facilities for peforming lagged regressions on various time series objects. If you don't want to use time series objects you can do the lag yourself so for vector x this will move everything forward one c(NA, head(x, -1)) dropping the last element and this will move everything backward one c(x[-1], NA) dropping the first element. On Mon, Aug 18, 2008 at 9:40 AM, Williams, Robin <robin.williams at metoffice.gov.uk> wrote:> Dear all, > I am having difficulties using the seemingly-simple function lag. > I have a dataframe with several weather variables (maxitemp, > windspeed, rainfall etc), and the response variable (admissions). The > dataset is fairly large (1530 observations). I simply want to model the > response against a lag of a couple of the explanatory variables, say > maxitemp and rainfall. I would like to look at lagtimes of 4 days, i.e. > 4 observations. My dataframe is called allengland. > I thought I would be able to do something like: > model1 <- lm(admissions~lag(maxitemp,4),data=allengland) > but when I compare this to a lag of 0 I have exactly the same output, > so I am clearly doing something wrong. > Could someone please point me in the right direction? The example in > the help for lag is not very detailed for what I need to do. > Thanks in advance, > > Robin Williams > Met Office summer intern - Health Forecasting > robin.williams at metoffice.gov.uk > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Robin, You can use Lag (upper case "L") from package Hmisc. Nestor Arguea On Aug 18, 2008, at 8:40 AM, Williams, Robin wrote:> Dear all, > I am having difficulties using the seemingly-simple function lag. > I have a dataframe with several weather variables (maxitemp, > windspeed, rainfall etc), and the response variable (admissions). The > dataset is fairly large (1530 observations). I simply want to model > the > response against a lag of a couple of the explanatory variables, say > maxitemp and rainfall. I would like to look at lagtimes of 4 days, > i.e. > 4 observations. My dataframe is called allengland. > I thought I would be able to do something like: > model1 <- lm(admissions~lag(maxitemp,4),data=allengland) > but when I compare this to a lag of 0 I have exactly the same output, > so I am clearly doing something wrong. > Could someone please point me in the right direction? The example in > the help for lag is not very detailed for what I need to do. > Thanks in advance, > > Robin Williams > Met Office summer intern - Health Forecasting > robin.williams at metoffice.gov.uk > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. > >------------------------------------------------------- Nestor M. Arguea, Chair Associate Professor, Economics Department of Marketing and Economics University of West Florida Pensacola, FL 32514 Phone: 850-474-3071 Fax: 850-474-3069 e-mail: narguea at uwf.edu