简体   繁体   English

如何估算Y轴的值,以便我在R中的时间序列(X轴)上具有相等的间隔

[英]Howto estimate values of Y axis, so that I have equal interval on timeseries(X-Axis) in R

I have a table in MYSQL with datetime object and contains Columns (Price) which is specific price at that particular time. 我在带有日期时间对象的MYSQL中有一个表,并且包含列(价格),这是在特定时间的特定价格。

[1] "2015-06-25 10:33:02 IST" "2015-06-25 10:35:32 IST"
[3] "2015-06-25 10:38:02 IST" "2015-06-25 10:40:02 IST"
[5] "2015-06-25 10:42:02 IST" "2015-06-25 10:44:02 IST"
[7] "2015-06-25 10:46:02 IST" "2015-06-25 10:49:02 IST"
[9] "2015-06-25 10:51:32 IST" "2015-06-25 10:53:02 IST"
[11] "2015-06-25 10:55:32 IST" "2015-06-25 10:57:32 IST"
[13] "2015-06-25 10:59:32 IST" "2015-06-25 11:01:02 IST"
[15] "2015-06-25 11:03:32 IST" "2015-06-25 11:05:32 IST"
[17] "2015-06-25 11:07:32 IST" "2015-06-25 11:09:02 IST"

Now as you see the time interval is not constant for each row in table. 现在,您看到表中每一行的时间间隔不是恒定的。 I want to plot the graph between Price and time, I want to keep the X-axis(Time) in fixed intervals and will try to plot estimated Y value(Price) 我想在价格和时间之间绘制图形,我想将X轴(时间)保持固定的间隔,并尝试绘制估算的Y值(价格)

For Ex: 例如:
Time series: 10:33:00, 10:35:00, 10:37:00, 10:39:00 etc 时间序列:10:3​​3:00、10:35:00、10:37:00、10:39:00等
Price(Estimated): ?? 价格(估算):??

I need help on following: 我需要以下帮助:

  1. Howto get Estimated value of Y-axis considering two nearby price. 如何获得考虑两个附近价格的Y轴估计值。
  2. I want to plot all these fixed interval points but X-Axis should be displaying scale in interval of 2 hours. 我想绘制所有这些固定的间隔点,但是X轴应该以2小时为间隔显示比例。

Below is my code, I am very new to R so please excuse if I am asking something very basic. 下面是我的代码,我对R还是很陌生,因此,如果我要问的是非常基本的内容,请原谅。

x<-data[,"idUnique"]
y<-data[,"Price"]
z<-data[,"datetime"]
z<-strptime(z, format="%d-%b-%Y %H:%M:%S",tz="IST") 
meanpivot<-mean(y)
meanx<-mean(x)
plot(x, y, type="l", col="red", xlim=c(meanx-400, meanx+400), ylim=c(meanpivot*0.99, meanpivot*1.01))

Right now I am using IDUnique, for X axis but I want to use Time(z) going forward. 现在我在X轴上使用IDUnique,但我想继续使用Time(z)。

Any help is appreciated. 任何帮助表示赞赏。 Thanks in advance !! 提前致谢 !!

As a side note for future questions - it would be lovely to have a small reproducible example of your problem. 作为未来问题的补充说明-拥有一个小且可重现的问题示例将非常有趣。 We can't copy-paste your code so far to try it out, because we don't have access to data . 到目前为止,我们无法复制粘贴您的代码以进行试用,因为我们无法访问data My answer demonstrates how you could have made a reproducible example (ie you can copy-paste it directly into your R console and it will work). 我的答案演示了如何制作可复制的示例(即,您可以将其直接复制粘贴到R控制台中,并且可以正常工作)。

The gist of the answer is to use approx to interpolate your per-interval prices, and then plot them using the package of your choice, adjusting the x axis intervals to whatever yo uwant. 答案的要点是使用approx来插值每个时间间隔的价格,然后使用您选择的软件包对它们进行绘制,将x轴间隔调整为您想要的任何值。

As far as I understand your question, your data has a column of datetimes and a column of prices, eg: 据我了解您的问题,您的数据包含一列日期时间和一列价格,例如:

# make an example dataframe with one value per (approximately) minute
x <- data.frame(datetime = as.POSIXct('2015-07-15 13:48:42') + jitter((1:60)*60),
                price = runif(60))

Now I want to calculate the price every 2 minutes (according to your example). 现在,我想每2分钟计算一次价格(根据您的示例)。 First we generate a vector of times we want to calculate at: 首先,我们生成一个要计算的时间向量:

# a vector of times from 13:50 every 2 minutes for an hour.
times.2min <- as.POSIXct('2015-07-15 13:50:00') + (0:29)*2*60

Note - you can't interpolate for times outside the raneg of your original datetimes, so be careful of that when you create your times.2min -equivalent. 注意-您无法插值原始日期时间范围以外的时间,因此在创建时间时要特别注意。 times.2min

Now to calculate the price at these times, we can do an interpolation. 现在要计算这些时间的价格,我们可以进行插值。 I'll just do linear approximation; 我只是做线性逼近; see ?approx for further details. 有关详细信息,请参见?approx

prices <- approx(x$datetime, x$price, xout=times.2min)

prices$x has our times.2min and prices$y has the calculated prices for those times. prices$x是我们的时间times.2minprices$y是这些时间的计算价格。

Then it's just a matter of plotting: 然后,只需绘制即可:

plot(y ~ x, prices, type='l')

The x axis ticks are automatically determined to not be too cluttered. x轴刻度线会自动确定为不太混乱。 If you want to specify them yourself (eg I'll do every 20 minutes here) 如果您想自己指定它们(例如,我每20分钟在这里做一次)

x.axis.ticks <- seq(from=round(min(prices$x), 'hour'),
                    to=round(max(prices$x), 'hour'),
                    by=20*60) # 14:00, 14:20, 14:40, 15:00
# plot points, skip x axis
plot(y ~ x, prices, type='l', xaxt='n')
# plot x axis
axis(1, at=x.axis.ticks, labels=as.character(x.axis.ticks))

在此处输入图片说明

Or if you like use ggplot to get something prettier 或者,如果您喜欢使用ggplot获得更漂亮的东西

library(ggplot2)
ggplot(data.frame(prices), aes(x=x, y=y)) +
    geom_line() +
    scale_x_datetime(breaks=date_breaks(width="20 min"))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM