> Oops, what I meant in the example below was:
> Given a sequence a[i], i=0,1,2,...,n, and some constant k,
> with 0 < k < 1, compute the sequence
> b[i] = k*b[i-1] + a[i], i=1,2,...,n
> NOT
> b[i]= k*a[i-1]+a[i]
> Sorry, Jack
> P.S. I know it can be done with apply()
Um. Your final example is, I think,
X[i] = k*X[i-i] + a[i], i=1,2,...,n
and this cannot be done directly with apply. Actually, it cannot be done at
all unless we know X[0], so let us assume this is specified. It is then an
AR(1) process that can be solved analytically:
X[1] = k*X[0] + a[1]
X[2] = k*X[1] + a[2] = k*k*X[0] + k*a[1] + a[2]
X[3] = k^3*X[0] + k^2*a[1] + k*a[2] + a[3]
...
so X = [k^i]X[0] + [k^(i-j-1)I(i>=j)]a
and that can be done by matrix multiplication in S. Alternatively, the
system of equations is
X[0] = given
X[i] - k*X[i-1] = a[i]
and that is of the form A %*% X = a and can be solved by solve. As A is so
simple, solve is overkill (even using the forms in library Matrix).
For an example of a calculation that cannot be done by a loop or
analytically, see V&R2 pp.116-7.
>
> Jack Lewis wrote:
>
> > Here is a simple example that I think requires looping,
> > (although I'd be thrilled to find out I'm wrong).
> > Given a sequence a[i], i=0,1,2,...,n, and some constant k,
> > with 0 < k < 1, compute the sequence
> > b[i] = k*a[i-1] + a[i], i=1,2,...,n
-- Brian D. Ripley, ripley@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595----------------------------------------------------------------------- This message was distributed by s-news@wubios.wustl.edu. To unsubscribe send e-mail to s-news-request@wubios.wustl.edu with the BODY of the message: unsubscribe s-news