【问题标题】:Coordinates of x axis where line cuts the curve直线与曲线相交的 x 轴坐标
【发布时间】:2017-11-10 20:59:04
【问题描述】:

我有一个如下所示的图表。如何找到水平灰线与曲线相交的x轴坐标?

下面是我用来生成x、y值的玩具数据集

df <- structure(list(x = c(0, -1, -2, -3, -4, -5, -6, -7, -8), 
    y = c(-5.22672371336103, -2.04798328990208, 
    -0.998312848674327, -1.13656559451279, -1.80175393429754, 
    -2.67597356058193, -3.62933726371666, -4.61213085819315, 
    -5.60579419730348)), .Names = c("x", "y"
), row.names = c(NA, -9L), class = "data.frame")

plot(df$x, df$y, asp = 1)
abline(h=-1.92,  col = "gray60")
lines(df$x, df$y)

【问题讨论】:

  • 您的样本数据没有复制该图 - 这看起来像一个对数似然曲线,某种函数?您能否提供更多有关其来源/来源的背景信息?

标签: r plot coordinates


【解决方案1】:

我确信有更聪明的方法可以做到这一点,但这里有一种使用样条曲线和蛮力的方法。

spl <- splinefun(df)
s <- seq(min(df$x), max(df$x), by=5e-3)
est <- spl(s)

xs <- s[diff(sign(diff(c(0, abs(-1.92 - est))))) > 0]

plot(df$x, df$y, asp=1)
abline(h=-1.92,  col = "gray60")
lines(s, est)
abline(v=xs, col="blue")

【讨论】:

  • 漂亮的样条线!
  • @AkselA 很好的解决方案,AkselA 我要研究这段代码
【解决方案2】:

这是另一个解决方案。

让我先分别定义 x 和 y 向量。

x= c(0, -1, -2, -3, -4, -5, -6, -7, -8)
y= c(-5.22672371336103, -2.04798328990208, 
     -0.998312848674327, -1.13656559451279, -1.80175393429754, 
     -2.67597356058193, -3.62933726371666, -4.61213085819315, 
     -5.60579419730348)

为了简化,我将不再关注两条曲线之间的交点,而是将曲线h 向上移动/移动。

y<-y+1.92

现在我的问题要简单得多:计算曲线的根。

我将拟合一个 4 次多项式(这有点随机,我不得不承认)。

fit4 <- lm(y~poly(x,4,raw=TRUE))
summary(fit4)

Coefficients:
                          Estimate Std. Error t value Pr(>|t|)    
(Intercept)             -3.2879360  0.0525516  -62.57 3.91e-07 ***
poly(x, 4, raw = TRUE)1 -4.4218115  0.1044875  -42.32 1.86e-06 ***
poly(x, 4, raw = TRUE)2 -1.4833140  0.0583804  -25.41 1.42e-05 ***
poly(x, 4, raw = TRUE)3 -0.1799201  0.0112986  -15.92 9.09e-05 ***
poly(x, 4, raw = TRUE)4 -0.0080516  0.0007005  -11.49 0.000327 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.05373 on 4 degrees of freedom
Multiple R-squared:  0.9995,    Adjusted R-squared:  0.9991 
F-statistic:  2130 on 4 and 4 DF,  p-value: 6.605e-07

如你所见,我的 R-squared 相当不错……所以够了

现在,我得到系数并得到多项式的根。

coef<-fit4$coefficients
polyroot(coef)

分别是 -1.094 和 -4.136。

【讨论】:

  • 你的也不错。不知道polyroot(),似乎是一个方便的功能。
猜你喜欢
  • 2022-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多