# Re: [R] how to pick a value from AR equation

From: Marco Geraci <marcodoc75_at_yahoo.com>
Date: Sat 27 May 2006 - 04:27:17 EST

Ciao,
you didn't provide lots of details so I'll try to answer your questions with examples you can find typing ?PP.test and ?ar. In general, when you run a command in R you get an output. You just need to understand what kind of object you get. For the PP.test

> x <- rnorm(1000)
> pp <- PP.test(x)

The command 'str' is very useful

> str(pp)

List of 5
\$ statistic: Named num -33.2
..- attr(*, "names")= chr "Dickey-Fuller"  \$ parameter: Named num 7
..- attr(*, "names")= chr "Truncation lag parameter"

``` \$ p.value  : num 0.01
\$ method   : chr "Phillips-Perron Unit Root Test"
\$ data.name: chr "x"
```
• attr(*, "class")= chr "htest"

It's a list. So you can select the element you need, e.g.

> pp\$statistic

Dickey-Fuller

-33.22576

or

> as.numeric(pp\$statistic)

 -33.22576

so you get rid of the attributes.
Now, same thing for 'ar'.

> fit.ar <- ar(lh)
> str(fit.ar)

List of 14

``` \$ order       : int 3
\$ ar          : num [1:3]  0.6534 -0.0636 -0.2269
\$ var.pred    : num 0.196
\$ x.mean      : num 2.4
\$ aic         : Named num [1:17] 18.307  0.996  0.538
```
0.000 1.490 ...
..- attr(*, "names")= chr [1:17] "0" "1" "2" "3" ...
``` \$ n.used      : int 48
\$ order.max   : num 16
\$ partialacf  : num [1:16, 1, 1]  0.576 -0.223 -0.227
0.103 -0.076 ...
\$ resid       : Time-Series [1:48] from 1 to 48:
NA     NA     NA -0.200 -0.169 ...
\$ method      : chr "Yule-Walker"
\$ series      : chr "lh"
\$ frequency   : num 1
\$ call        : language ar(x = lh)
```

\$ asy.var.coef: num [1:3, 1:3] 0.02156 -0.01518 0.00482 -0.01518 0.03117 ...
• attr(*, "class")= chr "ar"
>

This is for what concerns your questions. I'd like to give you a hint for a simpler programming code. You use the first row of your matrix as header
> omega<-array(0, dim=c(N+1,7))
>
omega[1,]<-("series","mean","std","max","min","ar(1)coeff","stdar(1)")

you can simply type

> omega<-array(0, dim=c(N,7))
> colnames(omega) <-
c("series","mean","std","max","min","ar(1)coeff","stdar(1)") so your matrix has Nx7 empty cells and avoid an headache trying to adjust the index 'i' in the loop 'for (i in (2:N+1))'.
Also, if you really need to specify the 'id' of the record
> omega[i,1]<-i-1

you just type before the loop
> omega[,1] <- 1:N
or
> rownames(omega) <- 1:N

In summary,

> omega<-array(0, dim=c(N,7))
> colnames(omega) <-
c("series","mean","std","max","min","ar(1)coeff","stdar(1)")
> omega[,1] <- 1:N
> for (i in 1:N){
> omega[i,2]<-mean(y[i,])
> (...)
> omega[i,6]<- (???????) # see before
> omega[i,7]<- (???????)} # see before

Finally, if 'y' is a prespecified matrix I suggest you to take a look at ?rowMeans, ?rowSums, and ?apply in order to execute 'mean' or 'sd' or any other function applied to 'y[i,]' at once instead of N times

Hope this helps,
Marco Geraci

• Lorenzo Bencivelli <lorenzo.bencivelli@unito.it> wrote:

> ok, i'll try to be more precise.
> both functions i need (ar() and PP.test()) are in
> the package stats.
> what i would like to do:
>
> omega<-array(0, dim=c(N+1,7))
>
omega[1,]<-("series","mean","std","max","min","ar(1)coeff","stdar(1)")
> for (i in (2:N+1)){
> omega[i,1]<-i-1
> omega[i,2]<-mean(y[(i-1),])
> (...)
> omega[i,6]<- (???????)
> omega[i,7]<- (???????)}
>
> for the PP.test, i would like to do something
> similar. N is the number
> of the series for which i would like to compute the
> AR(1) model and the
> unitary root test. thanks in advance. L
>
> --
>
>
>

> credo nella ragione umana,
> e nella liberta' e nella giustizia che dalla ragione
> scaturiscono. (sciascia)
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help