MLR Version2
MLR Version2
## Methods: Multiple Linear Regression, Random Forest Regression and Support Vector
Regression
# Prepared by"
# Carlito O. Daarol
# Faculty/Statistician/Data Scientist
# Mathematics Department
# Mindanao State University
# General Santos city
# January 1, 2021
(Year <-
c(2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2016,2016,2016,2016,2
016,2016,2016,2016,2016,2016,2016,2016))
(Month <- c(12, 11,10,9,8,7,6,5,4,3,2,1,12,11,10,9,8,7,6,5,4,3,2,1))
(Interest_Rate <-
c(2.75,2.5,2.5,2.5,2.5,2.5,2.5,2.25,2.25,2.25,2,2,2,1.75,1.75,1.75,1.75,1.75,1.75,1
.75,1.75,1.75,1.75,1.75))
(Unemployment_Rate <-
c(5.3,5.3,5.3,5.3,5.4,5.6,5.5,5.5,5.5,5.6,5.7,5.9,6,5.9,5.8,6.1,6.2,6.1,6.1,6.1,5.9
,6.2,6.2,6.1))
(Stock_Index_Price <-
c(1464,1394,1357,1293,1256,1254,1234,1195,1159,1167,1130,1075,1047,965,943,958,971,
949,884,866,876,822,704,719))
plot(x=Interest_Rate, y=Stock_Index_Price)
plot(x=Unemployment_Rate, y=Stock_Index_Price)
# the plot should suggest a linear pattern
# Call:
# lm(formula = Stock_Index_Price ~ Interest_Rate + Unemployment_Rate)
# Residuals:
# Min 1Q Median 3Q Max
# -158.205 -41.667 -6.248 57.741 118.810
# Coefficients:
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 1798.4 899.2 2.000 0.05861 .
# Interest_Rate 345.5 111.4 3.103 0.00539 **
# Unemployment_Rate -250.1 117.9 -2.121 0.04601 *
# ---
# Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
library(randomForest)
random_forest <- randomForest(Stock_Index_Price ~., data = filedata, ntree = 5000)
random_forest_prediction <- predict(random_forest, filedata)
residual <- random_forest_prediction - filedata$Stock_Index_Price
rmse_rf <- sqrt(mean(residual^2))
library(e1071)
svr <- svm(Stock_Index_Price ~., data = filedata)
OptModelsvm=tune(svm,Stock_Index_Price ~., data =
filedata,ranges=list(elsilon=seq(0,1,0.1), cost=1:100))
BestModel=OptModelsvm$best.model
svr_prediction <- predict(BestModel, filedata)
residual <- svr_prediction - filedata$Stock_Index_Price
rmse_svr <- sqrt(mean(residual^2))
library(ggplot2)