将变量存储在带有串联列的for循环中

时间:2018-07-26 07:05:00

标签: r

我正在做一个rpois仿真,我想创建一个函数来自动执行变量更改(lambda)。

我的函数应该能够更改lambda值。例如,在这里我要更改三个变量n1(175),n2(11)和n3(14) 像我的示例一样,毒物的数量取决于连接输入数据帧的行数。

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)

1 个答案:

答案 0 :(得分:1)

尝试一下:

yourfunction<-function(x=c(),seed=12358){
    set.seed(seed)
    require(tidyverse)
    require(ggrepel)
    require(broom)
    listdata<-list()
    for (i in 1:length(x)) {
        listdata[[i]]<- assign(paste("pois_",i),tidy(summary(as.factor(rpois(n = 1000000, lambda = (1/336981)*x[i]*11*14)))/1000000)[1:5,]) }

    df_info<-cbind.data.frame(occurence=as.character(rep(0:(5-1)), length(x)),fl=as.character(rep(x,each=5)) ,proba=dplyr::bind_rows(listdata)[,2])
    names(df_info)<-c("occurence" , "fl"    ,"proba")
    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"))
}
yourfunction(x=c(175,500,700,900))