# I use a program called R for tasks like this. You can download it for # free from http://cran.r-project.org/ # Input to the program is given here; lines beginning with # are comments. # # I first exported the data from Excell into a CSV file, and removed the # quotes. This split some fields into two, fortunately in a regular manner. # So my file now contains five fields that I will ignore, followed by months # and miles. The function scan will read in the data, with a first argument # my file name, the second argument a sampling of the data types for the # fields, and the third the field seperator. The syntax <- is an assignment # operator; data sets and the output of model fits are all assigned to # objects. The data are put into the data set rr; variables within a # data set are accessed by data set name, $, and the variable name; hence # rr$monthssq is the months squared. # I then call the regression # fitter. R does this in two steps; the first fits the model, and the # second formats the results for easy viewing. The fitter is lm; first # argument to lm is formula, the second is the data source, and the third # picks out the first three elements. The formula has the syntax first # response variable, then a tilde, then the explanatory variables. The # -1 removes the intercept. # Here's code to do the intercept-set-to-zero fit: rr<-as.data.frame( scan("UnionPacificDec3.csv",what=list(a="",b="",c="",d="",e="", months=0,miles=0), sep=",")) rr$monthssq<-rr$months^2 cat("Model with 0 intercept, linear in months, for first three ponts\n") summary(lma<-lm(miles~-1+months, data = rr, subset = 1:3)) cat("Model with 0 intercept, quadratic in months, for next three portions\n") summary(lmb<-lm(miles~months+monthssq, data = rr, subset = 3:7)) summary(lmc<-lm(miles~months+monthssq, data = rr, subset = 7:12)) summary(lmd<-lm(miles~months+monthssq, data = rr, subset = 12:17)) # Here's my suggestion about seasonality terms using sines and cosines. # -1 after cosine forces it to be zero at 0 months. rr$smonths<-sin(rr$months/12) rr$cmonths<-cos(rr$months/12)-1 lm0<-lm(miles~-1+months+monthssq,data=rr) lmout<-lm(miles~-1+months+monthssq+smonths+cmonths, data = rr) cat("Model with 0 intercept, quadratic in months, with seasonal effects\n") summary(lmout) pdf("railroad.pdf") plot(rr$months,rr$miles,main="Data",xlab="Months",ylab="Miles") lines(rr$months,fitted(lmout)) detrend<-list(months=rr$months,miles=lm0$residuals) plot(detrend$months,detrend$miles, main="Residuals from quadratic fit, to show the potential for a trig. seasonal effect",xlab="Months",ylab="Miles", sub="Note poor performance during the final winter")