简体   繁体   English

R中点的线性插值

[英]linear interpolation of points in R

This may seem a really simple question, but here goes: 这似乎是一个非常简单的问题,但这里有:

I have a data frame: 我有一个数据框:

test_df <- data.frame(x1 = c(277422033,24118536.4,2096819.0,
                               182293.4,15905,1330,105,16,1),
                      x2 = c(2.496e-3,2.495e-2,2.496e-1,
                               2.496e0,2.47e1,2.48e2,2.456e3,
                               3.7978e4,3.781e5))

and I would like to linearly interpolate this to increase the number of points. 我想对其进行线性插值以增加点数。 The variables are linearly related on a log scales, ie 这些变量在对数刻度上线性相关,即

plot(log10(test_df[,1]),log10(test_df[,2]))

在此处输入图片说明

So, my question is, how do I linearly interpolate these to increase the number of values? 因此,我的问题是,如何线性内插这些值以增加值的数量?

Here is my attempt using a linear model (as opposed to the approx function): 这是我尝试使用线性模型(而不是近似函数)的尝试:

I have defined a linear model as: 我将线性模型定义为:

test.lm <- lm(log10(x1) ~ log10(x2), data = test_df)

and then define a new variable for the new points: 然后为新点定义一个新变量:

ss <- seq(min(test_df$x2),max(test_df$x2),length.out = 100) # new x1

then predict the new values and plot the points 然后预测新值并绘制点

newY <- predict(test.lm, newdata = data.frame(x2 = ss)) # interpolated values

test_df2 <- data.frame(x1 = 10^newY,
                       x2 = ss)

points(newY,log10(ss),col = "red")

在此处输入图片说明

This works as I expect ie the graph in the end is as I expected. 这按我预期的那样工作,即最终的图形符合我的预期。

I would like to increase the number of points in test_df2 which can be done by increasing length.out eg 我想增加test_df2中的点数,这可以通过增加length.out来完成。

ss <- seq(min(test_df$x2),max(test_df$x2),length.out = 10000000)

but this makes the running time very long on my machine, to the point that I have to restart R. 但这使我的机器上的运行时间很长,以至于我必须重新启动R。

Is there a way that I can linearly interpolate at an evenly distributed number of points which also extend the entire number of points specified in ss? 有没有一种方法可以线性插值均匀分布的点数,从而扩展ss中指定的全部点数?

Just use 只需使用

ss <- 10^seq(log10(min(test_df$x2)),log10(max(test_df$x2)),length.out = 1000)

to have your new data evenly distributed on the log scale. 使您的新数据在对数刻度上均匀分布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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