# Outier detection using tsoutlier in R

I am trying to predict the weekly stock price of Nifty using ARIMA model. Data can be downloaded here. I tried the following three cases:

First Case: I used tso function from tsoutliers package to identify outliers (if any) and to fit ARIMA model. I got the results as ARIMA(1,1,1) with no outliers detected. Minimal code:

outliers1 <- tso(close, tsmethod = c("auto.arima"), args.tsmethod = list(allowdrift=TRUE))

Results Obtained:

```ARIMA(1,1,1)

Coefficients:
ar1      ma1
0.6112  -0.5684
s.e.  0.3496   0.3632

sigma^2 estimated as 25268:  log likelihood=-2523.68
AIC=5053.35   AICc=5053.41   BIC=5065.24

No outliers were detected.
```

Second Case: Since there were no outliers detected, I used auto.arima() from forecast package to see what model I get. As suggested in previous posts, I made stepwise and approximation to FALSE. I obtained ARIMA (3,1,2) model. Minimal code:

```close <- read.ts("close.csv", header = FALSE")
fit <- auto.arima(close, stepwise = FALSE, trace = TRUE, approximation = FALSE)
```

Results Obtained:

```Series: close
ARIMA(3,1,2) with drift

Coefficients:
ar1      ar2     ar3     ma1     ma2    drift
-1.7302  -0.7838  0.0624  1.7730  0.9097  10.4769
s.e.   0.0695   0.1125  0.0578  0.0483  0.0475   8.4509

sigma^2 estimated as 24413:  log likelihood=-2510.18
AIC=5034.37   AICc=5034.66   BIC=5062.11
```

Third Case: In my third case, I tried using ARIMA(3,1,2) obtained in second case in tso to check for any outliers. But the model detected no outliers. Minimal code:

```outlier2 <- tso(close, maxit = 10, tsmethod = c("arima"), args.tsmethod = list(order =c(3,1,2)))
```

Results obtained:

```    Coefficients:
ar1     ar2      ar3     ma1      ma2
-0.2224  0.3573  -0.0186  0.2451  -0.2548
s.e.   0.7914  0.4344   0.1107  0.7884   0.4603
sigma^2 estimated as 24986:  log likelihood = -2521.51,  aic = 5055.02
No outliers were detected.
```

My question is if there aren't any outliers in the data then why are the results different in cases 1 and 2. Is there something I am missing out in the model building? In addition, the forecasts obtained using both ARIMA (3,1,2) and (1,1,1) are poor.

In the first case tso is using the default argument stepwise=TRUE. In the second case you are setting stepwise=FALSE. This can lead to a different choice of the ARIMA model. Passing stepwise=FALSE through argument args.tsmethod in tso should yield the same result (unless outliers are found for this model).