可変長の値のサンプルを任意の数の異なる確率分布から取り出し、各分布からのサンプリングの重み付けされた確率で取り出したいとします。purrr;確率リストを持つ複数の列からのサンプル
は、任意の提案をありがたく受け取っ
library(tidyverse)
set.seed(20171127)
# sample from 5 different probability distributions
dists <- tibble(
samp_distA = round(rnorm(n=1000, mean=17, sd=4)),
samp_distB = round(rnorm(n=1000, mean=13, sd=4)),
samp_distC = round(rnorm(n=1000, mean=13, sd=4)),
samp_distD = round(rbeta(n=1000, 2,8)*10),
samp_distE = round(rnorm(n=1000, mean=8, sd=3))
)
# define number of samples to be drawn for each group
n.times <- c(20,15,35,8,6)
# define weights to be used for sampling from dists
probs <- tibble(A = c(0.80, 0.05, 0.05, 0.05, 0.05),
B = c(0.05, 0.80, 0.05, 0.05, 0.05),
C = c(0.05, 0.05, 0.80, 0.05, 0.05),
D = c(0.05, 0.05, 0.05, 0.80, 0.80),
E = c(0.05, 0.05, 0.05, 0.05, 0.80)
)
# sample from dists, n.times, and using probs as weights...
output <- map2(sample, size=n.times, weight=probs, tbl=dists)
#...doesn't work
...私はこの使用してpurrr
のmap
機能を行うことができるはずのように思えるが、苦労しています。
ここでやろうとしていることに苦労しています。 'nx 'は何を表していますか?これを基本的なR(non-tidyverse)の方法でどう書いていますか?希望の出力はどれくらいですか? – MrFlick
明快さの欠如に対する謝罪。簡素化するために、A、B、C、D、Eが人であると想像してください。各個人について、n×値を選択したい。これらの値は、dists tibbleの列から選択し、probs列の重みに比例する列から値を引き出す確率で選択する必要があります。出力は、各個人、例えばA < - c(17,15,4、...)、B < - c(12,13,12 ...)などのリスト(またはネストされたリストテーブル)であってもよい。 .. conceptualiseに苦労して... –
probsのラベルは必ずしもdistsラベルに対応しているとは限りません。すなわち、 probのA列の値はPerson Aの重みです。したがって、Person Aはsamp_distAから値を取得する可能性が80%あり、Person Cは値を取得する確率が80%です。 samp_distCから、distDまたはdistEからの値の50%の確率? (おそらく0.5はタイプミスです)? – crazybilly