0

I have one variable A

0
10
15
20
25
30
35
40
45
50
55
60
65
70
75
80
85
90

which is an input into the following function

 NoBeta <- function(A)
       {
          return(1-(1- B * (1-4000))/EXP(0.007*A) 
  }

The variable B is the result of this function how do I feed the result back into the function to calculate my next result? Here is B

0
0.07
0.10
0.13
0.16
0.19
0.22
0.24
0.27
0.30
0.32
0.34
0.37
0.39
0.41
0.43
0.45
0.47

So the function needs to return the values of B but also using B e.g. if we using A 10 as input then the input for B is 0, when the input for A is 15 the input for B is the result from the previous calculation 0.07

B is calculated with the following formula in Excel =1-(1-B1*(1-4000))/EXP(0.007*$A2) How do I implement this formula in R?

Makoto
  • 111
  • 5
  • 1
    This is unclear. What is it that you are actually trying to do? If your formula is iterative, you can just use a loop and return the final value of the calculation at the end of the function. – jonsca Nov 17 '14 at 11:17
  • 2
    Please add a [reproducible example](http://stackoverflow.com/a/5963610/1412059) to your question, i.e., provide some input data and show the corresponding expected output. – Roland Nov 17 '14 at 11:52
  • I imagine you need to use an external data structure (vector?) to store starting values and iteration results. If this needs to be a single function, then you'd define the estimator function (one that does the iteration) within the storage function (one that handles the data). – Maxim.K Nov 17 '14 at 11:58
  • 2
    Your example function doesn't use its argument (`x`) and it's not clear where `A8` comes from, but the idea of a recursive function which initialises a zero'th call is illustrated by this calculation of a factorial. `NoBeta <- function(x) if( x ==0) 1 else x*NoBeta(x-1)` – user20637 Nov 17 '14 at 12:06

1 Answers1

1

If I understand your question correctly you wish to reference a previous row in a calculation for the current row.

You can adapt a function that was provided in another SO question here.

rowShift <- function(x, shiftLen = 1L) {
  r <- (1L + shiftLen):(length(x) + shiftLen)
  r[r<1] <- NA
  return(x[r])
}

test <- data.frame(x = c(1:10), y = c(2:11))

test$z <- rowShift(test$x, -1) + rowShift(test$y, -1)
> test
    x  y  z
1   1  2 NA
2   2  3  3
3   3  4  5
4   4  5  7
5   5  6  9
6   6  7 11
7   7  8 13
8   8  9 15
9   9 10 17
10 10 11 19

Then what you want to achieve becomes

test$z2 <- 1- (1-rowShift(test$x, -1)*(1-4000))/exp(0.007*rowShift(test$y, -1))
> head(test)
  x y  z         z2
1 1 2 NA         NA
2 2 3  3  -3943.390
3 3 4  5  -7831.772
4 4 5  7 -11665.716
5 5 6  9 -15445.790
6 6 7 11 -19172.560
Community
  • 1
  • 1
r.bot
  • 5,309
  • 1
  • 34
  • 45
  • Thanks for this answer however there is only one vector/variable A that I have data for B needs to be calculated based on A current observation and B previous observation. To initialise B will start with 0 but then the result need to feedback into the function. – Makoto Nov 17 '14 at 19:31
  • Sorry guys the lag variable will not work as it's the result basically I'm trying to implement the following Excel formula in R =1-(1-B1*(1-4000))/EXP(0.007*$A2) – Makoto Nov 18 '14 at 11:18