-4

Now I want to obtain the values of slop coefficients of A and B in the below equation;

By = Ax + C, where I have 100 sets of x, y and C.

How can I solve this and get A & B by using R?


I am sorry for the incomplete explanation. I added more detail as below;

Now I have three values of y, x and C as follows:

 y <- c(-9216.656,-9134.369,-9186.813,-8780.633,-9006.787)                 
 x <- c(908.4656,932.6687,1015.0424,816.9116,703.2510)                       
 C <- c(-8.965961,-8.883207,-8.935910,-8.546677,-8.766842)

What I know is the y, x and C can be explained as below equation;

By = Ax + C

In this case, can I decide the slope values of A and B, by using the known parameters of y, x and C?

For example, the equation can be written as below;

-9216.656*B = 908.4656*A -8.965961 (using y[1], x[1], C[1])
-9134.369*B = 932.6687*A -8.883207 (using y[2], x[2], C[2])

I would like to get the value of A and B that meet the equations above. If I understand correctly, each combination of two sets of y, x and C can produce sets of A and B.

Here, one set of y,x & C would produce one equation. I want to solve all combinations of the two equations. In this case, for the total number of combinations should be

choose(5, 2) = 10.

Then, if I understand correctly, I would get 10 sets of A and B.

My final goal is to calculate the average & sd of each A and B.

imtaiky
  • 191
  • 1
  • 12
  • Can you please explain the problem a little more? – Tony Ladson Jun 19 '19 at 11:41
  • Not sure what you're asking. Is this an optimisation problem where you're trying to find the best `A` and `B` given the values for `x`, `y` and `C`? Or is this an algebraic problem where you're trying to solve multiple linear equations? You need to provide more details! – Maurits Evers Jun 19 '19 at 13:31
  • I am sorry for the incomplete explanation. I added more details, and I hope this works. – imtaiky Jun 19 '19 at 15:20
  • This is still not clear; are you trying to *fit* a model `y = A / B x + C / B` to your data? Why is `C` not a parameter? Why are `x` and `y` parameters (`x` and `y` are usually reserved for variables/covariates)? Or do you expect to solve 5 different equations to obtains five sets of `A` and `B` (in which you can't because you have a single equation but two unknowns)? Questions, questions, questions... You should include an explicit example where you work through what you're trying to do for one set of `x`, `y`, `C` values. Perhaps then things will become clearer. – Maurits Evers Jun 19 '19 at 22:13
  • I really apologize for my poor explanation. I added an example, and I hope this may help to work. – imtaiky Jun 20 '19 at 18:31
  • *"If I understand correctly, each combination of two sets of y, x and C can produce sets of A and B"* That is correct (two equations allow you to determine two unknowns). Your question however is still not clear (we seem to be getting closer though). *Which* combinations of two sets from `x`, `y`, `C` do you want to use? Does it matter? Do you need all combinations? What's the goal here? – Maurits Evers Jun 21 '19 at 09:13
  • I really appreciate your help. I added some explanations. – imtaiky Jun 24 '19 at 11:02
  • @imtaiky I'm curious: Does my answer below answer your question? – Maurits Evers Jun 28 '19 at 13:57
  • (I've moved the following good advice from Maurits' answer - it is better as a comment). First off and most importantly, for future questions it is important that you improve on the quality of your post. Please don't be discouraged by the down-votes, instead you should familiarise yourself with SO posting guidelines. (1/2) – halfer Jun 29 '19 at 20:38
  • For example, you should take a look at what you can (and should) do in order to provide a [minimal reproducible example/attempt](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). That way, you'll avoid a lot of frustration, both on your end (for us seemingly not understanding what it is you're trying to do), and on our end (for leaving us guessing what it is you're trying to do). (2/2) – halfer Jun 29 '19 at 20:38
  • 1
    I learned a lot from this post about how to ask queries through the system. I am really ashamed for myself bothering you all by frustration. I really appreciate your kind help, and I will be much better questioner in the future. Thank you very muchm – imtaiky Jul 01 '19 at 09:11

1 Answers1

1

Here's what you can do.

  1. It's illustrative to work through an explicit example based on the first two entries of x, y, and C (as per your edit). We can solve the system of two linear equations -A x + B y = C for A and B using base R's function solve

    idx <- c(1, 2)
    mat <- matrix(c(x[idx], y[idx]), ncol = 2)
    sol <- solve(mat, C[idx])
    sol
    #[1] 8.371691e-05 9.810516e-04
    

    We verify that indeed sol is a solution by multiplying the matrix of x and y values with the coefficients A and B

    mat %*% sol
    #          [,1]
    #[1,] -8.965961
    #[2,] -8.883207
    

    As expected, these are just the first two values of C, so all is good:-)

  2. In order to solve the linear equation system for all combinations of 2 entries from x, y and C we can use combn to obtain all combinations and then use the same method as above. The resulting object res is a matrix with coefficients for A and B in two rows

    res <- apply(combn(length(x), 2), 2, function(idx) {
        setNames(solve(matrix(c(x[idx], y[idx]), ncol = 2), C[idx]), c("A", "B"))
    })
    #          [,1]         [,2]         [,3]         [,4]          [,5]
    #A 8.371691e-05 9.311096e-06 0.0001004419 2.732112e-05 -2.208575e-05
    #B 9.810516e-04 9.737176e-04 0.0009827001 9.754928e-04  9.702486e-04
    #          [,6]         [,7]         [,8]         [,9]        [,10]
    #A 9.391842e-05 3.562515e-05 3.819669e-05 2.069634e-05 2.729837e-07
    #B 9.820932e-04 9.761412e-04 9.769091e-04 9.749755e-04 9.733808e-04
    
  3. It's now easy to calculate the mean/sd of the coefficient values for A and B; for example, to calculate the mean we can use rowMeans

    rowMeans(res)
    #           A            B
    #3.874148e-05 9.766710e-04
    

    For the standard deviation

    apply(res, 1, sd)
    #           A            B
    #4.134705e-05 4.087056e-06
    
halfer
  • 19,824
  • 17
  • 99
  • 186
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68