R: Sample from a neighborhood according to scores

0 votes

Consider a vector of numbers. I would like to sample a number which is between a given position in the vector and its neighbors such that the two closest neighbors have the largest impact. Also, this impact is in the decreasing according to the distance from the reference point.

For example, consider the following vector:

 vec = c(15, 16, 18, 21, 24, 30, 31)

and my reference is the number 16 in position #2. I would like to sample a number which will be with a high probability between 15 and 16 or (with the same high probability) between 16 and 18. The sampled numbers can be floats. Then, with a decreasing probability to sample a number between 16 and 21, and with a yet lower probability between 16 and 24, and so on.

The position of the reference is not known in advance, it can be anywhere in the vector.

I tried using with runif and quantiles, but I'm not sure how to design the scores of the neighbors.

Specifically, I wrote the following function but I suspect there might be a better/more efficient way of doing this:

GenerateNumbers <- function(Ind,N){
    dist <-  1/abs(Ind- 1:length(N))
    dist <-  dist[!is.infinite(dist)]
    dist <- dist/sum(dist)
    sum(dist) #sanity check --> 1

    V = numeric(length(N) - 1)
    for (i in 1:(length(N)-1)) {
      V[i] = runif(1, N[i], N[i+1])
    }
    sample(V,1,prob = dist)
}

where Ind is the position of the reference number (16 in this case), and N is the vector. "Dist" is a way of weighing the probabilities so that the closest neighbors have a higher impact.

Any help is highly appreciated.

May 28, 2018 in Data Analytics by DataKing99
• 8,100 points
14 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

I would suggest you to use the truncated Gaussian random sample generator.

On your considered example, you can try the following code::

# To install it: install.package("truncnorm")
library(truncnorm)
vec <- c(15, 16, 18, 21, 24, 30, 31)
x <- rtruncnorm(n=100, a=vec[1], b=vec[7], mean=vec[2], sd=1)

The histogram of the generated sample fulfills the given prerequisites.

Histogram

answered May 28, 2018 by darklord
• 6,140 points

Related Questions In Data Analytics

0 votes
2 answers

How to remove NA values from a Vector in R?

data = data[!is.na(data)]\ READ MORE

answered Apr 5 in Data Analytics by anonymous
3,537 views
0 votes
1 answer

How to import data from a Json file into R?

Easy and simple: library("rjson") json_file <- "file_name" json_data <- fromJSON(file=json_file)) or ...READ MORE

answered Apr 26, 2018 in Data Analytics by DeepCoder786
• 1,700 points
90 views
0 votes
1 answer

R programming: How to pass variables from a r program to mysql function?

To include the R variables called start.date and end.date, you can use paste to ...READ MORE

answered Dec 28, 2018 in Data Analytics by Tyrion anex
• 8,280 points
52 views
0 votes
1 answer

How to sample random rows in dataframe?

Create data frame and then implement as ...READ MORE

answered Jul 2, 2018 in Data Analytics by darklord
• 6,140 points
19 views
0 votes
1 answer

How to sample n random rows per group in a dataframe?

You can assign a random ID to ...READ MORE

answered Jul 2, 2018 in Data Analytics by darklord
• 6,140 points
51 views
0 votes
1 answer

Big Data transformations with R

Dear Koushik, Hope you are doing great. You can ...READ MORE

answered Dec 17, 2017 in Data Analytics by Sudhir
• 1,610 points
27 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,550 points
29 views
0 votes
1 answer

How to sort a data frame by columns in R?

You can just use the order function ...READ MORE

answered Apr 10, 2018 in Data Analytics by darklord
• 6,140 points
53 views
0 votes
1 answer

How to convert a list of dataframes in to a single dataframe using R?

You can use the plyr function: data <- ...READ MORE

answered Apr 13, 2018 in Data Analytics by darklord
• 6,140 points
61 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.