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 29, 2018 in Data Analytics by DataKing99
• 8,250 points
548 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 29, 2018 by Sahiti
• 6,370 points

Related Questions In Data Analytics

+1 vote
5 answers

How to remove NA values from a Vector in R?

Hello team, you can use na.omit x <- c(NA, 3, ...READ MORE

answered Dec 9, 2020 in Data Analytics by anonymous
• 82,880 points
194,848 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,720 points
2,327 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,700 points
1,299 views
0 votes
1 answer

How to sample random rows in dataframe?

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

answered Jul 3, 2018 in Data Analytics by Sahiti
• 6,370 points
836 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 3, 2018 in Data Analytics by Sahiti
• 6,370 points
5,085 views
0 votes
0 answers

100 samples of 20 from the dataset and drawing regression lines along with population regression line

I have a datasetwith two variables hours ...READ MORE

Apr 11, 2022 in Machine Learning by Dev
• 6,000 points
710 views
0 votes
1 answer

What happens when prob argument in sample sums to less/greater than 1?

Excellent query. The documentation is ambiguous on ...READ MORE

answered Jun 24, 2022 in Data Analytics by Sohail
• 3,040 points
558 views
+1 vote
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,766 views
+1 vote
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 14, 2018 in Data Analytics by Sahiti
• 6,370 points
6,963 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP