How to vary parameters of an equation?

Asked

Viewed 168 times

3

For an equation of the type y = ax + b, we have two values for a and two to b, That is, we have four different equations. We already have a code that returns us the values of x and y for each equation separately. We want to know if there is a way to realize a loop return the respective values of each x and y when we vary the values of "a" and "b". That is, how to find y1, y2, y3, y4, x1, x2, x3 e x4(for each combination of a and b) without having to run the code four times.

Obs: the program used is R

  • Mariana, put an example of what you’ve done. You want to find so much y how much x or just find y for a given x? Because the way you put it you have two variables and only one equation, so are endless solutions of x and y.

  • 1

    Hello, Carlos, actually the equation represents an econometric regression and we have a formula to find both the values of x and the values of y, which is too big to be put here. If we assume a fixed x, with varying parameters, how would we find y without having to run the code four times?

  • If it is not possible to place the original code, put an "illustrative" code that exemplifies the problem, because the question is ambiguous as it is. But I think I understand what you want, I put an answer below.

1 answer

2


Suppose you have two parameters a, two parameters b and a value of x:

a<- c(1,2)
b <-c(3,4)
x <- 10

You want to find all the y possible with the four combinations of the parameters, given the value of x. So the first thing to do is to generate these combinations:

parametros <- merge(a, b, by=NULL)
names(parametros) <-c("a", "b")
parametros
  a b
1 1 3
2 2 3
3 1 4
4 2 4

Now we define the function that generates the value of y and we can use mapply to find all the values of y combinations of parameters:

y<-mapply(function(x,a,b) y=a*x +b, x=x, a=parametros[1,], b=parametros[,2])
y
[1] 13 33 14 34

Browser other questions tagged

You are not signed in. Login or sign up in order to post.