链接两个函数

时间:2015-08-14 11:25:23

标签: r function user-defined-functions

我必须承认我是编码功能的新手,因此我需要你的帮助。

该代码应在ANOVA之后提供贝叶斯标准(pBIC),并自动从ANOVA表中读取必要的信息。

我有两个功能

## This is function 1

test_pBIC1 <- function(name,c){  ## name is the name of the ANOVA table, e.g. "ANOVA_ALL_wake" and c is the number of conditions
  c = c
  data = get(name)
  i = length(data$ANOVA$Effect)
  result1 = data.frame(name,c,i)
  return(result1)
}
## ----------------------------------------------------
## I now run and save the result of Function 1

result1 <- test_pBIC1("ANOVA_ALL_wake",3) ## for test

## ----------------------------------------------------
## This is function 2

test_pBIC2 <- function(result1){ 
  name1 <- as.character(result1$name)
  data = get(name1)
  count <- as.vector(result1$i)
  for (i in 1:count){
    s = (data$ANOVA$DFd[i]/data$ANOVA$DFn[i])+1
    n = s*(result1[2]-1)
    SSE1 = data$ANOVA$SSd[i]
    SSE0 = data$ANOVA$SSd[i]+data$ANOVA$SSn[i]
    deltaBIC = (n * log(SSE1/SSE0))+(data$ANOVA$DFn[i]*log(n))
    BF01 = exp(deltaBIC/2)
    pH0_D = (BF01/(1+BF01))
    pH1_D = (1-pH0_D)
    result = data.frame(pH0_D, pH1_D)
    colnames(result) <- c("pH0_D", "pH1_D")
    rownames(result) <- c(data$ANOVA$Effect[i]) 
    if (i == 1){
      result_all <- result
    } else {
      result_all <- rbind (result_all, result)
    }
  }
  return(result_all)
}
## ------------------------------------------------------
Now I run function 2 and receive the result

test_pBIC2(result1)

现在虽然这是它的工作,我想链接这两个函数,所以我只需要给出名称和参数c,最后仍然得到result_all,即无需运行这两个函数在彼此之后。

我试图提出这个解决方案:

test_pBIC <- function(name,c){   ## pass arguments as: test_pBIC(name = "ANOVA_all_wake", c = 3)
  c = c
  name = name
  result1 = data.frame(name,c)
  # return(result1)

  test_pBIC1 <- function(result1){
    c = as.vector(result1$c)
    name1 <- as.character(result1$name)
    data = get(name)
    i = length(data$ANOVA$Effect)
    result2 = data.frame(name,c,i)
  # return(result2)

    test_pBIC2 <- function(result2){ 
      name1 <- as.character(result2$name)
      data = get(name1)
      count <- as.numeric(integer$i)

      for (i in 1:count){
        s = (data$ANOVA$DFd[i]/data$ANOVA$DFn[i])+1
        n = s*(result1[2]-1)
        SSE1 = data$ANOVA$SSd[i]
        SSE0 = data$ANOVA$SSd[i]+data$ANOVA$SSn[i]
        deltaBIC = (n * log(SSE1/SSE0))+(data$ANOVA$DFn[i]*log(n))
        BF01 = exp(deltaBIC/2)
        pH0_D = (BF01/(1+BF01))
        pH1_D = (1-pH0_D)
        result = data.frame(pH0_D, pH1_D)
        colnames(result) <- c("pH0_D", "pH1_D")
        rownames(result) <- c(data$ANOVA$Effect[i]) 
        if (i == 1){
          result_all <- result
        } else {
          result_all <- rbind (result_all, result)
        }
      }
      return(result_all)
    }
  }
}


test_pBIC("ANOVA_all_wake", 3)

然而,我只是得到了......我找不到错误:(。

谢谢!

1 个答案:

答案 0 :(得分:1)

不完全确定问题是什么,可重复的例子会有很大帮助。如果你想把它组合成一个你可以做的功能......

test_overall <- function(name,c) {
  c = c
  data = get(name)
  i = length(data$ANOVA$Effect)
  result1 = data.frame(name,c,i)
  name1 <- as.character(result1$name)
  data = get(name1)
  count <- as.vector(result1$i)
  for (i in 1:count){
    s = (data$ANOVA$DFd[i]/data$ANOVA$DFn[i])+1
    n = s*(result1[2]-1)
    SSE1 = data$ANOVA$SSd[i]
    SSE0 = data$ANOVA$SSd[i]+data$ANOVA$SSn[i]
    deltaBIC = (n * log(SSE1/SSE0))+(data$ANOVA$DFn[i]*log(n))
    BF01 = exp(deltaBIC/2)
    pH0_D = (BF01/(1+BF01))
    pH1_D = (1-pH0_D)
    result = data.frame(pH0_D, pH1_D)
    colnames(result) <- c("pH0_D", "pH1_D")
    rownames(result) <- c(data$ANOVA$Effect[i]) 
    if (i == 1){
      result_all <- result
    } else {
      result_all <- rbind (result_all, result)
    }
  }
  return(result_all)
}