我正在做一个rpois模拟,我想创建一个自动变量更改(lambda)的函数。
我的函数应该能够更改lambda值。例如,这里我想改变三个变量
n1
(175)、,
n2
(11)和
n3
(十四)
以及毒药随机数,取决于连接输入数据帧的行数,如我的示例所示。
library(tidyverse)
library(ggrepel)
library(broom)
set.seed(12358)
pois_1 <- tidy(summary(as.factor(rpois(n = 1000000, lambda = (1/336981)*175*11*14)))/1000000)
pois_2 <- tidy(summary(as.factor(rpois(n = 1000000, lambda = (1/336981)*500*11*14)))/1000000)
pois_3 <- tidy(summary(as.factor(rpois(n = 1000000, lambda = (1/336981)*900*11*14)))/1000000)
df_info <- data.frame(pois_1[1:5, ], pois_2[1:5, 2], pois_3[1:5, 2])
names(df_info) <- c("occurence", "175",
"500", "900")
df_info %>%
gather(fl, proba, "175":"900") -> df_info
ggplot(data = df_info, aes(x = fl,
y = proba,
group = occurence)) +
geom_point(size = 2) +
geom_label_repel(aes(label = ifelse(proba > 0.02, as.character(round(proba, 2)), "")),
box.padding = 0.35,
point.padding = 0.5,
segment.color = 'grey50') +
geom_line(aes(linetype = occurence, color = occurence), size = 1) +
theme_bw() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
plot.title = element_text(hjust = 0.5, face = "bold"),
axis.title.x = element_text(hjust = 0.5, face = "bold"),
axis.title.y = element_text(hjust = 0.5, face = "bold"))
在这里,我想用for循环创建一个这样的函数,但看起来很复杂:
编辑:只使用一个向量,但我想这样做
n1> 1
vizFun <- function(n1, n2, n3){
df_info <- cbind(n1, n2, n3)
names(df_info) <- c("n1", "n2", "n3")
if (nrow(df_info) == 1){
for (i in seq_along(nrow(df_info))){
lambda <- (1/336981)*df_info[i,"n1"]*df_info[i, "n2"]*df_info[i, "n3"]
pois <- tidy(summary(as.factor(rpois(n = 1000000, lambda = lambda)))/1000000)
df_info <- data.frame(pois[, ])
names(df_info) <- c("occurence", "175")
df_info %>%
gather(fl, proba, "175") -> df_info
}
ggplot(data = df_info, aes(x = fl,
y = proba,
group = occurence)) +
geom_point(size = 2) +
geom_label_repel(aes(label = ifelse(proba > 0.02, as.character(round(proba, 2)), "")),
box.padding = 0.35,
point.padding = 0.5,
segment.color = 'grey50') +
geom_line(aes(linetype = occurence, color = occurence), size = 1) +
theme_bw() +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1),
plot.title = element_text(hjust = 0.5, face = "bold"),
axis.title.x = element_text(hjust = 0.5, face = "bold"),
axis.title.y = element_text(hjust = 0.5, face = "bold"))
}
}
vizFun(500, 11, 14)