# NOT RUN {
### Example 1: using arima.sim() to generate a 0-mean stationary time series.
nn <- 500
tsdata <- data.frame(x2 = runif(nn))
ar.coef.1 <- rhobitlink(-1.55, inverse = TRUE) # Approx -0.65
ar.coef.2 <- rhobitlink( 1.0, inverse = TRUE) # Approx 0.50
set.seed(1)
tsdata <- transform(tsdata,
index = 1:nn,
TS1 = arima.sim(nn, model = list(ar = ar.coef.1),
sd = exp(1.5)),
TS2 = arima.sim(nn, model = list(ar = ar.coef.2),
sd = exp(1.0 + 1.5 * x2)))
### An autoregressive intercept--only model. ###
### Using the exact EIM, and "nodrift = TRUE" ###
fit1a <- vglm(TS1 ~ 1, data = tsdata, trace = TRUE,
AR1(var.arg = FALSE, nodrift = TRUE,
type.EIM = "exact",
print.EIM = FALSE),
crit = "coefficients")
Coef(fit1a)
summary(fit1a)
# }
# NOT RUN {
### Two responses. Here, the white noise standard deviation of TS2 ###
### is modelled in terms of 'x2'. Also, 'type.EIM = exact'. ###
fit1b <- vglm(cbind(TS1, TS2) ~ x2,
AR1(zero = NULL, nodrift = TRUE,
var.arg = FALSE,
type.EIM = "exact"),
constraints = list("(Intercept)" = diag(4),
"x2" = rbind(0, 0, 1, 0)),
data = tsdata, trace = TRUE, crit = "coefficients")
coef(fit1b, matrix = TRUE)
summary(fit1b)
### Example 2: another stationary time series
nn <- 500
my.rho <- rhobitlink(1.0, inverse = TRUE)
my.mu <- 1.0
my.sd <- exp(1)
tsdata <- data.frame(index = 1:nn, TS3 = runif(nn))
set.seed(2)
for (ii in 2:nn)
tsdata$TS3[ii] <- my.mu/(1 - my.rho) +
my.rho * tsdata$TS3[ii-1] + rnorm(1, sd = my.sd)
tsdata <- tsdata[-(1:ceiling(nn/5)), ] # Remove the burn-in data:
### Fitting an AR(1). The exact EIMs are used.
fit2a <- vglm(TS3 ~ 1, AR1(type.likelihood = "exact", # "conditional",
type.EIM = "exact"),
data = tsdata, trace = TRUE, crit = "coefficients")
Coef(fit2a)
summary(fit2a) # SEs are useful to know
Coef(fit2a)["rho"] # Estimate of rho, for intercept-only models
my.rho # The 'truth' (rho)
Coef(fit2a)["drift"] # Estimate of drift, for intercept-only models
my.mu /(1 - my.rho) # The 'truth' (drift)
# }
Run the code above in your browser using DataLab