myfile<-"d:/Rstudio/student-mat.csv"
d1<-read.table(myfile, sep=";",header=TRUE)
library(randomForest)
## randomForest 4.6-14
## Type rfNews() to see new features/changes/bug fixes.
set.seed(123)
train <- sample(nrow(d1), nrow(d1)*0.7)
d1_train <- d1[train, ]
d1_test <- d1[-train, ]

set.seed(123)
d1_train.forest <- randomForest(G3~., data = d1_train, importance = TRUE)
d1_train.forest
## 
## Call:
##  randomForest(formula = G3 ~ ., data = d1_train, importance = TRUE) 
##                Type of random forest: regression
##                      Number of trees: 500
## No. of variables tried at each split: 10
## 
##           Mean of squared residuals: 2.965162
##                     % Var explained: 84.75
G3_predict <- predict(d1_train.forest, d1_train)
 
plot(d1_train$G3, G3_predict, main = 'Training Set',
    xlab = 'G3', ylab = 'Predict')
abline(1, 1)

G3_predict <- predict(d1_train.forest, d1_test)
 
plot(d1_test$G3, G3_predict, main = 'Testing Set',
    xlab = 'G3', ylab = 'Predict')
abline(1, 1)

summary(d1_train.forest)
##                 Length Class  Mode     
## call              4    -none- call     
## type              1    -none- character
## predicted       276    -none- numeric  
## mse             500    -none- numeric  
## rsq             500    -none- numeric  
## oob.times       276    -none- numeric  
## importance       64    -none- numeric  
## importanceSD     32    -none- numeric  
## localImportance   0    -none- NULL     
## proximity         0    -none- NULL     
## ntree             1    -none- numeric  
## mtry              1    -none- numeric  
## forest           11    -none- list     
## coefs             0    -none- NULL     
## y               276    -none- numeric  
## test              0    -none- NULL     
## inbag             0    -none- NULL     
## terms             3    terms  call
importance_d1 <- d1_train.forest$importance
head(importance_d1)
##               %IncMSE IncNodePurity
## school   0.0147867517      9.103365
## sex     -0.0015824939     12.910086
## age      0.1133724937     69.792140
## address -0.0024275316     11.877789
## famsize -0.0221735937     11.758974
## Pstatus  0.0002671014     11.445783
importance_d1 <- data.frame(importance(d1_train.forest), check.names = FALSE)
head(importance_d1)
##             %IncMSE IncNodePurity
## school   1.16989496      9.103365
## sex     -0.09989218     12.910086
## age      3.18971951     69.792140
## address -0.10927656     11.877789
## famsize -1.33271245     11.758974
## Pstatus  0.02107185     11.445783
varImpPlot(d1_train.forest, n.var = min(30, nrow(d1_train.forest$importance)),
    main = 'Top 30 - variable importance')

importance_d1 <- importance_d1[order(importance_d1$ IncNodePurity, decreasing = TRUE), ]
head(importance_d1)
##            %IncMSE IncNodePurity
## G2       39.395021    2431.14728
## G1       22.991368    1327.37653
## absences 18.663228     318.39054
## failures 10.087547     244.64509
## Fjob     -1.585402      82.32877
## age       3.189720      69.79214
write.table(importance_d1, 'importance_d1.txt', sep = '\t', col.names = NA, quote = FALSE)
set.seed(123)
d1_train.cv <- replicate(5, rfcv(d1_train[-ncol(d1_train)], d1_train$G3, cv.fold = 10, step = 1.5), simplify = FALSE)
 
d1_train.cv <- data.frame(sapply(d1_train.cv, '[[', 'error.cv'))
d1_train.cv$d1s <- rownames(d1_train.cv)
d1_train.cv <- reshape2::melt(d1_train.cv, id = 'd1s')
d1_train.cv$d1s <- as.numeric(as.character(d1_train.cv$d1s))
 
d1_train.cv.mean <- aggregate(d1_train.cv$value, by = list(d1_train.cv$d1s), FUN = mean)
head(d1_train.cv.mean, 10)
##   Group.1        x
## 1       1 3.010648
## 2       2 3.445202
## 3       3 3.087011
## 4       4 2.489154
## 5       6 2.826811
## 6       9 2.935255
## 7      14 3.642242
## 8      21 4.036282
## 9      32 4.671210
library(ggplot2)
## 
## 载入程辑包:'ggplot2'
## The following object is masked from 'package:randomForest':
## 
##     margin
ggplot(d1_train.cv.mean, aes(Group.1, x)) +
geom_line() +
theme(panel.grid = element_blank(), panel.background = element_rect(color = 'black', fill = 'transparent')) +
labs(title = '',x = 'Number of variables', y = 'Cross-validation error')

importance_d1 <- importance_d1[order(importance_d1$IncNodePurity, decreasing = TRUE), ]
 
importance_d1.select <- importance_d1[1:4, ]
importance_d1.select
##           %IncMSE IncNodePurity
## G2       39.39502     2431.1473
## G1       22.99137     1327.3765
## absences 18.66323      318.3905
## failures 10.08755      244.6451
write.table(importance_d1.select, 'importance_d1.select.txt', sep = '\t', col.names = NA, quote = FALSE)
d1_id.select <- rownames(importance_d1.select)
d1.select <- d1[ ,c(d1_id.select, 'G3')]
d1.select <- reshape2::melt(d1.select, id = 'G3')
 
ggplot(d1.select, aes(x = G3, y = value)) +
geom_point() +
geom_smooth() +
facet_wrap(~variable, ncol = 3, scale = 'free_y') +
labs(title = '',x = 'G3', y = 'Relative abundance')
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

d1.select <- d1[ ,c(d1_id.select, 'G3')]
 
set.seed(123)
train <- sample(nrow(d1.select), nrow(d1.select)*0.7)
d1_train.select <- d1.select[train, ]
d1_test.select <- d1.select[-train, ]
 
set.seed(123)
d1_train.select.forest <- randomForest(G3~., data = d1_train.select, importance = TRUE)
d1_train.select.forest
## 
## Call:
##  randomForest(formula = G3 ~ ., data = d1_train.select, importance = TRUE) 
##                Type of random forest: regression
##                      Number of trees: 500
## No. of variables tried at each split: 1
## 
##           Mean of squared residuals: 3.414538
##                     % Var explained: 82.43
G3_predict <- predict(d1_train.select.forest, d1_train.select)
 
plot(d1_train.select$G3, G3_predict, main = 'Training Set',
    xlab = 'G3', ylab = 'Predict')
abline(1, 1)

G3_predict <- predict(d1_train.select.forest, d1_test.select)
 
plot(d1_test.select$G3, G3_predict, main = 'Testing Set',
    xlab = 'G3', ylab = 'Predict')
abline(1, 1)

library(plyr)
CVgroup <- function(k,datasize,seed){
  cvlist <- list()
  set.seed(seed)
  n <- rep(1:k,ceiling(datasize/k))[1:datasize]   
  temp <- sample(n,datasize)  
  x <- 1:k
  dataseq <- 1:datasize
  cvlist <- lapply(x,function(x) dataseq[temp==x])  
  return(cvlist)
}

k <- 10
datasize <- nrow(d1)
cvlist <- CVgroup(k = k,datasize = datasize,seed = 123)

data <- d1
pred <- data.frame()   
library(plyr)
library(randomForest)
m <- seq(60,500,by = 20) 
for(j in m){   
  progress.bar <- create_progress_bar("text")  
  progress.bar$init(k)   
  for (i in 1:k){
    train <- data[-cvlist[[i]],]  
    test <- data[cvlist[[i]],]
    model <-randomForest(G3 ~.,data = d1,ntree = j)  
    prediction <- predict(model,subset(test,select = -G3))   
    randomtree <- rep(j,length(prediction))   
    kcross <- rep(i,length(prediction))   
    temp <- data.frame(cbind(subset(test,select = G3),prediction,randomtree,kcross))
    pred <- rbind(pred,temp)   
    print(paste("Random Tree:",j))  
  }
}
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 60"
## [1] "Random Tree: 60"
## [1] "Random Tree: 60"
## [1] "Random Tree: 60"
## [1] "Random Tree: 60"
## [1] "Random Tree: 60"
## [1] "Random Tree: 60"
## [1] "Random Tree: 60"
## [1] "Random Tree: 60"
## [1] "Random Tree: 60"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 80"
## [1] "Random Tree: 80"
## [1] "Random Tree: 80"
## [1] "Random Tree: 80"
## [1] "Random Tree: 80"
## [1] "Random Tree: 80"
## [1] "Random Tree: 80"
## [1] "Random Tree: 80"
## [1] "Random Tree: 80"
## [1] "Random Tree: 80"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 100"
## [1] "Random Tree: 100"
## [1] "Random Tree: 100"
## [1] "Random Tree: 100"
## [1] "Random Tree: 100"
## [1] "Random Tree: 100"
## [1] "Random Tree: 100"
## [1] "Random Tree: 100"
## [1] "Random Tree: 100"
## [1] "Random Tree: 100"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 120"
## [1] "Random Tree: 120"
## [1] "Random Tree: 120"
## [1] "Random Tree: 120"
## [1] "Random Tree: 120"
## [1] "Random Tree: 120"
## [1] "Random Tree: 120"
## [1] "Random Tree: 120"
## [1] "Random Tree: 120"
## [1] "Random Tree: 120"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 140"
## [1] "Random Tree: 140"
## [1] "Random Tree: 140"
## [1] "Random Tree: 140"
## [1] "Random Tree: 140"
## [1] "Random Tree: 140"
## [1] "Random Tree: 140"
## [1] "Random Tree: 140"
## [1] "Random Tree: 140"
## [1] "Random Tree: 140"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 160"
## [1] "Random Tree: 160"
## [1] "Random Tree: 160"
## [1] "Random Tree: 160"
## [1] "Random Tree: 160"
## [1] "Random Tree: 160"
## [1] "Random Tree: 160"
## [1] "Random Tree: 160"
## [1] "Random Tree: 160"
## [1] "Random Tree: 160"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 180"
## [1] "Random Tree: 180"
## [1] "Random Tree: 180"
## [1] "Random Tree: 180"
## [1] "Random Tree: 180"
## [1] "Random Tree: 180"
## [1] "Random Tree: 180"
## [1] "Random Tree: 180"
## [1] "Random Tree: 180"
## [1] "Random Tree: 180"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 200"
## [1] "Random Tree: 200"
## [1] "Random Tree: 200"
## [1] "Random Tree: 200"
## [1] "Random Tree: 200"
## [1] "Random Tree: 200"
## [1] "Random Tree: 200"
## [1] "Random Tree: 200"
## [1] "Random Tree: 200"
## [1] "Random Tree: 200"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 220"
## [1] "Random Tree: 220"
## [1] "Random Tree: 220"
## [1] "Random Tree: 220"
## [1] "Random Tree: 220"
## [1] "Random Tree: 220"
## [1] "Random Tree: 220"
## [1] "Random Tree: 220"
## [1] "Random Tree: 220"
## [1] "Random Tree: 220"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 240"
## [1] "Random Tree: 240"
## [1] "Random Tree: 240"
## [1] "Random Tree: 240"
## [1] "Random Tree: 240"
## [1] "Random Tree: 240"
## [1] "Random Tree: 240"
## [1] "Random Tree: 240"
## [1] "Random Tree: 240"
## [1] "Random Tree: 240"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 260"
## [1] "Random Tree: 260"
## [1] "Random Tree: 260"
## [1] "Random Tree: 260"
## [1] "Random Tree: 260"
## [1] "Random Tree: 260"
## [1] "Random Tree: 260"
## [1] "Random Tree: 260"
## [1] "Random Tree: 260"
## [1] "Random Tree: 260"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 280"
## [1] "Random Tree: 280"
## [1] "Random Tree: 280"
## [1] "Random Tree: 280"
## [1] "Random Tree: 280"
## [1] "Random Tree: 280"
## [1] "Random Tree: 280"
## [1] "Random Tree: 280"
## [1] "Random Tree: 280"
## [1] "Random Tree: 280"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 300"
## [1] "Random Tree: 300"
## [1] "Random Tree: 300"
## [1] "Random Tree: 300"
## [1] "Random Tree: 300"
## [1] "Random Tree: 300"
## [1] "Random Tree: 300"
## [1] "Random Tree: 300"
## [1] "Random Tree: 300"
## [1] "Random Tree: 300"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 320"
## [1] "Random Tree: 320"
## [1] "Random Tree: 320"
## [1] "Random Tree: 320"
## [1] "Random Tree: 320"
## [1] "Random Tree: 320"
## [1] "Random Tree: 320"
## [1] "Random Tree: 320"
## [1] "Random Tree: 320"
## [1] "Random Tree: 320"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 340"
## [1] "Random Tree: 340"
## [1] "Random Tree: 340"
## [1] "Random Tree: 340"
## [1] "Random Tree: 340"
## [1] "Random Tree: 340"
## [1] "Random Tree: 340"
## [1] "Random Tree: 340"
## [1] "Random Tree: 340"
## [1] "Random Tree: 340"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 360"
## [1] "Random Tree: 360"
## [1] "Random Tree: 360"
## [1] "Random Tree: 360"
## [1] "Random Tree: 360"
## [1] "Random Tree: 360"
## [1] "Random Tree: 360"
## [1] "Random Tree: 360"
## [1] "Random Tree: 360"
## [1] "Random Tree: 360"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 380"
## [1] "Random Tree: 380"
## [1] "Random Tree: 380"
## [1] "Random Tree: 380"
## [1] "Random Tree: 380"
## [1] "Random Tree: 380"
## [1] "Random Tree: 380"
## [1] "Random Tree: 380"
## [1] "Random Tree: 380"
## [1] "Random Tree: 380"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 400"
## [1] "Random Tree: 400"
## [1] "Random Tree: 400"
## [1] "Random Tree: 400"
## [1] "Random Tree: 400"
## [1] "Random Tree: 400"
## [1] "Random Tree: 400"
## [1] "Random Tree: 400"
## [1] "Random Tree: 400"
## [1] "Random Tree: 400"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 420"
## [1] "Random Tree: 420"
## [1] "Random Tree: 420"
## [1] "Random Tree: 420"
## [1] "Random Tree: 420"
## [1] "Random Tree: 420"
## [1] "Random Tree: 420"
## [1] "Random Tree: 420"
## [1] "Random Tree: 420"
## [1] "Random Tree: 420"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 440"
## [1] "Random Tree: 440"
## [1] "Random Tree: 440"
## [1] "Random Tree: 440"
## [1] "Random Tree: 440"
## [1] "Random Tree: 440"
## [1] "Random Tree: 440"
## [1] "Random Tree: 440"
## [1] "Random Tree: 440"
## [1] "Random Tree: 440"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 460"
## [1] "Random Tree: 460"
## [1] "Random Tree: 460"
## [1] "Random Tree: 460"
## [1] "Random Tree: 460"
## [1] "Random Tree: 460"
## [1] "Random Tree: 460"
## [1] "Random Tree: 460"
## [1] "Random Tree: 460"
## [1] "Random Tree: 460"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 480"
## [1] "Random Tree: 480"
## [1] "Random Tree: 480"
## [1] "Random Tree: 480"
## [1] "Random Tree: 480"
## [1] "Random Tree: 480"
## [1] "Random Tree: 480"
## [1] "Random Tree: 480"
## [1] "Random Tree: 480"
## [1] "Random Tree: 480"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 500"
## [1] "Random Tree: 500"
## [1] "Random Tree: 500"
## [1] "Random Tree: 500"
## [1] "Random Tree: 500"
## [1] "Random Tree: 500"
## [1] "Random Tree: 500"
## [1] "Random Tree: 500"
## [1] "Random Tree: 500"
## [1] "Random Tree: 500"
maefun <- function(pred, obs) mean(abs(pred - obs))
msefun <- function(pred, obs) mean((pred - obs)^2)
nmsefun <- function(pred, obs) mean((pred - obs)^2)/mean((mean(obs) - obs)^2)

library(dplyr)
## 
## 载入程辑包:'dplyr'
## The following objects are masked from 'package:plyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
## The following object is masked from 'package:randomForest':
## 
##     combine
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
eval2<- pred %>% group_by(randomtree, kcross) %>%   
  summarise(mae = maefun(prediction, G3),
            mse = msefun(prediction, G3),
            nmse = nmsefun(prediction, G3),)
## `summarise()` has grouped output by 'randomtree'. You can override using the `.groups` argument.
eval2
## # A tibble: 230 x 5
## # Groups:   randomtree [23]
##    randomtree kcross   mae   mse   nmse
##         <dbl>  <int> <dbl> <dbl>  <dbl>
##  1         60      1 0.573 0.808 0.0306
##  2         60      2 0.582 0.651 0.0329
##  3         60      3 0.672 1.13  0.0448
##  4         60      4 0.547 0.639 0.0250
##  5         60      5 0.426 0.380 0.0184
##  6         60      6 0.454 0.550 0.0329
##  7         60      7 0.433 0.498 0.0294
##  8         60      8 0.661 0.982 0.0438
##  9         60      9 0.524 0.752 0.0590
## 10         60     10 0.688 0.852 0.0503
## # ... with 220 more rows
library(plyr)
CVgroup <- function(k,datasize,seed){
  cvlist <- list()
  set.seed(seed)
  n <- rep(1:k,ceiling(datasize/k))[1:datasize]   
  temp <- sample(n,datasize)   
  x <- 1:k
  dataseq <- 1:datasize
  cvlist <- lapply(x,function(x) dataseq[temp==x])  
  return(cvlist)
}

k <- 10
datasize <- nrow(d1.select)
cvlist <- CVgroup(k = k,datasize = datasize,seed = 123)

data <- d1.select
pred <- data.frame()   
library(plyr)
library(randomForest)
m <- seq(60,500,by = 20)  
for(j in m){  
  progress.bar <- create_progress_bar("text")  
  progress.bar$init(k)   
  for (i in 1:k){
    train <- data[-cvlist[[i]],]  
    test <- data[cvlist[[i]],]
    model <-randomForest(G3~.,data=d1.select,ntree = j)   
    prediction <- predict(model,subset(test,select = -G3))   
    randomtree <- rep(j,length(prediction))   
    kcross <- rep(i,length(prediction))   
    temp <- data.frame(cbind(subset(test,select = G3),prediction,randomtree,kcross))
    pred <- rbind(pred,temp)   
    print(paste("Random Tree:",j))  
  }
}
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 60"
## [1] "Random Tree: 60"
## [1] "Random Tree: 60"
## [1] "Random Tree: 60"
## [1] "Random Tree: 60"
## [1] "Random Tree: 60"
## [1] "Random Tree: 60"
## [1] "Random Tree: 60"
## [1] "Random Tree: 60"
## [1] "Random Tree: 60"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 80"
## [1] "Random Tree: 80"
## [1] "Random Tree: 80"
## [1] "Random Tree: 80"
## [1] "Random Tree: 80"
## [1] "Random Tree: 80"
## [1] "Random Tree: 80"
## [1] "Random Tree: 80"
## [1] "Random Tree: 80"
## [1] "Random Tree: 80"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 100"
## [1] "Random Tree: 100"
## [1] "Random Tree: 100"
## [1] "Random Tree: 100"
## [1] "Random Tree: 100"
## [1] "Random Tree: 100"
## [1] "Random Tree: 100"
## [1] "Random Tree: 100"
## [1] "Random Tree: 100"
## [1] "Random Tree: 100"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 120"
## [1] "Random Tree: 120"
## [1] "Random Tree: 120"
## [1] "Random Tree: 120"
## [1] "Random Tree: 120"
## [1] "Random Tree: 120"
## [1] "Random Tree: 120"
## [1] "Random Tree: 120"
## [1] "Random Tree: 120"
## [1] "Random Tree: 120"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 140"
## [1] "Random Tree: 140"
## [1] "Random Tree: 140"
## [1] "Random Tree: 140"
## [1] "Random Tree: 140"
## [1] "Random Tree: 140"
## [1] "Random Tree: 140"
## [1] "Random Tree: 140"
## [1] "Random Tree: 140"
## [1] "Random Tree: 140"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 160"
## [1] "Random Tree: 160"
## [1] "Random Tree: 160"
## [1] "Random Tree: 160"
## [1] "Random Tree: 160"
## [1] "Random Tree: 160"
## [1] "Random Tree: 160"
## [1] "Random Tree: 160"
## [1] "Random Tree: 160"
## [1] "Random Tree: 160"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 180"
## [1] "Random Tree: 180"
## [1] "Random Tree: 180"
## [1] "Random Tree: 180"
## [1] "Random Tree: 180"
## [1] "Random Tree: 180"
## [1] "Random Tree: 180"
## [1] "Random Tree: 180"
## [1] "Random Tree: 180"
## [1] "Random Tree: 180"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 200"
## [1] "Random Tree: 200"
## [1] "Random Tree: 200"
## [1] "Random Tree: 200"
## [1] "Random Tree: 200"
## [1] "Random Tree: 200"
## [1] "Random Tree: 200"
## [1] "Random Tree: 200"
## [1] "Random Tree: 200"
## [1] "Random Tree: 200"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 220"
## [1] "Random Tree: 220"
## [1] "Random Tree: 220"
## [1] "Random Tree: 220"
## [1] "Random Tree: 220"
## [1] "Random Tree: 220"
## [1] "Random Tree: 220"
## [1] "Random Tree: 220"
## [1] "Random Tree: 220"
## [1] "Random Tree: 220"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 240"
## [1] "Random Tree: 240"
## [1] "Random Tree: 240"
## [1] "Random Tree: 240"
## [1] "Random Tree: 240"
## [1] "Random Tree: 240"
## [1] "Random Tree: 240"
## [1] "Random Tree: 240"
## [1] "Random Tree: 240"
## [1] "Random Tree: 240"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 260"
## [1] "Random Tree: 260"
## [1] "Random Tree: 260"
## [1] "Random Tree: 260"
## [1] "Random Tree: 260"
## [1] "Random Tree: 260"
## [1] "Random Tree: 260"
## [1] "Random Tree: 260"
## [1] "Random Tree: 260"
## [1] "Random Tree: 260"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 280"
## [1] "Random Tree: 280"
## [1] "Random Tree: 280"
## [1] "Random Tree: 280"
## [1] "Random Tree: 280"
## [1] "Random Tree: 280"
## [1] "Random Tree: 280"
## [1] "Random Tree: 280"
## [1] "Random Tree: 280"
## [1] "Random Tree: 280"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 300"
## [1] "Random Tree: 300"
## [1] "Random Tree: 300"
## [1] "Random Tree: 300"
## [1] "Random Tree: 300"
## [1] "Random Tree: 300"
## [1] "Random Tree: 300"
## [1] "Random Tree: 300"
## [1] "Random Tree: 300"
## [1] "Random Tree: 300"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 320"
## [1] "Random Tree: 320"
## [1] "Random Tree: 320"
## [1] "Random Tree: 320"
## [1] "Random Tree: 320"
## [1] "Random Tree: 320"
## [1] "Random Tree: 320"
## [1] "Random Tree: 320"
## [1] "Random Tree: 320"
## [1] "Random Tree: 320"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 340"
## [1] "Random Tree: 340"
## [1] "Random Tree: 340"
## [1] "Random Tree: 340"
## [1] "Random Tree: 340"
## [1] "Random Tree: 340"
## [1] "Random Tree: 340"
## [1] "Random Tree: 340"
## [1] "Random Tree: 340"
## [1] "Random Tree: 340"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 360"
## [1] "Random Tree: 360"
## [1] "Random Tree: 360"
## [1] "Random Tree: 360"
## [1] "Random Tree: 360"
## [1] "Random Tree: 360"
## [1] "Random Tree: 360"
## [1] "Random Tree: 360"
## [1] "Random Tree: 360"
## [1] "Random Tree: 360"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 380"
## [1] "Random Tree: 380"
## [1] "Random Tree: 380"
## [1] "Random Tree: 380"
## [1] "Random Tree: 380"
## [1] "Random Tree: 380"
## [1] "Random Tree: 380"
## [1] "Random Tree: 380"
## [1] "Random Tree: 380"
## [1] "Random Tree: 380"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 400"
## [1] "Random Tree: 400"
## [1] "Random Tree: 400"
## [1] "Random Tree: 400"
## [1] "Random Tree: 400"
## [1] "Random Tree: 400"
## [1] "Random Tree: 400"
## [1] "Random Tree: 400"
## [1] "Random Tree: 400"
## [1] "Random Tree: 400"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 420"
## [1] "Random Tree: 420"
## [1] "Random Tree: 420"
## [1] "Random Tree: 420"
## [1] "Random Tree: 420"
## [1] "Random Tree: 420"
## [1] "Random Tree: 420"
## [1] "Random Tree: 420"
## [1] "Random Tree: 420"
## [1] "Random Tree: 420"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 440"
## [1] "Random Tree: 440"
## [1] "Random Tree: 440"
## [1] "Random Tree: 440"
## [1] "Random Tree: 440"
## [1] "Random Tree: 440"
## [1] "Random Tree: 440"
## [1] "Random Tree: 440"
## [1] "Random Tree: 440"
## [1] "Random Tree: 440"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 460"
## [1] "Random Tree: 460"
## [1] "Random Tree: 460"
## [1] "Random Tree: 460"
## [1] "Random Tree: 460"
## [1] "Random Tree: 460"
## [1] "Random Tree: 460"
## [1] "Random Tree: 460"
## [1] "Random Tree: 460"
## [1] "Random Tree: 460"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 480"
## [1] "Random Tree: 480"
## [1] "Random Tree: 480"
## [1] "Random Tree: 480"
## [1] "Random Tree: 480"
## [1] "Random Tree: 480"
## [1] "Random Tree: 480"
## [1] "Random Tree: 480"
## [1] "Random Tree: 480"
## [1] "Random Tree: 480"
## 
  |                                                                            
  |                                                                      |   0%[1] "Random Tree: 500"
## [1] "Random Tree: 500"
## [1] "Random Tree: 500"
## [1] "Random Tree: 500"
## [1] "Random Tree: 500"
## [1] "Random Tree: 500"
## [1] "Random Tree: 500"
## [1] "Random Tree: 500"
## [1] "Random Tree: 500"
## [1] "Random Tree: 500"
maefun <- function(pred, obs) mean(abs(pred - obs))
msefun <- function(pred, obs) mean((pred - obs)^2)
nmsefun <- function(pred, obs) mean((pred - obs)^2)/mean((mean(obs) - obs)^2)

library(dplyr)
eval3<- pred %>% group_by(randomtree, kcross) %>%   
  summarise(mae = maefun(prediction, G3),
            mse = msefun(prediction, G3),
            nmse = nmsefun(prediction, G3),)
## `summarise()` has grouped output by 'randomtree'. You can override using the `.groups` argument.
eval3
## # A tibble: 230 x 5
## # Groups:   randomtree [23]
##    randomtree kcross   mae   mse   nmse
##         <dbl>  <int> <dbl> <dbl>  <dbl>
##  1         60      1 1.42   4.62 0.175 
##  2         60      2 1.17   2.81 0.142 
##  3         60      3 1.21   3.27 0.130 
##  4         60      4 1.11   2.08 0.0815
##  5         60      5 1.03   2.68 0.130 
##  6         60      6 1.08   2.54 0.152 
##  7         60      7 1.07   2.80 0.165 
##  8         60      8 1.14   3.04 0.136 
##  9         60      9 0.998  2.80 0.220 
## 10         60     10 1.18   2.18 0.129 
## # ... with 220 more rows