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
16 views

1 answer to this question.

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
4 answers

How to remove NA values from a Vector in R?

You can try na.omit() or na.exclude() too. ...READ MORE

answered Jul 31 in Data Analytics by anonymous
• 19,510 points
5,253 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
139 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
63 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
33 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
143 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
42 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
57 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
86 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
119 views