R function that uses its output as its own input repeatedly

后端 未结 3 2083
醉梦人生
醉梦人生 2020-12-11 12:59

Given R function auto (below), I was wondering if it might be possible that from the second run of the for loop output of ps be used a

3条回答
  •  无人及你
    2020-12-11 13:29

    Let me try to give an answer.

    So, firstly, I thought, that you need finite state automata function. This approach needs to use environment usage in which you need to store state and values for your functions call. But, you need only make choice based on your cycle index value. It's much easier, because you need only switch operator and nothing more.

    auto <- function(n, dat, n.loop){
    x <- 0.1;
    for(i in 1:n.loop){
    val <- switch(i, dbeta(x, 1, 1), dbinom(dat, n, x), dbeta(x, 1, 1)*dbinom(dat, n, x))
    print(val);
      }
    }
    
    auto(n = 100, dat = 55, n.loop = 3)
    

    I have 2 questions which should reorganize code well. 1) What is an x value which you use in auto? This is not an argument in your code, so, you should determine value of x in auto directly. Maybe is this an argument? 2) What value does your function need to return as a value?

    So, x <- 0.1 and print(val) were added for checking code execution.

提交回复
热议问题