quantile() function

34 views
Skip to first unread message

Darren Li

unread,
Apr 13, 2025, 10:12:12 PMApr 13
to slim-discuss
Hi,

I'm wondering if there's a way in Eidos to return the quantiles of a vector of values (say, x)? Effectively, it would work similar to quantile(), but instead of outputting the x values that correspond to probs (in the case where probs is null, these are the x values corresponding to quantiles c(0.0, 0.25, 0.5, 0.75, 1.0)), I want the quantile of each value in x to be output. 

Alternatively, is there a way I could see what is contained within the quantile() function, so that I can modify it and build my own function?

Thanks,
Darren

Peter Ralph

unread,
Apr 14, 2025, 12:39:29 AMApr 14
to Darren Li, slim-discuss
Hi, Darren - what you want here depends in detail on exactly how you define the quantile of a discrete set of values; however, anything you want is derivable from the rank( ) function. (So, one answer to your question is that
  rank(x) / length(x)
gives you for each entry of x, the proportion of values in x that are less than or equal to x (if they're unique).

Hope that answers your question?
 peter

From: slim-d...@googlegroups.com <slim-d...@googlegroups.com> on behalf of Darren Li <chong...@gmail.com>
Sent: Sunday, April 13, 2025 7:12 PM
To: slim-discuss <slim-d...@googlegroups.com>
Subject: quantile() function
 
--
SLiM forward genetic simulation: http://messerlab.org/slim/
---
You received this message because you are subscribed to the Google Groups "slim-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to slim-discuss...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/slim-discuss/93811074-23a2-4f6e-8cc3-d979fe330d51n%40googlegroups.com.

Ben Haller

unread,
Apr 14, 2025, 8:39:48 AMApr 14
to slim-discuss
Hi Darren!

I agree with Peter's answer; leveraging rank() is going to be the simple way to go here.

Regarding:

> is there a way I could see what is contained within the quantile() function, so that I can modify it and build my own function?

The code for the implementation of quantile() in Eidos is written in C++, and can be found here:


In principle, you could create your own Eidos function based on this, yes.  There's no documentation for doing so, and unless you're fairly familiar with C++ it would be an uphill battle; but I thought I'd show you the code just in case you're curious how things work under the hood.  :->  Use rank(), that should be easy.  Happy modeling!

Cheers,
-B.

Benjamin C. Haller
Messer Lab
Cornell University

Darren Li

unread,
Apr 14, 2025, 7:18:12 PMApr 14
to slim-discuss
Thanks for these replies, Peter and Ben!

I wasn't aware of the existence of rank() because I didn't find it in the Eidos manual. I found out that it was later added in 2023: https://github.com/MesserLab/SLiM/issues/379

Essentially, I was trying to use quantile mapping to map the distribution of my modeled variables (in this case, phenotype) to observations. I used the quantile() function (with default “type 7” algorithm) in R to obtain the quantile values for the distribution of my observations, input this to SLiM as a file and in the final step, I was trying to then use a similar quantile() function in SLiM to map these quantiles to the modeled distribution of phenotypes. 

Given your suggestions to use rank(), I think I should then change my approach: instead of using quantile() in R to obtain the quantile values for the distribution of my observations, I would then use rank() in R, and then use rank() again in SLiM, so that it's both using the same function for quantile mapping. I believe this should help me achieve the same goal if I was to use quantile() in both languages?

Thanks,
Darren

Darren Li

unread,
Apr 14, 2025, 8:24:50 PMApr 14
to slim-discuss
I've used rank() to compute quantiles as Peter suggested, but a bit modified:

normalized_ranks <- (ranks - 1) / (length(data) - 1)

Built my own user-defined function to approximate the quantile() function in R using rank() and compared the output with that of quantile(). It's pretty much similar, so unless if there's something that I'm missing that may make things go wrong, I'll do as I mentioned above.

Thank you again both, it's very helpful! 
Reply all
Reply to author
Forward
0 new messages