[R] is there a function to find the quantile of the mean of a vector?

David LeBauer dlebauer at illinois.edu
Fri Jun 18 22:19:15 CEST 2010


Jorge,

Thanks for your help. which.min() on the sorted vector divided by the
vector length gave me the value I was looking for (I was looking for
the probability p(mean(x) | x)):

> x <- runif(1000)
> x.sort <- sort(x)
> x.length <- length(x)
> x.mean <- mean(x)
> p.mean <- which.min((x.sort - x.mean)^2) / x.length

Another user pointed out a function suited for this purpose, findInterval()

> p.mean <- findInterval(x.mean, xsort) / x.length

Thanks for your help,

David

On Thu, Jun 17, 2010 at 10:15 PM, Jorge Ivan Velez
<jorgeivanvelez at gmail.com> wrote:
> Hi David,
> You might try:
>> set.seed(1)
>> x <- runif(10, 3, 7)
>> x
>  [1] 4.062035 4.488496 5.291413 6.632831 3.806728 6.593559 6.778701 5.643191
> 5.516456 3.247145
>> (x-mean(x))^2
>  [1] 1.308783661 0.514892188 0.007285983 2.035688832 1.958118177 1.925165288
> 2.473214156
>  [8] 0.191087609 0.096348590 3.837329960
>> which.min((x-mean(x))^2)
> [1] 3
>> x[which.min((x-mean(x))^2)]
> [1] 5.291413
>> which.min(scale(x, scale = FALSE)**2)
> [1] 3
> See ?which.min and ?scale for more information.
> HTH,
> Jorge
>
> On Thu, Jun 17, 2010 at 7:06 PM, David LeBauer <> wrote:
>>
>> Hello,
>>
>> I am interested in finding the quantile of the mean of a vector,
>> something analogous to using the pnorm(), but for an mcmc chain
>> instead of a distribution with known parameters.
>>
>> One approach would be to write a function that finds the index of x_i
>> that minimizes (x-mean(x))^2
>>
>> I suspect there is a function available to do this, but I can't find it?
>>
>> Thank you,
>>
>> David
>>
>> ______________________________________________
>> 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.
>
>



-- 
David LeBauer, PhD
Energy Biosciences Institute
University of Illinois Urbana-Champaign
1206 W. Gregory Drive
Urbana, IL  61801, U.S.A.



More information about the R-help mailing list