Skip to content

Instantly share code, notes, and snippets.

@JoshAshby
Created April 28, 2011 03:07
Show Gist options
  • Save JoshAshby/945723 to your computer and use it in GitHub Desktop.
Save JoshAshby/945723 to your computer and use it in GitHub Desktop.
Computes a graphs the last period interval of a dataset with design \|\|\|\ and computes the line of best fit
library(ggplot2)
mydata = read.csv("/home/joshua/Downloads/test_data.csv", header = TRUE)
m = length(mydata$x)
theta = matrix(c(0,0), nrow=1)
ux = matrix(c(rep(1,m), mydata$x), ncol=2)
uy= matrix(mydata$y, ncol=1)
delta = function(x,y,th) {
delta = (t(x) %*% ((x %*% t(th)) - y))
return(t(delta))
}
for (i in 0:m) {
j = (m - i)
if (uy[(j)] > uy[(j-1)]) {
oy=matrix(uy[j:m], ncol=1)
ox=matrix(c(rep(1,(i+1)), ux[j:m,2]), ncol=2)
break
}
}
data = data.frame(ox,oy)
dataplot = ggplot(data, aes(X2, oy)) + geom_point() + ylab('Quantity') + xlab('Days')
theta.normal = solve(t(ox) %*% ox) %*% (t(ox) %*% oy)
dataplot + geom_abline(intercept=theta.normal[1], slope=theta.normal[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment