-2

Write a function get_probs (taking as input a numeric vector) that is first computing the rank (you can use the R function rank for this) and then divides it by the number of observations to obtain the empiric probabilities. These probabilities should be returned as a vector.

I did not totally get every part of the task. Could someone help me out, please?

user438383
  • 5,716
  • 8
  • 28
  • 43
  • Welcome. What did you not understand? What did you try to understand it? – user438383 Jun 05 '22 at 10:07
  • What I got is that I should write first create a vector, like 'x = c(-1, 0, 1)' then a function 'get_probs <- function (rank(x)/ number of observations)'. I am not sure I understood it correctly – Francesco Jun 05 '22 at 10:17

1 Answers1

0

First I create a random numeric vector using c with some numbers. To get the empiric probabilities you can use rank and divide by the number of observations in your vector using length. This will calculate the number of observations, in this case 6. After that you can use your created function and call the numeric vector in your function which returns the probabilities as a vector. You can use the following code:

vector <- c(1,0,-1,1,1,0)

get_probs <- function(vector) {
  probs <- rank(vector)/length(vector)
  return(probs)
}
print(get_probs(vector))

Output:

[1] 0.8333333 0.4166667 0.1666667 0.8333333 0.8333333 0.4166667

Quantiles

You can use the function quantile like this:

quantile(get_probs(vector))

Output:

       0%       25%       50%       75%      100% 
0.1666667 0.4166667 0.6250000 0.8333333 0.8333333 
Quinten
  • 35,235
  • 5
  • 20
  • 53
  • Thank you so much! It helped me out a lot to complete the rest of the exercise. – Francesco Jun 05 '22 at 13:23
  • If i want the extend the function get_probs that first computes the probabilities and then uses these values to compute the corrisponding theoritical quantiles for the standard normal distribution, should I follow the same approach? Because I tried somehow following the same approach, but the new output is a bit weird – Francesco Jun 05 '22 at 13:26
  • @Francesco, I added some code to my answer. You can use the function `quantile`. You can extend this in the created function or outside. – Quinten Jun 05 '22 at 14:16
  • thank you! Just have one more point. I created the function and when I tried to create a new column$quantiles to my data, it gave me an error: x Existing data has 1811 rows. x Assigned data has 5 rows. For the first function everything was perfectly fine – Francesco Jun 05 '22 at 15:04
  • @Francesco, For that you can use the function `ntile` from `dplyr` package. So it will be `dataframe$quantiles <- ntile(dataframe$column, 4)`. – Quinten Jun 05 '22 at 15:21
  • thank you again. I am sorry to bother you that much and I really appreciate your patient, I am still learning. Should in this case the column be totally different from the function previously created? Maybe I should have posted the full task before: _Extend the function `get_probs`: write a new function `get_qs` that first computes the probabilities as before (or by callicng `get_probs`), and then uses these values to compute the corresponding theoretical quantiles for the standard normal distribution. Also add these quantiles to the data. Make the QQ plot_ ... – Francesco Jun 05 '22 at 15:45
  • @Francesco, No problem! Learning is fun they say! I would suggest you ask this as a new question because it becomes more off-topic from your question above. And I would suggest having a look at this: [how-to-make-reproducible-question](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). If my answer solved your problem, click the big checkbox to accept it as the answer. – Quinten Jun 05 '22 at 15:51