Search code examples
rloopsfor-loopminimumdistinct-values

How do I extract all the values of estquant using R?


I don't know how to extract each value of estquant from this loop. What code should I add at the end that gives me all the values instead of just one!

p <- 0.5
m <- 2
d1 <- as.matrix(d);d1
for (i in 1:m){
  Xj <- d1[,i]
  nj <- length(Xj)
  Fj <- pbeta(Fx,i,nj+1-i)
  a <- pbeta(p,i,nj+1-i)
  estFj <- knots(ecdf(Xj))
  estquant <- min(estFj[estFj >= a])
}

Solution

  • You want estquant to be a vector of length m.
    So:

    p <- 0.5
    m <- 2
    d1 <- as.matrix(d);d1
    estquant <- numeric(m)
    for (i in 1:m){
      Xj <- d1[,i]
      nj <- length(Xj)
      Fj <- pbeta(Fx,i,nj+1-i)
      a <- pbeta(p,i,nj+1-i)
      estFj <- knots(ecdf(Xj))
      estquant[i] <- min(estFj[estFj >= a])
    }
    estquant
    

    (It's important to predefine an object when assigning values to it 1-by-1 in a loop, otherwise R has to redefine the object for every iteration and that is time-consuming.)