Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performing constant interpolation in R

I have a set of data, for example:

x<-c(1, 2, 3, 4, 5, 6)
y<-c(100, 110, 121, 133.1, NA, 161.051)

Now, y is clearly increasing at a constant rate of 10%.

I want to be able to interpolate the data at x=5, and I want to print 146.41 as the answer. However, this function doesn't seem to do it:

approx(x,y,5)

This prints 147.0755, which is not the answer i'm looking for.

approx(x,y,5,method="constant")

doesn't do the trick either.

Where am I going wrong?

like image 715
Randomly Named User Avatar asked Feb 14 '23 02:02

Randomly Named User


2 Answers

Since you are dealing with rates, you need to translate y into log(y), interpolate, then use exp to get the result back into a linear scale:

exp(approx(x, log(y), x)$y)
# [1] 100.000 110.000 121.000 133.100 146.410 161.051
like image 111
flodel Avatar answered Feb 16 '23 17:02

flodel


short answer is you can't without giving it some more info, because it's either a linear fit, which doesn't deal with a non-linear function, or a constant fit, which also doesn't.

You can force it by telling it that the right hand point is 1.1x the emphasis of the left hand point:

approx(x,y,5, method="constant", f=1/2.1) # 1/ left weight (1) + right weight (1.1)

>$x
>[1] 5

>$y
>[1] 146.41

but that's probably not all that useful!

probably the closest you can get is:

spline(x,y,xout=5)

>$x
>[1] 5
>$y
>[1] 146.4125
like image 38
Troy Avatar answered Feb 16 '23 15:02

Troy