如何解决减少R中条件语句的数量?
我有大量的代码块,这些if语句具有不同的if语句和for语句。有没有一种方法可以减少此代码中if和for循环的数量,使其更加紧凑和高效?
library(tidyverse)
occ_simulation <- function(nyears,lambda,alpha,beta){
data_matrix <- matrix(,nrow = nyears,ncol = 6)
for (z in 1:nyears){
data_matrix[z][1] <- z
}
for (yr in 1:nyears){
poisson_sim = rpois(1,lambda)
for (number_of_events in poisson_sim){
if (number_of_events == 1){
beta_sim = rbeta(1,beta)
data_matrix[yr,2] <- beta_sim
} else if (number_of_events == 2){
for (i in 2:3){
beta_sim = rbeta(1,beta)
data_matrix[yr,i] <- beta_sim
}
} else if (number_of_events == 3){
for (i in 2:4){
beta_sim = rbeta(1,i] <- beta_sim
}
} else if (number_of_events == 4){
for (i in 2:5){
beta_sim = rbeta(1,i] <- beta_sim
}
} else{
for (i in 2:6){
beta_sim = rbeta(1,i] <- beta_sim
}
}
}
}
sorted_matrix <- cbind(data_matrix[,1],t(apply(data_matrix[,2:6],1,function(x) sort(x))))
G <- sorted_matrix %>% as.data.frame %>%
pivot_longer(-V1) %>%
ggplot(aes(x=factor(V1),y=value,color=name,group=name))+
geom_point()+
labs(color='Column',x='Time (Years)',y ='Probability')+
theme_bw()
return(G)
}
manual = occ_simulation(10,10,2,20)
manual
此代码的作用是输入您要模拟的时间范围,然后每年进行一次泊松模拟以计算该年发生的事件数。然后,根据当年发生的事件数(最多5次)。然后,根据发生的事件的数量,它会为每个事件运行beta模拟,因此您可以获取当年发生的5个“事件”的值。然后将其放入一个矩阵中,这样我们就剩下了一个年数值矩阵与已发生事件数的值矩阵。
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0.011466314 0.05644778 0.09415982 0.24654834 0.31950041
[2,] 2 0.005988383 0.05334661 0.07467286 0.10501165 0.18135706
[3,] 3 0.030178740 0.04501533 0.07642787 0.11537804 0.12156121
[4,] 4 0.033208015 0.04391486 0.10769396 0.12492813 0.19938551
[5,] 5 0.026646013 0.03085844 0.06188760 0.10742163 0.16181556
[6,] 6 0.021271089 0.03010617 0.08581937 0.11578462 0.40768207
[7,] 7 0.032381818 0.06602770 0.08423590 0.10511102 0.15810456
[8,] 8 0.030124977 0.04863202 0.08484395 0.08790361 0.09787569
[9,] 9 0.043273119 0.05148791 0.06314097 0.10162677 0.10308642
[10,] 10 0.045976676 0.06381831 0.07352065 0.08667746 0.11843700
干杯!
解决方法
这完成了您在文本中讨论的所有内容,但是由于您没有提及而跳过了排序,因此我不确定您为什么这样做:
occ_simulation2 = function(n_year,lambda,alpha,beta,max_event){
beta_events = matrix(rbeta(n_year * max_event,shape1 = alpha,shape2 = beta),nrow = n_year)
n_events_per_year = rpois(n_year,lambda = lambda)
for(i in which(n_events_per_year < max_event)) {
beta_events[i,(n_events_per_year[i] + 1):max_event] = NA
}
cbind(1:n_year,beta_events)
}
occ_simulation2(n_year = 10,lambda = 10,alpha = 2,beta = 20,max_event = 5)
# [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] 1 0.035977205 0.131498127 0.06717396 0.07395549 0.16532084
# [2,] 2 0.092411677 0.010091762 0.14054564 0.07560796 0.17471096
# [3,] 3 0.033394007 0.017621993 0.06469264 0.10337253 0.19579706
# [4,] 4 0.165301623 0.131111006 0.03285909 0.38068480 0.05473743
# [5,] 5 0.007820079 0.196192197 0.25096961 0.06851775 0.35516715
# [6,] 6 0.028011863 0.003841574 0.16924708 0.12446178 0.06525773
# [7,] 7 0.133261625 0.059417090 0.09608348 0.07471339 0.08303839
# [8,] 8 0.076545497 0.110469131 0.23364757 0.09250536 0.03295593
# [9,] 9 0.051244148 0.070419370 0.07127251 0.11847306 0.04112807
# [10,] 10 0.104567386 0.188888704 0.02556781 0.10075848 0.02456839
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。