1
$\begingroup$

I am trying to figure out how can I create an optimization function that will select from a data.frame or matrix, the row with the most drawn value (the numbers are not integer, so I need to maybe look for the number with the highest probability of occurrence) and at the same time minimize the second column (that represents the error columns).

This is how my data looks like:

dataset=data.frame(data=abs(rnorm(200,0,2)),error=runif(400,.3,.5))

How cand I do this?

$\endgroup$
1
$\begingroup$

A very simple approach would be to get the probability of occurrence for each value and then find the minimum of probability * 1/error.
You may want to add some scaling constant to increase the importance of either the probability or the error.

# Set seed for reproducibility
set.seed(12345)
# Create the dataset
dataset <- data.frame(data=abs(rnorm(200,0,2)),error=runif(400,.3,.5))
# Determine the probability of occurrence of each data point
dens <- density(dataset$data)

dataset$prob <- sapply(dataset$data, function(d)
                        {
                        id.x <- which.min(abs(d-dens$x))
                        prob <- dens$y[id.x]
                        })
res.id <- which.max(dataset$prob * 1/dataset$error)

print(dataset[res.id,])

Which gives

        data     error     prob
297 1.010087 0.3009827 0.364058

which looks good, as:

> range(dataset$error)
[1] 0.3008509 0.4993993

> range(dataset$prob)
[1] 0.01657322 0.36405803
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.