library(faraway)
data(longley)
? longley

#Q1 (refer Ex0404)
#(a)
options(digits=3)
cor(longley, use = "everything", method = "pearson")
##              GNP.deflator   GNP Unemployed Armed.Forces Population  Year
## GNP.deflator        1.000 0.992      0.621        0.465      0.979 0.991
## GNP                 0.992 1.000      0.604        0.446      0.991 0.995
## Unemployed          0.621 0.604      1.000       -0.177      0.687 0.668
## Armed.Forces        0.465 0.446     -0.177        1.000      0.364 0.417
## Population          0.979 0.991      0.687        0.364      1.000 0.994
## Year                0.991 0.995      0.668        0.417      0.994 1.000
## Employed            0.971 0.984      0.502        0.457      0.960 0.971
##              Employed
## GNP.deflator    0.971
## GNP             0.984
## Unemployed      0.502
## Armed.Forces    0.457
## Population      0.960
## Year            0.971
## Employed        1.000
#(b) either command can be used
plot(longley) 
pairs(longley)

#(c) (refer pg4)
fit = lm(Employed ~ GNP.deflator+GNP+Unemployed+Armed.Forces+Year+Population, data = longley)
summary(fit)
## 
## Call:
## lm(formula = Employed ~ GNP.deflator + GNP + Unemployed + Armed.Forces + 
##     Year + Population, data = longley)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.4101 -0.1577 -0.0282  0.1016  0.4554 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -3.48e+03   8.90e+02   -3.91  0.00356 ** 
## GNP.deflator  1.51e-02   8.49e-02    0.18  0.86314    
## GNP          -3.58e-02   3.35e-02   -1.07  0.31268    
## Unemployed   -2.02e-02   4.88e-03   -4.14  0.00254 ** 
## Armed.Forces -1.03e-02   2.14e-03   -4.82  0.00094 ***
## Year          1.83e+00   4.55e-01    4.02  0.00304 ** 
## Population   -5.11e-02   2.26e-01   -0.23  0.82621    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.305 on 9 degrees of freedom
## Multiple R-squared:  0.995,  Adjusted R-squared:  0.992 
## F-statistic:  330 on 6 and 9 DF,  p-value: 4.98e-10
vif(fit)
## GNP.deflator          GNP   Unemployed Armed.Forces         Year 
##       135.53      1788.51        33.62         3.59       758.98 
##   Population 
##       399.15
#(d) (refer pg6)
fit2 = lm(Population ~ GNP.deflator+GNP+Unemployed+Armed.Forces+Year, data = longley)
fit3 = lm(Employed ~ GNP.deflator+GNP+Unemployed+Armed.Forces+Year, data = longley)
cor(fit2$residuals,fit3$residuals)
## [1] -0.0751
plot(fit2$residuals,fit3$residuals)
abline(h=0,lty=2)
abline(v=0,lty=2)
abline(lm(fit3$residuals ~ fit2$residuals))

#(e) (refer pg6)
fit4 = lm(Employed ~ Unemployed+Armed.Forces+Year, data = longley)
summary(fit4)
## 
## Call:
## lm(formula = Employed ~ Unemployed + Armed.Forces + Year, data = longley)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.5729 -0.1199  0.0409  0.1398  0.7530 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -1.80e+03   6.86e+01  -26.18  5.9e-12 ***
## Unemployed   -1.47e-02   1.67e-03   -8.79  1.4e-06 ***
## Armed.Forces -7.72e-03   1.84e-03   -4.20   0.0012 ** 
## Year          9.56e-01   3.55e-02   26.92  4.2e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.332 on 12 degrees of freedom
## Multiple R-squared:  0.993,  Adjusted R-squared:  0.991 
## F-statistic:  555 on 3 and 12 DF,  p-value: 3.92e-13
vif(fit4)
##   Unemployed Armed.Forces         Year 
##         3.32         2.22         3.89
#(f) (refer HW8i)
anova(fit,fit4)
## Analysis of Variance Table
## 
## Model 1: Employed ~ GNP.deflator + GNP + Unemployed + Armed.Forces + Year + 
##     Population
## Model 2: Employed ~ Unemployed + Armed.Forces + Year
##   Res.Df   RSS Df Sum of Sq    F Pr(>F)
## 1      9 0.836                         
## 2     12 1.323 -3    -0.487 1.75   0.23