Kalinkin Alexey
Kalinkin Alexey

Reputation: 175

Finding a threshold value in R

I have followind data frame:

element    type     values
elem1    control    14.580546
elem2    decoy      1.863077
elem3    control    15.595858
elem4    control    14.822892
elem5    decoy      8.922175
elem6    control    17.484545

And I have to find a threshold value T at which the elements with type "decoy" is the 5% of the elements of the type "control". In the case of misunderstanding, I put the link with drawing in the post: https://yadi.sk/i/Guxu32nqhoxmi

How I can do that in R? Many thanks in advance.

Upvotes: 0

Views: 4707

Answers (1)

dzeltzer
dzeltzer

Reputation: 1000

Note that the threshold only depends on the distribution of "decoy".

library(ggplot2)

df = rbind(
  data.frame(values=rnorm(10000, mean=0, sd=1), type="decoy"),
  data.frame(values=rnorm(10000, mean=2, sd=.5), type="control")
)

threshold <- quantile(df$values[df$type=="decoy"], probs=0.95)

ggplot(df, aes(x=values, color=type)) + geom_density() + geom_vline(xintercept=threshold)

enter image description here

Upvotes: 1

Related Questions