Tengku Hanis
Tengku Hanis

Reputation: 87

Simplify an equation by giving some variable a value in R

I want to simplify the equation below, and at the same time, varies the value of b

b*(b*((1-b)*x) + (1-b)*y) + (1-b)*z

So, if I give b = 0.9,

b <- 0.9
# the answer will be:
0.081x + 0.09y + 0.1z

The reason is I want to see how different values of b, will impact the weights/coefficients of x, y, and z.

I have no idea how to do this, or if it even possible in R.
Any help is appreciated.

Upvotes: 0

Views: 124

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 102211

I guess you may try Reduce like below

Reduce(function(u, v) b * u + v, (1 - b) * c(x, y, z))

and you will see

> b <- 0.9

> x <- 1e3

> y <- 1e2

> z <- 1e1

> Reduce(function(u, v) b * u + v, (1 - b) * c(x, y, z))
[1] 91

If you want to see the coefficients of x, y and z, you can use

> f <- function(b) (1 - b) * b^((3:1) - 1)

> f(0.9)
[1] 0.081 0.090 0.100

and the sum of weighted x, y, and z can be written as

s = sum(f(0.9)*c(x,y,z))

Upvotes: 1

Related Questions