4

I'm trying to find the mean, mean ± standard deviation and certain quantiles (5%, 50%, 95%) for the columns of a matrix.

The matrix has dimensions 10*20 (row indicate sample number, column indicates time):

enter image description here

Now, from this dataset, I would like to find the quantiles as above. I tried the following:

enter image description here

But the functions give me only a single value. I am hoping to get the above meansures (means, standard deviations, quantiles) for each time.

Then, how do we plot a graph where the x axis is the time t (t=1 to t=20), the y axis is the Fund value and all 5 lines are shown (mean, mean ± standard deviation, 5% quantile, 50% quantile and 95% quantile).

Your kind help is greatly appreciated.

Many thanks

1 Answer 1

5

apply will let you apply a function to each row or column of a matrix. You define the matrix, the dimension to apply to (rows=1, columns=2), the function (e.g. quantile), and additional arguments for the function.

Example:

set.seed(1)
Fund <- matrix(rnorm(20*10), ncol=20, nrow=10)
qs <- apply(Fund, 2, quantile, probs=c(0.05, 0.5, 0.95))
qs
ylim=range(qs)
plot(seq(ncol(Fund)), qs[1,], t="l", lty=2, ylim=ylim) #5%
lines(seq(ncol(Fund)), qs[2,], lty=1, lwd=2) #50%
lines(seq(ncol(Fund)), qs[3,], lty=2, col=2)  #95%
legend("topleft", legend=rev(rownames(qs)), lwd=c(1,2,1), col=c(2,1,1), lty=c(2,1,2))

enter image description here

5
  • And how do we print the label next to the plot? I have tried text(locator(), labels = c("5% quantile", "50% quantile","95% quantile")) but nothing happen
    – NSAA
    Nov 3, 2013 at 17:16
  • @Nor Abd - I'm confused by your ask here, so I can't provide too much help. But, have you tried exploring the ggplot2 & reshape pacakges? Nov 3, 2013 at 17:23
  • @NorAbd - I have now added a legend to the plot. It's unclear to me what else you were hoping to do here. Nov 4, 2013 at 7:56
  • From my understanding, in order to find a quantile, we first sort the values in ascending. For example, i have 10 sample of data at time t=1 which is [2,3,4,5,8,9,11,12,19,20].To get 25% of quantile, we then calculate it as 25% x 10samples = 2.5. So it means that the quantiles is in between the second and third samples. So, the 25% quantiles should be (3+4)/2 = 3.5. right? However, if you look at my sample, for eg at t=1, the sample values are all 66.7. How can the quantiles be way different from the sample? (95% quantiles = 1.645).Can someone explain to me? or do i have a wrong concept here?
    – NSAA
    Nov 4, 2013 at 10:23
  • @NorAbd - This is not a statistics forum. I suggest you look elsewhere if you are looking for the underlying mathematics behind the function. Start with ?quantile Nov 4, 2013 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.