---
title: "Simulation"
author: "Tian Qin"
date: "2024-01-30"
output: html_document
---

## Simulation data

### Simulation

```{r,warning=FALSE}

library(RiemannLebesgueForest)
library(randomForest)
library(MASS)
library(caret)
set.seed(42)


X = data.frame(replicate(100,(runif(1500,min=0, max=1))))  #U[0,1]^100


fx <- function(x){
  
  10* prod( exp(-2*x[1:5]^2) )+sum(x[6:35])
  
  
}
ysig <- apply(X,1,fx)


yNoise = rnorm(1500,sd=1.3)
y = ysig + yNoise

# train_index <- sample(1:nrow(X),size = 1000)
# xtrain <- X[train_index,]
# ytrain <- y[train_index]
# xtest <- X[-train_index,]
# ytest <- y[-train_index]
```


#Feature selection
```{r}
set.seed(42)
folds <- createFolds(y, k = 10,list = FALSE)

rff <- randomForest(y~.,data=X,ntree=100,importance=TRUE)
i_scores <- importance(rff)
i_scores_ind <- sort(i_scores[,1],index.return=TRUE,decreasing = TRUE)$ix[1:35]

selected_col <- rownames(i_scores)[i_scores_ind]


mse_rf <- c()
mse_rlf <- c()
time_rf <- c()
time_rlf <- c()
mse_rlfR <- c()
mse_rfR <- c()
for(i in 1:10){
    testIndexes <- which(folds==i,arr.ind=TRUE)
    xtest<- X[testIndexes, selected_col]
    #xtest<- xtest[,-2]
    ytest <- y[testIndexes]
    xtrain <- X[-testIndexes, selected_col]
    #xtrain <- xtrain[,-2]
    ytrain <- y[-testIndexes]
    
  rlfR <- RLForest(ytrain~.,data=X[-testIndexes,],ntree=100,ntreesub=1,replace=FALSE,Lp=0.8)
   predR <- predict(rlfR,newdata = X[testIndexes,])
mse_rlfR[i] <- mean((predR-ytest)^2)
   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   
   
  rfr <- RLForest(ytrain~.,data=X[-testIndexes,],ntree=100)
   predrR <- predict(rfr,newdata = X[testIndexes,])
mse_rfR[i] <- mean((predrR-ytest)^2)
   
   rm(rf)
   rm(xtest)
   rm(ytest)
   rm(xtrain)
   rm(ytrain)
   rm(pred)
  gc()

}

cat('\nMSE of RF:',mean(mse_rfR))
cat('\nMSE of RLF:',mean(mse_rlfR))

cat('\nMSE of RF+F:',mean(mse_rf))
cat('\nMSE of RLF+F:',mean(mse_rlf))
cat('\n margin of error of RF:',qt(0.975,9)* sd(mse_rfR)/sqrt(10)  )
cat('\n margin of error LF:',qt(0.975,9)* sd(mse_rlfR)/sqrt(10) )

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

