library("Hmisc")
## Loading required package: lattice
## Loading required package: survival
## Loading required package: Formula
## Loading required package: ggplot2
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
## 
##     format.pval, round.POSIXt, trunc.POSIXt, units
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:Hmisc':
## 
##     combine, src, summarize
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
getHdata(titanic)
head(titanic)
##   pclass survived                                            name     age
## 1    1st        1                    Allen, Miss Elisabeth Walton 29.0000
## 2    1st        0                     Allison, Miss Helen Loraine  2.0000
## 3    1st        0             Allison, Mr Hudson Joshua Creighton 30.0000
## 4    1st        0 Allison, Mrs Hudson J.C. (Bessie Waldo Daniels) 25.0000
## 5    1st        1                   Allison, Master Hudson Trevor  0.9167
## 6    1st        1                              Anderson, Mr Harry 47.0000
##      embarked                       home.dest room     ticket  boat    sex
## 1 Southampton                    St Louis, MO  B-5 24160 L221     2 female
## 2 Southampton Montreal, PQ / Chesterville, ON  C26             <NA> female
## 3 Southampton Montreal, PQ / Chesterville, ON  C26            (135)   male
## 4 Southampton Montreal, PQ / Chesterville, ON  C26             <NA> female
## 5 Southampton Montreal, PQ / Chesterville, ON  C22               11   male
## 6 Southampton                    New York, NY E-12                3   male
str(titanic$survived)
##  int [1:1313] 1 0 0 0 1 1 1 0 1 0 ...
titanic2 <- mutate(titanic)

Exercise 1

summary(titanic2$survived)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   0.000   0.000   0.342   1.000   1.000
ggplot(titanic2, aes(survived)) +
  geom_bar()

summary(titanic2$pclass)
## 1st 2nd 3rd 
## 322 280 711
ggplot(titanic2, aes(pclass)) +
  geom_bar()

summary(titanic2$age)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##  0.1667 21.0000 30.0000 31.1900 41.0000 71.0000     680
ggplot(titanic2, aes(age)) +
  geom_bar()
## Warning: Removed 680 rows containing non-finite values (stat_count).

summary(titanic2$sex)
## female   male 
##    463    850
ggplot(titanic2, aes(sex)) +
  geom_bar()

str(titanic2$pclass)
##  Factor w/ 3 levels "1st","2nd","3rd": 1 1 1 1 1 1 1 1 1 1 ...

Exercise 2 I believe that younger people are less likely to have their age documented because there is a lesser amount of records on them. The older you get the more you interact with state government and they will be more likely to record your age. Probably less likely to have age if in 3rd class as well because they are less likely to have gone to hospital and to doctors because they cannot afford it.

titanic2$age_mis <- factor(is.na(titanic2$age))
mosaic::tally(~sex|age_mis, data=titanic2)
##         age_mis
## sex      FALSE TRUE
##   female   243  220
##   male     390  460
mosaic::tally(~pclass|age_mis, data=titanic2)
##       age_mis
## pclass FALSE TRUE
##    1st   226   96
##    2nd   212   68
##    3rd   195  516
mosaic::tally(~sex|age_mis, data=titanic2)
##         age_mis
## sex      FALSE TRUE
##   female   243  220
##   male     390  460
mosaic::tally(~embarked|age_mis, data=titanic2)
##              age_mis
## embarked      FALSE TRUE
##   Cherbourg     133   70
##   Queenstown     31   14
##   Southampton   463  110
##   <NA>            6  486
mosaic::tally(~home.dest|age_mis, data=titanic2)
##                                                     age_mis
## home.dest                                            FALSE TRUE
##   ?Havana, Cuba                                          1    0
##   Aberdeen / Portland, OR                                2    0
##   Albany, NY                                             1    0
##   Altdorf, Switzerland                                   2    0
##   Amenia, ND                                             2    0
##   Antwerp, Belgium / Stanton, OH                         1    0
##   Argentina                                              1    0
##   Asarum, Sweden Brooklyn, NY                            1    0
##   Ascot, Berkshire / Rochester, NY                       1    0
##   Auburn, NY                                             1    0
##   Aughnacliff, Co Longford, Ireland New York, NY         0    1
##   Australia Fingal, ND                                   1    0
##   Austria                                                4    0
##   Austria Niagara Falls, NY                              0    1
##   Austria-Hungary                                        0    1
##   Austria-Hungary / Germantown, Philadelphia, PA         1    0
##   Ballydehob, Co Cork, Ireland New York, NY              1    0
##   Bangkok, Thailand / Roseville, IL                      3    0
##   Barcelona, Spain / Havana, Cuba                        0    2
##   Barre, Co Washington, VT                               1    0
##   Basel, Switzerland                                     2    0
##   Bath, England / Massachusetts                          1    0
##   Bayside, Queens, NY                                    1    0
##   Belfast                                                0    7
##   Belfast, NI                                            1    0
##   Belgium  Montreal, PQ                                  0    1
##   Belgium Detroit, MI                                    1    0
##   Belmont, MA                                            1    0
##   Bennington, VT                                         1    0
##   Bergen, Norway                                         1    0
##   Berne, Switzerland / Central City, IA                  0    1
##   Birkdale, England Cleveland, Ohio                      1    0
##   Bishopstoke, Hants / Fayette Valley, ID                3    0
##   Boston, MA                                             3    0
##   Bournemouth, England                                   1    0
##   Bournemouth, England Newark, NJ                        2    0
##   Bournmouth, England                                    2    2
##   Brennes, Norway New York                               1    0
##   Bridgerule, Devon                                      2    0
##   Bridgwater, Somerset, England                          0    1
##   Brighton, MA                                           0    1
##   Brighton, Sussex                                       1    0
##   Bristol, Avon / Jacksonville, FL                       1    0
##   Bristol, England / New Britain, CT                     2    0
##   Bristol, England Cleveland, OH                         1    0
##   Brockton, MA                                           1    0
##   Bromsgrove, England / Montreal, PQ                     2    0
##   Bronx, NY                                              2    0
##   Brookline, MA                                          1    0
##   Brooklyn, NY                                           6    1
##   Broomfield, Chelmsford, England                        1    0
##   Brunswick, ME                                          2    0
##   Bryn Mawr, PA                                          4    0
##   Bryn Mawr, PA, USA                                     1    0
##   Buenos Aires, Argentina / New Jersey, NJ               1    0
##   Buffalo, NY                                            1    0
##   Bulgaria Chicago, IL                                   5    0
##   Bulgaria Coon Rapids, IA                               0    1
##   Calgary, AB                                            2    0
##   Cambridge, MA                                          1    0
##   Cape Town, South Africa / Seattle, WA                  3    0
##   Catford, Kent / Detroit, MI                            1    0
##   Chelsea, London                                        1    0
##   Chicago, IL                                            3    1
##   Cincinatti, OH                                         1    0
##   Clevedon, England                                      0    1
##   Co Athlone, Ireland New York, NY                       2    0
##   Co Clare, Ireland Washington, DC                       0    1
##   Co Cork, Ireland Charlestown, MA                       1    0
##   Co Cork, Ireland Roxbury, MA                           1    0
##   Co Limerick, Ireland Sherbrooke, PQ                    1    0
##   Co Longford, Ireland New York, NY                      2    0
##   Co Sligo, Ireland Hartford, CT                         1    0
##   Co Sligo, Ireland New York, NY                         1    0
##   Cologne, Germany                                       1    0
##   Columbus, OH                                           0    1
##   Cooperstown, NY                                        1    0
##   Copenhagen, Denmark                                    1    0
##   Cornwall                                               2    0
##   Cornwall / Akron, OH                                   9    0
##   Cornwall / Camden, NJ                                  2    0
##   Cornwall / Clear Creek, CO                             2    0
##   Cornwall / Hancock, MI                                 2    0
##   Cornwall / Houghton, MI                                1    1
##   Cornwall / Spokane, WA                                 2    0
##   Cornwall, England Houghton, MI                         0    1
##   Croatia                                                3    1
##   Dagsas, Sweden Fower, MN                               1    0
##   Deephaven, MN / Cedar Rapids, IA                       2    0
##   Deer Lodge, MT                                         1    0
##   Denmark / New York, NY                                 1    0
##   Denmark Hill, Surrey / Chicago                         2    0
##   Denver, CO                                             1    0
##   Detroit, MI                                            2    0
##   Devon, England Wichita, KS                             4    0
##   Devonport, England                                     0    1
##   Dorchester, MA                                         1    0
##   Dorking, Surrey, England                               1    0
##   Dowagiac, MI                                           2    0
##   Duluth, MN                                             3    0
##   East Bridgewater, MA                                   1    0
##   East Orange, NJ                                        1    0
##   East Providence, RI                                    3    0
##   Effington Rut, SD                                      1    0
##   Elizabeth, NJ                                          2    0
##   Elkins Park, PA                                        3    0
##   Elmira, NY / Orange, NJ                                1    0
##   England                                                2    0
##   England / Bennington, VT                               2    0
##   England / Detroit, MI                                  1    0
##   England / Hartford, CT                                 1    0
##   England / Philadelphia, PA                             0    1
##   England / Sacramento, CA                               2    0
##   England / San Francisco, CA                            1    1
##   England Albion, NY                                     2    0
##   England Brooklyn, NY                                   3    0
##   England New York, NY                                   1    0
##   England Oglesby, IL                                    1    0
##   England Salt Lake City, Utah                           1    0
##   Finland / Minneapolis, MN                              1    0
##   Finland / Washington, DC                               1    0
##   Finland Sudbury, ON                                    1    0
##   Folkstone, Kent / New York, NY                         1    0
##   Fond du Lac, WI                                        2    0
##   Foresvik, Norway Portland, ND                          1    0
##   Frankfort, KY                                          1    0
##   Gallipolis, Ohio / ? Paris / New York                  0    1
##   Geneva, Switzerland / Radnor, PA                       2    0
##   Germantown, Philadelphia, PA                           1    0
##   Glasgow                                                0    1
##   Glasgow / Bangor, ME                                   0    1
##   Glen Ridge, NJ                                         1    0
##   Goteborg, Sweden / Rockford, IL                        1    0
##   Goteborg, Sweden Huntley, IL                           1    0
##   Greece                                                 2    0
##   Green Bay, WI                                          1    0
##   Greenport, NY                                          3    0
##   Greensburg, PA                                         1    0
##   Greenwich, CT                                          2    0
##   Guernsey                                               2    0
##   Guernsey / Elizabeth, NJ                               2    2
##   Guernsey / Montclair, NJ and/or Toledo, Ohio           1    0
##   Guernsey / Wilmington, DE                              1    0
##   Guernsey, England / Edgewood, RI                       0    1
##   Gunnislake, England / Butte, MT                        1    0
##   Guntur, India / Benton Harbour, MI                     4    0
##   Haddenfield, NJ                                        1    0
##   Halesworth, England                                    0    1
##   Halifax, NS                                            1    1
##   Hamilton, ON                                           1    0
##   Harrisburg, PA                                         0    1
##   Harrow, England                                        0    1
##   Harrow-on-the-Hill, Middlesex                          1    0
##   Hartford, CT                                           1    0
##   Hartford, Huntingdonshire                              1    0
##   Haverford, PA                                          4    0
##   Haverford, PA / Cooperstown, NY                        5    0
##   Helsinki, Finland Ashtabula, Ohio                      1    0
##   Hessle, Yorks                                          0    1
##   Holley, NY                                             0    1
##   Hong Kong New York, NY                                 2    1
##   Hornsey, England                                       0    1
##   Hudson, NY                                             3    0
##   Huntington, WV                                         2    0
##   Ilford, Essex / Winnipeg, MB                           3    0
##   Ilfracombe, Devon                                      2    0
##   Illinois, USA                                          1    0
##   India / Pittsburgh, PA                                 1    0
##   India / Rapid City, SD                                 0    1
##   Indianapolis, IN                                       2    0
##   Ireland                                                2    1
##   Ireland Brooklyn, NY                                   1    0
##   Ireland Chicago, IL                                    2    2
##   Ireland New York, NY                                   3    1
##   Ireland Philadelphia, PA                               1    0
##   Isle of Wight, England                                 1    0
##   Isleworth, England                                     0    2
##   Italy Philadelphia, PA                                 0    1
##   Jacksonville, FL                                       1    0
##   Janjgir, India / Pennsylvania                          1    0
##   Karberg, Sweden Jerome Junction, AZ                    1    0
##   Kilmacowen, Co Sligo, Ireland New York, NY             1    0
##   Kingston, Surrey                                       0    1
##   Kingwilliamstown, Co Cork, Ireland Glens Falls, NY     1    0
##   Kingwilliamstown, Co Cork, Ireland New York, NY        1    0
##   Kontiolahti, Finland / Detroit, MI                     1    0
##   Krakoryd, Sweden Bloomington, IL                       1    0
##   Krakudden, Sweden Moune, IL                            1    0
##   Lake Arthur, Chavez County, NM                         1    0
##   Lakewood, NJ                                           3    0
##   Lexington, MA                                          3    0
##   Lima, Peru                                             0    1
##   Little Onn Hall, Staffs                                1    1
##   Liverpool                                              1    1
##   Liverpool / Montreal, PQ                               2    0
##   Liverpool, England / Belfast                           0    1
##   Liverpool, England Bedford, OH                         0    2
##   London                                                 6    8
##   London  Vancouver, BC                                  1    0
##   London /  East Orange, NJ                              1    1
##   London / Birmingham                                    0    1
##   London / Chicago, IL                                   0    1
##   London / Fort Byron, NY                                1    0
##   London / Middlesex                                     0    1
##   London / Montreal, PQ                                  1    0
##   London / New York, NY                                  2    0
##   London / Paris                                         2    0
##   London / Staten Island, NY                             1    0
##   London / Winnipeg, MB                                  0    1
##   London Brooklyn, NY                                    1    0
##   London New York, NY                                    1    0
##   London Skanteales, NY                                  1    0
##   London, England                                        1    3
##   London, England / Marietta, Ohio and Milwaukee, WI     1    0
##   London, England Norfolk, VA                            2    0
##   Los Angeles, CA                                        2    1
##   Lower Clapton, Middlesex or Erdington, Birmingham      1    0
##   Lucca, Italy / California                              2    0
##   Lyndhurst, England                                     1    0
##   Madrid, Spain                                          2    0
##   Mamaroneck, NY                                         2    0
##   Manchester, England                                    0    2
##   Medeltorp, Sweden Chicago, IL                          1    0
##   Merrill, WI                                            1    0
##   Mexico City, Mexico                                    0    1
##   Middleburg Heights, OH                                 1    0
##   Milford, NH                                            0    1
##   Milwaukee, WI                                          3    0
##   Minneapolis, MN                                        4    0
##   Montevideo, Uruguay                                    1    2
##   Montreal, PQ                                           9    1
##   Montreal, PQ / Chesterville, ON                        4    0
##   Moscow / Bronx, NY                                     1    1
##   Mt Airy, Philadelphia, PA                              2    0
##   Myren, Sweden New York, NY                             1    0
##   New Britain, CT                                        1    0
##   New Forest, England                                    2    0
##   New York, NY                                          48   17
##   New York, NY /  Stamford CT                            2    0
##   New York, NY / Briarcliff Manor NY                     1    0
##   New York, NY / Greenwich CT                            1    0
##   New York, NY / Ithaca, NY                              2    0
##   New York, NY / Washington, DC                          1    0
##   Newark, NJ                                             3    0
##   Nice, France                                           3    0
##   Norrlot, Sweden Chicago, IL                            1    0
##   North Evington, England                                0    1
##   Norway Los Angeles, CA                                 1    0
##   Norwich / New York, NY                                 2    0
##   Omaha, NE                                              1    0
##   Oskarshamn, Sweden Minneapolis, MN                     1    0
##   Oslo, Norway Bayonne, NJ                               1    0
##   Oslo, Norway Cameron, WI                               1    0
##   Ottawa, ON                                             2    3
##   Paris                                                  0    3
##   Paris /  New York, NY                                  0    1
##   Paris / Haiti                                          4    0
##   Paris / Montreal, PQ                                   2    2
##   Paris, France                                          4    5
##   Paris, France / New York, NY                           1    1
##   Paris, France New York, NY                             1    0
##   Pennsylvania                                           1    0
##   Penzance, Cornwall / Akron, OH                         2    0
##   Perkins County, SD                                     1    0
##   Petworth, Sussex                                       0    1
##   Philadelphia, PA                                       7    1
##   Plymouth, Devon / Detroit, MI                          3    0
##   Plymouth, Dorset / Houghton, MI                        1    0
##   Plymouth, England                                      2    0
##   Pomeroy, WA                                            1    0
##   Pondersend, England / New Durham, NJ                   1    0
##   Portland, OR                                           2    1
##   Portugal                                               2    0
##   Portugal / Sau Paulo, Brazil                           0    1
##   Providence, RI                                         2    0
##   Provo, UT                                              1    0
##   Roachdale, IN                                          0    1
##   Rochester, NY                                          3    0
##   Rotherfield, Sussex, England Essex Co, MA              5    0
##   Rotterdam, Netherlands                                 0    1
##   Ruotsinphyhtaa, Finland New York, NY                   1    0
##   Ruotsinphytaa, Finland New York, NY                    4    0
##   Russia                                                 1    0
##   Russia New York, NY                                    2    0
##   Salo, Finland Astoria, OR                              1    0
##   San Francisco, CA                                      2    2
##   Sarnia, ON                                             0    1
##   Sault St Marie, ON                                     1    0
##   Scituate, MA                                           2    0
##   Seattle, WA                                            0    1
##   Seattle, WA / Toledo, OH                               1    0
##   Sittingbourne, England / San Diego, CA                 1    0
##   Skara, Sweden / Rockford, IL                           2    0
##   Somerset / Bernardsville, NJ                           5    0
##   Southampton                                            3    0
##   Southampton / New York, NY                             1    0
##   Southington / Noank, CT                                1    1
##   Southsea, Hants                                        1    0
##   Spain                                                  1    0
##   Spain / Havana, Cuba                                   0    2
##   Springfield, MA                                        1    0
##   St Andrews, Guernsey                                   1    0
##   St Anne's-on-Sea, Lancashire                           1    0
##   St Austall, Cornwall                                   1    0
##   St Denys, Southampton, Hants                           1    0
##   St Ives, Cornwall / Calumet, MI                        2    0
##   St Ives, Cornwall / Hancock, MI                        2    0
##   St Ives, Cornwall / Houghton, MI                       0    1
##   St James, Long Island, NY                              1    0
##   St Leonards-on-Sea, England Ohio                       1    1
##   St Louis, MO                                           4    0
##   Stanton, IA                                            3    0
##   Stockholm, Sweden                                      2    0
##   Stockholm, Sweden / Washington, DC                     1    0
##   Stockholm, Sweden New York                             1    0
##   Stoughton, MA                                          0    1
##   Streatham, Surrey                                      0    1
##   Strood, Kent, England Detroit, MI                      2    1
##   Surbiton Hill, Surrey                                  0    1
##   Sweden  Worcester, MA                                  3    0
##   Sweden / Arlington, NJ                                 1    0
##   Sweden Akeley, MN                                      1    0
##   Sweden Chicago, IL                                     1    0
##   Sweden Joliet, IL                                      1    0
##   Sweden Winnipeg, MN                                    7    0
##   Sweden Worcester, MA                                   5    0
##   Swindon, England                                       0    2
##   Sydney, Australia                                      1    0
##   Syria                                                  1    3
##   Syria Fredericksburg, VA                               0    1
##   Syria Kent, ON                                         2    1
##   Syria New York, NY                                     1    3
##   Syria Ottawa, ON                                       2    1
##   Syria Youngstown, OH                                   1    0
##   Taalintehdas, Finland Hoboken, NJ                      1    0
##   Tampico, MT                                            2    0
##   Tofta, Sweden Joliet, IL                               1    0
##   Tokyo, Japan                                           1    0
##   Toronto, ON                                            1    0
##   Tranvik, Finland New York                              1    0
##   Treherbert, Cardiff, Wales                             1    0
##   Trenton, NJ                                            2    0
##   Tuxedo Park, NY                                        3    0
##   Union Hill, NJ                                         1    0
##   Upper Burma, India Pittsburgh, PA                      0    1
##   Vadsbro, Sweden Ministee, MI                           1    0
##   Vancouver, BC                                          2    0
##   Victoria, BC                                           0    1
##   Walthamstow, England                                   0    1
##   Warwick, England                                       2    0
##   Washington, DC                                         4    0
##   Waukegan, Chicago, IL                                  1    0
##   West Bromwich, England Pontiac, MI                     3    0
##   West Hampstead, London / Neepawa, MB                   3    0
##   West Haven, CT                                         2    0
##   West Hoboken, NJ                                       1    0
##   West Kensington, London                                1    0
##   Westcliff-on-Sea, Essex                                0    1
##   Weston-Super-Mare / Moose Jaw, SK                      1    0
##   Weston-Super-Mare, Somerset                            1    1
##   Wiltshire, England Niagara Falls, NY                   8    0
##   Wimbledon Park, London / Hayling Island, Hants         1    0
##   Windsor, England New York, NY                          1    0
##   Winnipeg, MB                                           7    1
##   Winnipeg, MN                                           1    0
##   Woodford County, KY                                    1    0
##   Worcester, England                                     1    0
##   Worcester, MA                                          2    0
##   Yoevil, England / Cottage Grove, OR                    1    0
##   Youngstown, OH                                         4    1
##   Zurich, Switzerland                                    3    0
##   <NA>                                                  33  526
mosaic::tally(~boat|age_mis, data=titanic2)
##        age_mis
## boat    FALSE TRUE
##   (101)     1    0
##   (103)     1    0
##   (108)     1    0
##   (109)     0    1
##   (110)     1    0
##   (121)     1    0
##   (122)     1    0
##   (124)     1    0
##   (126)     1    0
##   (130)     1    0
##   (133)     1    0
##   (135)     1    0
##   (140)     1    0
##   (142)     1    0
##   (147)     1    0
##   (148)     1    0
##   (149)     1    0
##   (15)      1    0
##   (165)     1    0
##   (166)     0    1
##   (169)     1    0
##   (17)      1    0
##   (171)     0    1
##   (172)     1    0
##   (174)     1    0
##   (175)     1    0
##   (18)      1    0
##   (189)     1    0
##   (19)      1    0
##   (190)     1    0
##   (207)     1    0
##   (208)     1    0
##   (209)     1    0
##   (22)      1    0
##   (230)     1    0
##   (232)     1    0
##   (234)     1    0
##   (236)     1    0
##   (241)     1    0
##   (245)     1    0
##   (249)     1    0
##   (256)     1    0
##   (258)     1    0
##   (259)     1    0
##   (260)     1    0
##   (263)     1    0
##   (269)     1    0
##   (271)     1    0
##   (275)     1    0
##   (283)     1    0
##   (286)     0    1
##   (287)     0    1
##   (292)     1    0
##   (293)     1    0
##   (294)     1    0
##   (295)     1    0
##   (297)     1    0
##   (299)     1    0
##   (305)     1    0
##   (307)     1    0
##   (322)     1    0
##   (35)      1    0
##   (38)      1    0
##   (43)      1    0
##   (45)      1    0
##   (46)      1    0
##   (52)      1    0
##   (62)      1    0
##   (72)      1    0
##   (75)      1    0
##   (79)      1    0
##   (80)      1    0
##   (81)      1    0
##   (89)      1    0
##   (96)      1    0
##   (97)      1    0
##   1         3    1
##   10        7    1
##   11       13    4
##   12       11    2
##   13       13    3
##   14       11    4
##   14/12     2    0
##   14/D      1    0
##   15        4    2
##   16        2    0
##   2         9    2
##   3        17    2
##   4        25    2
##   5        23    4
##   5/7       1    3
##   6        12    5
##   7        18    4
##   8        12    6
##   9        13    2
##   A         3    2
##   B         5    1
##   C         5    1
##   D         8    2
##   <NA>    344  622

Age missing clearly can be predicted by class. Also if they didn’t have age more likely to not have specifics on origin, dest, or boat.

mosaic::tally(~pclass|survived, data=titanic2)
##       survived
## pclass   0   1
##    1st 129 193
##    2nd 161 119
##    3rd 574 137
mosaic::tally(~age|survived, data=titanic2)
##         survived
## age        0   1
##   0.1667   0   1
##   0.3333   1   0
##   0.8333   0   3
##   0.9167   0   1
##   1        0   4
##   2        2   4
##   3        0   6
##   4        1   3
##   5        1   2
##   6        3   2
##   7        0   1
##   8        0   4
##   9        5   2
##   10       1   0
##   11       2   1
##   12       0   2
##   13       2   2
##   14       2   1
##   15       0   3
##   16       5   5
##   17       4   6
##   18      15  10
##   19      10  11
##   20      12   5
##   21      14   7
##   22      14   9
##   23      11   9
##   24      12  10
##   25      13   5
##   26      15   5
##   27      10   6
##   28      12   7
##   29       6   4
##   30      20   8
##   31       5   7
##   32      10   8
##   33       6   6
##   34       7   4
##   35       8   6
##   36       7  16
##   37       4   2
##   38       7   3
##   39       6   5
##   40       8   5
##   41       5   2
##   42       6   3
##   43       4   2
##   44       4   3
##   45       8   9
##   46       7   1
##   47       5   3
##   48       4   7
##   49       3   6
##   50       5   6
##   51       2   1
##   52       3   3
##   53       0   3
##   54       4   3
##   55       3   3
##   56       2   2
##   57       4   0
##   58       2   4
##   59       2   1
##   60       1   4
##   61       3   0
##   62       1   1
##   63       1   1
##   64       5   1
##   65       2   0
##   67       1   0
##   69       0   1
##   70       1   0
##   71       3   0
##   <NA>   512 168
mosaic::tally(~sex|survived, data=titanic2)
##         survived
## sex        0   1
##   female 156 307
##   male   708 142
titanic_no_NA <- dplyr::filter(titanic2, age_mis==FALSE)

Exercise 3 As class rises (3rd to 1st) survival rate will increase as well. Survival and sex (female) will be positively correlated. Women from first class will be the ones that most survived, Males will have inverse relationship between survival and age (younger they are without getting too young (around 20s) will have higher non-Survival rates

Exercise 4

ggplot(titanic_no_NA, aes(as.factor(survived), pclass)) +
  geom_jitter(aes(colour = sex))

#sex by class factor survived
#many more males overal, more females in 1st class than other two
#almost no women in 1st classs died, more deaths as class lowers (inversely realted)
ggplot(titanic_no_NA, aes(sex, pclass)) +
  geom_point() +
  geom_jitter(aes(col = survived))

#class by age factor by survived
#age and class positively related, more older people in first class
ggplot(titanic_no_NA, aes(pclass, age)) +
  geom_jitter(aes(colour = as.factor(survived)))

#sex by age class as factor
#many more old people in first class, 3rd class centered around 20 to 40
ggplot(titanic_no_NA, aes(sex, age)) +
  geom_jitter(aes(colour = pclass))

Exercise 5

#survived by class
fm1 <- glm(survived ~ pclass, data = titanic_no_NA, family = binomial)
summary(fm1)
## 
## Call:
## glm(formula = survived ~ pclass, family = binomial, data = titanic_no_NA)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.3818  -1.0982  -0.7336   0.9860   1.6996  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)   0.4686     0.1367   3.428 0.000609 ***
## pclass2nd    -0.6578     0.1942  -3.387 0.000707 ***
## pclass3rd    -1.6439     0.2171  -7.571 3.69e-14 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 869.54  on 632  degrees of freedom
## Residual deviance: 806.29  on 630  degrees of freedom
## AIC: 812.29
## 
## Number of Fisher Scoring iterations: 4
#survived by age
fm2 <- glm(survived ~ age, data = titanic_no_NA, family = binomial)
summary(fm2)
## 
## Call:
## glm(formula = survived ~ age, family = binomial, data = titanic_no_NA)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.2260  -1.0972  -0.9908   1.2502   1.4601  
## 
## Coefficients:
##              Estimate Std. Error z value Pr(>|z|)  
## (Intercept)  0.117195   0.187746   0.624   0.5325  
## age         -0.011029   0.005493  -2.008   0.0446 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 869.54  on 632  degrees of freedom
## Residual deviance: 865.47  on 631  degrees of freedom
## AIC: 869.47
## 
## Number of Fisher Scoring iterations: 4
#surivved by sex
fm3 <- glm(survived ~ sex, data = titanic_no_NA, family = binomial)
summary(fm3)
## 
## Call:
## glm(formula = survived ~ sex, family = binomial, data = titanic_no_NA)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -1.8487  -0.6871  -0.6871   0.6321   1.7660  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)   1.5091     0.1666   9.059   <2e-16 ***
## sexmale      -2.8325     0.2078 -13.629   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 869.54  on 632  degrees of freedom
## Residual deviance: 631.03  on 631  degrees of freedom
## AIC: 635.03
## 
## Number of Fisher Scoring iterations: 4
#survived by sex and age
fm4 <- glm(survived ~ sex + age, data = titanic_no_NA, family = binomial)
summary(fm4)
## 
## Call:
## glm(formula = survived ~ sex + age, family = binomial, data = titanic_no_NA)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -2.0153  -0.7062  -0.6071   0.6452   1.9332  
## 
## Coefficients:
##              Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  1.915850   0.278035   6.891 5.55e-12 ***
## sexmale     -2.841503   0.209064 -13.592  < 2e-16 ***
## age         -0.012921   0.006864  -1.882   0.0598 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 869.54  on 632  degrees of freedom
## Residual deviance: 627.45  on 630  degrees of freedom
## AIC: 633.45
## 
## Number of Fisher Scoring iterations: 4
#survived by sex and class
fm5 <- glm(survived ~ sex + pclass, data = titanic_no_NA, family = binomial)
summary(fm5)
## 
## Call:
## glm(formula = survived ~ sex + pclass, family = binomial, data = titanic_no_NA)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -2.2484  -0.6673  -0.3995   0.6081   2.2661  
## 
## Coefficients:
##             Estimate Std. Error z value Pr(>|z|)    
## (Intercept)   2.4444     0.2463   9.924  < 2e-16 ***
## sexmale      -2.9829     0.2289 -13.029  < 2e-16 ***
## pclass2nd    -0.8503     0.2447  -3.474 0.000512 ***
## pclass3rd    -1.9493     0.2791  -6.984 2.86e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 869.54  on 632  degrees of freedom
## Residual deviance: 575.25  on 629  degrees of freedom
## AIC: 583.25
## 
## Number of Fisher Scoring iterations: 5
#survived by sex age and class
fm6 <- glm(survived ~ sex + age + pclass, data = titanic_no_NA, family = binomial)
summary(fm6)
## 
## Call:
## glm(formula = survived ~ sex + age + pclass, family = binomial, 
##     data = titanic_no_NA)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -2.9784  -0.6520  -0.3142   0.5894   2.7022  
## 
## Coefficients:
##              Estimate Std. Error z value Pr(>|z|)    
## (Intercept)  4.522163   0.471008   9.601  < 2e-16 ***
## sexmale     -3.086709   0.241063 -12.805  < 2e-16 ***
## age         -0.049309   0.008732  -5.647 1.63e-08 ***
## pclass2nd   -1.495229   0.281986  -5.302 1.14e-07 ***
## pclass3rd   -2.841271   0.338897  -8.384  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 869.54  on 632  degrees of freedom
## Residual deviance: 539.71  on 628  degrees of freedom
## AIC: 549.71
## 
## Number of Fisher Scoring iterations: 5

Exercise 6

titanic_no_NA$pclass_preds <- predict(fm1, type = "response")
titanic_no_NA$age_preds <- predict(fm2, type = "response")
titanic_no_NA$sex_preds <- predict(fm3, type = "response")
titanic_no_NA$SexAge_preds <- predict(fm4, type = "response")
titanic_no_NA$SexClass_preds <- predict(fm5, type = "response")
titanic_no_NA$All3_preds <- predict(fm6, type = "response")

titanic_no_NA$fm1_prediction_accuracy <- titanic_no_NA$survived - titanic_no_NA$pclass_preds
titanic_no_NA$fm2_prediction_accuracy <- titanic_no_NA$survived - titanic_no_NA$age_preds
titanic_no_NA$fm3_prediction_accuracy <- titanic_no_NA$survived - titanic_no_NA$sex_preds
titanic_no_NA$fm4_prediction_accuracy <- titanic_no_NA$survived - titanic_no_NA$SexAge_preds
titanic_no_NA$fm5_prediction_accuracy <- titanic_no_NA$survived - titanic_no_NA$SexClass_preds
titanic_no_NA$fm6_prediction_accuracy <- titanic_no_NA$survived - titanic_no_NA$All3_preds

mean(titanic_no_NA$fm1_prediction_accuracy)
## [1] -1.331729e-13
#-1.331729e-13
mean(titanic_no_NA$fm2_prediction_accuracy)
## [1] 2.246054e-17
#2.246054e-17
mean(titanic_no_NA$fm3_prediction_accuracy)
## [1] -6.130264e-14
#-6.130264e-14
mean(titanic_no_NA$fm4_prediction_accuracy)
## [1] -9.954812e-14
#-9.954812e-14
mean(titanic_no_NA$fm5_prediction_accuracy)
## [1] -9.60286e-17
#-9.60286e-17
mean(titanic_no_NA$fm6_prediction_accuracy)
## [1] -2.156745e-14
#-2.156745e-14

abs(min(titanic_no_NA$fm1_prediction_accuracy))
## [1] 0.6150442
#0.6150442
abs(min(titanic_no_NA$fm2_prediction_accuracy))
## [1] 0.5283493
#0.5283493
abs(min(titanic_no_NA$fm3_prediction_accuracy))
## [1] 0.81893
#0.81893
abs(min(titanic_no_NA$fm4_prediction_accuracy))
## [1] 0.8687564
#0.8687564
abs(min(titanic_no_NA$fm5_prediction_accuracy))
## [1] 0.9201477
#0.9201477
abs(min(titanic_no_NA$fm6_prediction_accuracy))
## [1] 0.9881505
#0.9881505

#residuals of fm1 prediction accuracy
titanic_no_NA$fm1_prediction_accuracy <- abs(titanic_no_NA$fm1_prediction_accuracy)
qplot(fm1_prediction_accuracy, data= titanic_no_NA)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#residuals of fm2 prediction accuracy
titanic_no_NA$fm2_prediction_accuracy <- abs(titanic_no_NA$fm2_prediction_accuracy)
qplot(fm2_prediction_accuracy, data= titanic_no_NA)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

titanic_no_NA %>%
  filter(fm2_prediction_accuracy, fm2_prediction_accuracy > .5) %>% 
  summarise (n = n(), pclass = mean(pclass), age = mean(age), sex = mean(sex), survived = mean(survived)) %>%
  arrange(desc(n))
## Warning in mean.default(pclass): argument is not numeric or logical:
## returning NA
## Warning in mean.default(sex): argument is not numeric or logical: returning
## NA
##     n pclass     age sex  survived
## 1 262     NA 31.8944  NA 0.9465649
#residuals of fm3 prediction accuracy
titanic_no_NA$fm3_prediction_accuracy <- abs(titanic_no_NA$fm3_prediction_accuracy)
qplot(fm3_prediction_accuracy, data= titanic_no_NA)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#residuals of fm4 prediction accuracy
titanic_no_NA$fm4_prediction_accuracy <- abs(titanic_no_NA$fm4_prediction_accuracy)
qplot(fm4_prediction_accuracy, data= titanic_no_NA)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#residuals of fm5 prediction accuracy
titanic_no_NA$fm5_prediction_accuracy <- abs(titanic_no_NA$fm5_prediction_accuracy)
qplot(fm5_prediction_accuracy, data= titanic_no_NA)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#residuals of fm6 prediction accuracy
titanic_no_NA$fm6_prediction_accuracy <- abs(titanic_no_NA$fm6_prediction_accuracy)
qplot(fm6_prediction_accuracy, data= titanic_no_NA)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

My fitted model 2 seems to have predicted wrong for mainly women in the first class. There were 137 cases from the 1st class that were predicted wrong and it seemed that most of those people were women. Across all three classes it seems that it was predicted that more women would die than actually did. All of these women also seem to be around 30 to 37 years old. I believe that this was predicted wrong because my fitted model 2 was a prediction of age by survival rate and since most elderly people survived it seems to have weighted too much on the age of these women and predicted they would die because they were younger but not children. Data that would be helpful to make a better prediction here would maybe to add in class to help with the prediction. It would also be helpful to know if they could swim or not, their health status at the time of the event, and maybe how close their room was to a safety boat. All of these could help us predict if someone were to survive better.