如何使用x轴日期值R绘制直线

时间:2016-07-13 17:20:45

标签: r plot xts zoo

我开始使用像这样的xts / zoo对象变量'my.data':

            VTI
2015-05-15 107.3203
2015-05-18 107.7205
2015-05-19 107.6717
2015-05-20 107.6034
2015-05-21 107.8767
2015-05-22 107.6717
2015-05-26 106.5491
2015-05-27 107.5253
2015-05-28 107.4277
2015-05-29 106.7053
2015-06-01 106.9689
2015-06-02 106.8908
2015-06-03 107.2617
2015-06-04 106.4027
2015-06-05 106.3149
2015-06-08 105.6608
2015-06-09 105.5730
2015-06-10 106.8615
2015-06-11 107.1544
2015-06-12 106.4613
2015-06-15 105.9830
2015-06-16 106.5589
2015-06-17 106.7346
2015-06-18 107.7986
2015-06-19 107.3203
2015-06-22 107.9743
2015-06-23 108.0817
2015-06-24 107.2617
2015-06-25 106.9298
2015-06-26 106.8691
2015-06-29 104.6338
2015-06-30 104.9181
2015-07-01 105.6044

我可以使用绘图(my.data,type =“l”)绘制此图,但在此数据集上运行线性回归可得到567.99的截距和-0.0278的斜率。如果我尝试在情节上绘制回归线:

abline(z$coefficients[1], coefficients[2], type = "l")
什么也没出现。我尝试用我的图表计算正确的截距:

intcpt <- z$coefficients[1] + z$coefficients[2] * as.numeric(my.data[1,2])

它给了我正确的数字,但如果我使用abline仍然没有:

abline(intcpt, z$coefficients[2], type = "l")

阅读有关使用abts with xts / zoo对象的问题的帖子,我尝试按如下方式转换数据框:

y <- coredata(my.data)
x <- index(my.data)
new.data <- data.frame(prices = y, dates = x)
z = lm(prices ~ dates, data = new.data)

abline()仍然没有绘制。任何建议表示赞赏。

2 个答案:

答案 0 :(得分:0)

这是一个使用基础R的解决方案。可能发生的是你正在对点位置进行线性拟合,而不是事件的日期。

您的数据:

my.data<-structure(list(VTI = c(107.3203, 107.7205, 107.6717, 107.6034, 
107.8767, 107.6717, 106.5491, 107.5253, 107.4277, 106.7053, 106.9689, 
106.8908, 107.2617, 106.4027, 106.3149, 105.6608, 105.573, 106.8615, 
107.1544, 106.4613, 105.983, 106.5589, 106.7346, 107.7986, 107.3203, 
107.9743, 108.0817, 107.2617, 106.9298, 106.8691, 104.6338, 104.9181, 
105.6044)), .Names = "VTI", class = "data.frame", row.names = c("2015-05-15", 
"2015-05-18", "2015-05-19", "2015-05-20", "2015-05-21", "2015-05-22", 
"2015-05-26", "2015-05-27", "2015-05-28", "2015-05-29", "2015-06-01", 
"2015-06-02", "2015-06-03", "2015-06-04", "2015-06-05", "2015-06-08", 
"2015-06-09", "2015-06-10", "2015-06-11", "2015-06-12", "2015-06-15", 
"2015-06-16", "2015-06-17", "2015-06-18", "2015-06-19", "2015-06-22", 
"2015-06-23", "2015-06-24", "2015-06-25", "2015-06-26", "2015-06-29", 
"2015-06-30", "2015-07-01"))

解决方案:

#convert row names into a column of dates
my.data$x<-row.names(my.data)
my.data$x<-as.Date(my.data$x, "%Y-%m-%d")

plot(my.data$VTI, x=my.data$x, type = "l")
#perform the linear regression VTI by x
model<-lm(VTI~x, data=my.data)
#plot the linear regression model
abline(model)

答案 1 :(得分:0)

new.data <- data.frame(Date = as.Date(index(my.data)), VTI = coredata(my.data)) 
plot(new.data$Date, new.data$VTI, type = "l") 
model <- lm(VTI ~ Date, data = new.data) 
abline(model) 

这是根据戴夫的建议解决我的问题的代码。