我有我写的 R 代码:
flipCoinPoissonTimes <- function() {
lambda <- 5 # initial parameter for the Poisson distribution
num_flips <- rpois(1, lambda) # generate a random number from a Poisson distribution
p <- 0.5 # initial probability of heads
set <- 1 # initial set number
results <- data.frame(set=integer(), turn_number=integer(), value_of_p=numeric(), result=character(), lambda=numeric(), num_flips=integer())
while(TRUE) {
for (turn_number in 1:num_flips) {
flip <- runif(1) # generate a random number between 0 and 1
if (flip < p) {
results <- rbind(results, data.frame(set=set, turn_number=turn_number, value_of_p=p, result="Heads", lambda=lambda, num_flips=num_flips))
return(results) # stop once the first head is observed
} else {
results <- rbind(results, data.frame(set=set, turn_number=turn_number, value_of_p=p, result="Tails", lambda=lambda, num_flips=num_flips))
p <- p / 2 # reduce the probability of heads by half
}
}
# if no heads were observed in this set, generate a new Poisson random number centered around the previous num_flips
lambda <- num_flips
num_flips <- rpois(1, lambda)
p <- 0.5 # reset the probability of heads
set <- set + 1 # increment the set number
}
}
results <- flipCoinPoissonTimes()
print(results)
results$iteration = 1
results$total_turns = 1:nrow(results)
final = results
我尝试运行此代码 100 次并将所有结果存储在单个垂直数据框中(包含所有列,包括迭代和总转数)。
我尝试了这段代码:
> replicate(100, flipCoinPoissonTimes())
但它没有产生正确的结果。
有人可以告诉我如何正确地做吗?
我自己尝试使用 lapply 解决这个问题:
flipCoinPoissonTimes <- function(iteration) {
lambda <- 5 # initial parameter for the Poisson distribution
num_flips <- rpois(1, lambda) # generate a random number from a Poisson distribution
p <- 0.5 # initial probability of heads
set <- 1 # initial set number
results <- data.frame(set=integer(), turn_number=integer(), value_of_p=numeric(), result=character(), lambda=numeric(), num_flips=integer(), iteration=integer())
while(TRUE) {
for (turn_number in 1:num_flips) {
flip <- runif(1) # generate a random number between 0 and 1
if (flip < p) {
results <- rbind(results, data.frame(set=set, turn_number=turn_number, value_of_p=p, result="Heads", lambda=lambda, num_flips=num_flips, iteration=iteration))
return(results) # stop once the first head is observed
} else {
results <- rbind(results, data.frame(set=set, turn_number=turn_number, value_of_p=p, result="Tails", lambda=lambda, num_flips=num_flips, iteration=iteration))
p <- p / 2 # reduce the probability of heads by half
}
}
# if no heads were observed in this set, generate a new Poisson random number centered around the previous num_flips
lambda <- num_flips
num_flips <- rpois(1, lambda)
p <- 0.5 # reset the probability of heads
set <- set + 1 # increment the set number
}
}
现在,我启动该功能:
# lapply
all_results <- do.call(rbind, lapply(1:100, function(i) {
result <- flipCoinPoissonTimes(i)
result$total_turns <- 1:nrow(result)
return(result)
}))
作为奖励,我绘制了结果(保留每次迭代的最后一行):
library(dplyr)
last_rows <- all_results %>%
group_by(iteration) %>%
filter(row_number() == n())
# Reshape
all_results_long <- last_rows %>%
pivot_longer(cols = c(turn_number, value_of_p, lambda, num_flips, total_turns),
names_to = "variable",
values_to = "value")
# Plot
ggplot(all_results_long, aes(x = log(value), color = variable, fill = variable)) +
geom_density(alpha = 0.3) +
labs(title = "Density Distributions of Various Columns",
x = "Value",
y = "Density") +
theme_minimal()