# Choosing data sets
data()
# First choice
Seatbelts
## DriversKilled drivers front rear kms PetrolPrice VanKilled law
## Jan 1969 107 1687 867 269 9059 0.10297181 12 0
## Feb 1969 97 1508 825 265 7685 0.10236300 6 0
## Mar 1969 102 1507 806 319 9963 0.10206249 12 0
## Apr 1969 87 1385 814 407 10955 0.10087330 8 0
## May 1969 119 1632 991 454 11823 0.10101967 10 0
## Jun 1969 106 1511 945 427 12391 0.10058119 13 0
## Jul 1969 110 1559 1004 522 13460 0.10377398 11 0
## Aug 1969 106 1630 1091 536 14055 0.10407640 6 0
## Sep 1969 107 1579 958 405 12106 0.10377398 10 0
## Oct 1969 134 1653 850 437 11372 0.10302640 16 0
## Nov 1969 147 2152 1109 434 9834 0.10273011 13 0
## Dec 1969 180 2148 1113 437 9267 0.10199719 14 0
## Jan 1970 125 1752 925 316 9130 0.10127456 14 0
## Feb 1970 134 1765 903 311 8933 0.10070398 6 0
## Mar 1970 110 1717 1006 351 11000 0.10013961 8 0
## Apr 1970 102 1558 892 362 10733 0.09862110 11 0
## May 1970 103 1575 990 486 12912 0.09834929 7 0
## Jun 1970 111 1520 866 429 12926 0.09808018 13 0
## Jul 1970 120 1805 1095 551 13990 0.09727921 13 0
## Aug 1970 129 1800 1204 646 14926 0.09741062 11 0
## Sep 1970 122 1719 1029 456 12900 0.09742524 11 0
## Oct 1970 183 2008 1147 475 12034 0.09638063 14 0
## Nov 1970 169 2242 1171 456 10643 0.09573896 16 0
## Dec 1970 190 2478 1299 468 10742 0.09510631 14 0
## Jan 1971 134 2030 944 356 10266 0.09673597 17 0
## Feb 1971 108 1655 874 271 10281 0.09610922 16 0
## Mar 1971 104 1693 840 354 11527 0.09536725 15 0
## Apr 1971 117 1623 893 427 12281 0.09470959 13 0
## May 1971 157 1805 1007 465 13587 0.09411762 13 0
## Jun 1971 148 1746 973 440 13049 0.09353215 15 0
## Jul 1971 130 1795 1097 539 16055 0.09295405 12 0
## Aug 1971 140 1926 1194 646 15220 0.09283979 6 0
## Sep 1971 136 1619 988 457 13824 0.09272474 9 0
## Oct 1971 140 1992 1077 446 12729 0.09226965 13 0
## Nov 1971 187 2233 1045 402 11467 0.09170669 14 0
## Dec 1971 150 2192 1115 441 11351 0.09126207 15 0
## Jan 1972 159 2080 1005 359 10803 0.09071160 14 0
## Feb 1972 143 1768 857 334 10548 0.09027633 3 0
## Mar 1972 114 1835 879 312 12368 0.08995192 12 0
## Apr 1972 127 1569 887 427 13311 0.08909964 13 0
## May 1972 159 1976 1075 434 13885 0.08867919 12 0
## Jun 1972 156 1853 1121 486 14088 0.08815929 8 0
## Jul 1972 138 1965 1190 569 16932 0.08890206 8 0
## Aug 1972 120 1689 1058 523 16164 0.08818133 15 0
## Sep 1972 117 1778 939 418 14883 0.08894029 8 0
## Oct 1972 170 1976 1074 452 13532 0.08772661 5 0
## Nov 1972 168 2397 1089 462 12220 0.08742885 17 0
## Dec 1972 198 2654 1208 497 12025 0.08703543 14 0
## Jan 1973 144 2097 903 354 11692 0.08644992 13 0
## Feb 1973 146 1963 916 347 11081 0.08587264 5 0
## Mar 1973 109 1677 787 276 13745 0.08539822 8 0
## Apr 1973 131 1941 1114 472 14382 0.08382198 5 0
## May 1973 151 2003 1014 487 14391 0.08459078 12 0
## Jun 1973 140 1813 1022 505 15597 0.08413690 11 0
## Jul 1973 153 2012 1114 619 16834 0.08377841 13 0
## Aug 1973 140 1912 1132 640 17282 0.08351074 15 0
## Sep 1973 161 2084 1111 559 15779 0.08280639 11 0
## Oct 1973 168 2080 1008 453 13946 0.08117889 11 0
## Nov 1973 152 2118 916 418 12701 0.08285361 10 0
## Dec 1973 136 2150 992 419 10431 0.09419012 13 0
## Jan 1974 113 1608 731 262 11616 0.09239984 8 0
## Feb 1974 100 1503 665 299 10808 0.10816148 6 0
## Mar 1974 103 1548 724 303 12421 0.10721169 8 0
## Apr 1974 103 1382 744 401 13605 0.11404297 14 0
## May 1974 121 1731 910 413 14455 0.11245412 12 0
## Jun 1974 134 1798 883 426 15019 0.11131625 14 0
## Jul 1974 133 1779 900 516 15662 0.11030125 13 0
## Aug 1974 129 1887 1057 600 16745 0.10819718 9 0
## Sep 1974 144 2004 1076 459 14717 0.10702744 4 0
## Oct 1974 154 2077 919 443 13756 0.10494698 13 0
## Nov 1974 156 2092 920 412 12531 0.11935775 6 0
## Dec 1974 163 2051 953 400 12568 0.11762190 15 0
## Jan 1975 122 1577 664 278 11249 0.13302742 12 0
## Feb 1975 92 1356 607 302 11096 0.13084524 16 0
## Mar 1975 117 1652 777 381 12637 0.12831848 7 0
## Apr 1975 95 1382 633 279 13018 0.12354745 12 0
## May 1975 96 1519 791 442 15005 0.11858681 10 0
## Jun 1975 108 1421 790 409 15235 0.11633748 9 0
## Jul 1975 108 1442 803 416 15552 0.11516148 9 0
## Aug 1975 106 1543 884 511 16905 0.11450120 6 0
## Sep 1975 140 1656 769 393 14776 0.11352298 7 0
## Oct 1975 114 1561 732 345 14104 0.11193018 13 0
## Nov 1975 158 1905 859 391 12854 0.11061053 14 0
## Dec 1975 161 2199 994 470 12956 0.11527439 13 0
## Jan 1976 102 1473 704 266 12177 0.11379349 14 0
## Feb 1976 127 1655 684 312 11918 0.11234958 11 0
## Mar 1976 125 1407 671 300 13517 0.11175347 11 0
## Apr 1976 101 1395 643 373 14417 0.10964252 10 0
## May 1976 97 1530 771 412 15911 0.10844090 4 0
## Jun 1976 112 1309 644 322 15589 0.10788494 8 0
## Jul 1976 112 1526 828 458 16543 0.10908477 9 0
## Aug 1976 113 1327 748 427 17925 0.10757145 10 0
## Sep 1976 108 1627 767 346 15406 0.10616402 10 0
## Oct 1976 128 1748 825 421 14601 0.10630000 5 0
## Nov 1976 154 1958 810 344 13107 0.10482531 13 0
## Dec 1976 162 2274 986 370 12268 0.10345175 12 0
## Jan 1977 112 1648 714 291 11972 0.10144992 10 0
## Feb 1977 79 1401 567 224 12028 0.10040232 9 0
## Mar 1977 82 1411 616 266 14033 0.09886203 7 0
## Apr 1977 127 1403 678 338 14244 0.10249615 5 0
## May 1977 108 1394 742 298 15287 0.10302743 10 0
## Jun 1977 110 1520 840 386 16954 0.10217891 5 0
## Jul 1977 123 1528 888 479 17361 0.09983664 6 0
## Aug 1977 103 1643 852 473 17694 0.09263669 8 0
## Sep 1977 97 1515 774 332 16222 0.09181496 6 0
## Oct 1977 140 1685 831 391 14969 0.09072430 12 0
## Nov 1977 165 2000 889 370 13624 0.09002121 15 0
## Dec 1977 183 2215 1046 431 13842 0.08933071 7 0
## Jan 1978 148 1956 889 366 12387 0.08844273 14 0
## Feb 1978 111 1462 626 250 11608 0.08835257 4 0
## Mar 1978 116 1563 808 355 15021 0.08675736 10 0
## Apr 1978 115 1459 746 304 14834 0.08499524 8 0
## May 1978 100 1446 754 379 16565 0.08456794 7 0
## Jun 1978 106 1622 865 440 16882 0.08443190 11 0
## Jul 1978 134 1657 980 500 18012 0.08435088 3 0
## Aug 1978 125 1638 959 511 18855 0.08360098 5 0
## Sep 1978 117 1643 856 384 17243 0.08341726 11 0
## Oct 1978 122 1683 798 366 16045 0.08274514 10 0
## Nov 1978 153 2050 942 432 14745 0.08523527 10 0
## Dec 1978 178 2262 1010 390 13726 0.08477030 7 0
## Jan 1979 114 1813 796 306 11196 0.08445892 10 0
## Feb 1979 94 1445 643 232 12105 0.08535212 11 0
## Mar 1979 128 1762 794 342 14723 0.08755921 9 0
## Apr 1979 119 1461 750 329 15582 0.09038292 7 0
## May 1979 111 1556 809 394 16863 0.09078329 8 0
## Jun 1979 110 1431 716 355 16758 0.10874278 13 0
## Jul 1979 114 1427 851 385 17434 0.11414223 8 0
## Aug 1979 118 1554 931 463 18359 0.11299293 5 0
## Sep 1979 115 1645 834 453 17189 0.11132071 8 0
## Oct 1979 132 1653 762 373 16909 0.10912623 7 0
## Nov 1979 153 2016 880 401 15380 0.10769846 12 0
## Dec 1979 171 2207 1077 466 15161 0.10760157 10 0
## Jan 1980 115 1665 748 306 14027 0.10377502 7 0
## Feb 1980 95 1361 593 263 14478 0.10711417 4 0
## Mar 1980 92 1506 720 323 16155 0.10737477 10 0
## Apr 1980 100 1360 646 310 16585 0.11169537 4 0
## May 1980 95 1453 765 424 18117 0.11063818 8 0
## Jun 1980 114 1522 820 403 17552 0.11185521 8 0
## Jul 1980 102 1460 807 406 18299 0.10974234 7 0
## Aug 1980 104 1552 885 466 19361 0.10819393 10 0
## Sep 1980 132 1548 803 381 17924 0.10625536 8 0
## Oct 1980 136 1827 860 369 17872 0.10419303 14 0
## Nov 1980 117 1737 825 378 16058 0.10193397 8 0
## Dec 1980 137 1941 911 392 15746 0.10279382 9 0
## Jan 1981 111 1474 704 284 15226 0.10476034 8 0
## Feb 1981 106 1458 691 316 14932 0.10400254 6 0
## Mar 1981 98 1542 688 321 16846 0.11665552 7 0
## Apr 1981 84 1404 714 358 16854 0.11516148 6 0
## May 1981 94 1522 814 378 18146 0.11298954 5 0
## Jun 1981 105 1385 736 382 17559 0.11386064 4 0
## Jul 1981 123 1641 876 433 18655 0.11911808 5 0
## Aug 1981 109 1510 829 506 19453 0.12448999 10 0
## Sep 1981 130 1681 818 428 17923 0.12322295 7 0
## Oct 1981 153 1938 942 479 17915 0.12067793 10 0
## Nov 1981 134 1868 782 370 16496 0.12104898 12 0
## Dec 1981 99 1726 823 349 13544 0.11696857 7 0
## Jan 1982 115 1456 595 238 13601 0.11275026 4 0
## Feb 1982 104 1445 673 285 15667 0.10807931 5 0
## Mar 1982 131 1456 660 324 17358 0.10883852 6 0
## Apr 1982 108 1365 676 346 18112 0.11129177 4 0
## May 1982 103 1487 755 410 18581 0.11130401 4 0
## Jun 1982 115 1558 815 411 18759 0.11545436 8 0
## Jul 1982 122 1488 867 496 20668 0.11476830 8 0
## Aug 1982 122 1684 933 534 21040 0.11720743 3 0
## Sep 1982 125 1594 798 396 18993 0.11907640 7 0
## Oct 1982 137 1850 950 470 18668 0.11796586 12 0
## Nov 1982 138 1998 825 385 16768 0.11744913 2 0
## Dec 1982 152 2079 911 411 16551 0.11698846 7 0
## Jan 1983 120 1494 619 281 16231 0.11261054 8 0
## Feb 1983 95 1057 426 300 15511 0.11365702 3 1
## Mar 1983 100 1218 475 318 18308 0.11314445 2 1
## Apr 1983 89 1168 556 391 17793 0.11849553 6 1
## May 1983 82 1236 559 398 19205 0.11796940 3 1
## Jun 1983 89 1076 483 337 19162 0.11768661 7 1
## Jul 1983 60 1174 587 477 20997 0.12005924 6 1
## Aug 1983 84 1139 615 422 20705 0.11943775 8 1
## Sep 1983 113 1427 618 495 18759 0.11888127 8 1
## Oct 1983 126 1487 662 471 19240 0.11846236 4 1
## Nov 1983 122 1483 519 368 17504 0.11801660 3 1
## Dec 1983 118 1513 585 345 16591 0.11770662 5 1
## Jan 1984 92 1357 483 296 16224 0.11777609 5 1
## Feb 1984 86 1165 434 319 16670 0.11479699 3 1
## Mar 1984 81 1282 513 349 18539 0.11573525 4 1
## Apr 1984 84 1110 548 375 19759 0.11535626 3 1
## May 1984 87 1297 586 441 19584 0.11481536 6 1
## Jun 1984 90 1185 522 465 19976 0.11477748 6 1
## Jul 1984 79 1222 601 472 21486 0.11493598 7 1
## Aug 1984 96 1284 644 521 21626 0.11479699 5 1
## Sep 1984 122 1444 643 429 20195 0.11409316 7 1
## Oct 1984 120 1575 641 408 19928 0.11646552 7 1
## Nov 1984 137 1737 711 490 18564 0.11602611 4 1
## Dec 1984 154 1763 721 491 18149 0.11606673 7 1
# Second choice
rock
## area peri shape perm
## 1 4990 2791.900 0.0903296 6.3
## 2 7002 3892.600 0.1486220 6.3
## 3 7558 3930.660 0.1833120 6.3
## 4 7352 3869.320 0.1170630 6.3
## 5 7943 3948.540 0.1224170 17.1
## 6 7979 4010.150 0.1670450 17.1
## 7 9333 4345.750 0.1896510 17.1
## 8 8209 4344.750 0.1641270 17.1
## 9 8393 3682.040 0.2036540 119.0
## 10 6425 3098.650 0.1623940 119.0
## 11 9364 4480.050 0.1509440 119.0
## 12 8624 3986.240 0.1481410 119.0
## 13 10651 4036.540 0.2285950 82.4
## 14 8868 3518.040 0.2316230 82.4
## 15 9417 3999.370 0.1725670 82.4
## 16 8874 3629.070 0.1534810 82.4
## 17 10962 4608.660 0.2043140 58.6
## 18 10743 4787.620 0.2627270 58.6
## 19 11878 4864.220 0.2000710 58.6
## 20 9867 4479.410 0.1448100 58.6
## 21 7838 3428.740 0.1138520 142.0
## 22 11876 4353.140 0.2910290 142.0
## 23 12212 4697.650 0.2400770 142.0
## 24 8233 3518.440 0.1618650 142.0
## 25 6360 1977.390 0.2808870 740.0
## 26 4193 1379.350 0.1794550 740.0
## 27 7416 1916.240 0.1918020 740.0
## 28 5246 1585.420 0.1330830 740.0
## 29 6509 1851.210 0.2252140 890.0
## 30 4895 1239.660 0.3412730 890.0
## 31 6775 1728.140 0.3116460 890.0
## 32 7894 1461.060 0.2760160 890.0
## 33 5980 1426.760 0.1976530 950.0
## 34 5318 990.388 0.3266350 950.0
## 35 7392 1350.760 0.1541920 950.0
## 36 7894 1461.060 0.2760160 950.0
## 37 3469 1376.700 0.1769690 100.0
## 38 1468 476.322 0.4387120 100.0
## 39 3524 1189.460 0.1635860 100.0
## 40 5267 1644.960 0.2538320 100.0
## 41 5048 941.543 0.3286410 1300.0
## 42 1016 308.642 0.2300810 1300.0
## 43 5605 1145.690 0.4641250 1300.0
## 44 8793 2280.490 0.4204770 1300.0
## 45 3475 1174.110 0.2007440 580.0
## 46 1651 597.808 0.2626510 580.0
## 47 5514 1455.880 0.1824530 580.0
## 48 9718 1485.580 0.2004470 580.0
Seatbelts
This data set is a time series that provides monthly records of drivers who are either killed or severely injured during an car accidents from January 1969 to December 1984. In 1963, wearing seat belts were made mandatory while driving in Britain.
# Activate the neccessary packaged needed for later
library(TSstudio)
# Basic information about the data set
ts_info(Seatbelts)
## The Seatbelts series is a mts object with 8 variables and 192 observations
## Frequency: 12
## Start time: 1969 1
## End time: 1984 12
# Variables included in the data set
colnames(Seatbelts)
## [1] "DriversKilled" "drivers" "front" "rear"
## [5] "kms" "PetrolPrice" "VanKilled" "law"DriversKilled = Car drivers killed
Drivers = Total number of drivers involved in car accidents
Front = front-seat passengers killed or seriously injured.
rear = rear-seat passengers killed or seriously injured
kms = distance driven
PetrolPrice = Price of gas in that given year
Vankilled = number of van (‘light goods vehicle’) drivers killed
law = (0/1) was the law in effect that month?
#Graph using TSstudio package
ts_plot(Seatbelts)
Conclusion: From the the way the data set is arranged as well as the graph, we could tell that the data is time series.
rock
This data provides us with measurements on 48 rock samples from one petroleum reservoir.
colnames(rock)
## [1] "area" "peri" "shape" "perm"Area = area of pores space, in pixels out of 256 by 256
peri = perimeter in pixels
Shape = perimeter/sqrt(area)
Perm = Permeability in milli-Darcies
# Graph
plot(rock)
Conclusion: From the the way the data set is arranged as well as the graph, we could tell that the data is cross-sectional.
Q1. In your own words, what is covariance? What is Variance?
Covariance measures the linear relationship between two random variables. However, unlike correlation which provides us with both the direction and the strength of linear relationship, covariance only enables us to understand the direction of the linear relationship between two variables.
Variance, in statistics, is the measure of how far are all the data points in a data set from its mean.
Q2. Why would dividing the covariance of y and x by the variance of x give you the slope coefficient from a simple linear regression (one x variable only)?
Q3. Show Q2 in R.
```r
mtcars
```
```
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
```
```r
coefficients(lm(data = mtcars,
formula = hp ~ mpg))
```
```
## (Intercept) mpg
## 324.082314 -8.829731
```