简体   繁体   中英

2D linear interpolation in R with non-equally spaced values

I have a question concerning 2D interpolation of non-equally spaced values in R. I was able to do a 2D linear interpolation with equally-spaced values using the interp2() from the pracma package (see example below).

Equally-spaced 2D linear interpolation

library(dplyr)
library(tidyr)

### 2D interpolation with equally-spaced values ###

# Create sample data
data <- expand_grid(val1 = c(seq(1, 5, 1)), val2 = seq(1, 5, 1))
df <- data %>% mutate(z = val1 *val2)

# Transform into matrix
mat <- reshape2::acast(df, val1~val2, value.var="z" )
mat
#>   1  2  3  4  5
#> 1 1  2  3  4  5
#> 2 2  4  6  8 10
#> 3 3  6  9 12 15
#> 4 4  8 12 16 20
#> 5 5 10 15 20 25

# Show plot
lattice::levelplot(mat)

# Create vectors containing coordinates for interpolation
interp_coords <- expand_grid(val1 = seq(1, 5, 0.1), val2 = seq(1, 5, 0.1))

# Interpolate using the interp() from the pracma library
interp_vals <- pracma::interp2(y = c(seq(1, 5, 1)), x = seq(1, 5, 1), Z = mat, xp = interp_coords$val1, yp = interp_coords$val2)

# Bind in new df, convert to matrix and plot
df_2 <- cbind(interp_coords, interp_vals)
mat2 <- reshape2::acast(df_2, val1~val2, value.var="interp_vals" )
lattice::levelplot(mat2)

Non-equally spaced 2D linear interpolation

Next, I tried to accomplish the same with a non-equally-spaced dataset (see image below; c(seq(1, 5, 1), seq(6, 10, 2) ).

### 2D interpolation with NON-equally spaced values ###
# Create sample data
df <- expand_grid(val1 = c(seq(1, 5, 1), seq(6, 10, 2)), val2 = seq(1, 5, 1)) %>% 
  mutate(z = val1 *val2)

# Transform into matrix
mat <- reshape2::acast(df, val1~val2, value.var="z" )
mat
#>     1  2  3  4  5
#> 1   1  2  3  4  5
#> 2   2  4  6  8 10
#> 3   3  6  9 12 15
#> 4   4  8 12 16 20
#> 5   5 10 15 20 25
#> 6   6 12 18 24 30
#> 8   8 16 24 32 40
#> 10 10 20 30 40 50
# Show plot
lattice::levelplot(mat)

Apparently, something goes wrong with the interpolation, and I do not understand what it is.

# Create vectors containing coordinates for interpolation
interp_coords <- expand_grid(val1 = seq(1, 10, 0.1), val2 = seq(1, 5, 0.1))

# Interpolate using the interp() from the pracma library
interp_vals <- pracma::interp2(y = c(seq(1, 5, 1), seq(6, 10, 2)), x = seq(1, 5, 1), Z = mat, xp = interp_coords$val1, yp = interp_coords$val2)

# Bind in new df, convert to matrix and plot
df_2 <- cbind(interp_coords, interp_vals)
mat2 <- reshape2::acast(df_2, val1~val2, value.var="interp_vals" )
lattice::levelplot(mat2)

Question

How can I perform a non-equally spaced 2D linear interpolation in R?

Created on 2020-10-30 by the reprex package (v0.3.0)

You have simply mixed up the xp and yp arguments of interp2 :

# Create vectors containing coordinates for interpolation
interp_coords <- expand_grid(val1 = seq(1, 10, 0.1), val2 = seq(1, 5, 0.1))

# Interpolate using the interp() from the pracma library
interp_vals <- pracma::interp2(y  = c(seq(1, 5, 1), seq(6, 10, 2)), 
                               x  = seq(1, 5, 1), Z = mat, 
                               xp = interp_coords$val2, 
                               yp = interp_coords$val1)

# Bind in new df, convert to matrix and plot
df_2 <- cbind(interp_coords, interp_vals)
mat2 <- reshape2::acast(df_2, val1~val2, value.var="interp_vals" )
lattice::levelplot(mat2)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM