[R] creating a function

Rui Barradas ruipbarradas at sapo.pt
Sun Dec 23 15:10:55 CET 2012


Hello,

If I understand it correctly, a simple change to my previous code will 
do. I have also change the name of function 'x' to 'FUN', to make it 
more readable.

Yfun <- function(Y, p, FUN, epsilon){
     for (i in 2:length(Y)) {
         Y[i] <- Y[i-1] + (1/p)*sum(FUN(Y[1:(i-1)])) + epsilon[i]
     }
     Y
}


Hope this helps,

Rui Barradas
Em 23-12-2012 08:39, Simone Gogna escreveu:
> Hi,
> thank you Rui for your reply.
> I understand I haven’t made it clear enough.
> What I do want to make is a kind of autoregressive process where Y_t+1 depends on its lagged value Y_t.
> If I wanted to simulate a simple AR(1) process I would have done something as follows:
>
> n<-500
> Y_init_cond<-0
> Et<-ts(rnorm(n,mean=0,sd=1))
> Yt<-Et*0
> Yt[1]<-Y_init_cond+Et[1]
> for(i in 2:(n)) {
>    Yt[i]<-Yt[i-1]+Et[i]
> }
> Yt<-ts(Yt[(length(Yt)-n+1):length(Yt)])
>
> where Et is a vector of random errors.
> Since I do not want to simulate a simple autoregressive process the code above is no longer suitable for my purpose.
> In fact what I want to do is a function as Y_t+1 = Y_t + (1\p)*summation(x(Y_t,Y_t-1,...)) + epsilon.t,
> where Y_t+1 depends on its lagged value Y_t, but also on 1/p (which is a parameter) times summation(x(Y_t,Y_t-1,...)) where x is a function that also depends on the lagged values of Y_t+1 from Y_t to Y_t-n. Epsilon_t is, as You said a vector of random errors.
>
> Any suggestion is very appreciated.
>
> thanks and best regards,
> Simone
>
> From: Rui Barradas
> Sent: Saturday, December 22, 2012 8:29 PM
> To: Simone Gogna
> Cc: r-help at r-project.org
> Subject: Re: [R] creating a function
>
> Hello,
>
> It's a bad idea to name a function and one of it's arguments Y, use Yfun and Y.
> What does summation(x(Y.t, Y.t-1, ...)) mean? Is there a multiplication sign between x and (Y.t, ...)?
> And is epsilon a vector of errors, one for each Y.t?
> If so, the following might do it.
>
>
> Yfun <- function(Y, p, x, epsilon){
>      for (i in 2:length(Y)) {
>          Y[i] <- Y[i-1] + (1/p)*sum(x*Y[1:(i-1)]) + epsilon[i]
>      }
>      Y
> }
>
>
> Hope this helps,
>
> Rui Barradas
>
> Em 22-12-2012 09:29, Simone Gogna escreveu:
> Dear R users,
> I’d like to create a function as:
>
> Y.t+1 = Y.t + (1\p)*summation(x(Y.t,Y.t-1,...)) + epsilon.t
>
> where x is a function of Y.t, Y.t-1 and so on, epsilon is a random error and p is a parameter.
>
> Do you think something of the following form might be appropriate?
>
> Y<-function(Y,p,x,epsilon){
>                         for (i in 2:length(Y)) {
>                           Y[i]<-Y[i-1]+(1/p)*sum(x(Y[i-1]))+epsilo.t}
>                         Y
> }
>
> Any indication is warmly appreciated.
>
> thanks and best regards
> 	[[alternative HTML version deleted]]
>
>
>     
>
> ______________________________________________
> R-help at r-project.org 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.
>
>




More information about the R-help mailing list