[R] Help with multiple use of "quantile"

arun smartpink111 at yahoo.com
Sat Jun 8 19:28:13 CEST 2013


HI,
You could also use:

set.seed(24)
aaa <- matrix(rnorm(60), ncol=3)
bbb <- matrix(runif(15), ncol=3)
ccc1<- mapply(quantile,as.data.frame(aaa),as.data.frame(bbb))
ccc <- sapply(1:dim(aaa)[2], function(i) quantile(aaa[, i], bbb[, i])) #Jean's solution

colnames(ccc1)<-NULL

  identical(ccc,ccc1)
#[1] TRUE
A.K.



----- Original Message -----
From: "Adams, Jean" <jvadams at usgs.gov>
To: "Parodi, Pietro" <Pietro.Parodi at willis.com>
Cc: "r-help at r-project.org" <r-help at r-project.org>
Sent: Saturday, June 8, 2013 9:49 AM
Subject: Re: [R] Help with multiple use of "quantile"

Try this, Pietro ...

# example data
aaa <- matrix(rnorm(60), ncol=3)
bbb <- matrix(runif(15), ncol=3)

# calculate quantiles
ccc <- sapply(1:dim(aaa)[2], function(i) quantile(aaa[, i], bbb[, i]))
# change rownames
rownames(ccc) <- seq(dim(ccc)[1])

Jean


On Sat, Jun 8, 2013 at 8:20 AM, Parodi, Pietro <Pietro.Parodi at willis.com>wrote:

> Hello
>
> I have a matrix aaa like this:
>
>            aa1       aa2       aa3
> [1,]  8371.417  27613.57  1170.466
> [2,] 14317.999  42421.82  3423.934
> [3,] 22026.466  59874.14  8103.084
> [4,] 33884.986  84506.34 19176.764
> [5,] 57954.968 129824.30 56097.310
>
> where each column represents an empirical distribution of random variates
> (normally the number of rows would be, say, 10,000 or 100,000) and a matrix
> bbb of percentiles like this:
>
> bbb
>
>            [,1]      [,2]      [,3]
> [1,] 0.70092980 0.8144194 0.6200732
> [2,] 0.77968803 0.5804948 0.5463661
> [3,] 0.01509415 0.9313509 0.8611973
> [4,] 0.22654757 0.6183386 0.4962867
> [5,] 0.36548835 0.6608696 0.3062784
> What I'd like to do is to apply the quantiles in the three columns of bbb
> to the columns of aaa independently, so as to obtain a matrix ccc such
> that, for example, ccc[3,1]=quantile(aaa[,1],bbb[3,1]). THe complete matrix
> is:
>
>
> ccc =    quantile(aaa[,1],bbb[1,1]), quantile(aaa[,2],bbb[1,2]),
> quantile(aaa[,3],bbb[1,3])
>             quantile(aaa[,1],bbb[2,1]), quantile(aaa[,2],bbb[2,2]),
> quantile(aaa[,3],bbb[2,3])
>             quantile(aaa[,1],bbb[3,1]), quantile(aaa[,2],bbb[3,2]),
> quantile(aaa[,3],bbb[3,3])
>             quantile(aaa[,1],bbb[4,1]), quantile(aaa[,2],bbb[4,2]),
> quantile(aaa[,3],bbb[4,3])
>             quantile(aaa[,1],bbb[5,1]), quantile(aaa[,2],bbb[5,2]),
> quantile(aaa[,3],bbb[5,3])
>
>
>
> Now if I only two vectors, it would be enough for me to define
> ccc=quantile(aaa,bbb). However, if I do this when aaa and bbb are matrix,
> quantile(aaa,bbb) does something else -- it gives the percentiles bbb of
> the whole set of matrix elements rather than considering separately the
> quantiles of the different columns.
>
> I suspect I'll need to use "apply" in some form but have been able to come
> up with the correct form.
>
> As you might have imagined, this arises in the  context of simulating
> random variables from different distribution in empirical form that are
> correlated through a copula.
>
> Thanks in advance for your help!
>
> Pietro
> _
>
> ______________________________________________________________________
>
> For information pertaining to Willis' email confidentiality and monitoring
> policy, usage restrictions, or for specific company registration and
> regulatory status information, please visit
> http://www.willis.com/email_trailer.aspx
>
> We are now able to offer our clients an encrypted email capability for
> secure communication purposes. If you wish to take advantage of this
> service or learn more about it, please let me know or contact your Client
> Advocate for full details. ~W67897
> ______________________________________________________________________
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

    [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list