Created
April 1, 2022 07:11
-
-
Save vankesteren/59223992e75417101901825473d72612 to your computer and use it in GitHub Desktop.
exponential decay with AR(1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# exponential decay estimation with AR model | |
N <- 30 | |
y <- numeric(N) | |
first_obs <- 2 # where do we start? | |
asymptote <- .5 # where do we go? | |
ar1_param <- .6 # how slow do we go there? (between -1 and 1) | |
sigma <- 0 | |
y[1] <- first_obs | |
for (n in 2:N) { | |
y[n] <- asymptote + ar1_param*(y[n-1] - asymptote) + rnorm(1, sd = sigma) | |
} | |
plot(y, type = "l") | |
# now estimate an AR(1) model | |
first_dummy <- c(1, rep(0, N-1)) # dummy variable for first | |
y_lag <- c(0, y[-N]) # lagged y | |
res <- lm(y ~ 1 + first_dummy + y_lag) # estimate | |
# give the coefficients nice names | |
alpha <- unname(coef(res)[1]) | |
phi <- unname(coef(res)[3]) | |
first <- unname(coef(res)[2]) | |
# now get the original parameters back | |
asymptote_estimated <- alpha / (1 - phi) | |
first_obs_estimated <- alpha + first | |
ar1_param_estimated <- phi |
Author
vankesteren
commented
Apr 4, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment