R:在图上找到与特定Y值对应的X值

时间:2015-09-03 08:35:56

标签: r

我研究了一个连续变量,每10分钟测量2小时。 我想知道变量在什么时候加倍并且增加了两倍。

Example data:
# The time variable
time <- seq(from = 0, to = 120, by=10)
# The measured variable
value <- c(5, 5.5, 7.8, 8.3, 9.5, 10.9, 11.5, 12, 13, 14, 12.5, 11.1, 9)
# Put together
df <- data.frame(time, value)
# Plotted
ggplot(df, aes(time, value)) + geom_line()

enter image description here

# At what time point (what X value) does Y equal (for example) 10?

# I've tried (according to previous suggestions on this site (but they turned out to be not reliable, and heavily dependent upon the "interval" specified.

f1 <- approxfun(df$time, df$value)
optimize(function(t0) abs(f1(t0) - 10), interval = c(0, 120))[[1]]

有没有人知道任何其他可以找到X值而不依赖于间隔的函数。我再次询问的原因是,稍微改变间隔(但保持在真值之内)会改变结果......

感谢您的任何建议

1 个答案:

答案 0 :(得分:1)

我不知道它对您是否有用和实用,但我的想法是将(多项式)曲线拟合到您的数据,然后使用此曲线“预测”(找到)任何y值的x值。如果您的y值对应多个x值,您将保留第一个值。

我建议您逐步运行该过程,以了解初始数据集的转换方式。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tinysort/2.2.2/tinysort.js"></script>
<table class="blue" id="xtable">
  <thead>
    <tr>
      <th data-order="asc">
        <a>Item
          </a>
            </th>
            <th>Qty
              </th>
                <th data-order="asc"><a>Price</a> </th>
            <th>Desc</th>
    </tr>
    </thead>
    <tbody>
    <tr> <td> Porche </td><td>1</td> <td> $100, 000.00 </td><td>Sports car</td> </tr>
      <tr><td>Toilet Paper</td> <td> 5 </td><td>$50.00</td> <td> 8 rolls </td></tr>
    <tr> <td> Laptop </td><td>1</td> <td> $600.00 </td><td>HP i7 12GB 1TB</td> </tr>
    </tbody>
    </table>

enter image description here

您需要一个排除重叠部分的流程。我正在使用一个在“值”值(x轴)开始变小时发现的过程。这些案件被排除在外。

library(ggplot2)
library(dplyr)

# The time variable
time <- seq(from = 0, to = 120, by=10)
# The measured variable
value <- c(5, 5.5, 7.8, 8.3, 9.5, 10.9, 11.5, 12, 13, 14, 12.5, 11.1, 9)
# Put together
df <- data.frame(time, value)

# Plot value (x axis) againt time (y axis)
ggplot(df, aes(time, value)) + 
  geom_point()

enter image description here

这些是您需要考虑的数据点。

# create a row index
df %>% mutate(id = row_number()) -> df

df_updated = 
    df %>%
    group_by(id) %>%          # for each row
    do(data.frame(.,max_value = max(df$value[df$id <= .$id]))) %>%   # obtain the maximum value up to that point
    ungroup() %>%
    filter(value >= max_value)     # exclude declining parts


# Plot value (x axis) againt time (y axis) from the updated dataset
ggplot(df_updated, aes(time, value)) + 
  geom_point()

enter image description here

# filt a polynomial curve that best describes your data
fit <- lm(time~poly(value,8,raw=TRUE), data = df_updated)   ## NOTE that here it requires some extra work to find which degree gives you an acceptable fit (you can create a process that calculates your optimal degree. Here I used 8).

# check how good your fitting is
ggplot(df_updated, aes(time, value)) + 
  geom_point() +
  geom_line(aes(predict(fit, df_updated), value))