library(faraway)
data("airpass")
attach(airpass)
plot(pass~year,type="l")

passmodel <- lm(pass~year)
summary(passmodel)
## 
## Call:
## lm(formula = pass ~ year)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -93.858 -30.727  -5.757  24.489 164.999 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1474.771     61.106  -24.14   <2e-16 ***
## year           31.886      1.108   28.78   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 46.06 on 142 degrees of freedom
## Multiple R-squared:  0.8536, Adjusted R-squared:  0.8526 
## F-statistic: 828.2 on 1 and 142 DF,  p-value: < 2.2e-16
library(faraway)
data(aatemp)
names(aatemp)
## [1] "year" "temp"
attach(aatemp)
## The following object is masked from airpass:
## 
##     year
plot(temp~year,type="l")
abline(0,0)

tempmod <- lm(temp~year)
summary(tempmod)
## 
## Call:
## lm(formula = temp ~ year)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.9843 -0.9113 -0.0820  0.9946  3.5343 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)   
## (Intercept) 24.005510   7.310781   3.284  0.00136 **
## year         0.012237   0.003768   3.247  0.00153 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.466 on 113 degrees of freedom
## Multiple R-squared:  0.08536,    Adjusted R-squared:  0.07727 
## F-statistic: 10.55 on 1 and 113 DF,  p-value: 0.001533
tempmod <- lm(temp~year+I(year^2))
summary(tempmod)
## 
## Call:
## lm(formula = temp ~ year + I(year^2))
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.0412 -0.9538 -0.0624  0.9959  3.5820 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)
## (Intercept) -2.127e+02  3.837e+02  -0.554    0.580
## year         2.567e-01  3.962e-01   0.648    0.518
## I(year^2)   -6.307e-05  1.022e-04  -0.617    0.539
## 
## Residual standard error: 1.47 on 112 degrees of freedom
## Multiple R-squared:  0.08846,    Adjusted R-squared:  0.07218 
## F-statistic: 5.434 on 2 and 112 DF,  p-value: 0.005591
library(faraway)
data(divusa)
attach(divusa)
## The following object is masked from aatemp:
## 
##     year
## The following object is masked from airpass:
## 
##     year
birthmod <- lm(birth~year+I(year^2))
summary(birthmod)
## 
## Call:
## lm(formula = birth ~ year + I(year^2))
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -24.2952 -11.8939  -0.7406  12.5154  26.3901 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -5.428e+04  1.464e+04  -3.709 0.000400 ***
## year         5.604e+01  1.495e+01   3.748 0.000351 ***
## I(year^2)   -1.444e-02  3.818e-03  -3.782 0.000313 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 14.8 on 74 degrees of freedom
## Multiple R-squared:  0.4403, Adjusted R-squared:  0.4252 
## F-statistic: 29.11 on 2 and 74 DF,  p-value: 4.718e-10
residuals <- birthmod$residuals
plot(residuals,year)

library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
dwtest(birthmod)
## 
##  Durbin-Watson test
## 
## data:  birthmod
## DW = 0.07405, p-value < 2.2e-16
## alternative hypothesis: true autocorrelation is greater than 0