-1
$\begingroup$

this is my function! Hello, How Can i write this function in R? and any simulation codes about SETAR model including codes, links, books or any guide, would be appreciated.? Thanks in advance

$\endgroup$
1
$\begingroup$

Please expect this to be removed from CrossValidated soon and moved to stackoverflow or something like this, as your question is mostly about programming.

If you can't write this function, I wonder, what you will do with it. However, this should be your function in R:

X_t <- function(X_tminus1, lambda_tminus1){
  if(X_tminus1 <= 4) 
       lambda <- .9+.2*X_tminus1 + .4*lambda_tminus1
  else 
       lambda <- 2.2 + .3*X_tminus1 + .5*lambda_tminus1 
  return(list(lambda = lambda, X_t = rpois(1, lambda)))
}

I guess, you likely want to produce time series with this. This is an example that should work (no guarantee, though. Please test before usage):

X_t <- function(X_tminus1, lambda_tminus1){
  if(X_tminus1<=4) 
       lambda <- .9+.2*X_tminus1 + .4*lambda_tminus1
  else 
       lambda = 2.2 + .3*X_tminus1 + .5*lambda_tminus1 
  return(list(lambda=lambda, X_t=rpois(1, lambda)))
}

create_n_values <- function(X, lambda, n){
  xs <- numeric(n)
  for(i in 1:n){
    X_t_val <- X_t(X, lambda)
    X <- X_t_val[[1]]
    lambda <- X_t_val[[2]]
    xs[i] <- X
  }
  return(xs)
}

#example create 25 values wit X_tminus1=1 and lambda_tminus1=1:
create_n_values(X=1, lambda=1, n=25)
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.