lag
does not do what you think it does. It converts the object to ts
and changes the index, not the actual vector values. The sign of the value for k
is also opposite of what you'd expect.
set.seed(21)
z <- rnorm(6)
as.ts(z)
# Time Series:
# Start = 1
# End = 8
# Frequency = 1
# [1] 0.7930132 0.5222513 1.7462222 -1.2713361 2.1973895 0.4331308
lag(as.ts(z),1)
# Time Series:
# Start = 0
# End = 7
# Frequency = 1
# [1] 0.7930132 0.5222513 1.7462222 -1.2713361 2.1973895 0.4331308
xts::lag.xts
provides the behavior you expect:
library(xts)
x <- .xts(z, 1:6)
xData <- merge(x=x, x1=lag(x,1))
fit3 <- lm(x ~ x1, data=xData)
summary(fit3)