---
title: "Untitled"
author: "Tian Qin"
date: "2024-01-14"
output: html_document
---


###Library
```{r}
library(foreign)
library(RiemannLebesgueForest)
library(randomForest)
library(MASS)
library(caret)
library(gbm)
library(xgboost)
library(RWeka)
```


###Moneyball

```{r}



#clean up memory
rm(list=ls())
gc()
baseball<- read.arff('baseball.arff')

 set.seed(42)
df<-baseball[sample(nrow(baseball)),]
df$RankPlayoffs <- as.numeric(df$RankPlayoffs)
df$RankSeason <- as.numeric(df$RankSeason)
folds <- createFolds(df$RS, k = 10,list = FALSE)
na.median <- function(x) replace(x, is.na(x), median(x, na.rm = TRUE))   
library(magrittr)
ok <- sapply(df, is.numeric)
df<- replace(df, ok, lapply(df[ok], na.median))

```


```{r,warning=FALSE}


#folds <- cut(seq(1,nrow(df)),breaks=10,labels=FALSE)
#cut(seq(1,nrow(df)),breaks=10,labels=FALSE)
mse_rf <- c()
mse_rlf <- c()
time_rf <- c()
time_rlf <- c()

for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -4]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,4]
    xtrain <- df[-testIndexes, -4]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,4]
    
# 
   start.time_rlf <- Sys.time()
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=1,replace=FALSE,Lp=0.8)
   pred <- predict(rlf,newdata = xtest)
   end.time_rlf <- Sys.time()
   mse_rlf[i] <- mean((pred-ytest)^2)

   time.taken_rlf <-difftime(end.time_rlf, start.time_rlf, units = "secs")[[1]]
   print(mse_rlf[i])
   time_rlf[i] <-  time.taken_rlf
   rm(rlf)
    
   start.time_rf <- Sys.time()
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   pred <- predict(rf,newdata = xtest)
   mse_rf[i]<- mean((pred-ytest)^2)
   end.time_rf <- Sys.time()
   print(mse_rf[i])
   time.taken_rf <- difftime(end.time_rf, start.time_rf, units = "secs")[[1]]
   time_rf[i] <- time.taken_rf   
   

   
  #  rm(rf)
  #  rm(xtest)
  #  rm(ytest)
  #  rm(xtrain)
  #  rm(ytrain)
  #  rm(pred)
  # gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of RLF:',mean(mse_rlf))

cat('\n margin of error of RF:',qt(0.975,9)* sd(mse_rf)/sqrt(10)  )
cat('\n margin of error LF:',qt(0.975,9)* sd(mse_rlf)/sqrt(10) )


# 
# # 
# rlf_forestfires <- data.frame(lf=mse_rlf,rf=mse_rf)
# write.csv(rlf_forestfires,'rlf_forestfires.csv')
#rlf_forestfires_time <- data.frame(lf=time_rlf,rf=time_rf)
#write.csv(rlf_forestfires_time,'rlf_forestfires_time.csv')
```



###FPS

```{r}



#clean up memory
rm(list=ls())
gc()
fpsbenchmark<- read.arff('fpsbenchmark.arff')

 set.seed(42)
df<-fpsbenchmark[sample(nrow(fpsbenchmark)),-c(1,16,25)]

folds <- createFolds(df$FPS, k = 10,list = FALSE)
na.median <- function(x) replace(x, is.na(x), median(x, na.rm = TRUE))   
library(magrittr)
ok <- sapply(df, is.numeric)
df<- replace(df, ok, lapply(df[ok], na.median))

```


```{r,warning=FALSE}


#folds <- cut(seq(1,nrow(df)),breaks=10,labels=FALSE)
#cut(seq(1,nrow(df)),breaks=10,labels=FALSE)
mse_rf <- c()
mse_rlf <- c()
time_rf <- c()
time_rlf <- c()

for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -41]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,41]
    xtrain <- df[-testIndexes, -41]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,41]
    
# 
   start.time_rlf <- Sys.time()
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=1,replace=FALSE,Lp=0.8)
   pred <- predict(rlf,newdata = xtest)
   end.time_rlf <- Sys.time()
   mse_rlf[i] <- mean((pred-ytest)^2)

   time.taken_rlf <-difftime(end.time_rlf, start.time_rlf, units = "secs")[[1]]
   print(mse_rlf[i])
   time_rlf[i] <-  time.taken_rlf
   rm(rlf)
    
   start.time_rf <- Sys.time()
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   pred <- predict(rf,newdata = xtest)
   mse_rf[i]<- mean((pred-ytest)^2)
   end.time_rf <- Sys.time()
   print(mse_rf[i])
   time.taken_rf <- difftime(end.time_rf, start.time_rf, units = "secs")[[1]]
   time_rf[i] <- time.taken_rf   
   

   
  #  rm(rf)
  #  rm(xtest)
  #  rm(ytest)
  #  rm(xtrain)
  #  rm(ytrain)
  #  rm(pred)
  # gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of RLF:',mean(mse_rlf))

cat('\n margin of error of RF:',qt(0.975,9)* sd(mse_rf)/sqrt(10)  )
cat('\n margin of error LF:',qt(0.975,9)* sd(mse_rlf)/sqrt(10) )


# 
# # 
# rlf_forestfires <- data.frame(lf=mse_rlf,rf=mse_rf)
# write.csv(rlf_forestfires,'rlf_forestfires.csv')
#rlf_forestfires_time <- data.frame(lf=time_rlf,rf=time_rf)
#write.csv(rlf_forestfires_time,'rlf_forestfires_time.csv')
```

