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


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


```




###Forestfire

```{r}

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

 set.seed(42)
df<-forestfires[sample(nrow(forestfires)),]
folds <- createFolds(df$area, k = 10,list = FALSE)

```
```{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_lf <- c()
time_rf <- c()
time_rlf <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -13]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,13]
    xtrain <- df[-testIndexes, -13]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,13]
    
    # preproc <- preProcess(xtrain, method = c("center", "scale"))
    # scaled.train <- predict(preproc, newdata = xtrain)
    # scaled.test <- predict(preproc, newdata = xtest)

   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)
   mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
   print(mse_lf[i])

   rm(rlf)
    
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata = xtest)-ytest)^2)
   print(mse_rf[i])
   
   rm(rf)
   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
   gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
# 
rlf_forestfires <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_forestfires,'rlf_forestfires.csv')
```

###Studentperformance

```{r}
#clean up memory
rm(list=ls())
gc()
Studentperformance<- read.arff('Studentperformance.arff')


 set.seed(42)
df<-Studentperformance[sample(nrow(Studentperformance)),]
#df$area <- log(df$area+1)

```
```{r,warning=FALSE}


#folds <- cut(seq(1,nrow(df)),breaks=10,labels=FALSE)
folds <- createFolds(df$G3, k = 10,list = FALSE)#cut(seq(1,nrow(df)),breaks=10,labels=FALSE)
mse_rf <- c()
mse_lf <- c()
mse_lf1 <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -33]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,33]
    xtrain <- df[-testIndexes, -33]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,33]
    

    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    rm(rlf)
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata = xtest)-ytest)^2)
   print(mse_rf[i])
   
   rm(rf)
   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()
}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_Studentperformance <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_Studentperformance,'rlf_Studentperformance.csv')
```




###cars

```{r}
#clean up memory
rm(list=ls())
gc()
cars<- read.arff('cars.arff')

 set.seed(42)
df<-cars[sample(nrow(cars)),]
#df$area <- log(df$area+1)
folds <- createFolds(df$Price, k = 10,list = FALSE)
```
```{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_lf <- c()
mse_lf1 <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -1]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,1]
    xtrain <- df[-testIndexes, -1]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,1]
    
    # preproc <- preProcess(xtrain, method = c("center", "scale"))
    # scaled.train <- predict(preproc, newdata = xtrain)
    # scaled.test <- predict(preproc, newdata = xtest)
    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    rm(rlf)
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata = xtest)-ytest)^2)
   print(mse_rf[i])
   rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()
}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_cars <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_cars,'rlf_cars.csv')
```


###abalone

```{r}
#clean up memory
rm(list=ls())
gc()
abalone <- read.arff("abalone.arff")



 set.seed(42)
df<-abalone[sample(nrow(abalone)),]
#df$area <- log(df$area+1)
folds <- createFolds(df$rings, k = 10,list = FALSE)



```

```{r,warning=FALSE}


mse_rf <- c()
mse_lf <- c()
mse_rlf <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -9]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,9]
    xtrain <- df[-testIndexes, -9]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,9]
    


    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata =xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    # 
     rm(rlf)
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata = xtest)-ytest)^2)
   print(mse_rf[i])
   rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()
}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_abalone <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_abalone,'rlf_abalone.csv')

```


###Kinematics of Robot Arm

```{r}
#clean up memory
rm(list=ls())
gc()
#untar("Ailerons/delta_ailerons.tgz",files='delta_ailerons.data')
Kinematics<-  read.arff("kin8nm.arff")#read.table("Kinematics/kin8nm.data",header = FALSE)

set.seed(42)
df<-Kinematics[sample(nrow(Kinematics)),]

folds <- createFolds(df$y, k = 10,list = FALSE)#cut(seq(1,nrow(df)),breaks=10,labels=FALSE)
#folds <- cut(seq(1,nrow(df)),breaks=10,labels=FALSE)



```





```{r,warning=FALSE}


mse_rf <- c()
mse_lf <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -9]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,9]
    xtrain <- df[-testIndexes, -9]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,9]
    

    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    rm(rlf)
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata =xtest)-ytest)^2)
   print(mse_rf[i])
   
      rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_kin8nm <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_kin8nm,'rlf_kin8nm.csv')


```



###naval plant

```{r}
#clean up memory
rm(list=ls())
gc()

naval<-  read.arff("naval_propulsion.arff")

set.seed(42)
df<-naval[sample(nrow(naval)),]

folds <- createFolds(df$gt_compressor_decay_state_coefficient, k = 10,list = FALSE)




```





```{r,warning=FALSE}


mse_rf <- c()
mse_lf <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -15]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,15]
    xtrain <- df[-testIndexes, -15]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,15]
    

    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    rm(rlf)
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata =xtest)-ytest)^2)
   print(mse_rf[i])
   
      rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_naval <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_naval,'rlf_naval.csv')


```




### geographical_origin_of_music

```{r}
#clean up memory
rm(list=ls())
gc()

geomusic<-  read.arff("geomusic.arff")

set.seed(42)
df<-geomusic[sample(nrow(geomusic)),-118] #remove longitude

folds <- createFolds(df$latitude, k = 10,list = FALSE)




```





```{r,warning=FALSE}


mse_rf <- c()
mse_lf <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -117]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,117]
    xtrain <- df[-testIndexes, -117]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,117]
    

    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    rm(rlf)
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata =xtest)-ytest)^2)
   print(mse_rf[i])
   
      rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_geomusic <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_geomusic,'rlf_geomusic.csv')


```

### solar

```{r}
#clean up memory
rm(list=ls())
gc()

solar_flare<-  read.arff("solar_flare.arff")

set.seed(42)
df<-solar_flare[sample(nrow(solar_flare)),-c(12,13)] #remove longitude

folds <- createFolds(df$c_class_flares, k = 10,list = FALSE)




```





```{r,warning=FALSE}


mse_rf <- c()
mse_lf <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -11]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,11]
    xtrain <- df[-testIndexes, -11]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,11]
    

    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    rm(rlf)
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata =xtest)-ytest)^2)
   print(mse_rf[i])
   
      rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_solar <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_solar,'rlf_solar.csv')


```



### concrete

```{r}
#clean up memory
rm(list=ls())
gc()

concrete<-  read.arff("concrete.arff")

set.seed(42)
df<-concrete[sample(nrow(concrete)),] #remove longitude

folds <- createFolds(df$strength, k = 10,list = FALSE)




```





```{r,warning=FALSE}


mse_rf <- c()
mse_lf <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -9]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,9]
    xtrain <- df[-testIndexes, -9]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,9]
    

    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    rm(rlf)
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata =xtest)-ytest)^2)
   print(mse_rf[i])
   
      rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_concrete <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_concrete,'rlf_concrete.csv')


```




### socmob

```{r}
#clean up memory
rm(list=ls())
gc()

socmob<-  read.arff("socmob.arff")

set.seed(42)
df<-socmob[sample(nrow(socmob)),] #remove longitude

folds <- createFolds(df$counts_for_sons_current_occupation, k = 10,list = FALSE)




```





```{r,warning=FALSE}


mse_rf <- c()
mse_lf <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -6]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,6]
    xtrain <- df[-testIndexes, -6]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,6]
    

    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    rm(rlf)
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata =xtest)-ytest)^2)
   print(mse_rf[i])
   
      rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_socmob <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_socmob,'rlf_socmob.csv')


```


### energy

```{r}
#clean up memory
rm(list=ls())
gc()

energy<-  read.arff("energy.arff")

set.seed(42)
df<-energy[sample(nrow(energy)),-10] #remove cooling load

folds <- createFolds(df$heating_load, k = 10,list = FALSE)




```





```{r,warning=FALSE}


mse_rf <- c()
mse_lf <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -9]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,9]
    xtrain <- df[-testIndexes, -9]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,9]
    

    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    rm(rlf)
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata =xtest)-ytest)^2)
   print(mse_rf[i])
   
      rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_energy <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_energy,'rlf_energy.csv')


```

### fish

```{r}
#clean up memory
rm(list=ls())
gc()

fish<-  read.arff("fish.arff")

set.seed(42)
df<-fish[sample(nrow(fish)),] #remove cooling load

folds <- createFolds(df$LC50, k = 10,list = FALSE)




```





```{r,warning=FALSE}


mse_rf <- c()
mse_lf <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -7]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,7]
    xtrain <- df[-testIndexes, -7]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,7]
    

    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    rm(rlf)
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata =xtest)-ytest)^2)
   print(mse_rf[i])
   
      rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_fish <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_fish,'rlf_fish.csv')


```


### airfoil

```{r}
#clean up memory
rm(list=ls())
gc()

airfoil<-  read.arff("airfoil.arff")

set.seed(42)
df<-airfoil[sample(nrow(airfoil)),] #remove cooling load

folds <- createFolds(df$sound_pressure, k = 10,list = FALSE)




```





```{r,warning=FALSE}


mse_rf <- c()
mse_lf <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -6]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,6]
    xtrain <- df[-testIndexes, -6]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,6]
    

    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    rm(rlf)
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata =xtest)-ytest)^2)
   print(mse_rf[i])
   
      rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_airfoil <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_airfoil,'rlf_airfoil.csv')


```



### redwine

```{r}
#clean up memory
rm(list=ls())
gc()

redwine<-  read.arff("redwine.arff")

set.seed(42)
df<-redwine[sample(nrow(redwine)),] #remove cooling load

folds <- createFolds(df$quality, k = 10,list = FALSE)




```





```{r,warning=FALSE}


mse_rf <- c()
mse_lf <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -12]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,12]
    xtrain <- df[-testIndexes, -12]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,12]
    

    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    rm(rlf)
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata =xtest)-ytest)^2)
   print(mse_rf[i])
   
      rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_redwine <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_redwine,'rlf_redwine.csv')


```


### auction

```{r}
#clean up memory
rm(list=ls())
gc()

auction<-  read.arff("auction.arff")

set.seed(42)
df<-auction[sample(nrow(auction)),-8] #remove cooling load

folds <- createFolds(df$verification.time, k = 10,list = FALSE)




```





```{r,warning=FALSE}


mse_rf <- c()
mse_lf <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -8]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,8]
    xtrain <- df[-testIndexes, -8]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,8]
    

    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    rm(rlf)
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata =xtest)-ytest)^2)
   print(mse_rf[i])
   
      rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_auction <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_auction,'rlf_auction.csv')


```


### space_ga

```{r}
#clean up memory
rm(list=ls())
gc()

space_ga<-  read.arff("space_ga.arff")

set.seed(42)
df<-space_ga[sample(nrow(space_ga)),] #remove cooling load

folds <- createFolds(df$ln_votes_pop, k = 10,list = FALSE)




```





```{r,warning=FALSE}


mse_rf <- c()
mse_lf <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -1]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,1]
    xtrain <- df[-testIndexes, -1]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,1]
    

    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    rm(rlf)
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata =xtest)-ytest)^2)
   print(mse_rf[i])
   
      rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_space <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_space,'rlf_space.csv')


```

### whitewine

```{r}
#clean up memory
rm(list=ls())
gc()

whitewine<-  read.arff("whitewine.arff")

set.seed(42)
df<-whitewine[sample(nrow(whitewine)),] 

folds <- createFolds(df$quality, k = 10,list = FALSE)




```





```{r,warning=FALSE}


mse_rf <- c()
mse_lf <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -12]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,12]
    xtrain <- df[-testIndexes, -12]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,12]
    

    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    rm(rlf)
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata =xtest)-ytest)^2)
   print(mse_rf[i])
   
      rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_white <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_white,'rlf_white.csv')


```



### cpu

```{r}
#clean up memory
rm(list=ls())
gc()

cpu<-  read.arff("cpu.arff")

set.seed(42)
df<-cpu[sample(nrow(cpu)),] 

folds <- createFolds(df$usr, k = 10,list = FALSE)




```





```{r,warning=FALSE}


mse_rf <- c()
mse_lf <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -22]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,22]
    xtrain <- df[-testIndexes, -22]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,22]
    

    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    rm(rlf)
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata =xtest)-ytest)^2)
   print(mse_rf[i])
   
      rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_cpu <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_cpu,'rlf_cpu.csv')


```


### pumadyn

```{r}
#clean up memory
rm(list=ls())
gc()

pumadyn<-  read.arff("pumadyn.arff")

set.seed(42)
df<-pumadyn[sample(nrow(pumadyn)),] 

folds <- createFolds(df$thetadd6, k = 10,list = FALSE)




```





```{r,warning=FALSE}


mse_rf <- c()
mse_lf <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -33]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,33]
    xtrain <- df[-testIndexes, -33]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,33]
    

    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    rm(rlf)
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata =xtest)-ytest)^2)
   print(mse_rf[i])
   
      rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_pumadyn <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_pumadyn,'rlf_pumadyn.csv')


```




### grid

```{r}
#clean up memory
rm(list=ls())
gc()

grid<-  read.arff("grid.arff")

set.seed(42)
df<-grid[sample(nrow(grid)),-14] #remove stabf 

folds <- createFolds(df$stab, k = 10,list = FALSE)




```





```{r,warning=FALSE}


mse_rf <- c()
mse_lf <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -13]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,13]
    xtrain <- df[-testIndexes, -13]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,13]
    

    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    rm(rlf)
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata =xtest)-ytest)^2)
   print(mse_rf[i])
   
      rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_grid <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_grid,'rlf_grid.csv')


```



### Brazil Housing

```{r}
#clean up memory
rm(list=ls())
gc()

brazilianhousing<-  read.arff("brazilianhousing.arff")

set.seed(42)
df<-brazilianhousing[sample(nrow(brazilianhousing)),] 
df$total <- log(df$total)
folds <- createFolds(df$total, k = 10,list = FALSE)




```





```{r,warning=FALSE}


mse_rf <- c()
mse_lf <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -13]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,13]
    xtrain <- df[-testIndexes, -13]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,13]
    

    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    rm(rlf)
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata =xtest)-ytest)^2)
   print(mse_rf[i])
   
      rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_brazilianhousing <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_brazilianhousing,'rlf_brazilianhousing.csv')


```






### Miami Housing

```{r}
#clean up memory
rm(list=ls())
gc()

miamihousing<-  read.arff("miamihousing.arff")

set.seed(42)
df<-miamihousing[sample(nrow(miamihousing)),] 
df$SALE_PRC <- log(df$SALE_PRC)
folds <- createFolds(df$SALE_PRC, k = 10,list = FALSE)




```





```{r,warning=FALSE}


mse_rf <- c()
mse_lf <- 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]
    

    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    rm(rlf)
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata =xtest)-ytest)^2)
   print(mse_rf[i])
   
      rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_miamihousing <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_miamihousing,'rlf_miamihousing.csv')


```




### Fifa

```{r}
#clean up memory
rm(list=ls())
gc()

fifa<-  read.arff("fifa.arff")

set.seed(42)
df<-fifa[sample(nrow(fifa)),-5]  #remove nationality since RF can't handle  categorical predictors with more than 53 categories.
df$wage_eur <- log(df$wage_eur)
folds <- createFolds(df$wage_eur, k = 10,list = FALSE)




```





```{r,warning=FALSE}


mse_rf <- c()
mse_lf <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -1]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,1]
    xtrain <- df[-testIndexes, -1]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,1]
    

    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    rm(rlf)
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata =xtest)-ytest)^2)
   print(mse_rf[i])
   
      rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_fifa <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_fifa,'rlf_fifa.csv')


```


### kingscounty

```{r}
#clean up memory
rm(list=ls())
gc()

kingscounty<-  read.arff("kingscounty.arff")

set.seed(42)


df<-kingscounty[sample(nrow(kingscounty)),-15]  #remove zipcode variable since RF can't handle  categorical predictors with more than 53 categories.

df$price <- log(df$price)
folds <- createFolds(df$price, k = 10,list = FALSE)




```





```{r,warning=FALSE}


mse_rf <- c()
mse_lf <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -1]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,1]
    xtrain <- df[-testIndexes, -1]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,1]
    

    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    rm(rlf)
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata =xtest)-ytest)^2)
   print(mse_rf[i])
   
      rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_kingscounty <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_kingscounty,'rlf_kingscounty.csv')


```




###Califonia housing price
```{r}

#clean up memory
rm(list=ls())
gc()
set.seed(42)
calihou <- read.arff('calinforniahousing.arff')

df<-calihou[sample(nrow(calihou)),] 

df$medianHouseValue <- log(df$medianHouseValue)
folds <- createFolds(df$medianHouseValue, k = 10,list = FALSE)#cut(seq(1,nrow(df)),breaks=10,labels=FALSE)

```

```{r,warning=FALSE}


#folds <- cut(seq(1,nrow(df)),breaks=10,labels=FALSE)

mse_rf <- c()
mse_lf <- c()
mse_lf1 <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -9]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,9]
    xtrain <- df[-testIndexes, -9]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,9]
    
    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata = xtest)-ytest)^2)
   print(mse_rf[i])
   

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_calihou <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_calihou,'rlf_calihou.csv')

```

###superconductivity
```{r}
#clean up memory
rm(list=ls())
gc()
set.seed(42)
superconductivity <- read.arff('superconductivity.arff')

df<-superconductivity[sample(nrow(superconductivity)),] 


folds <- createFolds(df$critical_temp, k = 10,list = FALSE)#cut(seq(1,nrow(df)),breaks=10,labels=FALSE)

```

```{r,warning=FALSE}


#folds <- cut(seq(1,nrow(df)),breaks=10,labels=FALSE)

mse_rf <- c()
mse_lf <- c()
mse_lf1 <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -82]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,82]
    xtrain <- df[-testIndexes, -82]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,82]
    
    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata = xtest)-ytest)^2)
   print(mse_rf[i])
   

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_superconductivity <- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_superconductivity,'rlf_superconductivity.csv')

```


###insurance
```{r}
#clean up memory
rm(list=ls())
gc()
set.seed(42)
healthinsurance <- read.arff('health_insurance.arff')

df<-healthinsurance[sample(nrow(healthinsurance)),-13] #ignore sampling weight


folds <- createFolds(df$whrswk, k = 10,list = FALSE)#cut(seq(1,nrow(df)),breaks=10,labels=FALSE)

```

```{r,warning=FALSE}


#folds <- cut(seq(1,nrow(df)),breaks=10,labels=FALSE)

mse_rf <- c()
mse_lf <- c()
mse_lf1 <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -1]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,1]
    xtrain <- df[-testIndexes, -1]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,1]
    
    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata = xtest)-ytest)^2)
   print(mse_rf[i])
   

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_healthinsurance<- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_healthinsurance,'rlf_healthinsurance.csv')

```


###cpswage
```{r}
#clean up memory
rm(list=ls())
gc()
set.seed(42)
cps88wage <- read.arff('cps88wage.arff')

df<-cps88wage[sample(nrow(cps88wage)),] #ignore sampling weight

df$wage <- log(df$wage)

folds <- createFolds(df$wage, k = 10,list = FALSE)#cut(seq(1,nrow(df)),breaks=10,labels=FALSE)

```

```{r,warning=FALSE}


#folds <- cut(seq(1,nrow(df)),breaks=10,labels=FALSE)

mse_rf <- c()
mse_lf <- c()
mse_lf1 <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -1]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,1]
    xtrain <- df[-testIndexes, -1]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,1]
    
    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata = xtest)-ytest)^2)
   print(mse_rf[i])
   

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_cps88wage<- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_cps88wage,'rlf_cps88wage.csv')

```


###protein
```{r}

#clean up memory
rm(list=ls())
gc()
set.seed(42)
protein <- read.arff('protein.arff')

df<-protein[sample(nrow(protein)),] 



folds <- createFolds(df$RMSD , k = 10,list = FALSE)#cut(seq(1,nrow(df)),breaks=10,labels=FALSE)

```

```{r,warning=FALSE}


#folds <- cut(seq(1,nrow(df)),breaks=10,labels=FALSE)

mse_rf <- c()
mse_lf <- c()
mse_lf1 <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -1]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,1]
    xtrain <- df[-testIndexes, -1]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,1]
    
    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)
  print(i)

          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    #


   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata = xtest)-ytest)^2)
   print(mse_rf[i])

       rm(rlf)
       rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_protein<- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_protein,'rlf_protein.csv')

```


###sarcos
```{r}
#clean up memory
rm(list=ls())
gc()
sarcos <- read.arff('sarcos.arff')

df<-sarcos[sample(nrow(sarcos)),] 



folds <- createFolds(df$V22 , k = 10,list = FALSE)#cut(seq(1,nrow(df)),breaks=10,labels=FALSE)

```

```{r,warning=FALSE}


#folds <- cut(seq(1,nrow(df)),breaks=10,labels=FALSE)

mse_rf <- c()
mse_lf <- c()
mse_lf1 <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- df[testIndexes, -22]
    #xtest<- xtest[,-2]
    ytest <- df[testIndexes,22]
    xtrain <- df[-testIndexes, -22]
    #xtrain <- xtrain[,-2]
    ytrain <- df[-testIndexes,22]
    
    
   rlf <- RLForest(ytrain~.,data=xtrain,ntree=100,ntreesub=10,replace=FALSE,data_driven=TRUE)


          mse_lf[i] <- mean((predict(rlf,newdata = xtest)-ytest)^2)
      print(mse_lf[i])
    # 
    
    
   rf <- randomForest(ytrain~.,data=xtrain,ntree=100)
   mse_rf[i]<- mean((predict(rf,newdata = xtest)-ytest)^2)
   print(mse_rf[i])
   
       rm(rlf)
         rm(rf)

   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
  gc()

}


cat('\nMSE of RF:',mean(mse_rf))
cat('\nMSE of LF:',mean(mse_lf))
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_lf)/sqrt(10) )
rlf_sarcos<- data.frame(lf=mse_lf,rf=mse_rf)
write.csv(rlf_sarcos,'rlf_sarcos.csv')

```





