What is wrong with my implementation of AdaBoost?










1














I tried to implement the AdaBoost algorithm of Freund and Schapire as close to the original as possible (see p. 2 here: http://rob.schapire.net/papers/explaining-adaboost.pdf):



library(rpart)
library(OneR)

maxdepth <- 1
T <- 100 # number of rounds

# Given: (x_1, y_1),...,(x_m, y_m) where x_i element of X, y_i element of -1, +1
myocarde <- read.table("http://freakonometrics.free.fr/myocarde.csv", head = TRUE, sep = ";")
#myocarde <- read.table("data/myocarde.csv", header = TRUE, sep = ";")
y <- (myocarde[ , "PRONO"] == "SURVIE") * 2 - 1
x <- myocarde[ , 1:7]
m <- nrow(x)
data <- data.frame(x, y)

# Initialize: D_1(i) = 1/m for i = 1,...,m
D <- rep(1/m, m)

H <- replicate(T, list())
a <- vector(mode = "numeric", T)
set.seed(123)

# For t = 1,...,T
for(t in 1:T)
# Train weak learner using distribution D_t
# Get weak hypothesis h_t: X -> -1, +1
data_D_t <- data[sample(m, 10*m, replace = TRUE, prob = D), ]
H[[t]] <- rpart(y ~., data = data_D_t, maxdepth = maxdepth, method = "class")
# Aim: select h_t with low weighted error: e_t = Pr_i~D_t[h_t(x_i) != y_i]
h <- predict(H[[t]], x, type = "class")
e <- sum(h != y) / m
# Choose a_t = 0.5 * log((1-e) / e)
a[t] <- 0.5 * log((1-e) / e)
# Update for i = 1,...,m: D_t+1(i) = (D_t(i) * exp(-a_t * y_i * h_t(x_i))) / Z_t
# where Z_t is a normalization factor (chosen so that Dt+1 will be a distribution)
D <- D * exp(-a[t] * y * as.numeric(h))
D <- D / sum(D)

# Output the final hypothesis: H(x) = sign(sum of a_t * h_t(x) for t=1 to T)
newdata <- x
H_x <- sapply(H, function(x) as.numeric(as.character(predict(x, newdata = newdata, type = "class"))))
H_x <- t(a * t(H_x))
pred <- sign(rowSums(H_x))

#H
#a
eval_model(pred, y)
##
## Confusion matrix (absolute):
## Actual
## Prediction -1 1 Sum
## -1 0 1 1
## 1 29 41 70
## Sum 29 42 71
##
## Confusion matrix (relative):
## Actual
## Prediction -1 1 Sum
## -1 0.00 0.01 0.01
## 1 0.41 0.58 0.99
## Sum 0.41 0.59 1.00
##
## Accuracy:
## 0.5775 (41/71)
##
## Error rate:
## 0.4225 (30/71)
##
## Error rate reduction (vs. base rate):
## -0.0345 (p-value = 0.6436)


As can be seen the accuracy of the model is horrible compared to other AdaBoost implementations, e.g.:



library(JOUSBoost)
## JOUSBoost 2.1.0
boost <- adaboost(as.matrix(x), y, tree_depth = maxdepth, n_rounds = T)
pred <- predict(boost, x)
eval_model(pred, y)
##
## Confusion matrix (absolute):
## Actual
## Prediction -1 1 Sum
## -1 29 0 29
## 1 0 42 42
## Sum 29 42 71
##
## Confusion matrix (relative):
## Actual
## Prediction -1 1 Sum
## -1 0.41 0.00 0.41
## 1 0.00 0.59 0.59
## Sum 0.41 0.59 1.00
##
## Accuracy:
## 1 (71/71)
##
## Error rate:
## 0 (0/71)
##
## Error rate reduction (vs. base rate):
## 1 (p-value < 2.2e-16)


My question

Could you please give me a hint what went wrong in my implementation? Thank you










share|improve this question




























    1














    I tried to implement the AdaBoost algorithm of Freund and Schapire as close to the original as possible (see p. 2 here: http://rob.schapire.net/papers/explaining-adaboost.pdf):



    library(rpart)
    library(OneR)

    maxdepth <- 1
    T <- 100 # number of rounds

    # Given: (x_1, y_1),...,(x_m, y_m) where x_i element of X, y_i element of -1, +1
    myocarde <- read.table("http://freakonometrics.free.fr/myocarde.csv", head = TRUE, sep = ";")
    #myocarde <- read.table("data/myocarde.csv", header = TRUE, sep = ";")
    y <- (myocarde[ , "PRONO"] == "SURVIE") * 2 - 1
    x <- myocarde[ , 1:7]
    m <- nrow(x)
    data <- data.frame(x, y)

    # Initialize: D_1(i) = 1/m for i = 1,...,m
    D <- rep(1/m, m)

    H <- replicate(T, list())
    a <- vector(mode = "numeric", T)
    set.seed(123)

    # For t = 1,...,T
    for(t in 1:T)
    # Train weak learner using distribution D_t
    # Get weak hypothesis h_t: X -> -1, +1
    data_D_t <- data[sample(m, 10*m, replace = TRUE, prob = D), ]
    H[[t]] <- rpart(y ~., data = data_D_t, maxdepth = maxdepth, method = "class")
    # Aim: select h_t with low weighted error: e_t = Pr_i~D_t[h_t(x_i) != y_i]
    h <- predict(H[[t]], x, type = "class")
    e <- sum(h != y) / m
    # Choose a_t = 0.5 * log((1-e) / e)
    a[t] <- 0.5 * log((1-e) / e)
    # Update for i = 1,...,m: D_t+1(i) = (D_t(i) * exp(-a_t * y_i * h_t(x_i))) / Z_t
    # where Z_t is a normalization factor (chosen so that Dt+1 will be a distribution)
    D <- D * exp(-a[t] * y * as.numeric(h))
    D <- D / sum(D)

    # Output the final hypothesis: H(x) = sign(sum of a_t * h_t(x) for t=1 to T)
    newdata <- x
    H_x <- sapply(H, function(x) as.numeric(as.character(predict(x, newdata = newdata, type = "class"))))
    H_x <- t(a * t(H_x))
    pred <- sign(rowSums(H_x))

    #H
    #a
    eval_model(pred, y)
    ##
    ## Confusion matrix (absolute):
    ## Actual
    ## Prediction -1 1 Sum
    ## -1 0 1 1
    ## 1 29 41 70
    ## Sum 29 42 71
    ##
    ## Confusion matrix (relative):
    ## Actual
    ## Prediction -1 1 Sum
    ## -1 0.00 0.01 0.01
    ## 1 0.41 0.58 0.99
    ## Sum 0.41 0.59 1.00
    ##
    ## Accuracy:
    ## 0.5775 (41/71)
    ##
    ## Error rate:
    ## 0.4225 (30/71)
    ##
    ## Error rate reduction (vs. base rate):
    ## -0.0345 (p-value = 0.6436)


    As can be seen the accuracy of the model is horrible compared to other AdaBoost implementations, e.g.:



    library(JOUSBoost)
    ## JOUSBoost 2.1.0
    boost <- adaboost(as.matrix(x), y, tree_depth = maxdepth, n_rounds = T)
    pred <- predict(boost, x)
    eval_model(pred, y)
    ##
    ## Confusion matrix (absolute):
    ## Actual
    ## Prediction -1 1 Sum
    ## -1 29 0 29
    ## 1 0 42 42
    ## Sum 29 42 71
    ##
    ## Confusion matrix (relative):
    ## Actual
    ## Prediction -1 1 Sum
    ## -1 0.41 0.00 0.41
    ## 1 0.00 0.59 0.59
    ## Sum 0.41 0.59 1.00
    ##
    ## Accuracy:
    ## 1 (71/71)
    ##
    ## Error rate:
    ## 0 (0/71)
    ##
    ## Error rate reduction (vs. base rate):
    ## 1 (p-value < 2.2e-16)


    My question

    Could you please give me a hint what went wrong in my implementation? Thank you










    share|improve this question


























      1












      1








      1


      1





      I tried to implement the AdaBoost algorithm of Freund and Schapire as close to the original as possible (see p. 2 here: http://rob.schapire.net/papers/explaining-adaboost.pdf):



      library(rpart)
      library(OneR)

      maxdepth <- 1
      T <- 100 # number of rounds

      # Given: (x_1, y_1),...,(x_m, y_m) where x_i element of X, y_i element of -1, +1
      myocarde <- read.table("http://freakonometrics.free.fr/myocarde.csv", head = TRUE, sep = ";")
      #myocarde <- read.table("data/myocarde.csv", header = TRUE, sep = ";")
      y <- (myocarde[ , "PRONO"] == "SURVIE") * 2 - 1
      x <- myocarde[ , 1:7]
      m <- nrow(x)
      data <- data.frame(x, y)

      # Initialize: D_1(i) = 1/m for i = 1,...,m
      D <- rep(1/m, m)

      H <- replicate(T, list())
      a <- vector(mode = "numeric", T)
      set.seed(123)

      # For t = 1,...,T
      for(t in 1:T)
      # Train weak learner using distribution D_t
      # Get weak hypothesis h_t: X -> -1, +1
      data_D_t <- data[sample(m, 10*m, replace = TRUE, prob = D), ]
      H[[t]] <- rpart(y ~., data = data_D_t, maxdepth = maxdepth, method = "class")
      # Aim: select h_t with low weighted error: e_t = Pr_i~D_t[h_t(x_i) != y_i]
      h <- predict(H[[t]], x, type = "class")
      e <- sum(h != y) / m
      # Choose a_t = 0.5 * log((1-e) / e)
      a[t] <- 0.5 * log((1-e) / e)
      # Update for i = 1,...,m: D_t+1(i) = (D_t(i) * exp(-a_t * y_i * h_t(x_i))) / Z_t
      # where Z_t is a normalization factor (chosen so that Dt+1 will be a distribution)
      D <- D * exp(-a[t] * y * as.numeric(h))
      D <- D / sum(D)

      # Output the final hypothesis: H(x) = sign(sum of a_t * h_t(x) for t=1 to T)
      newdata <- x
      H_x <- sapply(H, function(x) as.numeric(as.character(predict(x, newdata = newdata, type = "class"))))
      H_x <- t(a * t(H_x))
      pred <- sign(rowSums(H_x))

      #H
      #a
      eval_model(pred, y)
      ##
      ## Confusion matrix (absolute):
      ## Actual
      ## Prediction -1 1 Sum
      ## -1 0 1 1
      ## 1 29 41 70
      ## Sum 29 42 71
      ##
      ## Confusion matrix (relative):
      ## Actual
      ## Prediction -1 1 Sum
      ## -1 0.00 0.01 0.01
      ## 1 0.41 0.58 0.99
      ## Sum 0.41 0.59 1.00
      ##
      ## Accuracy:
      ## 0.5775 (41/71)
      ##
      ## Error rate:
      ## 0.4225 (30/71)
      ##
      ## Error rate reduction (vs. base rate):
      ## -0.0345 (p-value = 0.6436)


      As can be seen the accuracy of the model is horrible compared to other AdaBoost implementations, e.g.:



      library(JOUSBoost)
      ## JOUSBoost 2.1.0
      boost <- adaboost(as.matrix(x), y, tree_depth = maxdepth, n_rounds = T)
      pred <- predict(boost, x)
      eval_model(pred, y)
      ##
      ## Confusion matrix (absolute):
      ## Actual
      ## Prediction -1 1 Sum
      ## -1 29 0 29
      ## 1 0 42 42
      ## Sum 29 42 71
      ##
      ## Confusion matrix (relative):
      ## Actual
      ## Prediction -1 1 Sum
      ## -1 0.41 0.00 0.41
      ## 1 0.00 0.59 0.59
      ## Sum 0.41 0.59 1.00
      ##
      ## Accuracy:
      ## 1 (71/71)
      ##
      ## Error rate:
      ## 0 (0/71)
      ##
      ## Error rate reduction (vs. base rate):
      ## 1 (p-value < 2.2e-16)


      My question

      Could you please give me a hint what went wrong in my implementation? Thank you










      share|improve this question















      I tried to implement the AdaBoost algorithm of Freund and Schapire as close to the original as possible (see p. 2 here: http://rob.schapire.net/papers/explaining-adaboost.pdf):



      library(rpart)
      library(OneR)

      maxdepth <- 1
      T <- 100 # number of rounds

      # Given: (x_1, y_1),...,(x_m, y_m) where x_i element of X, y_i element of -1, +1
      myocarde <- read.table("http://freakonometrics.free.fr/myocarde.csv", head = TRUE, sep = ";")
      #myocarde <- read.table("data/myocarde.csv", header = TRUE, sep = ";")
      y <- (myocarde[ , "PRONO"] == "SURVIE") * 2 - 1
      x <- myocarde[ , 1:7]
      m <- nrow(x)
      data <- data.frame(x, y)

      # Initialize: D_1(i) = 1/m for i = 1,...,m
      D <- rep(1/m, m)

      H <- replicate(T, list())
      a <- vector(mode = "numeric", T)
      set.seed(123)

      # For t = 1,...,T
      for(t in 1:T)
      # Train weak learner using distribution D_t
      # Get weak hypothesis h_t: X -> -1, +1
      data_D_t <- data[sample(m, 10*m, replace = TRUE, prob = D), ]
      H[[t]] <- rpart(y ~., data = data_D_t, maxdepth = maxdepth, method = "class")
      # Aim: select h_t with low weighted error: e_t = Pr_i~D_t[h_t(x_i) != y_i]
      h <- predict(H[[t]], x, type = "class")
      e <- sum(h != y) / m
      # Choose a_t = 0.5 * log((1-e) / e)
      a[t] <- 0.5 * log((1-e) / e)
      # Update for i = 1,...,m: D_t+1(i) = (D_t(i) * exp(-a_t * y_i * h_t(x_i))) / Z_t
      # where Z_t is a normalization factor (chosen so that Dt+1 will be a distribution)
      D <- D * exp(-a[t] * y * as.numeric(h))
      D <- D / sum(D)

      # Output the final hypothesis: H(x) = sign(sum of a_t * h_t(x) for t=1 to T)
      newdata <- x
      H_x <- sapply(H, function(x) as.numeric(as.character(predict(x, newdata = newdata, type = "class"))))
      H_x <- t(a * t(H_x))
      pred <- sign(rowSums(H_x))

      #H
      #a
      eval_model(pred, y)
      ##
      ## Confusion matrix (absolute):
      ## Actual
      ## Prediction -1 1 Sum
      ## -1 0 1 1
      ## 1 29 41 70
      ## Sum 29 42 71
      ##
      ## Confusion matrix (relative):
      ## Actual
      ## Prediction -1 1 Sum
      ## -1 0.00 0.01 0.01
      ## 1 0.41 0.58 0.99
      ## Sum 0.41 0.59 1.00
      ##
      ## Accuracy:
      ## 0.5775 (41/71)
      ##
      ## Error rate:
      ## 0.4225 (30/71)
      ##
      ## Error rate reduction (vs. base rate):
      ## -0.0345 (p-value = 0.6436)


      As can be seen the accuracy of the model is horrible compared to other AdaBoost implementations, e.g.:



      library(JOUSBoost)
      ## JOUSBoost 2.1.0
      boost <- adaboost(as.matrix(x), y, tree_depth = maxdepth, n_rounds = T)
      pred <- predict(boost, x)
      eval_model(pred, y)
      ##
      ## Confusion matrix (absolute):
      ## Actual
      ## Prediction -1 1 Sum
      ## -1 29 0 29
      ## 1 0 42 42
      ## Sum 29 42 71
      ##
      ## Confusion matrix (relative):
      ## Actual
      ## Prediction -1 1 Sum
      ## -1 0.41 0.00 0.41
      ## 1 0.00 0.59 0.59
      ## Sum 0.41 0.59 1.00
      ##
      ## Accuracy:
      ## 1 (71/71)
      ##
      ## Error rate:
      ## 0 (0/71)
      ##
      ## Error rate reduction (vs. base rate):
      ## 1 (p-value < 2.2e-16)


      My question

      Could you please give me a hint what went wrong in my implementation? Thank you







      r adaboost






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 18 at 11:38

























      asked Nov 11 at 11:17









      vonjd

      1,96232641




      1,96232641






















          1 Answer
          1






          active

          oldest

          votes


















          1














          There are quite a few contributing factors as to why your implementation is not working.



          1. You were not using rpart correctly. Adaboost implementation does not mention upsampling with the weights - but rpart itself can accept weights. My example below shows how rpart should be used for this purpose.


          2. Calculation of the weighted error was wrong. You were calculating the error proportion (number of samples calculated incorrectly divided by number of samples). Adaboost uses the sum of the weights that were incorrectly predicted (sum(D[y != yhat])).


          3. Final predictions seemed to be incorrect too, I just ended up doing a simple loop.


          Next time I recommend diving into the source code the the other implementations you are comparing against.



          https://github.com/cran/JOUSBoost/blob/master/R/adaboost.R uses almost identical code to my below example - and probably would have helped guide you originally.



          Additionally using T as a variable could potentially interfere with the logical TRUE and it's shorthand T, so I'd avoid it.



          ### packages ###
          library(rpart)
          library(OneR)

          ### parameters ###
          maxdepth <- 1
          rounds <- 100
          set.seed(123)

          ### data ###
          myocarde <- read.table("http://freakonometrics.free.fr/myocarde.csv", head = TRUE, sep = ";")
          y <- (myocarde[ , "PRONO"] == "SURVIE") * 2 - 1
          x <- myocarde[ , 1:7]
          m <- nrow(x)
          dataset <- data.frame(x, y)

          ### initialisation ###
          D <- rep(1/m, m)
          H <- list()
          a <- vector(mode = "numeric", length = rounds)

          for (i in seq.int(rounds))
          # train weak learner
          H[[i]] = rpart(y ~ ., data = dataset, weights = D, maxdepth = maxdepth, method = "class")
          # predictions
          yhat <- predict(H[[i]], x, type = "class")
          yhat <- as.numeric(as.character(yhat))
          # weighted error
          e <- sum(D[yhat != y])
          # alpha coefficient
          a[i] <- 0.5 * log((1 - e) / e)
          # updating weights (D)
          D <- D * exp(-a[i] * y * yhat)
          D <- D / sum(D)


          # predict with each weak learner on dataset
          y_hat_final <- vector(mode = "numeric", length = m)
          for (i in seq(rounds))
          pred = predict(H[[i]], dataset, type = "class")
          pred = as.numeric(as.character(pred))
          y_hat_final = y_hat_final + (a[i] * pred)

          pred <- sign(y_hat_final)

          eval_model(pred, y)



          > eval_model(pred, y)

          Confusion matrix (absolute):
          Actual
          Prediction -1 1 Sum
          -1 29 0 29
          1 0 42 42
          Sum 29 42 71

          Confusion matrix (relative):
          Actual
          Prediction -1 1 Sum
          -1 0.41 0.00 0.41
          1 0.00 0.59 0.59
          Sum 0.41 0.59 1.00

          Accuracy:
          1 (71/71)

          Error rate:
          0 (0/71)

          Error rate reduction (vs. base rate):
          1 (p-value < 2.2e-16)





          share|improve this answer






















          • Thank you very much for your help! Concerning the weights in rpart – the long vignette says that they are not supported yet?!? see p. 23: cran.r-project.org/web/packages/rpart/vignettes/longintro.pdf
            – vonjd
            Nov 18 at 14:52






          • 1




            @vonjd its likely just not updated - you can test this empirically quite easily.
            – zacdav
            Nov 18 at 23:06










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248179%2fwhat-is-wrong-with-my-implementation-of-adaboost%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          There are quite a few contributing factors as to why your implementation is not working.



          1. You were not using rpart correctly. Adaboost implementation does not mention upsampling with the weights - but rpart itself can accept weights. My example below shows how rpart should be used for this purpose.


          2. Calculation of the weighted error was wrong. You were calculating the error proportion (number of samples calculated incorrectly divided by number of samples). Adaboost uses the sum of the weights that were incorrectly predicted (sum(D[y != yhat])).


          3. Final predictions seemed to be incorrect too, I just ended up doing a simple loop.


          Next time I recommend diving into the source code the the other implementations you are comparing against.



          https://github.com/cran/JOUSBoost/blob/master/R/adaboost.R uses almost identical code to my below example - and probably would have helped guide you originally.



          Additionally using T as a variable could potentially interfere with the logical TRUE and it's shorthand T, so I'd avoid it.



          ### packages ###
          library(rpart)
          library(OneR)

          ### parameters ###
          maxdepth <- 1
          rounds <- 100
          set.seed(123)

          ### data ###
          myocarde <- read.table("http://freakonometrics.free.fr/myocarde.csv", head = TRUE, sep = ";")
          y <- (myocarde[ , "PRONO"] == "SURVIE") * 2 - 1
          x <- myocarde[ , 1:7]
          m <- nrow(x)
          dataset <- data.frame(x, y)

          ### initialisation ###
          D <- rep(1/m, m)
          H <- list()
          a <- vector(mode = "numeric", length = rounds)

          for (i in seq.int(rounds))
          # train weak learner
          H[[i]] = rpart(y ~ ., data = dataset, weights = D, maxdepth = maxdepth, method = "class")
          # predictions
          yhat <- predict(H[[i]], x, type = "class")
          yhat <- as.numeric(as.character(yhat))
          # weighted error
          e <- sum(D[yhat != y])
          # alpha coefficient
          a[i] <- 0.5 * log((1 - e) / e)
          # updating weights (D)
          D <- D * exp(-a[i] * y * yhat)
          D <- D / sum(D)


          # predict with each weak learner on dataset
          y_hat_final <- vector(mode = "numeric", length = m)
          for (i in seq(rounds))
          pred = predict(H[[i]], dataset, type = "class")
          pred = as.numeric(as.character(pred))
          y_hat_final = y_hat_final + (a[i] * pred)

          pred <- sign(y_hat_final)

          eval_model(pred, y)



          > eval_model(pred, y)

          Confusion matrix (absolute):
          Actual
          Prediction -1 1 Sum
          -1 29 0 29
          1 0 42 42
          Sum 29 42 71

          Confusion matrix (relative):
          Actual
          Prediction -1 1 Sum
          -1 0.41 0.00 0.41
          1 0.00 0.59 0.59
          Sum 0.41 0.59 1.00

          Accuracy:
          1 (71/71)

          Error rate:
          0 (0/71)

          Error rate reduction (vs. base rate):
          1 (p-value < 2.2e-16)





          share|improve this answer






















          • Thank you very much for your help! Concerning the weights in rpart – the long vignette says that they are not supported yet?!? see p. 23: cran.r-project.org/web/packages/rpart/vignettes/longintro.pdf
            – vonjd
            Nov 18 at 14:52






          • 1




            @vonjd its likely just not updated - you can test this empirically quite easily.
            – zacdav
            Nov 18 at 23:06















          1














          There are quite a few contributing factors as to why your implementation is not working.



          1. You were not using rpart correctly. Adaboost implementation does not mention upsampling with the weights - but rpart itself can accept weights. My example below shows how rpart should be used for this purpose.


          2. Calculation of the weighted error was wrong. You were calculating the error proportion (number of samples calculated incorrectly divided by number of samples). Adaboost uses the sum of the weights that were incorrectly predicted (sum(D[y != yhat])).


          3. Final predictions seemed to be incorrect too, I just ended up doing a simple loop.


          Next time I recommend diving into the source code the the other implementations you are comparing against.



          https://github.com/cran/JOUSBoost/blob/master/R/adaboost.R uses almost identical code to my below example - and probably would have helped guide you originally.



          Additionally using T as a variable could potentially interfere with the logical TRUE and it's shorthand T, so I'd avoid it.



          ### packages ###
          library(rpart)
          library(OneR)

          ### parameters ###
          maxdepth <- 1
          rounds <- 100
          set.seed(123)

          ### data ###
          myocarde <- read.table("http://freakonometrics.free.fr/myocarde.csv", head = TRUE, sep = ";")
          y <- (myocarde[ , "PRONO"] == "SURVIE") * 2 - 1
          x <- myocarde[ , 1:7]
          m <- nrow(x)
          dataset <- data.frame(x, y)

          ### initialisation ###
          D <- rep(1/m, m)
          H <- list()
          a <- vector(mode = "numeric", length = rounds)

          for (i in seq.int(rounds))
          # train weak learner
          H[[i]] = rpart(y ~ ., data = dataset, weights = D, maxdepth = maxdepth, method = "class")
          # predictions
          yhat <- predict(H[[i]], x, type = "class")
          yhat <- as.numeric(as.character(yhat))
          # weighted error
          e <- sum(D[yhat != y])
          # alpha coefficient
          a[i] <- 0.5 * log((1 - e) / e)
          # updating weights (D)
          D <- D * exp(-a[i] * y * yhat)
          D <- D / sum(D)


          # predict with each weak learner on dataset
          y_hat_final <- vector(mode = "numeric", length = m)
          for (i in seq(rounds))
          pred = predict(H[[i]], dataset, type = "class")
          pred = as.numeric(as.character(pred))
          y_hat_final = y_hat_final + (a[i] * pred)

          pred <- sign(y_hat_final)

          eval_model(pred, y)



          > eval_model(pred, y)

          Confusion matrix (absolute):
          Actual
          Prediction -1 1 Sum
          -1 29 0 29
          1 0 42 42
          Sum 29 42 71

          Confusion matrix (relative):
          Actual
          Prediction -1 1 Sum
          -1 0.41 0.00 0.41
          1 0.00 0.59 0.59
          Sum 0.41 0.59 1.00

          Accuracy:
          1 (71/71)

          Error rate:
          0 (0/71)

          Error rate reduction (vs. base rate):
          1 (p-value < 2.2e-16)





          share|improve this answer






















          • Thank you very much for your help! Concerning the weights in rpart – the long vignette says that they are not supported yet?!? see p. 23: cran.r-project.org/web/packages/rpart/vignettes/longintro.pdf
            – vonjd
            Nov 18 at 14:52






          • 1




            @vonjd its likely just not updated - you can test this empirically quite easily.
            – zacdav
            Nov 18 at 23:06













          1












          1








          1






          There are quite a few contributing factors as to why your implementation is not working.



          1. You were not using rpart correctly. Adaboost implementation does not mention upsampling with the weights - but rpart itself can accept weights. My example below shows how rpart should be used for this purpose.


          2. Calculation of the weighted error was wrong. You were calculating the error proportion (number of samples calculated incorrectly divided by number of samples). Adaboost uses the sum of the weights that were incorrectly predicted (sum(D[y != yhat])).


          3. Final predictions seemed to be incorrect too, I just ended up doing a simple loop.


          Next time I recommend diving into the source code the the other implementations you are comparing against.



          https://github.com/cran/JOUSBoost/blob/master/R/adaboost.R uses almost identical code to my below example - and probably would have helped guide you originally.



          Additionally using T as a variable could potentially interfere with the logical TRUE and it's shorthand T, so I'd avoid it.



          ### packages ###
          library(rpart)
          library(OneR)

          ### parameters ###
          maxdepth <- 1
          rounds <- 100
          set.seed(123)

          ### data ###
          myocarde <- read.table("http://freakonometrics.free.fr/myocarde.csv", head = TRUE, sep = ";")
          y <- (myocarde[ , "PRONO"] == "SURVIE") * 2 - 1
          x <- myocarde[ , 1:7]
          m <- nrow(x)
          dataset <- data.frame(x, y)

          ### initialisation ###
          D <- rep(1/m, m)
          H <- list()
          a <- vector(mode = "numeric", length = rounds)

          for (i in seq.int(rounds))
          # train weak learner
          H[[i]] = rpart(y ~ ., data = dataset, weights = D, maxdepth = maxdepth, method = "class")
          # predictions
          yhat <- predict(H[[i]], x, type = "class")
          yhat <- as.numeric(as.character(yhat))
          # weighted error
          e <- sum(D[yhat != y])
          # alpha coefficient
          a[i] <- 0.5 * log((1 - e) / e)
          # updating weights (D)
          D <- D * exp(-a[i] * y * yhat)
          D <- D / sum(D)


          # predict with each weak learner on dataset
          y_hat_final <- vector(mode = "numeric", length = m)
          for (i in seq(rounds))
          pred = predict(H[[i]], dataset, type = "class")
          pred = as.numeric(as.character(pred))
          y_hat_final = y_hat_final + (a[i] * pred)

          pred <- sign(y_hat_final)

          eval_model(pred, y)



          > eval_model(pred, y)

          Confusion matrix (absolute):
          Actual
          Prediction -1 1 Sum
          -1 29 0 29
          1 0 42 42
          Sum 29 42 71

          Confusion matrix (relative):
          Actual
          Prediction -1 1 Sum
          -1 0.41 0.00 0.41
          1 0.00 0.59 0.59
          Sum 0.41 0.59 1.00

          Accuracy:
          1 (71/71)

          Error rate:
          0 (0/71)

          Error rate reduction (vs. base rate):
          1 (p-value < 2.2e-16)





          share|improve this answer














          There are quite a few contributing factors as to why your implementation is not working.



          1. You were not using rpart correctly. Adaboost implementation does not mention upsampling with the weights - but rpart itself can accept weights. My example below shows how rpart should be used for this purpose.


          2. Calculation of the weighted error was wrong. You were calculating the error proportion (number of samples calculated incorrectly divided by number of samples). Adaboost uses the sum of the weights that were incorrectly predicted (sum(D[y != yhat])).


          3. Final predictions seemed to be incorrect too, I just ended up doing a simple loop.


          Next time I recommend diving into the source code the the other implementations you are comparing against.



          https://github.com/cran/JOUSBoost/blob/master/R/adaboost.R uses almost identical code to my below example - and probably would have helped guide you originally.



          Additionally using T as a variable could potentially interfere with the logical TRUE and it's shorthand T, so I'd avoid it.



          ### packages ###
          library(rpart)
          library(OneR)

          ### parameters ###
          maxdepth <- 1
          rounds <- 100
          set.seed(123)

          ### data ###
          myocarde <- read.table("http://freakonometrics.free.fr/myocarde.csv", head = TRUE, sep = ";")
          y <- (myocarde[ , "PRONO"] == "SURVIE") * 2 - 1
          x <- myocarde[ , 1:7]
          m <- nrow(x)
          dataset <- data.frame(x, y)

          ### initialisation ###
          D <- rep(1/m, m)
          H <- list()
          a <- vector(mode = "numeric", length = rounds)

          for (i in seq.int(rounds))
          # train weak learner
          H[[i]] = rpart(y ~ ., data = dataset, weights = D, maxdepth = maxdepth, method = "class")
          # predictions
          yhat <- predict(H[[i]], x, type = "class")
          yhat <- as.numeric(as.character(yhat))
          # weighted error
          e <- sum(D[yhat != y])
          # alpha coefficient
          a[i] <- 0.5 * log((1 - e) / e)
          # updating weights (D)
          D <- D * exp(-a[i] * y * yhat)
          D <- D / sum(D)


          # predict with each weak learner on dataset
          y_hat_final <- vector(mode = "numeric", length = m)
          for (i in seq(rounds))
          pred = predict(H[[i]], dataset, type = "class")
          pred = as.numeric(as.character(pred))
          y_hat_final = y_hat_final + (a[i] * pred)

          pred <- sign(y_hat_final)

          eval_model(pred, y)



          > eval_model(pred, y)

          Confusion matrix (absolute):
          Actual
          Prediction -1 1 Sum
          -1 29 0 29
          1 0 42 42
          Sum 29 42 71

          Confusion matrix (relative):
          Actual
          Prediction -1 1 Sum
          -1 0.41 0.00 0.41
          1 0.00 0.59 0.59
          Sum 0.41 0.59 1.00

          Accuracy:
          1 (71/71)

          Error rate:
          0 (0/71)

          Error rate reduction (vs. base rate):
          1 (p-value < 2.2e-16)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 18 at 14:12

























          answered Nov 18 at 13:32









          zacdav

          2,8331826




          2,8331826











          • Thank you very much for your help! Concerning the weights in rpart – the long vignette says that they are not supported yet?!? see p. 23: cran.r-project.org/web/packages/rpart/vignettes/longintro.pdf
            – vonjd
            Nov 18 at 14:52






          • 1




            @vonjd its likely just not updated - you can test this empirically quite easily.
            – zacdav
            Nov 18 at 23:06
















          • Thank you very much for your help! Concerning the weights in rpart – the long vignette says that they are not supported yet?!? see p. 23: cran.r-project.org/web/packages/rpart/vignettes/longintro.pdf
            – vonjd
            Nov 18 at 14:52






          • 1




            @vonjd its likely just not updated - you can test this empirically quite easily.
            – zacdav
            Nov 18 at 23:06















          Thank you very much for your help! Concerning the weights in rpart – the long vignette says that they are not supported yet?!? see p. 23: cran.r-project.org/web/packages/rpart/vignettes/longintro.pdf
          – vonjd
          Nov 18 at 14:52




          Thank you very much for your help! Concerning the weights in rpart – the long vignette says that they are not supported yet?!? see p. 23: cran.r-project.org/web/packages/rpart/vignettes/longintro.pdf
          – vonjd
          Nov 18 at 14:52




          1




          1




          @vonjd its likely just not updated - you can test this empirically quite easily.
          – zacdav
          Nov 18 at 23:06




          @vonjd its likely just not updated - you can test this empirically quite easily.
          – zacdav
          Nov 18 at 23:06

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248179%2fwhat-is-wrong-with-my-implementation-of-adaboost%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo