rolling time 3 hour, predict time 4 hours later,training set is 100 samples

modelErrors <- function(predicted, actual) {
  sal <- vector(mode="numeric", length=3)
  names(sal) <- c( "MAE", "RMSE", "RELE")
  meanPredicted <- mean(predicted)
  meanActual <- mean(actual)
  sumPred <- sum((predicted - meanPredicted)^2)
  sumActual <- sum((actual - meanActual)^2)
  n<- length(actual)
  p3<-vector(mode="numeric", length=n)
  for (i in c(1:n)) {
    if (actual[i]==0) {p3[i]<-abs(predicted[i])
    } else { p3[i]<-((abs(predicted[i]-actual[i]))/actual[i])
    }}
  sal[1] <- mean(abs(predicted - actual))
  sal[2] <- sqrt(sum((predicted - actual)^2)/n)
  sal[3] <- mean(p3)
  sal
}
unormalized<-function(x,y){
        ((y-0.1)*(max(x)-min(x))/0.8) + min(x)
}
library(caret)
## Loading required package: lattice
## Loading required package: ggplot2
load("~/PED/newWayPrepareData/PED_r3_p4_2000s.RData")
##########################100samples##################################
##########without sa feature selection########################
load("~/PED/SA/dataset_ PED_r3_p4_train100 _rfFit.RData")
predict(rfFit,inputsTest)->r3_p4_train100_pred
## Loading required package: randomForest
## randomForest 4.6-10
## Type rfNews() to see new features/changes/bug fixes.
###############sa feature selection#####################
load("~/PED/SA/dataset_ PED_r3_p4_train100 _rf_sa.RData")
plot(rf_sa) + theme_bw()

plot of chunk unnamed-chunk-1

#########variable importance#########
rf_sa$fit$importance
##            IncNodePurity
## SEASON end       0.02175
## HORA end         0.27736
## RH MAX           0.05577
## TMP MAX          0.04542
## O3 MIN           0.04724
## WDR MIN          0.11586
## WSP MIN          0.14807
## SO2 MIN          0.07916
## TMP MEAN         0.06340
## O3 MEDIAN        0.08460
## NO2 MEDIAN       0.08309
## WDR SUM          0.10507
## WSP SUM          0.12315
## CO SUM           0.06867
######select the varialbes which the SA selected###########
subset(inputsTest,select=rownames(rf_sa$fit$importance))->inputsTestImp
###predict ######
rfSA$pred(rf_sa$fit,inputsTestImp)->r3_p4_train100_sa_pred
cbind(r3_p4_train100_pred,r3_p4_train100_sa_pred,targetsTest)->r3_p4_train100_predVsReal
####denormalized########
load("~/PED/newWayPrepareData/r3_p4.RData")
r3_p4[,"O3"]->O3
apply(r3_p4_train100_predVsReal,2,function(x) unormalized(O3,x))->r3_p4_train100_predVsReal_denorm
##############the limition is 0.11ppm/m3#############
####select the rows that targetsTest is more than 0.11
r3_p4_train100_predVsReal_denorm[r3_p4_train100_predVsReal_denorm[,"targetsTest"]>0.11,]->r3_p4_train100_predVsReal_denorm_H
r3_p4_train100_predVsReal_denorm[r3_p4_train100_predVsReal_denorm[,"targetsTest"]<=0.11,]->r3_p4_train100_predVsReal_denorm_L
####1689 rows that higher than 0.11####
######33416 rows that lower than 0.11###
###########errors #########
#########change the coloum names############
colnames(r3_p4_train100_predVsReal_denorm)<-c("RF","SA+RF","Real")
#error between RF with real value#####
modelErrors(r3_p4_train100_predVsReal_denorm[,"RF"],r3_p4_train100_predVsReal_denorm[,"Real"])->error_r3_p4_train100_RF
#error between SA+RF with real value#####
modelErrors(r3_p4_train100_predVsReal_denorm[,"SA+RF"],r3_p4_train100_predVsReal_denorm[,"Real"])->error_r3_p4_train100_RFvsSARF
error_r3_p4_train100_RF
##     MAE    RMSE    RELE 
## 0.02560 0.03253 3.04011
error_r3_p4_train100_RFvsSARF
##     MAE    RMSE    RELE 
## 0.02416 0.03015 2.97655
save(error_r3_p4_train100_RF,error_r3_p4_train100_RFvsSARF,file="error_rf_r3_p4_training100.RData")

data.frame(r3_p4_train100_predVsReal_denorm)->r3_p4_train100_predVsReal_denorm
r3_p4_train100_predVsReal_denorm$level<-ifelse(r3_p4_train100_predVsReal_denorm[,"Real"]>0.11,"H","L")
###############reshpae all the level data################
reshape(r3_p4_train100_predVsReal_denorm[1:400,],varying=list(names(r3_p4_train100_predVsReal_denorm[,1:3])),v.names="Ozone",timevar="modelType",times=names(r3_p4_train100_predVsReal_denorm[,1:3]),direction = "long")->r3_p4_train100_test400_reshape

pdf("r3_p4_train100_test400.pdf",width=11,height=6,bg="transparent")
ggplot(r3_p4_train100_test400_reshape,aes(x=id,y=Ozone,group=modelType,color=modelType,shape=modelType))+
        geom_line(aes(linetype=modelType),size=0.8)+
        geom_point(size=2,fill="white")+
        xlab("Samples")+ylab("Ozone")+ggtitle("Rolling time 3, predict next 4 hours, trainSize100,testSize400")->r3_p4_plot

r3_p4_plot<-r3_p4_plot+theme(
    panel.background = element_rect(fill = "transparent"), # or theme_blank()
    panel.grid.minor = element_blank(), 
    panel.grid.major = element_blank(),
    plot.background = element_rect(fill = "transparent"),
    axis.line=element_line(colour="black") 
)

r3_p4_plot<-r3_p4_plot+theme(axis.title.x=element_text(colour="black",size=17),axis.title.y=element_text(colour="black",size=17))
r3_p4_plot<-r3_p4_plot+theme(axis.text.x=element_text(colour="black",size=15),axis.text.y=element_text(colour="black",size=13))
r3_p4_plot<-r3_p4_plot+theme(legend.title = element_text(colour="black", size=17, face="bold"))
r3_p4_plot
dev.off()
## pdf 
##   2
r3_p4_plot

plot of chunk unnamed-chunk-1

#################high level##################################
colnames(r3_p4_train100_predVsReal_denorm_H)<-c("RF","SA+RF","Real")
####errors between RF and Real
modelErrors(r3_p4_train100_predVsReal_denorm_H[,"RF"],r3_p4_train100_predVsReal_denorm_H[,"Real"])->error_r3_p4_train100_H_RF
error_r3_p4_train100_H_RF
##     MAE    RMSE    RELE 
## 0.05546 0.06374 0.40494
###errors between SA + RF Real####
modelErrors(r3_p4_train100_predVsReal_denorm_H[,"SA+RF"],r3_p4_train100_predVsReal_denorm_H[,"Real"])->error_r3_p4_train100_H_RFvsSARF
error_r3_p4_train100_H_RFvsSARF
##     MAE    RMSE    RELE 
## 0.04729 0.05505 0.34364
save(error_r3_p4_train100_H_RF,error_r3_p4_train100_H_RFvsSARF,file="error_rf_r3_p4_train100_H.RData")
data.frame(r3_p4_train100_predVsReal_denorm_H)->r3_p4_train100_predVsReal_denorm_H
###reshape####
reshape(r3_p4_train100_predVsReal_denorm_H[1:300,],varying=list(names(r3_p4_train100_predVsReal_denorm_H[,1:3])),v.names="Ozone",timevar="modelType",times=names(r3_p4_train100_predVsReal_denorm_H[,1:3]),direction = "long")->r3_p4_train100_test300_H_reshape

pdf("r3_p4_train100_test300_H.pdf",width=11,height=6,bg="transparent")
ggplot(r3_p4_train100_test300_H_reshape,aes(x=id,y=Ozone,group=modelType,color=modelType,shape=modelType))+
        geom_line(aes(linetype=modelType),size=0.8)+
        geom_point(size=2,fill="white")+
        xlab("Samples")+ylab("Ozone")+ggtitle("Rolling time 3, predict next 4 hours, trainSize100,testSize300,high Level")->r3_p4_H_plot

r3_p4_H_plot<-r3_p4_H_plot+theme(
    panel.background = element_rect(fill = "transparent"), # or theme_blank()
    panel.grid.minor = element_blank(), 
    panel.grid.major = element_blank(),
    plot.background = element_rect(fill = "transparent"),
    axis.line=element_line(colour="black") 
)

r3_p4_H_plot<-r3_p4_H_plot+theme(axis.title.x=element_text(colour="black",size=17),axis.title.y=element_text(colour="black",size=17))
r3_p4_H_plot<-r3_p4_H_plot+theme(axis.text.x=element_text(colour="black",size=15),axis.text.y=element_text(colour="black",size=13))
r3_p4_H_plot<-r3_p4_H_plot+theme(legend.title = element_text(colour="black", size=17, face="bold"))
r3_p4_H_plot
dev.off()
## pdf 
##   2
r3_p4_H_plot

plot of chunk unnamed-chunk-1

#################low level##################################
colnames(r3_p4_train100_predVsReal_denorm_L)<-c("RF","SA+RF","Real")
####errors between RF and Real
modelErrors(r3_p4_train100_predVsReal_denorm_L[,"RF"],r3_p4_train100_predVsReal_denorm_L[,"Real"])->error_r3_p4_train100_L_RF
###errors between SA + RF Real####
modelErrors(r3_p4_train100_predVsReal_denorm_L[,"SA+RF"],r3_p4_train100_predVsReal_denorm_L[,"Real"])->error_r3_p4_train100_L_RFvsSARF
save(error_r3_p4_train100_L_RF,error_r3_p4_train100_L_RFvsSARF,file="error_rf_r3_p4_train100_L.RData")
data.frame(r3_p4_train100_predVsReal_denorm_L)->r3_p4_train100_predVsReal_denorm_L
reshape(r3_p4_train100_predVsReal_denorm_L[1:300,],varying=list(names(r3_p4_train100_predVsReal_denorm_L[,1:3])),v.names="Ozone",timevar="modelType",times=names(r3_p4_train100_predVsReal_denorm_L[,1:3]),direction = "long")->r3_p4_train100_test300_L_reshape


pdf("r3_p4_train100_test300_L.pdf",width=11,height=6,bg="transparent")
ggplot(r3_p4_train100_test300_L_reshape,aes(x=id,y=Ozone,group=modelType,color=modelType,shape=modelType))+
        geom_line(aes(linetype=modelType),size=0.8)+
        geom_point(size=2,fill="white")+
        xlab("Samples")+ylab("Ozone")+ggtitle("Rolling time 3, predict next 4 hours, trainSize100,testSize300,low Level")->r3_p4_L_plot

r3_p4_L_plot<-r3_p4_L_plot+theme(
    panel.background = element_rect(fill = "transparent"), # or theme_blank()
    panel.grid.minor = element_blank(), 
    panel.grid.major = element_blank(),
    plot.background = element_rect(fill = "transparent"),
    axis.line=element_line(colour="black") 
)
r3_p4_L_plot<-r3_p4_L_plot+theme(axis.title.x=element_text(colour="black",size=17),axis.title.y=element_text(colour="black",size=17))
r3_p4_L_plot<-r3_p4_L_plot+theme(axis.text.x=element_text(colour="black",size=15),axis.text.y=element_text(colour="black",size=13))
r3_p4_L_plot<-r3_p4_L_plot+theme(legend.title = element_text(colour="black", size=17, face="bold"))
r3_p4_L_plot
dev.off()
## pdf 
##   2
r3_p4_L_plot

plot of chunk unnamed-chunk-1