-1

enter image description here

Please see the above equation. As you can see 0<= i <j <= n. I wrote the following command in R. When i=0, we consider X_0 = 0. I generated two observations n=2 and I manually calculated the values. They are, 4.540201, 1.460604. However, my r codes overwrite the results. I got this output 1.460604 1.460604. I couldn't figure it out. What is the reason for that?

I updated the code below.

n = 2
set.seed(2)
x = rexp(n, 1)
xo = sort(x)
xo
value1 = matrix(NA, nrow = 2,2)

for(j in 1:n){
  for(i in 0:(j-1)){
    value1[i,j] =  ifelse(i==0,((n - j + 1)*sum(xo[i+1] - 0)),  ((n - j + 1)*sum(xo[i+1] - xo[i])) )
  }
  
}
value1
score324
  • 687
  • 10
  • 18
  • One problem is that you're looping over `k` on the outside. One iteration of `k` will run through all of the possible values of `i` and `j`. Another problem, I think, is that the output should produce more than 2 values. You've got (j=1,i=0), (j=2,i=0) and (j=2,i=1). Do you want to save all three of these or do two of them get added up? It would seem if you wanted to save all of them, you would be better off populating the lower (or upper) triangle of a matrix where the rows correspond to `i` and the columns to `j` or vice versa. – DaveArmstrong Mar 17 '21 at 16:57
  • @DaveArmstrong, I would like to save all outputs. Should I say `value1[i,j]`? – score324 Mar 17 '21 at 17:03
  • @DaveArmstrong, here is what I did. `rm(list = ls()) n = 2 set.seed(2) x = rexp(n, 1) xo = sort(x) xo value1 = matrix(NA) for(j in 1:n){ for(i in 0:j-1){ value1[i,j] = ifelse(i==0,((n - j + 1)*sum(xo[i+1] - 0)), ((n - j + 1)*sum(xo[i+1] - xo[i])) ) } } `` I got errors. I think the reason was matrix rows and columns cannot be represented as zeros. How can i overcome this issue? – score324 Mar 17 '21 at 17:13
  • FYI, `0:j-1` and `0:(j-1)` are not the same thing. – r2evans Mar 17 '21 at 17:32
  • @r2evans, thanks, I modified my code. `n = 2 set.seed(2) x = rexp(n, 1) xo = sort(x) xo value1 = matrix(NA, nrow = 2,2) for(j in 1:n){ for(i in 0:(j-1)){ value1[i,j] = ifelse(i==0,((n - j + 1)*sum(xo[i+1] - 0)), ((n - j + 1)*sum(xo[i+1] - xo[i])) ) } } value1 `But I don't get all the results. – score324 Mar 17 '21 at 17:37
  • 1
    Code doesn't work well in comments, and comments can be missed by readers and/or hidden by the StackExchange interface. Please [edit] your question and either replace or augment your current code block. Thanks! – r2evans Mar 17 '21 at 17:38
  • 1
    @r2evans, I just updated. Thanks. – score324 Mar 17 '21 at 17:40
  • Your `i` loop starts at 0, so you would need to do ` value1[(i+1),j] <- ...` – DaveArmstrong Mar 17 '21 at 18:06
  • @DaveArmstrong, thanks, it's working. You saved my time. – score324 Mar 17 '21 at 18:13
  • FYI, you should not be using `ifelse` in a situation where you intend it to be length-1, for at least two reasons: (1) `ifelse` does not short-circuit, whether you want it or not; (2) `ifelse` [munges `class` of objects](https://stackoverflow.com/q/6668963/3358272), which is not a problem until it is. And possibly, (3) declarative code: even if you intend to always have this conditional be length-1, if in a mistake it is ever longer, then `ifelse` will happily continue with wrong results, whereas `if (.) ... else ...` will warn/err. – r2evans Mar 17 '21 at 18:51
  • 1
    @r2evans, thanks, I will change it to `if... else`. – score324 Mar 17 '21 at 19:02

1 Answers1

0

You could write that in a way more simple way by using matrix multiplication.

Assuming your X_k and your X_i are vectors, you could:

X_k <- as.matrix(X_k)
X_i <- as.matrix(X_i)
difference <- (X_k - X_i)
output <- (n - j + 1) * (t(difference) %*% difference)

Where t() calculates the transpose of a matrix and %*% is matrix multiplication.

eduardokapp
  • 1,612
  • 1
  • 6
  • 28