English 中文(简体)
根据 2 个条件从抽样中创建矢量
原标题:Create a vector from sampling based on 2 conditions

我想从矢量 s<-0:1440 中样本值,以创建矢量 >u ,使 sum(u)=x long(u) <k , 给定 。 显然, k*max(s) & gt;sum(u)

是否还有办法让野兽强力模拟许许多多的 < code> u/code> 矢量? 我想避免干扰概率分布(用于取样),我不在乎某些 < code> u 矢量是否会被丢弃。

< 强度 > EDIT: < 强度 > 关于 P Lapointe 的 < 强度 > 关于 < code > 长( u) < / code > 的精细评论。 重要的是, < code > 长( u) < / code > 不应固定( < code > 长( u) & lt;k < / code > ), 使矢量 < code > < code > 长( u) < / k/ code > 具有变量长度。 另一种办法是修正 < code > 长( u) =k < / code > 。 另一种办法是修正 < code > 长( u) =k < code >, 但增加零后, < code> sum( u) < / code > 会维持不变,但 < 长( u) < / code > 增加1 (直到 < code) > (直到 < code > lex > (u) < lex > lex) < lex > lex=k < ) > ) > 。

最佳回答

OK, 这里为回答您的问题的 algo 。 基本上, 我们正在做两个随机的样本。 首先找到一个 k, 满足 < code> later( u) & lt; k 的限制。 使用 k, 我们然后使用另一个样本来查找 < code> k-1 < / code > 编号 。 这在 algo 中被称为 < code> first 。 当我们发现一个样本 < code> k-1 低于 < code> x , 也就是想要的总量, 我们添加了 < code> x- sum ( firm) 的差数来完成序列 。

#Inputs
x <-2500 # desired sum
s1<-0:min(1440,x) #universe
max.k <-10

k <-sample(3:(max.k-1),1) #length(u)<k, starts at 3 because low k can be problematic 
                          #with current inputs
initial <-x+1 #deliberately above limit to initialize the while
u <-s1+1      #deliberately above limit to initialize the while

while (sum(initial)>x | max(u)>max(s1)) {
initial <-sample(s1,k-1,replace=TRUE) #Find k-1 samples
u <-c(initial,x-sum(initial)) #add number that makes sum == x
}
 #example 
 > k
[1] 4
> x
[1] 2500
> u
[1]  282 1337  876    5
> sum(u)
[1] 2500

另外,如果您有一个大 max.k , 添加一个概率矢量或许是件好事, 概率矢量会使样本中的低数值具有更大的概率。 否则, 在目前的示例中, 如果您的数值超过1000, 则很难得到 $2500 。

prob1 <-1/((s1+1)*max.k ) #gives more probality to low numbers

while (sum(initial)>x | max(u)>max(s1)) {
initial <-sample(s1,k-1,replace=TRUE,prob=prob1) #Find k-1 samples
u <-c(initial,x-sum(initial)) #add number that makes sum == x
}
问题回答

我不认为您可以在没有 MIP 的情况下强制使用 < code> 长( u) & lt; k 限制。 但如果您将其修正为数字, 您可以使用 < code> sample

在此示例中,头五行是样本,总和将添加到矩阵最后一行。如果找不到匹配值,您可以增加 replia (目前为2000) 的大小。您还应该检查 range(all[k+1]) (all[k+1]) ),看看您想要的数额(x )是否在抽样总和之内。

set.seed(1)
s<-0:1440 #universe
k <-5 #fixed number of items in sample
x <-2500 # desired sum
all <-replicate(20000, sample(s,k,replace=TRUE))
all <-rbind(all,colSums(all))
all[,all[k+1,]==x,drop=FALSE] #gives two possible samples

      [,1] [,2]
[1,]  410  241
[2,]  189  687
[3,]  988  401
[4,]  897  983
[5,]   16  188
[6,] 2500 2500




相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签