Open In App

Interpolation Functions in R

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will be looking towards the approx() and the aproxfun() interpolation function with working examples in the R Programming language.

Approx() and Approxfun() interpolation function

These functions return a list of points that linearly interpolates given data points, or a function performing the linear (or constant) interpolation.

Syntax: 

  • approx   (x, y = NULL, xout, method = “linear”, n = 50,
              yleft, yright, rule = 1, f = 0, ties = mean)
  • approxfun(x, y = NULL,       method = “linear”,
              yleft, yright, rule = 1, f = 0, ties = mean)

Parameters:

  • x, y :numeric vectors giving the coordinates of the points to be interpolated.
  • xout: an optional set of numeric values specifying where interpolation is to take place.
  • method:specifies the interpolation method to be used. Choices are “linear” or “constant”.
  • n:If xout is not specified, interpolation takes place at n equally spaced points spanning the interval [min(x), max(x)].
  • yleft: the value to be returned when input x values are less than min(x). 
  • yright: the value to be returned when input x values are greater than max(x).
  • rule:an integer (of length 1 or 2) describing how interpolation is to take place outside the interval [min(x), max(x)].
  • ties: handling of tied x values.

Method 1: Apply approx Function to Two Coordinates

In this method, we will be applying the approx function to two coordinates using the approx function passed with the given vector as its parameters to get a list of points which interpolate given two different data points.

Example:

In this example, we have used two vectors, first from 0 to 10 and another from 0 to 20, and further with the call of the approx function and passing the created vector to the function we get the list of points that linearly interpolate n and further form better visualization we have to create the plot the points in the R programming language.

R




# Create vector
x <- c(0, 10)    
y <- c(0, 20)  
  
# Apply approx function
data_approx1 <- approx(x, y)       
data_approx1    
  
# Draw output of approx function
plot(data_approx1$x,                 
     data_approx1$y)
points(x, y,
       col = "red",
       pch = 16)


Output:

$x

 [1]  0.0000000  0.2040816  0.4081633  0.6122449  0.8163265  1.0204082  1.2244898  1.4285714  1.6326531  1.8367347

[11]  2.0408163  2.2448980  2.4489796  2.6530612  2.8571429  3.0612245  3.2653061  3.4693878  3.6734694  3.8775510

[21]  4.0816327  4.2857143  4.4897959  4.6938776  4.8979592  5.1020408  5.3061224  5.5102041  5.7142857  5.9183673

[31]  6.1224490  6.3265306  6.5306122  6.7346939  6.9387755  7.1428571  7.3469388  7.5510204  7.7551020  7.9591837

[41]  8.1632653  8.3673469  8.5714286  8.7755102  8.9795918  9.1836735  9.3877551  9.5918367  9.7959184 10.0000000

$y

 [1]  0.0000000  0.4081633  0.8163265  1.2244898  1.6326531  2.0408163  2.4489796  2.8571429  3.2653061  3.6734694

[11]  4.0816327  4.4897959  4.8979592  5.3061224  5.7142857  6.1224490  6.5306122  6.9387755  7.3469388  7.7551020

[21]  8.1632653  8.5714286  8.9795918  9.3877551  9.7959184 10.2040816 10.6122449 11.0204082 11.4285714 11.8367347

[31] 12.2448980 12.6530612 13.0612245 13.4693878 13.8775510 14.2857143 14.6938776 15.1020408 15.5102041 15.9183673

[41] 16.3265306 16.7346939 17.1428571 17.5510204 17.9591837 18.3673469 18.7755102 19.1836735 19.5918367 20.0000000

 

Method 2: Apply approx Function to Multiple Coordinates

In this method to apply an approx function to multiple coordinates, the user has to create the vectors containing multiple coordinates which further have to be passed as done in the above approach function which will be returning the linearly interpolates a list of the vector passed to the user.

Example:

In this example, we will be creating the vector accordingly having the multiple coordinates and then passing to the approx function to get the interpolates list of the vector passed further for the visualization we have to create the plot of the points.

R




# Create vector
x <- c(1,8,9,4,7,6,5)    
y <- c(0,6,5,1,4,7,9)  
  
# Apply approx function
data_approx1 <- approx(x, y)       
data_approx1    
  
# Draw output of approx function
plot(data_approx1$x,                 
     data_approx1$y)
points(x, y,
       col = "red",
       pch = 16)


Output:

$x

 [1] 1.000000 1.163265 1.326531 1.489796 1.653061 1.816327 1.979592 2.142857 2.306122 2.469388 2.632653 2.795918 2.959184

[14] 3.122449 3.285714 3.448980 3.612245 3.775510 3.938776 4.102041 4.265306 4.428571 4.591837 4.755102 4.918367 5.081633

[27] 5.244898 5.408163 5.571429 5.734694 5.897959 6.061224 6.224490 6.387755 6.551020 6.714286 6.877551 7.040816 7.204082

[40] 7.367347 7.530612 7.693878 7.857143 8.020408 8.183673 8.346939 8.510204 8.673469 8.836735 9.000000

$y

 [1] 0.00000000 0.05442177 0.10884354 0.16326531 0.21768707 0.27210884 0.32653061 0.38095238 0.43537415 0.48979592

[11] 0.54421769 0.59863946 0.65306122 0.70748299 0.76190476 0.81632653 0.87074830 0.92517007 0.97959184 1.81632653

[21] 3.12244898 4.42857143 5.73469388 7.04081633 8.34693878 8.83673469 8.51020408 8.18367347 7.85714286 7.53061224

[31] 7.20408163 6.81632653 6.32653061 5.83673469 5.34693878 4.85714286 4.36734694 4.08163265 4.40816327 4.73469388

[41] 5.06122449 5.38775510 5.71428571 5.97959184 5.81632653 5.65306122 5.48979592 5.32653061 5.16326531 5.00000000

 

Method 3: Create a User-Defined Interpolation Function Using approxfun

In this method to create a user-defined interpolation function, the user needs to call the approxfun() function and passed the required parameters to get the interpolation accordingly in the R programming language.

Example:

In this example, we will be creating the vector accordingly and passing this given vector to the approxfun function, and plotting the plot of the x and y points with the curve given by the approxfun function in the R programming language.

R




# Create vector
x <- c(1,8,9,4,7,6,5)    
y <- c(0,6,5,1,4,7,9)  
  
# Apply approx function
data_approxfum <- approxfun(x, y)       
data_approxfum    
  
# Draw output of approx function
plot(x, y)                        
curve(data_approxfum, add = TRUE)


Output:

function (v) 
.approxfun(x, y, v, method, yleft, yright, f, na.rm)
<bytecode: 0x000001bf4ba22068>
<environment: 0x000001bf4d2c0408>

 



Last Updated : 02 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads