2017-11-10 104 views
1

我有一個圖如下所示。如何在水平灰線切割曲線的x軸上找到座標。x軸座標,其中線切割曲線

enter image description here

下面

是玩具的數據集,我用生成的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) 
+0

您的示例數據不會複製數字 - 這看起來像是對數似然配置文件,某種功能?你能否提供更多的背景知識來自何處/來自哪裏? –

回答

2

我敢肯定有這樣做的更聰明的方法,但在這裏是用樣條的方法和蠻力。

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") 

enter image description here

+0

不錯的一個樣條! – Neoromanzer

+0

@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) 

而不是集中於兩條曲線的交點,簡化,打算做什麼I'm是移動/移動你的曲線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平方是相當不錯的...所以足夠

現在,我得到係數,並得到我的多項式的根。

coef<-fit4$coefficients 
polyroot(coef) 

它們是-1.094和-4.136。

+0

你的也不錯。不知道'polyroot()',看起來像一個方便的函數。 – AkselA