一枚公平的硬币被抛了10次。求X的概率质量函数(PMF)和观察到的最长头的长度。需要编写R代码来完成这一任务。下面是用于此任务的一些函数:as.integer()、intToBits()、rev()、rle()。
我对函数的使用有了初步的认识,但没有足够的知识将其联系起来计算PMF和计算最长运行的长度。
toBinary <- function(n){
paste0(as.integer(rev(intToBits(n)[1:10])),collapse = "")
}
toBinary(4)
toBinary(1023)
for(i in 0:1023){
print(toBinary(i))
}发布于 2022-10-30 01:36:26
为了完成这项任务,我增加了以下几行:
# number of trials
trials <- 10
# probability of success
success <- 0.5
# x is number of random variable X
x <- 0:trials
# number of probabilities for a binomial distribution
prob <- dbinom(x,trials,success)
prob
# create a table with the data from above
prob_table<-cbind(x,prob)
prob_table
# specify the column names from the probability table
colnames(prob_table)<-c("x", "P(X=x)")
prob_tablehttps://stackoverflow.com/questions/74246164
复制相似问题