library(datasets)
library(psych)
library(kableExtra)
require(graphics)

## Swiss Data
swiss
##              Fertility Agriculture Examination Education Catholic
## Courtelary        80.2        17.0          15        12     9.96
## Delemont          83.1        45.1           6         9    84.84
## Franches-Mnt      92.5        39.7           5         5    93.40
## Moutier           85.8        36.5          12         7    33.77
## Neuveville        76.9        43.5          17        15     5.16
## Porrentruy        76.1        35.3           9         7    90.57
## Broye             83.8        70.2          16         7    92.85
## Glane             92.4        67.8          14         8    97.16
## Gruyere           82.4        53.3          12         7    97.67
## Sarine            82.9        45.2          16        13    91.38
## Veveyse           87.1        64.5          14         6    98.61
## Aigle             64.1        62.0          21        12     8.52
## Aubonne           66.9        67.5          14         7     2.27
## Avenches          68.9        60.7          19        12     4.43
## Cossonay          61.7        69.3          22         5     2.82
## Echallens         68.3        72.6          18         2    24.20
## Grandson          71.7        34.0          17         8     3.30
## Lausanne          55.7        19.4          26        28    12.11
## La Vallee         54.3        15.2          31        20     2.15
## Lavaux            65.1        73.0          19         9     2.84
## Morges            65.5        59.8          22        10     5.23
## Moudon            65.0        55.1          14         3     4.52
## Nyone             56.6        50.9          22        12    15.14
## Orbe              57.4        54.1          20         6     4.20
## Oron              72.5        71.2          12         1     2.40
## Payerne           74.2        58.1          14         8     5.23
## Paysd'enhaut      72.0        63.5           6         3     2.56
## Rolle             60.5        60.8          16        10     7.72
## Vevey             58.3        26.8          25        19    18.46
## Yverdon           65.4        49.5          15         8     6.10
## Conthey           75.5        85.9           3         2    99.71
## Entremont         69.3        84.9           7         6    99.68
## Herens            77.3        89.7           5         2   100.00
## Martigwy          70.5        78.2          12         6    98.96
## Monthey           79.4        64.9           7         3    98.22
## St Maurice        65.0        75.9           9         9    99.06
## Sierre            92.2        84.6           3         3    99.46
## Sion              79.3        63.1          13        13    96.83
## Boudry            70.4        38.4          26        12     5.62
## La Chauxdfnd      65.7         7.7          29        11    13.79
## Le Locle          72.7        16.7          22        13    11.22
## Neuchatel         64.4        17.6          35        32    16.92
## Val de Ruz        77.6        37.6          15         7     4.97
## ValdeTravers      67.6        18.7          25         7     8.65
## V. De Geneve      35.0         1.2          37        53    42.34
## Rive Droite       44.7        46.6          16        29    50.43
## Rive Gauche       42.8        27.7          22        29    58.33
##              Infant.Mortality
## Courtelary               22.2
## Delemont                 22.2
## Franches-Mnt             20.2
## Moutier                  20.3
## Neuveville               20.6
## Porrentruy               26.6
## Broye                    23.6
## Glane                    24.9
## Gruyere                  21.0
## Sarine                   24.4
## Veveyse                  24.5
## Aigle                    16.5
## Aubonne                  19.1
## Avenches                 22.7
## Cossonay                 18.7
## Echallens                21.2
## Grandson                 20.0
## Lausanne                 20.2
## La Vallee                10.8
## Lavaux                   20.0
## Morges                   18.0
## Moudon                   22.4
## Nyone                    16.7
## Orbe                     15.3
## Oron                     21.0
## Payerne                  23.8
## Paysd'enhaut             18.0
## Rolle                    16.3
## Vevey                    20.9
## Yverdon                  22.5
## Conthey                  15.1
## Entremont                19.8
## Herens                   18.3
## Martigwy                 19.4
## Monthey                  20.2
## St Maurice               17.8
## Sierre                   16.3
## Sion                     18.1
## Boudry                   20.3
## La Chauxdfnd             20.5
## Le Locle                 18.9
## Neuchatel                23.0
## Val de Ruz               20.0
## ValdeTravers             19.5
## V. De Geneve             18.0
## Rive Droite              18.2
## Rive Gauche              19.3
str(swiss)
## 'data.frame':    47 obs. of  6 variables:
##  $ Fertility       : num  80.2 83.1 92.5 85.8 76.9 76.1 83.8 92.4 82.4 82.9 ...
##  $ Agriculture     : num  17 45.1 39.7 36.5 43.5 35.3 70.2 67.8 53.3 45.2 ...
##  $ Examination     : int  15 6 5 12 17 9 16 14 12 16 ...
##  $ Education       : int  12 9 5 7 15 7 7 8 7 13 ...
##  $ Catholic        : num  9.96 84.84 93.4 33.77 5.16 ...
##  $ Infant.Mortality: num  22.2 22.2 20.2 20.3 20.6 26.6 23.6 24.9 21 24.4 ...
describe(swiss)
##                  vars  n  mean    sd median trimmed   mad   min   max range
## Fertility           1 47 70.14 12.49  70.40   70.66 10.23 35.00  92.5 57.50
## Agriculture         2 47 50.66 22.71  54.10   51.16 23.87  1.20  89.7 88.50
## Examination         3 47 16.49  7.98  16.00   16.08  7.41  3.00  37.0 34.00
## Education           4 47 10.98  9.62   8.00    9.38  5.93  1.00  53.0 52.00
## Catholic            5 47 41.14 41.70  15.14   39.12 18.65  2.15 100.0 97.85
## Infant.Mortality    6 47 19.94  2.91  20.00   19.98  2.82 10.80  26.6 15.80
##                   skew kurtosis   se
## Fertility        -0.46     0.26 1.82
## Agriculture      -0.32    -0.89 3.31
## Examination       0.45    -0.14 1.16
## Education         2.27     6.14 1.40
## Catholic          0.48    -1.67 6.08
## Infant.Mortality -0.33     0.78 0.42
mydescribe=round(describe(swiss),3) 
mydescribe%>%kbl()%>%kable_classic(html_font = "Courier New")
vars n mean sd median trimmed mad min max range skew kurtosis se
Fertility 1 47 70.143 12.492 70.40 70.659 10.230 35.00 92.5 57.50 -0.456 0.260 1.822
Agriculture 2 47 50.660 22.711 54.10 51.156 23.870 1.20 89.7 88.50 -0.320 -0.886 3.313
Examination 3 47 16.489 7.978 16.00 16.077 7.413 3.00 37.0 34.00 0.446 -0.137 1.164
Education 4 47 10.979 9.615 8.00 9.385 5.930 1.00 53.0 52.00 2.268 6.140 1.403
Catholic 5 47 41.144 41.705 15.14 39.116 18.651 2.15 100.0 97.85 0.479 -1.665 6.083
Infant.Mortality 6 47 19.943 2.913 20.00 19.985 2.817 10.80 26.6 15.80 -0.331 0.777 0.425
## Histograms
#Fertility
hist(swiss[,1], main= "Fertility", xlab= "Fertility")

#Agriculture
hist(swiss[,2], main= "Agriculture", xlab= "Agriculture")

#Examination
hist(swiss[,3], main= "Examination", xlab= "Examination")

#Education
hist(swiss[,4], main= "Education", xlab= "Education")

#Catholic
hist(swiss[,5], main= "Catholic", xlab= "Catholic")

#Infant Mortality
hist(swiss[,6], main= "Infant.Mortality", xlab= "Infant.Mortality")

## Creating 95% Confidence Intervals
#Fertility
t.test(swiss[,1])
## 
##  One Sample t-test
## 
## data:  swiss[, 1]
## t = 38.495, df = 46, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  66.47485 73.81025
## sample estimates:
## mean of x 
##  70.14255
#Agriculture
t.test(swiss[,2])
## 
##  One Sample t-test
## 
## data:  swiss[, 2]
## t = 15.292, df = 46, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  43.99131 57.32784
## sample estimates:
## mean of x 
##  50.65957
#Examination
t.test(swiss[,3])
## 
##  One Sample t-test
## 
## data:  swiss[, 3]
## t = 14.17, df = 46, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  14.14697 18.83176
## sample estimates:
## mean of x 
##  16.48936
#Education
t.test(swiss[,4])
## 
##  One Sample t-test
## 
## data:  swiss[, 4]
## t = 7.8277, df = 46, p-value = 5.314e-10
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##   8.155534 13.801913
## sample estimates:
## mean of x 
##  10.97872
#Catholic
t.test(swiss[,5])
## 
##  One Sample t-test
## 
## data:  swiss[, 5]
## t = 6.7634, df = 46, p-value = 2.064e-08
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  28.89883 53.38883
## sample estimates:
## mean of x 
##  41.14383
#Infant Mortality
t.test(swiss[,6])
## 
##  One Sample t-test
## 
## data:  swiss[, 6]
## t = 46.939, df = 46, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  19.08735 20.79775
## sample estimates:
## mean of x 
##  19.94255
## Creating 99% Confidence Intervals
#Fertility
t.test(swiss[,1], conf.level=0.99)
## 
##  One Sample t-test
## 
## data:  swiss[, 1]
## t = 38.495, df = 46, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 99 percent confidence interval:
##  65.24654 75.03856
## sample estimates:
## mean of x 
##  70.14255
#Agriculture
t.test(swiss[,2], conf.level=0.99)
## 
##  One Sample t-test
## 
## data:  swiss[, 2]
## t = 15.292, df = 46, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 99 percent confidence interval:
##  41.75811 59.56104
## sample estimates:
## mean of x 
##  50.65957
#Examination
t.test(swiss[,3], conf.level=0.99)
## 
##  One Sample t-test
## 
## data:  swiss[, 3]
## t = 14.17, df = 46, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 99 percent confidence interval:
##  13.36250 19.61622
## sample estimates:
## mean of x 
##  16.48936
#Education
t.test(swiss[,4], conf.level=0.99)
## 
##  One Sample t-test
## 
## data:  swiss[, 4]
## t = 7.8277, df = 46, p-value = 5.314e-10
## alternative hypothesis: true mean is not equal to 0
## 99 percent confidence interval:
##   7.210049 14.747398
## sample estimates:
## mean of x 
##  10.97872
#Catholic
t.test(swiss[,5], conf.level=0.99)
## 
##  One Sample t-test
## 
## data:  swiss[, 5]
## t = 6.7634, df = 46, p-value = 2.064e-08
## alternative hypothesis: true mean is not equal to 0
## 99 percent confidence interval:
##  24.79798 57.48968
## sample estimates:
## mean of x 
##  41.14383
#Infant Mortality
t.test(swiss[,6], conf.level=0.99)
## 
##  One Sample t-test
## 
## data:  swiss[, 6]
## t = 46.939, df = 46, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 99 percent confidence interval:
##  18.80095 21.08416
## sample estimates:
## mean of x 
##  19.94255
## Creating 97% Confidence Intervals
#Fertility
t.test(swiss[,1], conf.level=0.97)
## 
##  One Sample t-test
## 
## data:  swiss[, 1]
## t = 38.495, df = 46, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 97 percent confidence interval:
##  66.06197 74.22314
## sample estimates:
## mean of x 
##  70.14255
#Agriculture
t.test(swiss[,2], conf.level=0.97)
## 
##  One Sample t-test
## 
## data:  swiss[, 2]
## t = 15.292, df = 46, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 97 percent confidence interval:
##  43.24064 58.07851
## sample estimates:
## mean of x 
##  50.65957
#Examination
t.test(swiss[,3], conf.level=0.97)
## 
##  One Sample t-test
## 
## data:  swiss[, 3]
## t = 14.17, df = 46, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 97 percent confidence interval:
##  13.88328 19.09545
## sample estimates:
## mean of x 
##  16.48936
#Education
t.test(swiss[,4], conf.level=0.97)
## 
##  One Sample t-test
## 
## data:  swiss[, 4]
## t = 7.8277, df = 46, p-value = 5.314e-10
## alternative hypothesis: true mean is not equal to 0
## 97 percent confidence interval:
##   7.837718 14.119729
## sample estimates:
## mean of x 
##  10.97872
#Catholic
t.test(swiss[,5], conf.level=0.97)
## 
##  One Sample t-test
## 
## data:  swiss[, 5]
## t = 6.7634, df = 46, p-value = 2.064e-08
## alternative hypothesis: true mean is not equal to 0
## 97 percent confidence interval:
##  27.52036 54.76730
## sample estimates:
## mean of x 
##  41.14383
#Infant Mortality
t.test(swiss[,6], conf.level=0.97)
## 
##  One Sample t-test
## 
## data:  swiss[, 6]
## t = 46.939, df = 46, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 97 percent confidence interval:
##  18.99108 20.89403
## sample estimates:
## mean of x 
##  19.94255