swiss.dt <- swiss
plot(Agriculture ~ Education, data = swiss.dt)
#Linear model
swiss.lm <- lm(Agriculture ~ Education, data = swiss.dt)

coef(swiss.lm)
## (Intercept)   Education 
##   67.243236   -1.510527
plot(Agriculture ~ Education, data = swiss.dt)
abline(coef(swiss.lm))

#Access fitted values:
fitted(swiss.lm)[1:5]
##   Courtelary     Delemont Franches-Mnt      Moutier   Neuveville 
##     49.11691     53.64849     59.69060     56.66954     44.58533
#Residuals 
residuals(swiss.lm)[1:5]
##   Courtelary     Delemont Franches-Mnt      Moutier   Neuveville 
##   -32.116908    -8.548490   -19.990599   -20.169545    -1.085326
summary(swiss.lm)
## 
## Call:
## lm(formula = Agriculture ~ Education, data = swiss.dt)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -42.927  -9.633   2.188  12.762  26.720 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  67.2432     3.9321   17.10  < 2e-16 ***
## Education    -1.5105     0.2707   -5.58  1.3e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 17.65 on 45 degrees of freedom
## Multiple R-squared:  0.409,  Adjusted R-squared:  0.3959 
## F-statistic: 31.14 on 1 and 45 DF,  p-value: 1.305e-06
confint(swiss.lm)
##                 2.5 %     97.5 %
## (Intercept) 59.323529 75.1629429
## Education   -2.055715 -0.9653397
#Graphing the confience and Prediction bands
library(HH)
## Loading required package: lattice
## Loading required package: grid
## Loading required package: latticeExtra
## Loading required package: RColorBrewer
## Loading required package: multcomp
## Loading required package: mvtnorm
## Loading required package: survival
## Loading required package: TH.data
## Loading required package: MASS
## 
## Attaching package: 'TH.data'
## The following object is masked from 'package:MASS':
## 
##     geyser
## Loading required package: gridExtra

ci.plot(swiss.lm)

#Hypothesis testing
summary(swiss.lm)
## 
## Call:
## lm(formula = Agriculture ~ Education, data = swiss.dt)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -42.927  -9.633   2.188  12.762  26.720 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  67.2432     3.9321   17.10  < 2e-16 ***
## Education    -1.5105     0.2707   -5.58  1.3e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 17.65 on 45 degrees of freedom
## Multiple R-squared:  0.409,  Adjusted R-squared:  0.3959 
## F-statistic: 31.14 on 1 and 45 DF,  p-value: 1.305e-06
#Pr is sig less than .05

#Simple coefficient of determination
anova(swiss.lm)
## Analysis of Variance Table
## 
## Response: Agriculture
##           Df Sum Sq Mean Sq F value    Pr(>F)    
## Education  1   9704  9704.0  31.141 1.305e-06 ***
## Residuals 45  14023   311.6                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
swisSum<- summary(swiss.lm)
swisSum$r.squared
## [1] 0.4089891
#Residual Analysis

#Normality assumption
hist(residuals(swiss.lm))

library(RcmdrMisc)
## Loading required package: car
## Loading required package: carData
## 
## Attaching package: 'car'
## The following objects are masked from 'package:HH':
## 
##     logit, vif
## Loading required package: sandwich
qqPlot(swiss.lm)

## La Chauxdfnd ValdeTravers 
##           40           44
#Shapiro test: if p-value is less than .05 then normality assumption is upheld
shapiro.test(residuals(swiss.lm))
## 
##  Shapiro-Wilk normality test
## 
## data:  residuals(swiss.lm)
## W = 0.95732, p-value = 0.08418
#Constant Variance Assumption
plot(swiss.lm, which = 3)

#Data appears to somewhate of a slight fanning out of the residuals
library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
bptest(swiss.lm)
## 
##  studentized Breusch-Pagan test
## 
## data:  swiss.lm
## BP = 0.22039, df = 1, p-value = 0.6387
#Indepedence Assumption
#There is no obvious cyclcial wave pattern or structure, looking for random and no structure
plot(swiss.lm, which = 1)

#Durbin-watson test
dwtest(swiss.lm, alternative = "two.sided")
## 
##  Durbin-Watson test
## 
## data:  swiss.lm
## DW = 1.0969, p-value = 0.0007551
## alternative hypothesis: true autocorrelation is not 0