Below are the solutions to these exercises on Analysis of stock prices – part 3.

# read data data <- read.csv("https://www.r-exercises.com/wp-content/uploads/2016/07/data.csv", sep=",", header=TRUE) # data frame from exercise 6, part 1 data.close <- reshape(data[c("Symbol", "Date", "Close")], timevar="Symbol", idvar="Date", direction="wide") colnames(data.close) <- c("Date", as.character(unique(data$Symbol))) data.close$Date <- as.Date(data.close$Date) data.close <- data.close[with(data.close, order(Date)), ] library("tseries") library("forecast") #################### # # # Exercise 1 # # # #################### diff.yhoo <- diff(data.close$YHOO) # histogram hist(diff.yhoo, prob="T", col="red", ylab="Frequencies", xlab="Price differencies", main="YHOO") lines(density(diff.yhoo)) mu <- mean(diff.yhoo) sigma <- sd(diff.yhoo) x <- seq(min(diff.yhoo), max(diff.yhoo), length = length(diff.yhoo)) y <- dnorm(x, mu, sigma) lines(x, y, lwd=2, col="blue")

#################### # # # Exercise 2 # # # #################### # q-q plot qqnorm(diff.yhoo, main="YHOO Normal Q-Q Plot") qqline(diff.yhoo)

#################### # # # Exercise 3 # # # #################### # Kolmogorov-Smirnov test ks.test(diff.yhoo, "pnorm", mean(diff.yhoo), sd(diff.yhoo))

## Warning in ks.test(diff.yhoo, "pnorm", mean(diff.yhoo), sd(diff.yhoo)): ## ties should not be present for the Kolmogorov-Smirnov test

## ## One-sample Kolmogorov-Smirnov test ## ## data: diff.yhoo ## D = 0.052668, p-value = 0.4867 ## alternative hypothesis: two-sided

# Shapiro test shapiro.test(diff.yhoo)

## ## Shapiro-Wilk normality test ## ## data: diff.yhoo ## W = 0.99146, p-value = 0.1503

# Yes #################### # # # Exercise 4 # # # #################### plot(x = data.close$Date, y=data.close$GE, type="l", main="Closing prices of GE", xlab="", ylab="Price") lines(x = data.close$Date, y=filter(data.close$GE, filter=rep(1/12,12), method="convolution", sides=1), col="red") lines(x = data.close$Date, y=filter(data.close$GE, filter=rep(1/50,50), method="convolution", sides=1), col="blue")

#################### # # # Exercise 5 # # # #################### data.close.ts <- ts(data.close[, -1]) tsdisplay(data.close.ts[, 'BAC'], main="Correlogram of closing prices of BAC")

```
"No"
```

## [1] "No"

#################### # # # Exercise 6 # # # #################### tsdisplay(diff(data.close.ts[, 'BAC']), main="Correlogram of differentiated closing prices of BAC")

```
"Yes"
```

## [1] "Yes"

#################### # # # Exercise 7 # # # #################### fit.diff <- auto.arima(diff(data.close.ts[, 'BAC']), stepwise=FALSE) fit.diff

## Series: diff(data.close.ts[, "BAC"]) ## ARIMA(2,0,0) with zero mean ## ## Coefficients: ## ar1 ar2 ## 0.0719 -0.1065 ## s.e. 0.0627 0.0627 ## ## sigma^2 estimated as 0.09424: log likelihood=-58.99 ## AIC=123.97 AICc=124.07 BIC=134.56

#################### # # # Exercise 8 # # # #################### acf(residuals(fit.diff), main="Correlogram of residuals of differentiated closing price of BAC")

#################### # # # Exercise 9 # # # #################### train <- data.close.ts[1:(0.8 * length(data.close.ts[, 'BAC']))] predictions = predict(arima(train, order = c(2, 0, 0)), # 2, 0 and 0 are the parametars of fit arima model we obtained in exercise 4 n.ahead = (0.2 * length(data.close.ts[, 'BAC'])))$pred #################### # # # Exercise 10 # # # #################### test <- data.close.ts[(0.8 * length(data.close.ts[, 'BAC']) + 1):length(data.close.ts[, 'BAC'])] ac<-accuracy(predictions, test)[2] stdev <- sd(test) ifelse(ac <= stdev, "Yes", "No")

## [1] "No"

Neha says

Hi,

How to get data for a particular stock say 1 month

Miodrag Sljukic says

Please look at part one of this series of exercises on stock prices analyses. In particular, exercise #2 shows how to subset data.

Niraj says

I employed ARIMA forecasting on tourism data, i have monthly data of tourism arrival data, which is from 1992 to 2015. and i want to forecast it till 2020 date, how can i forecast it, i am a little bit get confuse on coding.