Re: [R] Create an AR(1) covariance matrix

From: Dimitris Rizopoulos <dimitris.rizopoulos_at_med.kuleuven.be>
Date: Fri, 11 May 2007 16:45:20 +0200

one option is the following:

times <- 1:5
rho <- 0.5
sigma <- 2
###############

H <- abs(outer(times, times, "-"))
V <- sigma * rho^H
p <- nrow(V)

V[cbind(1:p, 1:p)] <- V[cbind(1:p, 1:p)] * sigma V

I hope it helps.

Best,
Dimitris



Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium

Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


> Hi All.
>
> I need to create a first-order autoregressive covariance matrix
> (AR(1)) for a longitudinal mixed-model simulation. I can do this
> using nested "for" loops but I'm trying to improve my R coding
> proficiency and am curious how it might be done in a more elegant
> manner.
>
> To be clear, if there are 5 time points then the AR(1) matrix is 5x5
> where the diagonal is a constant variance (sigma^2) and the
> covariances depend on the number of "steps" between trials. So, the
> first off-diagonal of the matrix is sigma*rho, the second
> off-diagonal
> is sigma*rho^2, the third off-diagonal is sigma*rho^3, and so forth.
>
> Any suggestions for an elegant method to flexibly create this
> matrix?
>
> Rick DeShon
>
> ______________________________________________
> R-help_at_stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



R-help_at_stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Fri 11 May 2007 - 14:52:39 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 11 May 2007 - 16:31:14 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.