【问题标题】:Extract x-axis value using y-axis data in R使用 R 中的 y 轴数据提取 x 轴值
【发布时间】:2013-02-24 05:47:00
【问题描述】:

我有一个这种格式的时间序列数据集:

       Time Val1 Val2
     0 0.68 0.39
    30 0.08 0.14
    35 0.12 0.07
    40 0.17 0.28
    45 0.35 0.31
    50 0.14 0.45
   100 1.01 1.31
   105 0.40 1.20
   110 2.02 0.57
   115 1.51 0.58
   130 1.32 2.01

使用此数据集,我想提取(而不是预测)FC1=1 和 FC2=1 的时间。这是我用要提取的注释点创建的图。

我正在寻找一种解决方案,使用 or 函数进行插值/截取以提取值。例如,如果我在折叠变化 1 处画一条直线(比如在 y 轴上),我想提取 X 轴上直线相交的所有点。

期待您的建议并提前致谢!

【问题讨论】:

  • 你会为外行定义FC吗?折叠变化?
  • 是的,它是折叠改变或折叠截断,或者在该测定中特别是转录开始的时间。

标签: time-series data-mining missing-data interpolation


【解决方案1】:

您可以使用approxfun 进行插值,使用uniroot 查找单根(线交叉的地方)。您需要多次运行 uniroot 才能找到所有交叉点,rle 函数可能有助于选择起点。

数据中的 FC 值永远不会接近 1,更不用说交叉了,因此您的数据必须比显示的多得多,或者表示不同的值。

如果您能提供更多细节(可能包括显示您想要的图表),那么我们可能会提供更详细的帮助。

编辑

好的,这里有一些 R 代码可以找到线的交叉点:

con <- textConnection('           Time Val1 Val2
         0 0.68 0.39
        30 0.08 0.14
        35 0.12 0.07
        40 0.17 0.28
        45 0.35 0.31
        50 0.14 0.45
       100 1.01 1.31
       105 0.40 1.20
       110 2.02 0.57
       115 1.51 0.58
       130 1.32 2.01')

mydat <- read.table(con, header=TRUE)

with(mydat, {
    plot( Time, Val1, ylim=range(Val1,Val2), col='green', type='l' )
    lines(Time, Val2, col='blue')
})
abline(h=1, col='red')

afun1 <- approxfun( mydat$Time, mydat$Val1 - 1 )
afun2 <- approxfun( mydat$Time, mydat$Val2 - 1 )
points1 <- cumsum( rle(sign(mydat$Val1 - 1))$lengths )
points2 <- cumsum( rle(sign(mydat$Val2 - 1))$lengths )

xval1 <- numeric( length(points1) - 1 )
xval2 <- numeric( length(points2) - 1 )

for( i in seq_along(xval1) ) {
    tmp <- uniroot(afun1, mydat$Time[ points1[c(i, i+1)] ])
    	xval1[i] <- tmp$root
}

for( i in seq_along(xval2) ) {
    tmp <- uniroot(afun2, mydat$Time[ points2[c(i, i+1)] ])
    	xval2[i] <- tmp$root
}

abline( v=xval1, col='green' )
abline( v=xval2, col='blue')

【讨论】:

  • 谢谢 Greg 我现在提供了正确的数据。这是一个使用我手动添加的带有注释的数据的图:postimage.org/image/x7qkax47b我想在用箭头突出显示的点处提取值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-03
  • 1970-01-01
  • 2012-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多