1

I have a time-series dataset in this format:

       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

Using this dataset I want to extract(not predict) Time at which FC1=1 and FC2=1. Here is a plot that I created with annotated points I would like to extract.

enter image description here

I am looking for a solution using or function to interpolate/intercept to extract values. For example, if I draw a straight line at fold change 1 (say in y-axis), I want to extract all the points on X-axis where the line intercepts.

Looking forward for suggestions and thanks in advance !

1 Answers1

2

You can use approxfun to do interpolations and uniroot to find single roots (places where the line crosses). You would need to run uniroot multiple times to find all the crossings, the rle function may help choose the starting points.

The FC values in your data never get close to 1 let alone cross it, so you must either have a lot more data than shown, or mean a different value.

If you can give more detail (possibly include a plot showing what you want) then we may be able to give more detailed help.

Edit

OK, here is some R code that finds where the lines cross:

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 Snow
  • 48,497
  • 6
  • 83
  • 110
  • Thanks Greg I have now provided correct data. Here is a plot using the data with annotations that I have added manually: http://postimage.org/image/x7qkax47b/ I want to extract value at the points highlighted with arrows. – Khader Shameer Mar 08 '13 at 00:56