library(openxlsx)
library(caret) 
library(pROC)
library(randomForest)
#install.packages("rfPermute")
library(rfPermute)

data <-read.xlsx("inputdata.xlsx", sheet="Tiger abundant",na.strings = "-") #Tiger abundant #Tiger less abundant



data$Area<-factor(data$Area) 
data$Year<-factor(data$Year)
data_onlyinter <-data[7:29]

MyNorm <- function(x){ (x-mean(x))/sd(x)}
MyMinMax<- function(x){ (x-min(x))/(max(x)-min(x)) }


data.fun <- data.frame(MesopredatorA=MesopredatorA,MesopredatorB=MesopredatorB,
                       Large.herbivore=Large.herbivore,Small.mammalA=Small.mammalA,  
                       Small.mammalB=Small.mammalB, Habitat.vegetation=Habitat.vegetation,
                       Human.activity=Human.activity, Top.predator=Top.predator)


set.seed(19961126)
biomass_rf<- randomForest(Large.herbivore  ~ ., 
                          #    Stop.deforestation + 
                          #    Season +
                          #   CoW +
                          #    Year  + 
                          #    Area +
                          # #  Top.predator +
                          #   Large.herbivore + 
                          #    MesopredatorA + 
                          #   MesopredatorB + 
                          #   Small.mammalA +
                          #   Small.mammalB + 
                          #    Habitat.vegetation + 
                          #  Human.activity,
                          data=data.fun,
                          ntree=1000, mtry=5,
                          importance =TRUE,
                          proximity=TRUE,
                          rsq=TRUE)
print(biomass_rf)
importance<- round(importance(biomass_rf,type=1), 2);data.frame(imporatance=importance[order(importance),decreasing=TRUE])
varImpPlot(biomass_rf, type=1,main="variable importance",)