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")