0

I thought this would be simple but I'm having real difficulty. I could use excel or R, whatever gets it done. I just need to know when these curves pass a certain value.

I tried adding in an extra line to my charts to see if there was an intersect function but no.

Years    Y
10      0
20      0
50      5.54
100     21.81
200     46.24
500     71.96
1000    84.74
1500    89.63
2000    91.94

I need to know where this curve crosses 90

Years   Y
10      1
20      0.96
50      0.28
100     0
200     0
500     0
1000    0
1500    0
2000    0

I need to know where this curve crosses 0.01

Edit in case it helps (not from OP):

SO19006597 question example

Hack-R
  • 22,422
  • 14
  • 75
  • 131
Colin
  • 161
  • 1
  • 1
  • 7
  • My (perhaps low tech) way would be to fit a curve to the data (R, and probably excel, has functions for this). Then work out the x value of the curve when y is 0.01 or 90 from its equation. Fitting a curve to the second data set however would probably benefit form more data in the 10-100 region if possible. – James Elderfield Sep 25 '13 at 13:51
  • 1
    That's not a curve; it's a collection of points. Have you fit a curve that you can share? – Peyton Sep 25 '13 at 13:52
  • I just put that into excel and plotted it as a scatter graph – Colin Sep 25 '13 at 14:11
  • If you fit models to each, you can use the answer here: http://stackoverflow.com/a/7114961/2338862. If you want the intersection of the two lines assuming linear interpolation between adjacent points, you can still use solve but you just have to figure out your slopes and interceptions from the data (rather than model coefficients). – Thomas Sep 25 '13 at 14:18
  • 1
    You should probably know exactly what kind of model you are using to fit a curve to your data, particularly if this is part of your thesis. There are many ways to do this, and you seem to be just going with excel's default without knowing what it is. The exact point where it crosses the thresholds will depend on the method of fit, so you are looking for a precise answer to an imprecisely specified question. – mrip Sep 25 '13 at 14:36

3 Answers3

2

If it is appropriate to set up the inverse model $x = f (y)$, e.g. Years ~ Y, you can build and evaluate that. (Think about the consequences of inverse modeling, though.)

  • For linear approximation, have a look at ?approx.
  • If you need a particular type of model, then maybe you can set up the inverse model to the dependency you request, then ?predict is your friend.

Here's an example of the differences between classical and inverse modeling of your 1st data set (with loess):

classical:
classical model

inverse:
inverse model

As the inverse model assumes error on Years to be >> error on Y, the Y = 90 line crosses the 95% confidence interval of the model fit at ca. Years = 1375 and 1900 for the inverse model. For the classical model, the error on Y is assumed to dominate. In that case, all years above ca. 800 are inside the 95% confidence interval of the fit, and the confidence interval almost touches the Y = 90 line already around Years = 350 for the first time.
Note that also the point estimates vary: Y = 90 crosses the classical fit at ca. 1435, whereas the inverse fit is crossed at 1635.

(Of course you may want to go for a more restrictive model)

cbeleites unhappy with SX
  • 13,717
  • 5
  • 45
  • 57
  • 1
    @pnuts: the OP says for the 1st data set: "I need to know where this curve crosses 90" - which by observation is expected somewhere between Years = 1500 and Years = 2000, probably closer to 1500 than to 2000, **not** betwetween 20 and 50. – cbeleites unhappy with SX Sep 25 '13 at 15:30
1

The first thing you need to do is decide how to fit a curve to your data. There are a lot of ways to do this, and obviously the answer will depend on your choice. If by "curve" you simply mean linear interpolation (i.e. "connect-the-dots") then a function like this will find the first time a threshold is crossed:

findCross<-function(years, y, value){
  over <- y > value
  w <- which(over != over[1])[1]
  frac <- (value - y[w-1]) / (y[w] - y[w-1])
  years[w-1] + (years[w] - years[w-1]) * frac
}
mrip
  • 14,913
  • 4
  • 40
  • 58
1

I would fit the data using either a linear, polynomial, or nonlinear fit as best suits your data and purpose. Then use the fit function you've created and run

uniroot(fit_fun-90,upperbound,lowerbound)

Edit- per Thomas' comment: The results of any of R 's fitting tools such as lm or nls include the coefficients of all terms in the fitting function. E.g., for a simple linear fit, the slope and intercept are provided. With this information, create a new fit_fun<-function(x) {intercept + slope*x} .

The reason to subtract 90 is that uniroot looks for a "root," i.e. zero value, so I basically applied an offset to the function. See ?uniroot for full details.

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • 1
    This would probably benefit from a bit more explanation. For example, what does `fit_fun` look like? Why -90? – Thomas Sep 25 '13 at 14:27