I have a code:
x1=rnorm(100,-10,1)
sum(x1>quantile(x1,0.05))/length(x1)
I would like to ask someone from the forum to explain to me what exactly the second line of code does. Thank you in advance for your help.
CodePudding user response:
x1 is 100 normally distributed #s from a population with mean of -10.
quantile(x1,0.05)
identifies the 5th percentile of that vector.
x1>quantile(x1,0.05)
tests each element in x1 to see if it's more than that 5th percentile, outputting TRUE (aka 1) if so, so its sum is a count of elements larger than the 5th percentile.
Finally, it's divided by the number of elements in x1: 100. So we're checking what percentage of the values are more than the 5th percentile. I'd expect 95%.