Question

I have a table which has an elasticity column. To each of the records, I want to assign a new elasticity value. That value is based on performing a sampling assuming uniform distribution. For eg, lets say I have 4 records with elasticity values (1.2, 1.3, 1.4, 1.5). So I take a sample of these 4 values 50 times, after which I have a matrix of 4X50. How do I assign the value that came up the most to the record?

num_vals_to_sample = sum(measurement_Elasticity); #Counts the no of records


Sampled_measurement_Elasticity = replicate(50, sample(measurement_Elasticity, num_vals_to_sample, replace = TRUE))

In the above code, I want a new measurement_Elasticity vector which has the value that came up the most during the sampling process.

Using Henry's code, I solved my problem this way:

num_vals_to_sample = sum(measurement_Elasticity);


New_measurement_Elasticity = c()

#Elasticity Sampling

for (i in 1:num_vals_to_sample)
{

  Sampled_measurement_Elasticity <- table(sample(measurement_Elasticity), 100, replace=TRUE))

  Most_Likely_Elas =as.numeric(names(Sampled_measurement_Elasticity)[max(which(Sampled_measurement_Elasticity==max(Sampled_measurement_Elasticity)))])

  append(New_measurement_Elasticity, Most_Likely_Elas)
}
Was it helpful?

Solution

You might want to consider this as a possibility

> set.seed(5)
> examplecounts <- table(sample(c(1.2, 1.3, 1.4, 1.5), 50, replace=TRUE))
> examplecounts
1.2 1.3 1.4 1.5 
 13  13  11  13 
> names(examplecounts)[which(examplecounts == max(examplecounts))]
[1] "1.2" "1.3" "1.5"
> as.numeric(names(examplecounts)[min(which(examplecounts==max(examplecounts)))])
[1] 1.2

Usually you will get a single value: try changing the seed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top