0

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.

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
Merle
  • 125
  • 1
  • 14
  • 1
    Hard to solve without seeing your data. Does `lm(RT ~ Code + Situation + Block, data = reg_df)` give an error? How about `bootReg(RT ~ Code + Situation + Block, data=reg_df, indices = 1:5)`? – Otto Kässi May 14 '20 at 12:32
  • The first one works!!!! Thanks so much!!! <3 – Merle May 14 '20 at 12:47
  • 1
    The first command is just a plain vanilla OLS without a bootstrap. I am not sure if that is what you are looking for? Maybe you would get better answers if you attach a part your data to the question using `?dput()` – Otto Kässi May 14 '20 at 13:04
  • I know, but I fixed the formula in the boot() function by adding lm() (I thought the ```fit <- lm(formula, data=d)``` part would do it automatically but apparently it didn't) and now it works. :-) – Merle May 14 '20 at 14:42
  • @OttoKässi please post your solution as an answer. That ensures its availability to future readers. – Carl Witthoft May 14 '20 at 14:52

2 Answers2

2

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)
Merle
  • 125
  • 1
  • 14
1

( I recognize that this is a comment at this point, but I'd like to keep it up as an answer for How-To solve the problem. I'll add specific diagnosis if the OP provides more source info)

A general tutorial for debugging:

First, try traceback() to see if an internal call threw the error or boot itself did.

Next, take a look at the class and the size of the object returned from your bootReg function. Is it exactly what boot will accept for the statistic input? Does your formula return what you expect it to return (again, class and length)? Are you certain that data and indices inputs are getting fed in the right order to your formula?

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73