r/RStudio • • 4d ago

Coding help How to find inverse of a function

Hi everyone! I've just began using R for Mathematical Statistics, where I often have to find the quantiles of a probability. For that I need to find the inverse of the cdf, which are often too complicated to solve by hand. I tried using R, but when I searched up the code for the inverse in the help articles, I didn't find any notes on how to specifically find the inverse of a function, whether univariate or multivariate. How do I go about in this instance? And as a seperate question, if it's not too much: how do I plot multiple graphs in one window? Thanks in advance!

7 Upvotes

5 comments sorted by

4

u/MortMath 4d ago
  1. Check out this SO answer: https://stackoverflow.com/a/10081571
  2. If you’re using ggplot2, you can first convert your data into a long format if it’s not already, then use facet_wrap

3

u/Tasty-Tie-7443 4d ago

facet_wrap is my BFF.

2

u/SprinklesFresh5693 4d ago

If you want to calculate quantiles , theres already a function called quantile() though

To plot multiple graphs in one window id look into patchwork, or facet_wrap/facet_grid if you want to plot multiple pannels depending on. Avariable

1

u/AutoModerator 4d ago

Looks like you're requesting help with something related to RStudio. Please make sure you've checked the stickied post on asking good questions and read our sub rules. We also have a handy post of lots of resources on R!

Keep in mind that if your submission contains phone pictures of code, it will be removed. Instructions for how to take screenshots can be found in the stickied posts of this sub.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/SalvatoreEggplant 3d ago

If you're working with a pre-defined distribution, like a normal distribution or log-normal distribution, there are built-in functions to get the probabilities or the quantiles, e.g.

qnorm(p=0.975, mean=0, sd=1, lower.tail=TRUE)

# 1.96

pnorm(q=1.96, mean=0, sd=1, lower.tail=TRUE)

# 0.975

If you're working with an empirical distribution, you can use the quantile() and ecdf() functions, e.g.

 A = c(1,2,3,3,3,3,4,4,4,4,4,4,5,6,6,6,6,6,6,7,7,7,8,8,9)

quantile(A, 0.50)

# 50% 
#   5 

ecdf(A)(5)

# 0.52