# Error Calculating MVN Likelihood of Time Series with AR(1) Errors in R

#### RKindman

##### New Member
Hi all,

I'm having trouble calculating the likelihood of a time series with AR(1) errors. I am generating my covariance matrix according to page 2 of (http://cran.r-project.org/doc/contrib/Fox-Companion/appendix-timeseries-regression.pdf), using the library mvtnorm and the multivariate normal density function dmvnorm(). Here's some example code:

Code:
library(mvtnorm)

# Generate a basic time series with AR(1) Errors:

t <- 1:100

error <- as.numeric(arima.sim(n = length(t), list(ar = c(0.8897)), sd = 10))

series <- 5*t + error

# Fit the series using a basic linear model assuming errors are IID Normal

naive.model <- lm(series ~ t -1)

# Examine and model the residuals

residuals <- series - t*coef(naive.model)

residual.model <-  arima(residuals, c(1,0,0), include.mean=F)

# Construct the covariance matrix, assuming the process variance (10^2) is known

sigma <- diag(length(t))

sigma[(abs(row(sigma)-col(sigma)) == 1)] = as.numeric(coef(residual.model))

sigma <- sigma*10^2

# Calculate the MVN density...

dmvnorm(series, t*coef(naive.model) ,sigma, log=T)
Without fail, I get the following error message:

Warning message: In log(eigen(sigma, symmetric = TRUE, only.values = TRUE)\$values) : NaNs produced.

It's worth noting that the matrix from the following (https://stat.ethz.ch/pipermail/r-help/2007-May/131728.html) "works", but I think is actually for an MA(1) process rather than an AR(1) process.

I gather the message means the proposed covariance matrix may not be invertible. This said I'm stuck on how to proceed and would be extremely appreciative of any thoughts.

Thank you very much,

#### Dason

##### Ambassador to the humans
Your covariance matrix isn't actually for an AR(1) process. You might want to reevaluate what it should look like in the AR case.

#### RKindman

##### New Member
Dason,

Thanks for a quick response. I'm a little stumped here as to how to specify the covariance matrix; I am generating mine based on the guidance of the document linked to in my post (http://cran.r-project.org/doc/contrib/Fox-Companion/appendix-timeseries-regression.pdf); if this is incorrect I'd really appreciate your pointing me towards a better resource. I really appreciate it; sorry for a somewhat elementary question.

Robert

#### Dason

##### Ambassador to the humans
I didn't take the take to read the pdf you linked to (plus the link was broken). But the correlation matrix you're using is for an MA model - not an AR model. And the problem is that you're using the parameter estimated for the AR model and pretending like thats the parameter for an MA model - but the estimated parameter is impossible for an MA model.

#### RKindman

##### New Member
Dason,

Thanks again for your help. Clearly the issue is how I'm generating my covariance matrix... I've checked a number of different resources, each of which seem to suggest that the covariance matrix for an AR process should be generated in the manner in which I'm generating it. Are you aware of any more reliable resources on the topic?

#### RKindman

##### New Member
Understood -- thanks so much for the help. This looks like a much better reference.

Robert