Time Series R Example
> n = 35
> t = 1:n
> y1 = c(294,260,265,260,331,314,288,331,417,426,355,353,310,278,302,260,307,275,266,300,287,302,298,323,291,258,287,266,288,313,355,326,367,373,380)
> y2 = c(268,225,266,230,268,252,287,287,363,371,325,279,275,196,248,193,241,248,253,294,282,280,284,269,266,267,268,258,272,269,290,326,307,339,349)
> x = matrix(c(rep(1,n),1:n),n,2)
> y1
[1] 294 260 265 260 331 314 288 331 417 426 355 353 310 278 302 260 307
[18] 275 266 300 287 302 298 323 291 258 287 266 288 313 355 326 367 373
[35] 380
> y2
[1] 268 225 266 230 268 252 287 287 363 371 325 279 275 196 248 193 241
[18] 248 253 294 282 280 284 269 266 267 268 258 272 269 290 326 307 339
[35] 349
> solve(t(x)%*%x)
[,1] [,2]
[1,] 0.119327731 -0.005042017
[2,] -0.005042017 0.000280112
> beta = solve(t(x)%*%x)%*%(t(x)%*%y1)
> beta
[,1]
[1,] 300.1949580
[2,] 0.6336134
> beta2 = solve(t(x)%*%x)%*%(t(x)%*%y2)
> beta2
[,1]
[1,] 258.183193
[2,] 1.045378
> u = solve(t(x)%*%x)
> u
[,1] [,2]
[1,] 0.119327731 -0.005042017
[2,] -0.005042017 0.000280112
> e = y1-x%*%beta
> s = sqrt(sum(e^2)/(n-2))
> s
[1] 44.07472
> e2 = y2-x%*%beta2
> s2 = sqrt(sum(e2^2)/(n-2))
> s2
[1] 39.51243
> d1 = sum((e[1:34]-e[2:35])^2)/sum(e^2)
> d2 = sum((e2[1:34]-e2[2:35])^2)/sum(e2^2)
> d1;d2
[1] 0.6408324
[1] 0.7310235
> d2
[1] 0.7310235
> sd = c(s*sqrt(u[1,1]),s*sqrt(u[2,2]))
> sd2 = c(s2*sqrt(u[1,1]),s2*sqrt(u[2,2]))
> sd;sd2
[1] 15.2251029 0.7376586
[1] 13.6491115 0.6613016
> sd2
[1] 13.6491115 0.6613016
> rsqr1 = 1-sum(e^2)/sum