简体   繁体   中英

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM