0
votes

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.

2

2 Answers

0
votes

Here is a solution using base R. What maybe happening is you are performing a linear fit against the point position and not the date of event.

Your data:

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"))

Solution:

#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)
0
votes
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) 

This is the code that solves my problem based on Dave's suggestions.