2nd order auto regression using lm function

I’m trying to do a 2nd order auto regression with lm, and I keep getting an error saying the ‘variable lengths differ (found for ‘infl2.lag2’) here is my code:

usmac <- read_excel("us_macro_quarterly.xlsx")

Fix format of date

usmac$...1 <- as.yearqtr(usmac$...1, format = "%Y:0%q")

Relabel column names in dataframe

colnames(usmac) <- c("Date", "GDPC96", "JAPAN_IP", "PCECTPI",
"GS10", "GS1", "TB3MS", "UNRATE", "EXUSUK", "CPIAUCSL")

PCECTPI series as xts object

PCEP <- xts(usmac$PCECTPI, usmac$Date)["1963::2013"]

Inflation series (annualized) as xts object

infl <- xts(400 * log(PCEP/lag(PCEP)))
infl2 <-xts(400*log(PCEP/lag(PCEP,2)))

T <- length(infl2)
infl2.t <-as.numeric(infl2[2:T])
infl2.lag <- as.numeric(infl2[1:T-1])
infl2.lag2 <- as.numeric(infl2[1:(T-2)])
ar2 <- lm(infl2.t ~ infl2.lag + infl2.lag2)
parameters(ar2, vcov= "HC1")

The first series has T-1 observations and the second had T-2 observations.

Thx for replying so fast, i thought i needed those 2 observations for the 2nd order auto regression using lm

You do. You are going to be "missing" one observation in infl2.lag and two observations in infl2.lag2. So infl2.t should be 3:T, then 2:T-1, then 1:T-2.

Oh ok, that makes so much more sense. Thank you very much

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.