Error in boot() related to replacement length and data or data types? - R

user2561533

boot() is failing with one dataset and succeeding with another ... must be a data issue? I just can't figure out the difference. But at least now I think I've got it reproducible. In both cases, an interaction between an integer and factor variable is regressed (lm) onto the numeric dependent variable. boot() command is failing with the error:

Error in boot(data = data, statistic = bs_p, R = 1000) : 
  number of items to replace is not a multiple of replacement length

My statistic function to return p-values is:

    bs_p <- function (data, i) {
      d <- data[i,]
      fit <- lm (y~x*fac, data=d)
      return(summary(fit)$coefficients[,4])
    }

When I generate random data in order to reproduce and post the question here, like this:

    L3 <- LETTERS[1:3]
    data <- data.frame(x=1:50, y=rnorm(1:50), fac=as.factor(sample(L3, 50, replace = TRUE)))

and then bootstrap:

    results <- boot(data=data, statistic=bs_p, R=1000)

bootstrapping works; no error; statistics generated. But with my own data (below), of the same types, boot() returns the error.

    y <- c(17.820, 13.764, 18.880, 25.830, 26.576, 29.832, 22.610, 24.180, 26.572, 26.030, 29.200, 28.560, 28.600, 16.614, 16.302, 18.080, 22.704, 28.101, 38.280, 17.100, 19.292, 33.165, 18.395, 19.434, 27.544, 17.010, 21.560, 28.120, 17.513, 21.646,24.060, 27.984, 20.830, 21.588, 26.280, 29.640, 17.313, 16.344, 16.362, 34.496, 22.785, 20.203, 29.040, 19.092, 20.890,20.739, 17.700, 17.424, 28.737, 18.318, 39.470, 28.072, 17.176, 28.098)
    x <- as.integer(c(9,  5,  0,  8,  3,  4,  9,  6,  9,  2, 15, 10,  5,  1, 11, 11,  4, 8, 13,  1,  2,  4,  7,  7, 12,  1,  6,  6,  4,  3,  5,  5,  7,  9,  8, 3, 3, 14,  6,  4,  3,  6, 17,  3,  6,  6,  7,  1,  6, 10 , 2, 14 , 5,  8))
    fac <- as.factor(c("F", "F", "F", "F", "F", "Ds", "F", "Ds","F","F","F","E", "Ds","F", "F", "E", "Ds","F", "Ds", "F", "Ds","E", "F", "E", "F", "Ds", "E", "Ds","F", "F", "F",  "Ds","Ds", "F", "Ds","F", "F", "E", "F","F","F", "F", "F", "Ds","F", "F", "F", "F", "Ds", "E", "F", "F", "F", "E"))
    data <- data.frame(x=x, y=y, fac=fac)

The linear model runs fine with these data on its own. traceback() doesn't yield anything but the boot call. Please, any thoughts most welcome. I'm on R 3.0.1 on MAC OSX. And thank you!

Roland

Some (or at least one) bootstrap resamples don't contain all factor levels, resulting in a smaller number of coefficients (and corresponding p-values), which results in the error when the bootstrap results are combined. I guess you need stratified bootstrap or bootstrap of the residuals (assuming that bootstrapping p-values is sensible, which I doubt).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why for loop is working with one data set, yet error of replacement has length zero with very similar data set r

From Dev

boot() generating an error on replacement - R

From Dev

R Error: replacement diagonal has wrong length

From Dev

R Error: replacement diagonal has wrong length

From Dev

Error: Replacement has length zero R

From Dev

'replacement has length zero' error in R

From Dev

R aggregate error: "replacement has <foo> rows, data has <bar>"

From Dev

R error in '[<-.data.frame'... replacement has # items, need #

From Dev

R Quantile Error - replacement has n rows, data has p

From Dev

Data Replacement based on conditions in R

From Dev

r data.table set key column length error

From Dev

number of items to replace is not a multiple of replacement length for a data frame

From Dev

R error: "number of items to replace is not a multiple of replacement length"

From Dev

Error in R :Number of items to replace is not a multiple of replacement length

From Dev

Plotting Related Data on Map in R

From Dev

Replacement values of a column in a data frame in R

From Dev

Using data.table with replacement functions in r

From Dev

Conditional Replacement of character elements in data frame R

From Dev

R pca ggbiplot error : replacement has 36 rows, data has 35

From Dev

sizeof operator error on data types

From Dev

sizeof operator error on data types

From Dev

Column data types classification in R

From Dev

Breezejs error when loading an entity with related data

From Dev

NoReverseMatch Error Related to get_context_data

From Dev

Editing related data in MVC results in error

From Dev

Apache Ignite: Serialization error related to Data Streaming

From Dev

How do I create several related data types in haskell?

From Dev

SharedPreferences replacement of data

From Dev

Optimizing replacement in a data frame

Related Related

  1. 1

    Why for loop is working with one data set, yet error of replacement has length zero with very similar data set r

  2. 2

    boot() generating an error on replacement - R

  3. 3

    R Error: replacement diagonal has wrong length

  4. 4

    R Error: replacement diagonal has wrong length

  5. 5

    Error: Replacement has length zero R

  6. 6

    'replacement has length zero' error in R

  7. 7

    R aggregate error: "replacement has <foo> rows, data has <bar>"

  8. 8

    R error in '[<-.data.frame'... replacement has # items, need #

  9. 9

    R Quantile Error - replacement has n rows, data has p

  10. 10

    Data Replacement based on conditions in R

  11. 11

    r data.table set key column length error

  12. 12

    number of items to replace is not a multiple of replacement length for a data frame

  13. 13

    R error: "number of items to replace is not a multiple of replacement length"

  14. 14

    Error in R :Number of items to replace is not a multiple of replacement length

  15. 15

    Plotting Related Data on Map in R

  16. 16

    Replacement values of a column in a data frame in R

  17. 17

    Using data.table with replacement functions in r

  18. 18

    Conditional Replacement of character elements in data frame R

  19. 19

    R pca ggbiplot error : replacement has 36 rows, data has 35

  20. 20

    sizeof operator error on data types

  21. 21

    sizeof operator error on data types

  22. 22

    Column data types classification in R

  23. 23

    Breezejs error when loading an entity with related data

  24. 24

    NoReverseMatch Error Related to get_context_data

  25. 25

    Editing related data in MVC results in error

  26. 26

    Apache Ignite: Serialization error related to Data Streaming

  27. 27

    How do I create several related data types in haskell?

  28. 28

    SharedPreferences replacement of data

  29. 29

    Optimizing replacement in a data frame

HotTag

Archive