Home > Mobile >  How to fix error message in R Studio from for loop?
How to fix error message in R Studio from for loop?

Time:12-21

I have a for loop attempting to generate 1000 datasets of a negative binomial with different n values. However, when I run the loop, it gives me an error message for each vector.

x10 <- rep(0,1000)
x25 <- rep(0,1000)
x50 <- rep(0,1000)
x100 <- rep(0,1000)

for (i in 1:1000) {
  x10[i] <- negative_binomial(10, 5, .7)
  x25[i] <- negative_binomial(25, 5, .7)
  x50[i] <- negative_binomial(50, 5, .7)
  x100[i] <- negative_binomial(100, 5, .7)
}

The error messages are (over and over and over again):

Warning in x10[i] <- negative_binomial(10, 5, 0.7) :
  number of items to replace is not a multiple of replacement length
Warning in x25[i] <- negative_binomial(25, 5, 0.7) :
  number of items to replace is not a multiple of replacement length
Warning in x50[i] <- negative_binomial(50, 5, 0.7) :
  number of items to replace is not a multiple of replacement length
Warning in x100[i] <- negative_binomial(100, 5, 0.7) :
  number of items to replace is not a multiple of replacement length

When I print the vectors, the values only go to 981 instead of 1000, so I think that is why I am receiving these errors but I do not know how to correct them.

Also, the function I created for this is:

negative_binomial <- function(N, r, p){
  results <- rep(0, N)
  for(i in 1:N){
   success <- 0
   trials <- 0
   while(success < r){
      trial.outcome <- sample(c(0,1), size = 1, replace = TRUE, prob = c(1-p, p))
      success <- success   trial.outcome
      trials <- trials   1
   }
  results[i] <- trials
  }
  results
}

CodePudding user response:

The error is telling you that the function is producing different length outputs, but you give it the same length vector to store the output. If the function does what you want then you need to provide out vectors of different sizes.

One option is to create matrices with the right dimensions to store each iteration as a row.

negative_binomial <- function(N, r, p){
  results <- rep(0, N)
  for(i in 1:N){
    success <- 0
    trials <- 0
    while(success < r){
      trial.outcome <- sample(c(0,1), size = 1, replace = TRUE, prob = c(1-p, p))
      success <- success   trial.outcome
      trials <- trials   1
    }
    results[i] <- trials
  }
  results
}

x10 <-  matrix(nrow = 1e3, ncol = length(negative_binomial(10, 5, .7)))
x25 <-  matrix(nrow = 1e3, ncol = length(negative_binomial(25, 5, .7)))
x50 <-  matrix(nrow = 1e3, ncol = length(negative_binomial(50, 5, .7)))
x100 <-  matrix(nrow = 1e3, ncol = length(negative_binomial(100, 5, .7)))

for (i in 1:1000) {
  x10[i,] <- negative_binomial(10, 5, .7)
  x25[i,] <- negative_binomial(25, 5, .7)
  x50[i,] <- negative_binomial(50, 5, .7)
  x100[i,] <- negative_binomial(100, 5, .7)
}

CodePudding user response:

Create a vector of length 1000 and type "list". Then use double brackets [[i]] to access/change the list entries.

x10 <- vector("list", 1000)
x25 <- vector("list", 1000)
x50 <- vector("list", 1000)
x100 <- vector("list", 1000)

for (i in 1:1000) {
    x10[[i]] <- negative_binomial(10, 5, .7)
    x25[[i]] <- negative_binomial(25, 5, .7)
    x50[[i]] <- negative_binomial(50, 5, .7)
    x100[[i]] <- negative_binomial(100, 5, .7)
}
  •  Tags:  
  • r
  • Related