crocefisso - 1 year ago 59

R Question

Be λ a eigenvalue, Λ a set of eigenvalues, and p the cardinal of Λ, I'm trying to make a loop in R allowing to find n so

Here is an example of input (L components always are decreasing) :

`> L`

[1] 4.903332 4.341607 3.920309 3.716966 3.554570 3.067658 2.891222 2.553127

> sumli

[1] 28.94879

> freq <- L/sumli

> freq

[1] 0.16937952 0.14997540 0.13542221 0.12839795 0.12278821 0.10596843 0.09987368 0.08819460

Answer Source

```
L <- c(4.903332, 4.341607, 3.920309, 3.716966, 3.55457, 3.067658,
2.891222, 2.553127)
```

I think we can use `cumsum`

(cumulative sum).

Depending on whether you want the first `n`

such that cumulative contribution is greater than 95%, or the last `n`

such that cumulative contribution is less than 95%, you need

```
which(cumsum(L) / sum(L) > 0.95)
# [1] 8
which(cumsum(L) / sum(L) > 0.95) - 1
# or: `sum(cumsum(L) / sum(L) <= 0.95)`
# or: `max(which(cumsum(L) / sum(L) <= 0.95))`
# [1] 7
```

(Your example `L`

is not very representative as eigen values do not have a sharp decay.)