#Linear model
plot(data = swiss, Fertility ~ Agriculture)

plot(data = swiss, Fertility ~ Education)

plot(data = swiss, Fertility ~ Infant.Mortality)

plot(data = swiss, Fertility ~ Examination)

plot(data = swiss, Fertility ~ Catholic)

swiss$Religious <- 0
swiss$Religious[swiss$Catholic > 50 ] <- 1
swiss.lm <- lm(data = swiss, Fertility ~ Examination + Agriculture + Infant.Mortality + Education + Religious + Catholic)
summary(swiss.lm)
## 
## Call:
## lm(formula = Fertility ~ Examination + Agriculture + Infant.Mortality + 
##     Education + Religious + Catholic, data = swiss)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -13.4636  -5.7845   0.7442   4.6055  13.8855 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       67.17073   10.22885   6.567 7.56e-08 ***
## Examination       -0.41096    0.25216  -1.630  0.11100    
## Agriculture       -0.16615    0.06722  -2.472  0.01780 *  
## Infant.Mortality   1.08950    0.36473   2.987  0.00479 ** 
## Education         -0.78911    0.17871  -4.416 7.46e-05 ***
## Religious        -17.39504    7.84206  -2.218  0.03228 *  
## Catholic           0.28591    0.08861   3.227  0.00250 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.846 on 40 degrees of freedom
## Multiple R-squared:  0.7389, Adjusted R-squared:  0.6997 
## F-statistic: 18.86 on 6 and 40 DF,  p-value: 2.837e-10
predict(swiss.lm)
##   Courtelary     Delemont Franches-Mnt      Moutier   Neuveville 
##     75.74695     81.15795     85.89096     82.42289     65.03913 
##   Porrentruy        Broye        Glane      Gruyere       Sarine 
##     89.56363     78.27161     81.35180     81.26881     78.14207 
##      Veveyse        Aigle      Aubonne     Avenches     Cossonay 
##     83.45709     59.18254     66.13670     65.80595     63.84963 
##    Echallens     Grandson     Lausanne    La Vallee       Lavaux 
##     76.14898     70.95583     56.63764     48.50461     62.73337 
##       Morges       Moudon        Nyone         Orbe         Oron 
##     61.40893     75.59205     62.72649     63.09821     73.18571 
##      Payerne Paysd'enhaut        Rolle        Vevey      Yverdon 
##     72.87634     72.12988     62.56830     65.49922     72.72669 
##      Conthey    Entremont       Herens     Martigwy      Monthey 
##     77.65166     78.12960     79.76767     76.54637     83.83833 
##   St Maurice       Sierre         Sion       Boudry La Chauxdfnd 
##     74.07947     78.31447     71.09521     64.35987     71.57075 
##     Le Locle    Neuchatel   Val de Ruz ValdeTravers V. De Geneve 
##     68.89589     54.50747     72.44618     71.98427     41.65953 
##  Rive Droite  Rive Gauche 
##     46.82083     50.95248
#GLM
swiss.glm <- glm(data = swiss, Fertility ~ Examination + Agriculture + Infant.Mortality + Education + Religious + Catholic)
summary(swiss.glm)
## 
## Call:
## glm(formula = Fertility ~ Examination + Agriculture + Infant.Mortality + 
##     Education + Religious + Catholic, data = swiss)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -13.4636   -5.7845    0.7442    4.6055   13.8855  
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       67.17073   10.22885   6.567 7.56e-08 ***
## Examination       -0.41096    0.25216  -1.630  0.11100    
## Agriculture       -0.16615    0.06722  -2.472  0.01780 *  
## Infant.Mortality   1.08950    0.36473   2.987  0.00479 ** 
## Education         -0.78911    0.17871  -4.416 7.46e-05 ***
## Religious        -17.39504    7.84206  -2.218  0.03228 *  
## Catholic           0.28591    0.08861   3.227  0.00250 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 46.86174)
## 
##     Null deviance: 7178.0  on 46  degrees of freedom
## Residual deviance: 1874.5  on 40  degrees of freedom
## AIC: 322.62
## 
## Number of Fisher Scoring iterations: 2
predict.glm(swiss.glm)
##   Courtelary     Delemont Franches-Mnt      Moutier   Neuveville 
##     75.74695     81.15795     85.89096     82.42289     65.03913 
##   Porrentruy        Broye        Glane      Gruyere       Sarine 
##     89.56363     78.27161     81.35180     81.26881     78.14207 
##      Veveyse        Aigle      Aubonne     Avenches     Cossonay 
##     83.45709     59.18254     66.13670     65.80595     63.84963 
##    Echallens     Grandson     Lausanne    La Vallee       Lavaux 
##     76.14898     70.95583     56.63764     48.50461     62.73337 
##       Morges       Moudon        Nyone         Orbe         Oron 
##     61.40893     75.59205     62.72649     63.09821     73.18571 
##      Payerne Paysd'enhaut        Rolle        Vevey      Yverdon 
##     72.87634     72.12988     62.56830     65.49922     72.72669 
##      Conthey    Entremont       Herens     Martigwy      Monthey 
##     77.65166     78.12960     79.76767     76.54637     83.83833 
##   St Maurice       Sierre         Sion       Boudry La Chauxdfnd 
##     74.07947     78.31447     71.09521     64.35987     71.57075 
##     Le Locle    Neuchatel   Val de Ruz ValdeTravers V. De Geneve 
##     68.89589     54.50747     72.44618     71.98427     41.65953 
##  Rive Droite  Rive Gauche 
##     46.82083     50.95248
swiss$Fertility
##  [1] 80.2 83.1 92.5 85.8 76.9 76.1 83.8 92.4 82.4 82.9 87.1 64.1 66.9 68.9
## [15] 61.7 68.3 71.7 55.7 54.3 65.1 65.5 65.0 56.6 57.4 72.5 74.2 72.0 60.5
## [29] 58.3 65.4 75.5 69.3 77.3 70.5 79.4 65.0 92.2 79.3 70.4 65.7 72.7 64.4
## [43] 77.6 67.6 35.0 44.7 42.8