How to recursive values within "ifelse" function

Sorry for posting similar questions again and again. However I would like to step forward, although my skills and knowledge in R are quite limited.

There are three parameters: a, b, and p. Their initial values are:

a <- 0
b <- 0

p <- function(a, b){
  p <- (1/(1+exp(-(a - b))))
  return(p)
}
> p(a, b)
[1] 0.5

Also, I have a data frame.

myframe <- data.frame(itm.1=c(1, 1, 1),
                      itm.2=c(0, 1, 1),
                      itm.3=c(0, 0, 0))
> myframe
  itm.1 itm.2 itm.3
1     1     0     0
2     1     1     0
3     1     1     0

The conditions are;

  • If the response is 1 (correct), the parameters a and b are updated to a^ and b^ following the formula.

a^ = a + 0.4*(1-p)
b^ = b - 0.4*(1-p)

  • If the response is 0 (wrong), the parameters a and b are updated to a^ and b^ following the formula.

a^ = a + 0.4*(0-p)
b^ = b - 0.4*(0-p)

  • The parameter p is also updated with updated a^ and b^ and recursively assigned to the formulas above to calculate a^ and b^.

To achieve this, I wrote a set of codes, but the parameters are simply updated and summed by each element, not using the previous data: a^, b^, and p^

These are codes I wrote:

a <- 0
b <- 0

p <- function(a, b){
  p <- (1/(1+exp(-(a - b))))
  return(p)
}

myframe <- data.frame(itm.1=c(1, 1, 1),
                      itm.2=c(0, 1, 1),
                      itm.3=c(0, 0, 0))

condition_a <- function(res){ifelse(res == 1, 0.4*(1-p(a, b)), 0.4*(0-p(a, b)))}
condition_b <- function(res){ifelse(res == 1, 0.4*(1-p(a, b)), 0.4*(1-p(a, b)))}

myframe.a <- mutate_all(myframe, ~condition_a(.))
myframe.a <- mutate_all(myframe.a, cumsum)
names(myframe.a) <- paste0("a_", names(myframe.a))

myframe.b <- mutate_all(myframe, ~condition_b(.))
myframe.b <- mutate_all(myframe.b, cumsum)
names(myframe.b) <- paste0("b_", names(myframe.b))

bind_cols(myframe, myframe.a, myframe.b)

To express what I would like to achieve, I also attached a jpg file of the excel with functions. I hope the file help you understand my concerns. Please save the file to enlarge.

I appreciate your help and advice. Thank you in advance.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.