Error when Bootstraping a Beta regression model in R with {betareg}

Viewed 45

I need to bootstrap a beta regression model to check its robustness - because of a data point with a large cook's distance - with the boot package (other suggestions welcomed).

I have the following error:

Error in t.star[r, ] <- res[[r]] : 
  incorrect number of subscripts on matrix

Here's a reproductible example:

library(betareg)
library(boot)

fake_data <- data.frame(diet = as.factor(c(rep("A",10),rep("B",10))),
                            fat = c(runif(10,.1,.5),runif(10,.4,.9)) )
    
plot(fat~diet, data = fake_data)
   
my_beta_reg <- function(data,i){
        data_i <- data[i,]
        mod <- betareg(data_i[,"fat"] ~ data_i[,"diet"])
        return(mod$coef)
      }
    
b = boot(fake_data, statistic = my_beta_reg, R= 50)

Error in t.star[r, ] <- res[[r]] : 
incorrect number of subscripts on matrix

What's the issue?
Thanks in advance.

1 Answers

The issue is that mod$coef is a list:

betareg(fat ~ diet, data = fake_data)$coef
#$mean
#(Intercept)       dietB 
#  -1.275793    2.490126 
#
#$precision
#   (phi) 
#20.59014 

You need to unlist it or preferably use the function you are supposed to use for extraction of coefficients:

my_beta_reg <- function(data,i){
  mod <- betareg(fat ~ diet, data = data[i,])
  #unlist(mod$coef)
  coef(mod)
}

b = boot(fake_data, statistic = my_beta_reg, R= 50)
print(b)
#ORDINARY NONPARAMETRIC BOOTSTRAP
#
#
#Call:
#boot(data = fake_data, statistic = my_beta_reg, R = 50)
#
#
#Bootstrap Statistics :
#     original       bias    std. error
#t1* -1.275793 -0.019847377   0.2003523
#t2*  2.490126  0.009008892   0.2314521
#t3* 20.590142  8.265394485  17.2271497
Related