I started out with an xts/zoo object like this for variable, '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
I can plot this fine using plot(my.data, type = "l") but running a linear regression on this data set gives an intercept of 567.99 and a slope of -0.0278. If I try and plot the regression line on the plot with:
abline(z$coefficients[1], coefficients[2], type = "l")
nothing appears. I tried calculating the correct intercept from my graph with:
intcpt <- z$coefficients[1] + z$coefficients[2] * as.numeric(my.data[1,2])
which gives me the right number, but still nothing if I use abline:
abline(intcpt, z$coefficients[2], type = "l")
Reading posts regarding issues with using abline with xts/zoo objects, I tried converting the data frame as follows:
y <- coredata(my.data)
x <- index(my.data)
new.data <- data.frame(prices = y, dates = x)
z = lm(prices ~ dates, data = new.data)
abline() still plots nothing. Any suggestions appreciated.