Before you ask, yes I did a thorough search, including in StackExchange. I checked out this question Find local minimum in bimodal distribution with r; it has useful stuff, but doesn't go all the way for me.
Here is my data: https://www.dropbox.com/s/ulsw5cfwjnh6tdu/output?dl=0
Actually, I have thousands of files like this - the data is organized into column vectors that I need to plot. Then, for every plot I have to figure out if the distribution is bimodal, and if so, find the x value for that minimum. In the file above, the data in column 2 seems to have a bimodal distribution.
This code seems to work, but I have to manually specify where the minimum is located:
datafile <- read.table("output")
data <- datafile$V2
d <- density(data) # returns the density data with defaults
hist(data,prob=TRUE)
lines(d)
optimize(approxfun(d$x,d$y),interval=c(5,10))$minimum
The last statement (I got it from Find local minimum in bimodal distribution with r) returns
[1] 6.841577
which seems to be correct. However, I have to hardcode the interval interval=c(5,10), i.e. I have to look at the graph and tell it where to look for the minimum. If I do
optimize(approxfun(d$x,d$y),interval=range(x$V2))$minimum
I get
[1] 25.30096
which is not what I'm looking for.
I have thousands of plots, so this is not an option. Is there any way to automate the process? Please help, I'm absolutely desperate.
P.S. I don't have enough reputation to post the plot. Sorry.