R编程:通过抽样获得最可能的价值

时间:2014-05-05 16:00:45

标签: r vector sampling

我有一张弹力柱的桌子。对于每个记录,我想分配一个新的弹性值。该值基于假设均匀分布的进行采样。例如,假设我有4条弹性值记录(1.2,1.3,1.4,1.5)。所以我对这4个值进行了50次采样,之后我得到了4X50的矩阵。如何为记录分配最多的值?

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))

在上面的代码中,我想要一个新的measurement_Elasticity向量,其值在采样过程中最大。

使用亨利的代码,我用这种方式解决了我的问题:

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)
}

1 个答案:

答案 0 :(得分:2)

您可能希望将此视为一种可能性

> 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

通常你会获得一个值:尝试更改种子。