Home > database >  Is there a loop or a function in R to reverse calculate marginal taxes?
Is there a loop or a function in R to reverse calculate marginal taxes?

Time:12-14

Need to solve this interdependent variables to find b and c

a <- 20000
b <- 10% * c
c <- a   b

Cant figure out what to do, stuck on this since a week.

CodePudding user response:

Algebra is the only thing needed here. Substitute the right-hand side of the third equation in place of c in the second equation and solve for b (b <- a/9).

If you really want to use R to help solve it, pass the system of equations to solve.

solve(
  rbind(
    c(a = 1, b = 0, c = 0), # coefficients of the first equation
    c(0, -1, 0.1),          # coefficients of the second equation
    c(1, 1, -1)             # coefficients of the third equation
  ),
  c(2e4, 0, 0)              # RHS of the three equations
)
#>         a         b         c 
#> 20000.000  2222.222 22222.222

CodePudding user response:

Here is a way.
The general principle is the same as in jblood94's answer, to solve a system of linear equations. I have set it up differently, it only solves for b and c, and wrote it as a function.

funSolve <- function(a, rate) {
  lhs <- cbind(b = c(1, -1),     # column vector for `b`
               c = c(-rate, 1))  # column vector for `c`
  rhs <- c(0, a)                 # independent term
  solve(lhs, rhs)
}
funSolve(20000, 0.1)
#>         b         c 
#>  2222.222 22222.222

Created on 2022-12-13 with reprex v2.0.2

  •  Tags:  
  • r
  • Related