如何将数学函数的xy坐标返回为任意长度的数据框?

huangapple go评论58阅读模式
英文:

How to return xy-coordinates of a mathematical function into a dataframe of arbitrary length?

问题

我想获得一个包含描述两点P1(x1, y1)和P2(x2, y2)之间线性函数的n个xy坐标的数据框。

我的方法是找到两点的xy坐标,然后计算这个线性方程的斜率和截距 y = slope * x + intercept

为了简化,让:

x1 = 0.1
y1 = 0.2
x2 = 1.1
y2 = 0.6

n = c(1:200) # 例子中我的数据的长度

slope = (y2 - y1) / (x2 - x1)
intercept = y1 - slope * x1

到目前为止都好,但现在我想将线性方程的各个xy坐标计算到一个名为baseline的数据框中,该数据框有length(n)行,包含x_baseliney_baseline 两列。

如何解决这个问题?到目前为止,我在网上找不到令人满意的答案,非常感谢任何帮助!

为了澄清,这是我期望的输出的示意图在这里

英文:

I want to get a dataframe containing n numbers of xy-coordinates that describe a linear function between two points P1(x1, y1) and P2(x2, y2) .

My approach would be to find the xy-coordinates of two points and calculate the slope and intercept of this linear equation y = slope * x + intercept.

For simplicity let: P

x1 = 0.1
y1 = 0.2
x2 = 1.1
y2 = 0.6

n = c(1:200) #example length of my data

slope = (y2 - y1) / (x2 - x1)
intercept = y1 - slope * x1

So far so good, but now i would like to compute the individual xy-coordinates of my linear equation into a dataframe baseline with length(n) rows with the columns x_baseline and y_baseline.

How would one go about solving this problem? So far, I could not find an satisfactory answer online, any help is highly appreciated!

For clarification a drawing of my desired output here

答案1

得分: 1

如果你有x1y1x2y2,你可以使用一些代数运算来计算点之间的插值。

steps <- (n-1)/(max(n)-1)
dd <- data.frame(
  x = x1 + (x2-x1) * steps,
  y = y1 + (y2-y1) * steps
)

然后,你可以使用以下代码来绘制它们:

plot(y~x, dd)
points(c(x1,x2), c(y1, y2), col="green", pch=16)

你也可以使用approx进行插值:

dd <- data.frame(
  x = approx(c(0,1), c(x1, x2), seq(0, 1, length.out = length(n)))$y,
  y = approx(c(0,1), c(y1, y2), seq(0, 1, length.out = length(n)))$y
)

或者,如果你真的想要使用斜率(slope)和截距(intercept):

dd <- data.frame(
  x = seq(x1, x2, length.out = length(n))
) %>%
  transform(y = intercept + x * slope)
英文:

If you have x1, y1, x2, y2 you can just use some some algebra to calculate the interpolation between the points

steps &lt;- (n-1)/(max(n)-1)
dd &lt;- data.frame(
  x = x1 + (x2-x1) * steps,
  y = y1 + (y2-y1) * steps
)

And we can plot them with

plot(y~x, dd)
points(c(x1,x2), c(y1, y2), col=&quot;green&quot;, pch=16)

You could also do interpolation with approx

dd &lt;- data.frame(
  x = approx(c(0,1), c(x1, x2), seq(0, 1, length.out = length(n)))$y,
  y = approx(c(0,1), c(y1, y2), seq(0, 1, length.out = length(n)))$y
)

or if you really wanted to use the slope and intercept you could do

dd &lt;- data.frame(
  x = seq(x1, x2, length.out = length(n))
) |&gt;
  transform(y = intercept + x * slope)
  

答案2

得分: 1

以下是翻译好的代码部分:

library(tidyverse)
library(modelr)

baseline <- tibble(
  x = c(0.1, 1.1), 
  y = c(0.2, 0.6)
)

mod <- lm(y ~ x, baseline)

baseline |>
  data_grid(x = seq_range(x, 200)) |>
  add_predictions(mod, var = "y_baseline") |>
  rename(x_baseline = x)

创建于2023年3月12日,使用reprex v2.0.2

英文:

If you don't want to do the math yourself, fit a linear model and predict from it:

library(tidyverse)
library(modelr)

baseline &lt;- tibble(
  x = c(0.1, 1.1), 
  y = c(0.2, 0.6)
)

mod &lt;- lm(y ~ x, baseline)

baseline |&gt; 
  data_grid(x = seq_range(x, 200)) |&gt; 
  add_predictions(mod, var = &quot;y_baseline&quot;) |&gt; 
  rename(x_baseline = x)
#&gt; # A tibble: 200 &#215; 2
#&gt;    x_baseline y_baseline
#&gt;         &lt;dbl&gt;      &lt;dbl&gt;
#&gt;  1      0.1        0.200
#&gt;  2      0.105      0.202
#&gt;  3      0.110      0.204
#&gt;  4      0.115      0.206
#&gt;  5      0.120      0.208
#&gt;  6      0.125      0.210
#&gt;  7      0.130      0.212
#&gt;  8      0.135      0.214
#&gt;  9      0.140      0.216
#&gt; 10      0.145      0.218
#&gt; # … with 190 more rows

<sup>Created on 2023-03-12 with reprex v2.0.2</sup>

答案3

得分: 0

您的x值将由seq(x1, x2, length = 100)给出,新的y值将由这个向量乘以slope再加上intercept

df <- data.frame(x_baseline = seq(x1, x2, length = 100), 
                 y_baseline = slope * seq(x1, x2, length = 100) + intercept)

让我们用红色绘制您的原始点,用黑色绘制数据框的点:

plot(c(x1, x2), c(y1, y2), col = "red", cex = 2)
points(df$x_baseline, df$y_baseline)

如何将数学函数的xy坐标返回为任意长度的数据框?

2023-03-11创建,使用reprex v2.0.2

英文:

Your x values would be given by seq(x1, x2, length = 100), and your new y values given by this vector multiplied by slope plus the intercept:

df &lt;- data.frame(x_baseline = seq(x1, x2, length = 100), 
                 y_baseline = slope * seq(x1, x2, length = 100) + intercept)

Let's plot your original points in red, and the dataframe's points in black:

plot(c(x1, x2), c(y1, y2), col = &quot;red&quot;, cex = 2)
points(df$x_baseline, df$y_baseline)

如何将数学函数的xy坐标返回为任意长度的数据框?

<sup>Created on 2023-03-11 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年3月12日 06:48:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710060.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定