Search code examples
rregressionstatistics-bootstrap

R error message in my bootstrapping & regression code


I wanted to use bootstrapping & run a regression, but for some reason, it doesn't work. I always get the error message "Anzahl der zu ersetzenden Elemente ist kein Vielfaches der Ersetzungslänge" (in English: apparently I have two sets of elements & I want to change one to the other but they do not match).

Does anyone see what could have gone wrong here:

    bootReg <- function(formula,data,indices)
    {
    d <- data[indices,]
    fit <- lm(formula, data=d)
    return(coef(fit))  
    }

    results <- boot(statistic = bootReg, 
    formula = RT ~ Code + Situation + Block, data = reg_df, R = 2000)



    #RT = reaction times (--> numeric)
    #Situation = "lab" or "online" 
    #Block = either 0,1,2 or 3 (--> as characters)
    #Code = each subject's individual code

The data in the groups are dependent (= there are RTs from each subject in each situation X block combination)

Thanks in advance!

P.S.: I googled the error message & compared my code with other people's (working) approaches, but don't know what happened here anyway.


Solution

  • This is what worked for me, you have to add the lm() in the formula in boot().

    bootReg <- function(formula, 
                        data, 
                        indices)
    {
      d <- data[indices,]
      fit <- lm(formula, data=d)
      return(coef(fit))  
    }
    
    results <- boot(statistic = bootReg, formula = lm(RT ~ Code + Situation + Block, data = df), data = df, R = 2000)