-1

I have this equation:

y = -0.00248793*x^2+20.77173764*x-371.01805798

And I would like to obtain the result of the equation when I give "y" numbers,

edited explanation 2/06/20: I want to add a vector as my "y", and receive an output of one vector also.

This problem is a biological one, in which I performed citokine bead array (CBA) and I stablished a reference curve which is sinusoidal.

after stablishing the degree of the equation making the following:

fitil6_1=lm(Standards$`IL6 median`~poly(concentration,1,raw=TRUE))
fitil6_2=lm(Standards$`IL6 median`~poly(concentration,2,raw=TRUE))
fitil6_3=lm(Standards$`IL6 median`~poly(concentration,3,raw=TRUE))  
fitil6_4=lm(Standards$`IL6 median`~poly(concentration,4,raw=TRUE))



lines(concentration,predict(fitil6_1,data.frame(x=concentration)),col="red")
lines(concentration,predict(fitil6_2,data.frame(x=concentration)),col="green") 
lines(concentration,predict(fitil6_3,data.frame(x=concentration)),col="blue")
lines(concentration,predict(fitil6_4,data.frame(x=concentration)),col="purple)
legend(20,40000,legend=c("de grau 1","de grau 2","de grau 3","de grau 4"),lty=1,col=c("red","green","blue","purple"))

I have chosen the degree 2 formula as it fits better to my dots for this cytokine (and most cytokines in this study)

So when I make

coef(fitil6_2)

 (Intercept)        poly(concentration, 2, raw = TRUE)1  poly(concentration, 2, raw = TRUE)2
-8.262381e+02                              2.371377e+01                       -2.847135e-03 

I receive that output and then I am able to build the formula (in this case):

y=-2.847135e-03 *x^2+2.371377e+01*x-8.262381e+02

but as my independent value is what I know is pretty difficult to isolate x! (end of the editing)

I have tried many things like making function(x,y) but when you specify this you need to give a number of y, so really I am litlle bit lost!

Thank you

Dave2e
  • 22,192
  • 18
  • 42
  • 50
Baldrion
  • 1
  • 1

2 Answers2

2

As @Dave2e said, you can solve this particular example by algebra. But you might need a programmatic solution, or you might be using the quadratic as an easy example. in which case...

Rewrite your problem as "what value of y satisfies -0.00248793*x^2+20.77173764*x-371.01805798 - y = 0?".

There are plenty of ways to find the zeroes of a function. That's what you've turned your problem into. Suppose your "known value of y" is 10...

f <- function(x, y) {
  -0.00248793*x^2+20.77173764*x-371.01805798 - y
}

answer <- stats::uniroot(f, interval=c(0, 50), y=10)

# Check we've got the right answer
f(answer$root, 10)

Giving

[1] -1.186322e-10

Using this method, you do need to find/guess a range within which the answer might lie. That's the purpose of the interval=c(0.50) part of the call to uniroot. You can read the online help for more information about the value returned by uniroot and things you might want to look out for.

Limey
  • 10,234
  • 2
  • 12
  • 32
  • Thank you yes as @Dave2e I have tried also in this way but I had problems also, well thank you I think this will work for me! – Baldrion Jun 02 '20 at 07:28
  • Excuse me again I don't know if it's is working properly, when I introduce a vector a warning message popped up: Error in stats::uniroot(f, interval = c(0, 50), y = citoquines_valero$`IL10 median`) : f() values at end points not of opposite sign In addition: Warning messages: 1: In if (is.na(f.lower)) stop("f.lower = f(lower) is NA") : the condition has length > 1 and only the first element will be used 2: In if (is.na(f.upper)) stop("f.upper = f(upper) is NA") : the condition has length > 1 and only the first element will be used – Baldrion Jun 02 '20 at 08:04
  • You really need to make a simple, self-contained example including input data, sample code and expected output. [This post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) may be helpful. Nonetheless, I see at least two potential issues: (1) for at least one element of `citoquines_valero$IL10 median`, `c(0, 50)` does not contain the zero. The solution to this problem is given in the online help. Have you read it? (2) `uniroot` is not vectorised, which is why you're getting the "length > 1" warning. – Limey Jun 02 '20 at 08:14
  • I think I need to read it more calmly because my first impression is that I don't understand, but well I will keep trying and if not I will make a better post. I will read that also thank you. Thank you really for your help!!!!!!!!!!! – Baldrion Jun 02 '20 at 08:25
0

Thank you for all who answered I have just started in this page, this worked for me:

isolating "y" and then making a function with the quadratic formula to x:

   delta<-function(y,a,b,c)
   {k=(-b+sqrt(b^2-4*a*(c-y)))/(2*a)
   print(k)
   }

delta(citoquines_valero$`IFNg median`,-1.957128e-03,1.665741e+01,-7.522327e+02)

#I will use that one as a provisional solution.
#I have also been told to use this, but is not working properly:

result <- function(y,a,b,c){
  # Constructing delta
  delta<-function(y,a,b,c){
    b^2-4*a*(c-y)
  }
  if(delta(a,b,d) > 0){ # first case D>0
    x_1 = (-b+sqrt(delta(y,a,b,c)))/(2*a)
    x_2 = (-b-sqrt(delta(y,a,b,c)))/(2*a)
    if (x_1 >= 0) {
      print(x_1)
      else if (x_2 >= 0){
        print(x_2)
      }
    }
    print(result)
    else if(delta(a,b,d) == 0){ # second case D=0
      x = -b/(2*a); return(x)
    }
    else {"There are no real roots."}; # third case D<0```
    return("There are no real roots.")
  }
}
Baldrion
  • 1
  • 1