在现有 R 数据框中实现随机数概率,使用的概率取决于另一列
Implementing random number probabilities in an existing R data frame, probabilities used are contingent upon another column
我正在使用 Walker 的别名方法来调整 运行dom 舍入数据,即基数 3。我已经将列值分配给数据框中的每个值 3,AliasColumn'. The values in the
AliasColumnare integers in the range
1through
5`。 I've used the Alias Method from here。数据框看起来像这样(它有 64 行):
Industry AliasColumn
1 5
2 5
3 4
4 2
5 3
6 1
7 2
8 2
9 3
10 5
11 4
12 4
13 4
14 2
15 2
16 1
17 4
18 3
19 5
20 5
根据 AliasColumn
值,我需要抛一枚装满的硬币来创建 "real" 业务计数 (NumBusinesses
),它介于 1 和 5 之间。装满的硬币table 是:
AliasColumn 1 2 3 4 5
"Heads prob" 8/12 11/12 1 10/12 5/12
"Alias prob" 4/12 1/12 - 2/12 7/12
Alias value 2 3 - 3 1
例如,如果 AliasColumn
值为 1,则 NumBusinesses
值的 8/12 时间将为 1,而 NumBusinesses
值的时间为 4/12将是 2。对于 3 的 AliasColumn
值,这是唯一可以分配给 NumBusinesses
.
的值
因此,NumBusinesses
收到两个值之一,概率取决于 AliasColumn
中的特定列。因为 NumBusinesses
列只能取两个值之一,并且因为它们是整数,并且根据 AliasColumn
中的值而有所不同,我希望在 R 中使用 sample() 函数。我一直无法让它工作。
我已经尝试过(我刚刚在我的代码中注意到我显示的 AliasColumn
值 4 的代码与 1 和 2 的代码不同,但输出似乎与当我 运行 它最初使用 1:2
和 2:3
而不是 1,2
和 2,3
:
foo$NumBusinesses[AliasCol==1] <-sample(c(1,2),1, replace=TRUE,prob=c(8,4))
foo$NumBusinesses[AliasCol==2] <-sample(c(2,3),1, replace=TRUE,prob=c(11,1))
foo$NumBusinesses[AliasCol==3] <- 3
foo$NumBusinesses[AliasCol==4] <-sample(c(3:4),1, replace=TRUE,prob=c(2,10))
foo$NumBusinesses[AliasCol==5] <-sample(c(1,5),1, replace=TRUE,prob=c(7,5))
这似乎将 NumBusinesses
值设置为与 AliasColumn
中的值相同,除了 NumBusinesses
值为 5,然后 AliasColumn
值被设置为 1。
我考虑过 ifelse 循环,并尝试了一个:
ifelse(foo$AliasCol==1, foo$NumBusinesses<- Sample(c(1,2),1, replace=TRUE,prob=c(8,4)),
ifelse(foo$AliasCol==2),
foo$NumBusinesses<- sample(c(2,3),1, replace=TRUE,prob=c(11,1)),
ifelse(foo$AliasCol==3), foo$NumBusinesses<- 3,
ifelse(foo$AliasCol==4),
foo$NumBusinesses <- sample(c(3:4),1, replace=TRUE,prob=c(2,10)),
foo$NumBusinesses <- sample(c(1,5),1, replace=TRUE,prob=c(7,5)))
而且我收到了这个错误(这让我相信我对循环想得太多了):
Error in ifelse(foo$AliasCol == 1, foo$NumBusinesses <- sample(c(1, : unused arguments (foo3$NumBusinesses <- sample(c(2, 3), 1, replace = TRUE, prob = c(11, 1)), ifelse(foo$AliasCol == 3), foo$NumBusinesses <- 3, ifelse(foo$AliasCol == 4), foo$NumBusinesses <- sample(c(3:4), 1, replace = TRUE, prob = c(2, 10)), foo$NumBusinesses <- sample(c(1, 5), 1, replace = TRUE, prob = c(7, 5)))
如何在一个步骤或一组步骤中生成条件输出?
假设你有这个:
#probabilities of not changing AliasColumn
headProb<-c(8/12, 11/12, 1 , 10/12, 5/12)
#alias values when AliasColumn changes
aliasValues<-c(2,3,NA,3,1)
#your data.frame
df<-structure(list(Industry = 1:20, AliasColumn = c(5L, 5L, 4L, 2L,
3L, 1L, 2L, 2L, 3L, 5L, 4L, 4L, 4L, 2L, 2L, 1L, 4L, 3L, 5L, 5L
)), .Names = c("Industry", "AliasColumn"), class = "data.frame", row.names = c(NA, -20L))
那你可以试试:
ifelse(runif(nrow(df))<=headProb[df$AliasColumn],
df$AliasColumn,aliasValues[df$AliasColumn])
我正在使用 Walker 的别名方法来调整 运行dom 舍入数据,即基数 3。我已经将列值分配给数据框中的每个值 3,AliasColumn'. The values in the
AliasColumnare integers in the range
1through
5`。 I've used the Alias Method from here。数据框看起来像这样(它有 64 行):
Industry AliasColumn
1 5
2 5
3 4
4 2
5 3
6 1
7 2
8 2
9 3
10 5
11 4
12 4
13 4
14 2
15 2
16 1
17 4
18 3
19 5
20 5
根据 AliasColumn
值,我需要抛一枚装满的硬币来创建 "real" 业务计数 (NumBusinesses
),它介于 1 和 5 之间。装满的硬币table 是:
AliasColumn 1 2 3 4 5
"Heads prob" 8/12 11/12 1 10/12 5/12
"Alias prob" 4/12 1/12 - 2/12 7/12
Alias value 2 3 - 3 1
例如,如果 AliasColumn
值为 1,则 NumBusinesses
值的 8/12 时间将为 1,而 NumBusinesses
值的时间为 4/12将是 2。对于 3 的 AliasColumn
值,这是唯一可以分配给 NumBusinesses
.
因此,NumBusinesses
收到两个值之一,概率取决于 AliasColumn
中的特定列。因为 NumBusinesses
列只能取两个值之一,并且因为它们是整数,并且根据 AliasColumn
中的值而有所不同,我希望在 R 中使用 sample() 函数。我一直无法让它工作。
我已经尝试过(我刚刚在我的代码中注意到我显示的 AliasColumn
值 4 的代码与 1 和 2 的代码不同,但输出似乎与当我 运行 它最初使用 1:2
和 2:3
而不是 1,2
和 2,3
:
foo$NumBusinesses[AliasCol==1] <-sample(c(1,2),1, replace=TRUE,prob=c(8,4))
foo$NumBusinesses[AliasCol==2] <-sample(c(2,3),1, replace=TRUE,prob=c(11,1))
foo$NumBusinesses[AliasCol==3] <- 3
foo$NumBusinesses[AliasCol==4] <-sample(c(3:4),1, replace=TRUE,prob=c(2,10))
foo$NumBusinesses[AliasCol==5] <-sample(c(1,5),1, replace=TRUE,prob=c(7,5))
这似乎将 NumBusinesses
值设置为与 AliasColumn
中的值相同,除了 NumBusinesses
值为 5,然后 AliasColumn
值被设置为 1。
我考虑过 ifelse 循环,并尝试了一个:
ifelse(foo$AliasCol==1, foo$NumBusinesses<- Sample(c(1,2),1, replace=TRUE,prob=c(8,4)),
ifelse(foo$AliasCol==2),
foo$NumBusinesses<- sample(c(2,3),1, replace=TRUE,prob=c(11,1)),
ifelse(foo$AliasCol==3), foo$NumBusinesses<- 3,
ifelse(foo$AliasCol==4),
foo$NumBusinesses <- sample(c(3:4),1, replace=TRUE,prob=c(2,10)),
foo$NumBusinesses <- sample(c(1,5),1, replace=TRUE,prob=c(7,5)))
而且我收到了这个错误(这让我相信我对循环想得太多了):
Error in ifelse(foo$AliasCol == 1, foo$NumBusinesses <- sample(c(1, : unused arguments (foo3$NumBusinesses <- sample(c(2, 3), 1, replace = TRUE, prob = c(11, 1)), ifelse(foo$AliasCol == 3), foo$NumBusinesses <- 3, ifelse(foo$AliasCol == 4), foo$NumBusinesses <- sample(c(3:4), 1, replace = TRUE, prob = c(2, 10)), foo$NumBusinesses <- sample(c(1, 5), 1, replace = TRUE, prob = c(7, 5)))
如何在一个步骤或一组步骤中生成条件输出?
假设你有这个:
#probabilities of not changing AliasColumn
headProb<-c(8/12, 11/12, 1 , 10/12, 5/12)
#alias values when AliasColumn changes
aliasValues<-c(2,3,NA,3,1)
#your data.frame
df<-structure(list(Industry = 1:20, AliasColumn = c(5L, 5L, 4L, 2L,
3L, 1L, 2L, 2L, 3L, 5L, 4L, 4L, 4L, 2L, 2L, 1L, 4L, 3L, 5L, 5L
)), .Names = c("Industry", "AliasColumn"), class = "data.frame", row.names = c(NA, -20L))
那你可以试试:
ifelse(runif(nrow(df))<=headProb[df$AliasColumn],
df$AliasColumn,aliasValues[df$AliasColumn])