##### Chapter 5: Classification using Decision Trees and Rules -------------------

#### Part 1: Decision Trees -------------------

## Understanding Decision Trees ----
# calculate entropy of a two-class segment
-0.60 * log2(0.60) - 0.40 * log2(0.40)

curve(-x * log2(x) - (1 - x) * log2(1 - x),
      col="red", xlab = "x", ylab = "Entropy", lwd=4)

## Example: Identifying Risky Bank Loans ----
## Step 2: Exploring and preparing the data ----
credit <- read.csv("credit.csv")
str(credit)

# look at two characteristics of the applicant
table(credit$checking_balance)
table(credit$savings_balance)

# look at two characteristics of the loan
summary(credit$months_loan_duration)
summary(credit$amount)

# look at the class variable
table(credit$default)

# create a random sample for training and test data
# use set.seed to use the same random number sequence as the tutorial
set.seed(12345)
credit_rand <- credit[order(runif(1000)), ]

# compare the credit and credit_rand data frames
summary(credit$amount)
summary(credit_rand$amount)
head(credit$amount)
head(credit_rand$amount)

# split the data frames
credit_train <- credit_rand[1:900, ]
credit_test  <- credit_rand[901:1000, ]

# check the proportion of class variable
prop.table(table(credit_train$default))
prop.table(table(credit_test$default))

## Step 3: Training a model on the data ----
# build the simplest decision tree
library(C50)
credit_model <- C5.0(credit_train[-17], credit_train$default)

# display simple facts about the tree
credit_model

# display detailed information about the tree
summary(credit_model)

## Step 4: Evaluating model performance ----
# create a factor vector of predictions on test data
credit_pred <- predict(credit_model, credit_test)

# cross tabulation of predicted versus actual classes
library(gmodels)
CrossTable(credit_test$default, credit_pred,
           prop.chisq = FALSE, prop.c = FALSE, prop.r = FALSE,
           dnn = c('actual default', 'predicted default'))

## Step 5: Improving model performance ----

## Boosting the accuracy of decision trees
# boosted decision tree with 10 trials
credit_boost10 <- C5.0(credit_train[-17], credit_train$default,
                       trials = 10)
credit_boost10
summary(credit_boost10)

credit_boost_pred10 <- predict(credit_boost10, credit_test)
CrossTable(credit_test$default, credit_boost_pred10,
           prop.chisq = FALSE, prop.c = FALSE, prop.r = FALSE,
           dnn = c('actual default', 'predicted default'))

# boosted decision tree with 100 trials (not shown in text)
credit_boost100 <- C5.0(credit_train[-17], credit_train$default,
                        trials = 100)
credit_boost_pred100 <- predict(credit_boost100, credit_test)
CrossTable(credit_test$default, credit_boost_pred100,
           prop.chisq = FALSE, prop.c = FALSE, prop.r = FALSE,
           dnn = c('actual default', 'predicted default'))

## Making some mistakes more costly than others
# create a cost matrix
error_cost <- matrix(c(0, 1, 4, 0), nrow = 2)
error_cost

# apply the cost matrix to the tree
credit_cost <- C5.0(credit_train[-17], credit_train$default,
                          costs = error_cost)
credit_cost_pred <- predict(credit_cost, credit_test)

CrossTable(credit_test$default, credit_cost_pred,
           prop.chisq = FALSE, prop.c = FALSE, prop.r = FALSE,
           dnn = c('actual default', 'predicted default'))

#### Part 2: Rule Learners -------------------

## Example: Identifying Poisonous Mushrooms ----
## Step 2: Exploring and preparing the data ----
mushrooms <- read.csv("mushrooms.csv", stringsAsFactors = TRUE)

# examine the structure of the data frame
str(mushrooms)

# drop the veil_type feature
mushrooms$veil_type <- NULL

# examine the class distribution
table(mushrooms$type)

## Step 3: Training a model on the data ----
library(RWeka)

# train OneR() on the data
mushroom_1R <- OneR(type ~ ., data = mushrooms)

## Step 4: Evaluating model performance ----
mushroom_1R
summary(mushroom_1R)

## Step 5: Improving model performance ----
mushroom_JRip <- JRip(type ~ ., data = mushrooms)
mushroom_JRip
summary(mushroom_JRip)

# Rule Learner Using C5.0 Decision Trees (not in text)
library(C50)
mushroom_c5rules <- C5.0(type ~ odor + gill_size, data = mushrooms, rules = TRUE)
summary(mushroom_c5rules)


#Series temporais e analises preditivas - Fernando Amaral
library(forecast)
library(ggplot2)

treino = window(fdeaths, start=c(1974,1), end=c(1977,12))
teste = window(fdeaths, start=c(1978,1), end=c(1979,12))

#regressão
modelo1 = tslm(treino ~ season + trend, data=treino)
prev1 = forecast(modelo1, h=24)
autoplot(prev1)

#rnn
modelo2 = nnetar(treino)
prev2 = forecast(modelo2, h=24)
autoplot(prev2)

plot(fdeaths)
lines(prev1$mean, col="blue")
lines(prev2$mean, col="red")
lines(teste, col="green")
legend("topright",legend=c("Reg.","Rn","Teste"), col = c("blue","red","green"), lty=1:2, cex=0.8)

accuracy(prev1,teste )
accuracy(prev2,teste )

Udemy/Series Temporais e Analises Preditivas/Resources/Codigo/10.1.Metricas de Desempenho.r


##### Chapter 3: Classification using Nearest Neighbors --------------------

## Example: Classifying Cancer Samples ----
## Step 2: Exploring and preparing the data ----

# import the CSV file
wbcd <- read.csv("wisc_bc_data.csv", stringsAsFactors = FALSE)

# examine the structure of the wbcd data frame
str(wbcd)

# drop the id feature
wbcd <- wbcd[-1]

# table of diagnosis
table(wbcd$diagnosis)

# recode diagnosis as a factor
wbcd$diagnosis <- factor(wbcd$diagnosis, levels = c("B", "M"),
                         labels = c("Benign", "Malignant"))

# table or proportions with more informative labels
round(prop.table(table(wbcd$diagnosis)) * 100, digits = 1)

# summarize three numeric features
summary(wbcd[c("radius_mean", "area_mean", "smoothness_mean")])

# create normalization function
normalize <- function(x) {
  return ((x - min(x)) / (max(x) - min(x)))
}

# test normalization function - result should be identical
normalize(c(1, 2, 3, 4, 5))
normalize(c(10, 20, 30, 40, 50))

# normalize the wbcd data
wbcd_n <- as.data.frame(lapply(wbcd[2:31], normalize))

# confirm that normalization worked
summary(wbcd_n$area_mean)

# create training and test data
wbcd_train <- wbcd_n[1:469, ]
wbcd_test <- wbcd_n[470:569, ]

# create labels for training and test data

wbcd_train_labels <- wbcd[1:469, 1]
wbcd_test_labels <- wbcd[470:569, 1]

## Step 3: Training a model on the data ----

# load the "class" library
library(class)

wbcd_test_pred <- knn(train = wbcd_train, test = wbcd_test,
                      cl = wbcd_train_labels, k = 21)

## Step 4: Evaluating model performance ----

# load the "gmodels" library
library(gmodels)

# Create the cross tabulation of predicted vs. actual
CrossTable(x = wbcd_test_labels, y = wbcd_test_pred,
           prop.chisq = FALSE)

## Step 5: Improving model performance ----

# use the scale() function to z-score standardize a data frame
wbcd_z <- as.data.frame(scale(wbcd[-1]))

# confirm that the transformation was applied correctly
summary(wbcd_z$area_mean)

# create training and test datasets
wbcd_train <- wbcd_z[1:469, ]
wbcd_test <- wbcd_z[470:569, ]

# re-classify test cases
wbcd_test_pred <- knn(train = wbcd_train, test = wbcd_test,
                      cl = wbcd_train_labels, k = 21)

# Create the cross tabulation of predicted vs. actual
CrossTable(x = wbcd_test_labels, y = wbcd_test_pred,
           prop.chisq = FALSE)

# try several different values of k
wbcd_train <- wbcd_n[1:469, ]
wbcd_test <- wbcd_n[470:569, ]

wbcd_test_pred <- knn(train = wbcd_train, test = wbcd_test, cl = wbcd_train_labels, k=1)
CrossTable(x = wbcd_test_labels, y = wbcd_test_pred, prop.chisq=FALSE)

wbcd_test_pred <- knn(train = wbcd_train, test = wbcd_test, cl = wbcd_train_labels, k=5)
CrossTable(x = wbcd_test_labels, y = wbcd_test_pred, prop.chisq=FALSE)

wbcd_test_pred <- knn(train = wbcd_train, test = wbcd_test, cl = wbcd_train_labels, k=11)
CrossTable(x = wbcd_test_labels, y = wbcd_test_pred, prop.chisq=FALSE)

wbcd_test_pred <- knn(train = wbcd_train, test = wbcd_test, cl = wbcd_train_labels, k=15)
CrossTable(x = wbcd_test_labels, y = wbcd_test_pred, prop.chisq=FALSE)

wbcd_test_pred <- knn(train = wbcd_train, test = wbcd_test, cl = wbcd_train_labels, k=21)
CrossTable(x = wbcd_test_labels, y = wbcd_test_pred, prop.chisq=FALSE)

wbcd_test_pred <- knn(train = wbcd_train, test = wbcd_test, cl = wbcd_train_labels, k=27)
CrossTable(x = wbcd_test_labels, y = wbcd_test_pred, prop.chisq=FALSE)


compute_normal <- function( vertex, face ) {

face <- t( face )
vertex <- t( vertex )
eps <- 1E-12

crossp <- function( x, y ) {
  z <- x;
  z[1,] = x[2,]*y[3,] - x[3,]*y[2,];
  z[2,] = x[3,]*y[1,] - x[1,]*y[3,];
  z[3,] = x[1,]*y[2,] - x[2,]*y[1,];
  return( z )
}

nface <- dim(face)[2]
nvert <- dim(vertex)[2]
normal <- array( 0, c(3, nvert) )

normalf <- crossp( vertex[,face[2,]]-vertex[,face[1,]], vertex[,face[3,]]-vertex[,face[1,]] );
d <- sqrt( apply( normalf^2, 2, sum )  )
d[d<eps] <- 1
normalf <- normalf / rbind( d, d, d )

normal <- array( 0, c(3, nvert) )
for (i in 1:nface) {
  f <- face[,i];
  for (j in 1:3) {
    normal[,f[j]] <- normal[,f[j]] + normalf[,i]
  }
}

# normalize
d <- sqrt( apply( normal^2, 2, sum )  )
d[d<eps] <- 1
normal <- normal / rbind( d, d, d )

# enforce that the normal are outward
mVertex <- apply( vertex, 2, mean )
v <- vertex - rbind( mVertex, mVertex, mVertex )
s <- apply( v*normal, 1, sum )
if ( sum(s>0)<sum(s<0) ) {
  #flip
  normal = -normal;
  normalf = -normalf;
}

return( normal )

}


# loads the library urca, which contains the command ur.df()
# and applies Unit-Root Testing based on Dickey-Fuller test
# install.packages(c('fGarch', 'urca','tseries','MASS'))
library(urca)
library(tseries)
library(MASS)
library(fGarch)

# Importing data into R can be carried out in various ways. Below, the command read.table is used:
data_all = read.table('/home/george/AUEB/TimeSeries/time_series_in_r/Data-Assignment-Feb-2018.txt')

xdata_cols <- c('V5','V6', 'V7','V8','V9','V10','V11','V12','V13','V14','V15','V16','V17','V18')

xdata <- data_all[c(xdata_cols)]

head(xdata)

y1 <- data_all$V1

j1 = ts(y1, frequency=12, start = c(1991, 1), end = c(2004, 12))
xfactors=ts(xdata, frequency=12,  start = c(1991, 1), end = c(2004, 12))

train_data = data_all[1: 168,]
test_data = data_all[169:180,]

attach(train_data)

fitnull <- lm(V1 ~ 1)

stepSR <- step(fitnull,
               scope=list(lower = ~ 1,
                          upper = ~ V5 + V6 + V7 + V8 + V9 + V10 + V11 + V12 + V13 + V14 + V15 + V16 + V17 + V18),
               direction="both",
               data=train_data)

stepSR$anova

summary(stepSR)

detach(train_data)

stepSRresidualsTS <- ts(stepSR$residuals,
                        frequency=12,
                        start = c(1991,1),
                        end = c(2004, 12))
stepSRresidualsTS

par(mfrow=c(3,2))

# set up the graphics
acf(ts(stepSRresidualsTS,freq=1), 48, main="ACF of residuals")
pacf(ts(stepSRresidualsTS,freq=1), 48, main="PACF of residuals")
acf(ts(stepSRresidualsTS^2,freq=1), 48, main="ACF of squared residuals")
pacf(ts(stepSRresidualsTS^2,freq=1), 48, main="PACF of squared residuals")
qqnorm(stepSRresidualsTS,main="Normal QQplot of residuals")
qqline(stepSRresidualsTS)
plot(j1, type="l", col='red', lwd=1, main="Time Series plot of Y1", ylab="Monthly returns")

ar8restricted <- arima(stepSRresidualsTS,
                       order=c(8,0,0),
                       fixed= c(NA, 0,0,0,0,0,0,NA, NA))
ar8restricted

par(mfrow=c(3,2))
ar8restricted_residualsTS <- ts(ar8restricted$residuals, frequency=12, start = c(1991, 1), end = c(2004, 12))

# set up the graphics
acf(ts(ar8restricted_residualsTS,freq=1), 48, main="ACF on residuals of restricted AR")
pacf(ts(ar8restricted_residualsTS,freq=1), 48, main="PACF on residuals of restricted AR")
acf(ts(ar8restricted_residualsTS^2,freq=1), 48, main="ACF on squared residuals of restricted AR")
pacf(ts(ar8restricted_residualsTS^2,freq=1), 48, main="PACF on squared residuals of restricted AR")
qqnorm(ar8restricted_residualsTS,main="Normal QQplot on residuals of restricted AR")
qqline(ar8restricted_residualsTS)

Box.test(ar8restricted_residualsTS, lag=12, type="Ljung")
Box.test(ar8restricted_residualsTS^2, lag=12, type="Ljung")

ma9restricted <- arima(stepSRresidualsTS,
                       order=c(0,0,9),
                       fixed= c(NA, NA, 0, 0, 0, 0, 0, NA, NA, NA))
ma9restricted

par(mfrow=c(3,2))
ma9restricted_residualsTS <- ts(ma9restricted$residuals, frequency=12, start = c(1991, 1), end = c(2004, 12))

# set up the graphics
acf(ts(ma9restricted_residualsTS,freq=1), 48, main="ACF on residuals of restricted MA")
pacf(ts(ma9restricted_residualsTS,freq=1), 48, main="PACF on residuals of restricted MA")
acf(ts(ma9restricted_residualsTS^2,freq=1), 48, main="ACF on squared residuals of restricted MA")
pacf(ts(ma9restricted_residualsTS^2,freq=1), 48, main="PACF on squared residuals of restricted MA")
qqnorm(ma9restricted_residualsTS,main="Normal QQplot on residuals of restricted MA")
qqline(ma9restricted_residualsTS)

Box.test(ma9restricted_residualsTS, lag=12, type="Ljung")
Box.test(ma9restricted_residualsTS^2, lag=12, type="Ljung")

arma89restricted <- arima(stepSRresidualsTS,

                       order=c(8,0,9),
                       fixed= c(NA, 0, 0, 0, 0, 0, 0, NA, NA, NA, 0, 0,0,0, 0, NA, NA, NA))
arma89restricted

par(mfrow=c(3,2))
arma89restricted_residualsTS <- ts(arma89restricted$residuals, frequency=12, start = c(1991, 1), end = c(2004, 12))

# set up the graphics
acf(ts(arma89restricted_residualsTS,freq=1), 48, main="ACF on residuals of restricted ARMA")
pacf(ts(arma89restricted_residualsTS,freq=1), 48, main="PACF on residuals of restricted ARMA")
acf(ts(arma89restricted_residualsTS^2,freq=1), 48, main="ACF on squared residuals of restricted ARMA")
pacf(ts(arma89restricted_residualsTS^2,freq=1), 48, main="PACF on squared residuals of restricted ARMA")
qqnorm(arma89restricted_residualsTS,main="Normal QQplot on residuals of restricted MA")
qqline(arma89restricted_residualsTS)

Box.test(arma89restricted_residualsTS, lag=12, type="Ljung")
Box.test(arma89restricted_residualsTS^2, lag=12, type="Ljung")

# First, we will fit an ARCH(1) model:
m1arch <- garchFit(~garch(1,0), data=stepSR$residuals, trace=F)
# trace = F reduces the summary
summary(m1arch)

predict(m1arch, 6)

m1arch_student=garchFit(~garch(1,0),data=stepSR$residuals, cond.dist="std",trace=F)
summary(m1arch_student)

m2garch=garchFit(~garch(1,1),data=stepSR$residuals,trace=F)
summary(m2garch)

arma11_garch11fit <- garchFit(formula= ~arma(1,1) + garch(1,1),
                              data=stepSR$residuals,
                              trace=F)
summary(arma11_garch11fit)

forecast <- predict(arma11_garch11fit, 12)

# plot of forecasts with 1 standard error
par(mfrow=c(1,1))
UL <- forecast$meanForecast + forecast$meanError
LL <- forecast$meanForecast - forecast$meanError

predTS <- ts(forecast$meanForecast, frequency=12, start = c(2004, 12))
UL_TS <- ts(forecast$meanForecast, frequency=12, start = c(2004, 12))
LL_TS <- ts(forecast$meanForecast, frequency=12, start = c(2004, 12))

min_x <- min(stepSRresidualsTS, LL)
max_x <- max(stepSRresidualsTS, UL)

ts.plot(stepSRresidualsTS, predTS)
lines(predTS, col="red", type="o")
lines(UL_TS, col="blue", lty="dashed")
lines(LL_TS, col="blue", lty="dashed")

library(hydroGOF)
MSE <- mse(forecast$meanForecast, forecast$meanError)
MSE