Homework #1 is worth 100 points and each question is worth 6.5 points each.

Submission Instructions: save the .HTML file as ‘Familiar_ Categorical_Data_Assignmentyourlastname.HTML’ and upload the HTML file to the assignment entitled ‘Getting Familiar with Categorical Data in R’ on Canvas on or before Thursday April 02, 2020 by 11:59p.m. EST. No late assignments are accepted.

  1. #2.1 p.p. 60-61

Run the code chunk below.

library(vcd)
## Warning: package 'vcd' was built under R version 3.6.3
## Loading required package: grid
library(grid)
library(gnm)
## Warning: package 'gnm' was built under R version 3.6.3
library(vcdExtra)
## Warning: package 'vcdExtra' was built under R version 3.6.3
ds <- datasets(package = c("vcd", "vcdExtra"))
str(ds, vec.len=2)
## 'data.frame':    76 obs. of  5 variables:
##  $ Package: chr  "vcd" "vcd" ...
##  $ Item   : chr  "Arthritis" "Baseball" ...
##  $ class  : chr  "data.frame" "data.frame" ...
##  $ dim    : chr  "84x5" "322x25" ...
##  $ Title  : chr  "Arthritis Treatment Data" "Baseball Data" ...
## 'data.frame':    76 obs. of  5 variables:
##  $ Package: chr  "vcd" "vcd" ...
##  $ Item   : chr  "Arthritis" "Baseball" ...
##  $ class  : chr  "data.frame" "data.frame" ...
##  $ dim    : chr  "84x5" "322x25" ...
##  $ Title  : chr  "Arthritis Treatment Data" "Baseball Data" ...

head(ds)
##   Package           Item      class     dim
## 1     vcd      Arthritis data.frame    84x5
## 2     vcd       Baseball data.frame  322x25
## 3     vcd BrokenMarriage data.frame    20x4
## 4     vcd     Bundesliga data.frame 14018x7
## 5     vcd  Bundestag2005      table    16x5
## 6     vcd      Butterfly      table      24
##                                     Title
## 1                Arthritis Treatment Data
## 2                           Baseball Data
## 3                    Broken Marriage Data
## 4      Ergebnisse der Fussball-Bundesliga
## 5 Votes in German Bundestag Election 2005
## 6             Butterfly Species in Malaya
##   Package           Item      class     dim
## 1     vcd      Arthritis data.frame    84x5
## 2     vcd       Baseball data.frame  322x25
## 3     vcd BrokenMarriage data.frame    20x4
## 4     vcd     Bundesliga data.frame 14018x7
## 5     vcd  Bundestag2005      table    16x5
## 6     vcd      Butterfly      table      24
##                                     Title
## 1                Arthritis Treatment Data
## 2                           Baseball Data
## 3                    Broken Marriage Data
## 4      Ergebnisse der Fussball-Bundesliga
## 5 Votes in German Bundestag Election 2005
## 6             Butterfly Species in Malaya

head(UCBAdmissions)
## [1] 512 313  89  19 353 207
## [1] 512 313  89  19 353 207

str(UCBAdmissions)
##  'table' num [1:2, 1:2, 1:6] 512 313 89 19 353 207 17 8 120 205 ...
##  - attr(*, "dimnames")=List of 3
##   ..$ Admit : chr [1:2] "Admitted" "Rejected"
##   ..$ Gender: chr [1:2] "Male" "Female"
##   ..$ Dept  : chr [1:6] "A" "B" "C" "D" ...
##  'table' num [1:2, 1:2, 1:6] 512 313 89 19 353 207 17 8 120 205 ...
##  - attr(*, "dimnames")=List of 3
##   ..$ Admit : chr [1:2] "Admitted" "Rejected"
##   ..$ Gender: chr [1:2] "Male" "Female"
##   ..$ Dept  : chr [1:6] "A" "B" "C" "D" ...
  1. How many data sets are there altogether? How many are there in each package? There are 33 datasets in vcd and 43 datasets in vcdExtra.
nrow(ds)
## [1] 76
## [1] 76

dsvcd <- datasets(package = "vcd")
nrow(dsvcd)
## [1] 33
## [1] 33

dsextra <- datasets(package = "vcdExtra")
nrow(dsextra)
## [1] 43
## [1] 43
  1. Make a tabular display of the frequencies by Package and class.
ds
##     Package            Item      class             dim
## 1       vcd       Arthritis data.frame            84x5
## 2       vcd        Baseball data.frame          322x25
## 3       vcd  BrokenMarriage data.frame            20x4
## 4       vcd      Bundesliga data.frame         14018x7
## 5       vcd   Bundestag2005      table            16x5
## 6       vcd       Butterfly      table              24
## 7       vcd      CoalMiners      table           2x2x9
## 8       vcd   DanishWelfare data.frame           180x5
## 9       vcd      Employment      table           2x6x2
## 10      vcd      Federalist      table               7
## 11      vcd         Hitters data.frame           154x4
## 12      vcd      HorseKicks      table               5
## 13      vcd        Hospital      table             3x3
## 14      vcd JobSatisfaction data.frame             8x4
## 15      vcd     JointSports data.frame            40x5
## 16      vcd       Lifeboats data.frame            18x8
## 17      vcd      MSPatients      array           4x4x2
## 18      vcd     NonResponse data.frame            12x4
## 19      vcd     OvaryCancer data.frame            16x5
## 20      vcd          PreSex      table         2x2x2x2
## 21      vcd      Punishment data.frame            36x5
## 22      vcd         RepVict      table             8x8
## 23      vcd        Rochdale      table 2x2x2x2x2x2x2x2
## 24      vcd          Saxony      table              13
## 25      vcd       SexualFun      table             4x4
## 26      vcd    SpaceShuttle data.frame            24x6
## 27      vcd         Suicide data.frame           306x6
## 28      vcd          Trucks data.frame            24x5
## 29      vcd        UKSoccer      table             5x5
## 30      vcd    VisualAcuity data.frame            32x4
## 31      vcd         VonBort data.frame           280x4
## 32      vcd      WeldonDice      table              11
## 33      vcd      WomenQueue      table              11
## 34 vcdExtra        Abortion      table           2x2x2
## 35 vcdExtra        Accident data.frame            80x5
## 36 vcdExtra        AirCrash data.frame           439x5
## 37 vcdExtra       Alligator data.frame            80x5
## 38 vcdExtra        Bartlett      table           2x2x2
## 39 vcdExtra            Burt data.frame            36x5
## 40 vcdExtra          Caesar      table         3x2x2x2
## 41 vcdExtra          Cancer      table           2x2x2
## 42 vcdExtra      Cormorants data.frame           343x8
## 43 vcdExtra   CyclingDeaths data.frame           208x2
## 44 vcdExtra    DaytonSurvey data.frame            32x6
## 45 vcdExtra         Depends      table              15
## 46 vcdExtra       Detergent      table         2x2x2x3
## 47 vcdExtra          Donner data.frame            90x5
## 48 vcdExtra       Draft1970 data.frame           366x3
## 49 vcdExtra  Draft1970table      table            12x3
## 50 vcdExtra            Dyke      table       2x2x2x2x2
## 51 vcdExtra       Fungicide      array         2x2x2x2
## 52 vcdExtra             GSS data.frame             6x3
## 53 vcdExtra        Geissler data.frame            90x4
## 54 vcdExtra           Gilby      table             6x4
## 55 vcdExtra           Glass data.frame            25x3
## 56 vcdExtra    HairEyePlace      array           4x5x2
## 57 vcdExtra        Hauser79 data.frame            25x3
## 58 vcdExtra           Heart      table           2x2x3
## 59 vcdExtra         Heckman      table       2x2x2x2x2
## 60 vcdExtra      HospVisits      table             3x3
## 61 vcdExtra            Hoyt      table         4x3x7x2
## 62 vcdExtra             ICU data.frame          200x22
## 63 vcdExtra          JobSat      table             4x4
## 64 vcdExtra      Mammograms     matrix             4x4
## 65 vcdExtra          Mental data.frame            24x3
## 66 vcdExtra            Mice data.frame            30x4
## 67 vcdExtra        Mobility      table             5x5
## 68 vcdExtra         PhdPubs data.frame           915x6
## 69 vcdExtra      ShakeWords data.frame           100x2
## 70 vcdExtra              TV      array          5x11x3
## 71 vcdExtra        Titanicp data.frame          1309x6
## 72 vcdExtra        Toxaemia data.frame            60x5
## 73 vcdExtra         Vietnam data.frame            40x4
## 74 vcdExtra        Vote1980 data.frame            28x4
## 75 vcdExtra       WorkerSat data.frame             8x4
## 76 vcdExtra     Yamaguchi87 data.frame            75x4
##                                                    Title
## 1                               Arthritis Treatment Data
## 2                                          Baseball Data
## 3                                   Broken Marriage Data
## 4                     Ergebnisse der Fussball-Bundesliga
## 5                Votes in German Bundestag Election 2005
## 6                            Butterfly Species in Malaya
## 7               Breathlessness and Wheeze in Coal Miners
## 8                              Danish Welfare Study Data
## 9                                      Employment Status
## 10                            'May' in Federalist Papers
## 11                                          Hitters Data
## 12                                  Death by Horse Kicks
## 13                                         Hospital data
## 14                                 Job Satisfaction Data
## 15                           Opinions About Joint Sports
## 16                              Lifeboats on the Titanic
## 17                       Diagnosis of Multiple Sclerosis
## 18                              Non-Response Survey Data
## 19                                     Ovary Cancer Data
## 20                           Pre-marital Sex and Divorce
## 21                              Corporal Punishment Data
## 22                             Repeat Victimization Data
## 23                                         Rochdale Data
## 24                                    Families in Saxony
## 25                                            Sex is Fun
## 26                         Space Shuttle O-ring Failures
## 27                              Suicide Rates in Germany
## 28                                  Truck Accidents Data
## 29                                      UK Soccer Scores
## 30                  Visual Acuity in Left and Right Eyes
## 31                      Von Bortkiewicz Horse Kicks Data
## 32                                    Weldon's Dice Data
## 33                                       Women in Queues
## 34                                 Abortion Opinion Data
## 35            Traffic Accident Victims in France in 1958
## 36                                        Air Crash Data
## 37                                 Alligator Food Choice
## 38                   Bartlett data on plum root cuttings
## 39      Burt (1950) Data on Hair, Eyes, Head and Stature
## 40        Risk Factors for Infection in Caesarian Births
## 41                    Survival of Breast Cancer Patients
## 42              Advertising Behavior by Males Cormorants
## 43                                 London Cycling Deaths
## 44                Dayton Student Survey on Substance Use
## 45                            Dependencies of R Packages
## 46                             Detergent preference data
## 47                          Survival in the Donner Party
## 48                           USA 1970 Draft Lottery Data
## 49                          USA 1970 Draft Lottery Table
## 50                        Sources of Knowledge of Cancer
## 51                   Carcinogenic Effects of a Fungicide
## 52      General Social Survey- Sex and Party affiliation
## 53                Geissler's Data on the Human Sex Ratio
## 54          Clothing and Intelligence Rating of Children
## 55              British Social Mobility from Glass(1954)
## 56    Hair Color and Eye Color in Caithness and Aberdeen
## 57                 Hauser (1979) Data on Social Mobility
## 58                     Sex, Occupation and Heart Disease
## 59 Labour Force Participation of Married Women 1967-1971
## 60                                  Hospital Visits Data
## 61                       Minnesota High School Graduates
## 62                                          ICU data set
## 63    Cross-classification of job satisfaction by income
## 64                                     Mammogram Ratings
## 65                     Mental impariment and parents SES
## 66                                   Mice Depletion Data
## 67                                  Social Mobility data
## 68                        Publications of PhD Candidates
## 69                   Shakespeare's Word Type Frequencies
## 70                                       TV Viewing Data
## 71                             Passengers on the Titanic
## 72                        Toxaemia Symptoms in Pregnancy
## 73                 Student Opinion about the Vietnam War
## 74       Race and Politics in the 1980 Presidential Vote
## 75                              Worker Satisfaction Data
## 76              Occupational Mobility in Three Countries
##     Package            Item      class             dim
## 1       vcd       Arthritis data.frame            84x5
## 2       vcd        Baseball data.frame          322x25
## 3       vcd  BrokenMarriage data.frame            20x4
## 4       vcd      Bundesliga data.frame         14018x7
## 5       vcd   Bundestag2005      table            16x5
## 6       vcd       Butterfly      table              24
## 7       vcd      CoalMiners      table           2x2x9
## 8       vcd   DanishWelfare data.frame           180x5
## 9       vcd      Employment      table           2x6x2
## 10      vcd      Federalist      table               7
## 11      vcd         Hitters data.frame           154x4
## 12      vcd      HorseKicks      table               5
## 13      vcd        Hospital      table             3x3
## 14      vcd JobSatisfaction data.frame             8x4
## 15      vcd     JointSports data.frame            40x5
## 16      vcd       Lifeboats data.frame            18x8
## 17      vcd      MSPatients      array           4x4x2
## 18      vcd     NonResponse data.frame            12x4
## 19      vcd     OvaryCancer data.frame            16x5
## 20      vcd          PreSex      table         2x2x2x2
## 21      vcd      Punishment data.frame            36x5
## 22      vcd         RepVict      table             8x8
## 23      vcd        Rochdale      table 2x2x2x2x2x2x2x2
## 24      vcd          Saxony      table              13
## 25      vcd       SexualFun      table             4x4
## 26      vcd    SpaceShuttle data.frame            24x6
## 27      vcd         Suicide data.frame           306x6
## 28      vcd          Trucks data.frame            24x5
## 29      vcd        UKSoccer      table             5x5
## 30      vcd    VisualAcuity data.frame            32x4
## 31      vcd         VonBort data.frame           280x4
## 32      vcd      WeldonDice      table              11
## 33      vcd      WomenQueue      table              11
## 34 vcdExtra        Abortion      table           2x2x2
## 35 vcdExtra        Accident data.frame            80x5
## 36 vcdExtra        AirCrash data.frame           439x5
## 37 vcdExtra       Alligator data.frame            80x5
## 38 vcdExtra        Bartlett      table           2x2x2
## 39 vcdExtra            Burt data.frame            36x5
## 40 vcdExtra          Caesar      table         3x2x2x2
## 41 vcdExtra          Cancer      table           2x2x2
## 42 vcdExtra      Cormorants data.frame           343x8
## 43 vcdExtra   CyclingDeaths data.frame           208x2
## 44 vcdExtra    DaytonSurvey data.frame            32x6
## 45 vcdExtra         Depends      table              15
## 46 vcdExtra       Detergent      table         2x2x2x3
## 47 vcdExtra          Donner data.frame            90x5
## 48 vcdExtra       Draft1970 data.frame           366x3
## 49 vcdExtra  Draft1970table      table            12x3
## 50 vcdExtra            Dyke      table       2x2x2x2x2
## 51 vcdExtra       Fungicide      array         2x2x2x2
## 52 vcdExtra             GSS data.frame             6x3
## 53 vcdExtra        Geissler data.frame            90x4
## 54 vcdExtra           Gilby      table             6x4
## 55 vcdExtra           Glass data.frame            25x3
## 56 vcdExtra    HairEyePlace      array           4x5x2
## 57 vcdExtra        Hauser79 data.frame            25x3
## 58 vcdExtra           Heart      table           2x2x3
## 59 vcdExtra         Heckman      table       2x2x2x2x2
## 60 vcdExtra      HospVisits      table             3x3
## 61 vcdExtra            Hoyt      table         4x3x7x2
## 62 vcdExtra             ICU data.frame          200x22
## 63 vcdExtra          JobSat      table             4x4
## 64 vcdExtra      Mammograms     matrix             4x4
## 65 vcdExtra          Mental data.frame            24x3
## 66 vcdExtra            Mice data.frame            30x4
## 67 vcdExtra        Mobility      table             5x5
## 68 vcdExtra         PhdPubs data.frame           915x6
## 69 vcdExtra      ShakeWords data.frame           100x2
## 70 vcdExtra              TV      array          5x11x3
## 71 vcdExtra        Titanicp data.frame          1309x6
## 72 vcdExtra        Toxaemia data.frame            60x5
## 73 vcdExtra         Vietnam data.frame            40x4
## 74 vcdExtra        Vote1980 data.frame            28x4
## 75 vcdExtra       WorkerSat data.frame             8x4
## 76 vcdExtra     Yamaguchi87 data.frame            75x4
##                                                    Title
## 1                               Arthritis Treatment Data
## 2                                          Baseball Data
## 3                                   Broken Marriage Data
## 4                     Ergebnisse der Fussball-Bundesliga
## 5                Votes in German Bundestag Election 2005
## 6                            Butterfly Species in Malaya
## 7               Breathlessness and Wheeze in Coal Miners
## 8                              Danish Welfare Study Data
## 9                                      Employment Status
## 10                            'May' in Federalist Papers
## 11                                          Hitters Data
## 12                                  Death by Horse Kicks
## 13                                         Hospital data
## 14                                 Job Satisfaction Data
## 15                           Opinions About Joint Sports
## 16                              Lifeboats on the Titanic
## 17                       Diagnosis of Multiple Sclerosis
## 18                              Non-Response Survey Data
## 19                                     Ovary Cancer Data
## 20                           Pre-marital Sex and Divorce
## 21                              Corporal Punishment Data
## 22                             Repeat Victimization Data
## 23                                         Rochdale Data
## 24                                    Families in Saxony
## 25                                            Sex is Fun
## 26                         Space Shuttle O-ring Failures
## 27                              Suicide Rates in Germany
## 28                                  Truck Accidents Data
## 29                                      UK Soccer Scores
## 30                  Visual Acuity in Left and Right Eyes
## 31                      Von Bortkiewicz Horse Kicks Data
## 32                                    Weldon's Dice Data
## 33                                       Women in Queues
## 34                                 Abortion Opinion Data
## 35            Traffic Accident Victims in France in 1958
## 36                                        Air Crash Data
## 37                                 Alligator Food Choice
## 38                   Bartlett data on plum root cuttings
## 39      Burt (1950) Data on Hair, Eyes, Head and Stature
## 40        Risk Factors for Infection in Caesarian Births
## 41                    Survival of Breast Cancer Patients
## 42              Advertising Behavior by Males Cormorants
## 43                                 London Cycling Deaths
## 44                Dayton Student Survey on Substance Use
## 45                            Dependencies of R Packages
## 46                             Detergent preference data
## 47                          Survival in the Donner Party
## 48                           USA 1970 Draft Lottery Data
## 49                          USA 1970 Draft Lottery Table
## 50                        Sources of Knowledge of Cancer
## 51                   Carcinogenic Effects of a Fungicide
## 52      General Social Survey- Sex and Party affiliation
## 53                Geissler's Data on the Human Sex Ratio
## 54          Clothing and Intelligence Rating of Children
## 55              British Social Mobility from Glass(1954)
## 56    Hair Color and Eye Color in Caithness and Aberdeen
## 57                 Hauser (1979) Data on Social Mobility
## 58                     Sex, Occupation and Heart Disease
## 59 Labour Force Participation of Married Women 1967-1971
## 60                                  Hospital Visits Data
## 61                       Minnesota High School Graduates
## 62                                          ICU data set
## 63    Cross-classification of job satisfaction by income
## 64                                     Mammogram Ratings
## 65                     Mental impariment and parents SES
## 66                                   Mice Depletion Data
## 67                                  Social Mobility data
## 68                        Publications of PhD Candidates
## 69                   Shakespeare's Word Type Frequencies
## 70                                       TV Viewing Data
## 71                             Passengers on the Titanic
## 72                        Toxaemia Symptoms in Pregnancy
## 73                 Student Opinion about the Vietnam War
## 74       Race and Politics in the 1980 Presidential Vote
## 75                              Worker Satisfaction Data
## 76              Occupational Mobility in Three Countries


table(ds$Package, ds$class)
##           
##            array data.frame matrix table
##   vcd          1         17      0    15
##   vcdExtra     3         24      1    15
##            array data.frame matrix table
##   vcd          1         17      0    15
##   vcdExtra     3         24      1    15
  1. Choose one or two data sets from this list, and examine their help files (e.g., help(Arthritis) or ?Arthritis). You can use, e.g., example(Arthritis) to run the R code for a given example.
?Arthritis
## starting httpd help server ... done
## starting httpd help server ... done

example("Arthritis")
## 
## Arthrt> data("Arthritis")
## 
## Arthrt> art <- xtabs(~ Treatment + Improved, data = Arthritis, subset = Sex == "Female")
## 
## Arthrt> art
##          Improved
## Treatment None Some Marked
##   Placebo   19    7      6
##   Treated    6    5     16
## 
## Arthrt> mosaic(art, gp = shading_Friendly)

## 
## Arthrt> mosaic(art, gp = shading_max)

## Arthrt> data("Arthritis")
## 
## Arthrt> art <- xtabs(~ Treatment + Improved, data = Arthritis, subset = Sex == "Female")
## 
## Arthrt> art
##          Improved
## Treatment None Some Marked
##   Placebo   19    7      6
##   Treated    6    5     16
## 
## Arthrt> mosaic(art, gp = shading_Friendly)
  1. p. 61 #2.3
  1. Find the total number of cases contained in this table.
sum(UCBAdmissions)
## [1] 4526
## [1] 4526
  1. For each department, find the total number of applicants.
margin.table(UCBAdmissions,3)
## Dept
##   A   B   C   D   E   F 
## 933 585 918 792 584 714
## Dept
##   A   B   C   D   E   F 
## 933 585 918 792 584 714
  1. For each department, find the overall proportion of applicants who were admitted.
UCBA_df<-as.data.frame(UCBAdmissions)
Total<-xtabs(Freq~Dept+Admit,data = UCBA_df)
prop.table(Total,1)
##     Admit
## Dept   Admitted   Rejected
##    A 0.64415863 0.35584137
##    B 0.63247863 0.36752137
##    C 0.35076253 0.64923747
##    D 0.33964646 0.66035354
##    E 0.25171233 0.74828767
##    F 0.06442577 0.93557423
##     Admit
## Dept   Admitted   Rejected
##    A 0.64415863 0.35584137
##    B 0.63247863 0.36752137
##    C 0.35076253 0.64923747
##    D 0.33964646 0.66035354
##    E 0.25171233 0.74828767
##    F 0.06442577 0.93557423
  1. Construct a tabular display of department (rows) and gender (columns), showing the proportion of applicants in each cell who were admitted relative to the total applicants in that cell.
Total2<-xtabs(Freq~Dept+Gender+Admit,data = UCBA_df)
prop.table(Total2,1)
## , , Admit = Admitted
## 
##     Gender
## Dept       Male     Female
##    A 0.54876742 0.09539121
##    B 0.60341880 0.02905983
##    C 0.13071895 0.22004357
##    D 0.17424242 0.16540404
##    E 0.09075342 0.16095890
##    F 0.03081232 0.03361345
## 
## , , Admit = Rejected
## 
##     Gender
## Dept       Male     Female
##    A 0.33547696 0.02036442
##    B 0.35384615 0.01367521
##    C 0.22331155 0.42592593
##    D 0.35227273 0.30808081
##    E 0.23630137 0.51198630
##    F 0.49159664 0.44397759
## , , Admit = Admitted
## 
##     Gender
## Dept       Male     Female
##    A 0.54876742 0.09539121
##    B 0.60341880 0.02905983
##    C 0.13071895 0.22004357
##    D 0.17424242 0.16540404
##    E 0.09075342 0.16095890
##    F 0.03081232 0.03361345
## 
## , , Admit = Rejected
## 
##     Gender
## Dept       Male     Female
##    A 0.33547696 0.02036442
##    B 0.35384615 0.01367521
##    C 0.22331155 0.42592593
##    D 0.35227273 0.30808081
##    E 0.23630137 0.51198630
##    F 0.49159664 0.44397759
  1. p. 61 #2.4 a, c, e
  1. Find the total number of cases represented in this table.
str(DanishWelfare)
## 'data.frame':    180 obs. of  5 variables:
##  $ Freq   : num  1 4 1 8 6 14 8 41 100 175 ...
##  $ Alcohol: Factor w/ 3 levels "<1","1-2",">2": 1 1 1 1 1 1 1 1 1 1 ...
##  $ Income : Factor w/ 4 levels "0-50","50-100",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ Status : Factor w/ 3 levels "Widow","Married",..: 1 1 1 1 1 2 2 2 2 2 ...
##  $ Urban  : Factor w/ 5 levels "Copenhagen","SubCopenhagen",..: 1 2 3 4 5 1 2 3 4 5 ...
## 'data.frame':    180 obs. of  5 variables:
##  $ Freq   : num  1 4 1 8 6 14 8 41 100 175 ...
##  $ Alcohol: Factor w/ 3 levels "<1","1-2",">2": 1 1 1 1 1 1 1 1 1 1 ...
##  $ Income : Factor w/ 4 levels "0-50","50-100",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ Status : Factor w/ 3 levels "Widow","Married",..: 1 1 1 1 1 2 2 2 2 2 ...
##  $ Urban  : Factor w/ 5 levels "Copenhagen","SubCopenhagen",..: 1 2 3 4 5 1 2 3 4 5 ...

sum(DanishWelfare$Freq)
## [1] 5144
## [1] 5144
  1. Convert this data frame to table form, DanishWelfare.tab, a 4-way array containing the frequencies with appropriate variable names and level names.
DanishWelfare.tab<-structable(Freq~Income+Status+Urban+Alcohol,data = DanishWelfare)
DanishWelfare.tab
##                       Status  Widow         Married         Unmarried        
##                       Alcohol    <1 1-2  >2      <1 1-2  >2        <1 1-2  >2
## Income  Urban                                                                
## 0-50    Copenhagen                1   3   2      14  15   1         6   2   3
##         SubCopenhagen             4   0   0       8   7   2         1   3   0
##         LargeCity                 1   1   2      41  15   2         2   9   1
##         City                      8   4   1     100  25   7         6   9   5
##         Country                   6   2   0     175  48   7         9   7   1
## 50-100  Copenhagen                8   1   3      42  39  14         7  12   2
##         SubCopenhagen             2   1   0      51  59  21         5   3   0
##         LargeCity                 7   3   2      62  68  14         9  11   3
##         City                     14   8   1     234 172  38        20  20  12
##         Country                   5   4   3     255 143  35        27  23  13
## 100-150 Copenhagen                2   5   2      21  32  20         3   6   0
##         SubCopenhagen             3   4   1      30  68  31         2  10   2
##         LargeCity                 1   1   1      23  43  10         1   5   3
##         City                      5   9   1      87 128  36        12  21   9
##         Country                   2   4   0      77  86  21         4  15   7
## >150    Copenhagen               42  26  21      24  43  23        33  36  38
##         SubCopenhagen            29  34  13      30  76  47        24  23  20
##         LargeCity                17  14   5      50  70  21        15  48  13
##         City                     95  48  20     167 198  53        64  89  39
##         Country                  46  24   8     232 136  36        68  64  26
##                       Status  Widow         Married         Unmarried        
##                       Alcohol    <1 1-2  >2      <1 1-2  >2        <1 1-2  >2
## Income  Urban                                                                
## 0-50    Copenhagen                1   3   2      14  15   1         6   2   3
##         SubCopenhagen             4   0   0       8   7   2         1   3   0
##         LargeCity                 1   1   2      41  15   2         2   9   1
##         City                      8   4   1     100  25   7         6   9   5
##         Country                   6   2   0     175  48   7         9   7   1
## 50-100  Copenhagen                8   1   3      42  39  14         7  12   2
##         SubCopenhagen             2   1   0      51  59  21         5   3   0
##         LargeCity                 7   3   2      62  68  14         9  11   3
##         City                     14   8   1     234 172  38        20  20  12
##         Country                   5   4   3     255 143  35        27  23  13
## 100-150 Copenhagen                2   5   2      21  32  20         3   6   0
##         SubCopenhagen             3   4   1      30  68  31         2  10   2
##         LargeCity                 1   1   1      23  43  10         1   5   3
##         City                      5   9   1      87 128  36        12  21   9
##         Country                   2   4   0      77  86  21         4  15   7
## >150    Copenhagen               42  26  21      24  43  23        33  36  38
##         SubCopenhagen            29  34  13      30  76  47        24  23  20
##         LargeCity                17  14   5      50  70  21        15  48  13
##         City                     95  48  20     167 198  53        64  89  39
##         Country                  46  24   8     232 136  36        68  64  26
  1. Use structable () or ftable () to produce a pleasing flattened display of the frequencies in the 4-way table. Choose the variables used as row and column variables to make it easier to compare levels of Alcohol across the other factors.
ftable(Alcohol~Income+Status+Urban,data = DanishWelfare.tab)
##                                 Alcohol  <1 1-2  >2
## Income  Status    Urban                            
## 0-50    Widow     Copenhagen              1   3   2
##                   SubCopenhagen           4   0   0
##                   LargeCity               1   1   2
##                   City                    8   4   1
##                   Country                 6   2   0
##         Married   Copenhagen             14  15   1
##                   SubCopenhagen           8   7   2
##                   LargeCity              41  15   2
##                   City                  100  25   7
##                   Country               175  48   7
##         Unmarried Copenhagen              6   2   3
##                   SubCopenhagen           1   3   0
##                   LargeCity               2   9   1
##                   City                    6   9   5
##                   Country                 9   7   1
## 50-100  Widow     Copenhagen              8   1   3
##                   SubCopenhagen           2   1   0
##                   LargeCity               7   3   2
##                   City                   14   8   1
##                   Country                 5   4   3
##         Married   Copenhagen             42  39  14
##                   SubCopenhagen          51  59  21
##                   LargeCity              62  68  14
##                   City                  234 172  38
##                   Country               255 143  35
##         Unmarried Copenhagen              7  12   2
##                   SubCopenhagen           5   3   0
##                   LargeCity               9  11   3
##                   City                   20  20  12
##                   Country                27  23  13
## 100-150 Widow     Copenhagen              2   5   2
##                   SubCopenhagen           3   4   1
##                   LargeCity               1   1   1
##                   City                    5   9   1
##                   Country                 2   4   0
##         Married   Copenhagen             21  32  20
##                   SubCopenhagen          30  68  31
##                   LargeCity              23  43  10
##                   City                   87 128  36
##                   Country                77  86  21
##         Unmarried Copenhagen              3   6   0
##                   SubCopenhagen           2  10   2
##                   LargeCity               1   5   3
##                   City                   12  21   9
##                   Country                 4  15   7
## >150    Widow     Copenhagen             42  26  21
##                   SubCopenhagen          29  34  13
##                   LargeCity              17  14   5
##                   City                   95  48  20
##                   Country                46  24   8
##         Married   Copenhagen             24  43  23
##                   SubCopenhagen          30  76  47
##                   LargeCity              50  70  21
##                   City                  167 198  53
##                   Country               232 136  36
##         Unmarried Copenhagen             33  36  38
##                   SubCopenhagen          24  23  20
##                   LargeCity              15  48  13
##                   City                   64  89  39
##                   Country                68  64  26
##                                 Alcohol  <1 1-2  >2
## Income  Status    Urban                            
## 0-50    Widow     Copenhagen              1   3   2
##                   SubCopenhagen           4   0   0
##                   LargeCity               1   1   2
##                   City                    8   4   1
##                   Country                 6   2   0
##         Married   Copenhagen             14  15   1
##                   SubCopenhagen           8   7   2
##                   LargeCity              41  15   2
##                   City                  100  25   7
##                   Country               175  48   7
##         Unmarried Copenhagen              6   2   3
##                   SubCopenhagen           1   3   0
##                   LargeCity               2   9   1
##                   City                    6   9   5
##                   Country                 9   7   1
## 50-100  Widow     Copenhagen              8   1   3
##                   SubCopenhagen           2   1   0
##                   LargeCity               7   3   2
##                   City                   14   8   1
##                   Country                 5   4   3
##         Married   Copenhagen             42  39  14
##                   SubCopenhagen          51  59  21
##                   LargeCity              62  68  14
##                   City                  234 172  38
##                   Country               255 143  35
##         Unmarried Copenhagen              7  12   2
##                   SubCopenhagen           5   3   0
##                   LargeCity               9  11   3
##                   City                   20  20  12
##                   Country                27  23  13
## 100-150 Widow     Copenhagen              2   5   2
##                   SubCopenhagen           3   4   1
##                   LargeCity               1   1   1
##                   City                    5   9   1
##                   Country                 2   4   0
##         Married   Copenhagen             21  32  20
##                   SubCopenhagen          30  68  31
##                   LargeCity              23  43  10
##                   City                   87 128  36
##                   Country                77  86  21
##         Unmarried Copenhagen              3   6   0
##                   SubCopenhagen           2  10   2
##                   LargeCity               1   5   3
##                   City                   12  21   9
##                   Country                 4  15   7
## >150    Widow     Copenhagen             42  26  21
##                   SubCopenhagen          29  34  13
##                   LargeCity              17  14   5
##                   City                   95  48  20
##                   Country                46  24   8
##         Married   Copenhagen             24  43  23
##                   SubCopenhagen          30  76  47
##                   LargeCity              50  70  21
##                   City                  167 198  53
##                   Country               232 136  36
##         Unmarried Copenhagen             33  36  38
##                   SubCopenhagen          24  23  20
##                   LargeCity              15  48  13
##                   City                   64  89  39
##                   Country                68  64  26
  1. p. 62 #2.5 a, b, c
#code from text
data("UKSoccer", package = "vcd") 
ftable(UKSoccer)
##      Away  0  1  2  3  4
## Home                    
## 0         27 29 10  8  2
## 1         59 53 14 12  4
## 2         28 32 14 12  4
## 3         19 14  7  4  1
## 4          7  8 10  2  0
##      Away  0  1  2  3  4
## Home                    
## 0         27 29 10  8  2
## 1         59 53 14 12  4
## 2         28 32 14 12  4
## 3         19 14  7  4  1
## 4          7  8 10  2  0

str(UKSoccer)
##  'table' num [1:5, 1:5] 27 59 28 19 7 29 53 32 14 8 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ Home: chr [1:5] "0" "1" "2" "3" ...
##   ..$ Away: chr [1:5] "0" "1" "2" "3" ...
##  'table' num [1:5, 1:5] 27 59 28 19 7 29 53 32 14 8 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ Home: chr [1:5] "0" "1" "2" "3" ...
##   ..$ Away: chr [1:5] "0" "1" "2" "3" ...
    1. Verify that the total number of games represented in this table is 380.
margin.table(UKSoccer)
## [1] 380
## [1] 380
  1. Find the marginal total of the number of goals scored by each of the home and away teams.
addmargins(UKSoccer)
##      Away
## Home    0   1   2   3   4 Sum
##   0    27  29  10   8   2  76
##   1    59  53  14  12   4 142
##   2    28  32  14  12   4  90
##   3    19  14   7   4   1  45
##   4     7   8  10   2   0  27
##   Sum 140 136  55  38  11 380
##      Away
## Home    0   1   2   3   4 Sum
##   0    27  29  10   8   2  76
##   1    59  53  14  12   4 142
##   2    28  32  14  12   4  90
##   3    19  14   7   4   1  45
##   4     7   8  10   2   0  27
##   Sum 140 136  55  38  11 380
  1. Express each of the marginal totals as proportions.
addmargins(prop.table(UKSoccer))
##      Away
## Home            0           1           2           3           4         Sum
##   0   0.071052632 0.076315789 0.026315789 0.021052632 0.005263158 0.200000000
##   1   0.155263158 0.139473684 0.036842105 0.031578947 0.010526316 0.373684211
##   2   0.073684211 0.084210526 0.036842105 0.031578947 0.010526316 0.236842105
##   3   0.050000000 0.036842105 0.018421053 0.010526316 0.002631579 0.118421053
##   4   0.018421053 0.021052632 0.026315789 0.005263158 0.000000000 0.071052632
##   Sum 0.368421053 0.357894737 0.144736842 0.100000000 0.028947368 1.000000000
##      Away
## Home            0           1           2           3           4
##   0   0.071052632 0.076315789 0.026315789 0.021052632 0.005263158
##   1   0.155263158 0.139473684 0.036842105 0.031578947 0.010526316
##   2   0.073684211 0.084210526 0.036842105 0.031578947 0.010526316
##   3   0.050000000 0.036842105 0.018421053 0.010526316 0.002631579
##   4   0.018421053 0.021052632 0.026315789 0.005263158 0.000000000
##   Sum 0.368421053 0.357894737 0.144736842 0.100000000 0.028947368
##      Away
## Home          Sum
##   0   0.200000000
##   1   0.373684211
##   2   0.236842105
##   3   0.118421053
##   4   0.071052632
##   Sum 1.000000000
  1. Run the code below and notice there is a data frame entitled SpaceShuttle. Using the R help, read about the details of this data frame. That is, familiarize yourself with the context and understand the meaning of the different rows.
library(vcd)
library(vcdExtra)

ds <- datasets(package = c("vcd", "vcdExtra"))
str(ds)
## 'data.frame':    76 obs. of  5 variables:
##  $ Package: chr  "vcd" "vcd" "vcd" "vcd" ...
##  $ Item   : chr  "Arthritis" "Baseball" "BrokenMarriage" "Bundesliga" ...
##  $ class  : chr  "data.frame" "data.frame" "data.frame" "data.frame" ...
##  $ dim    : chr  "84x5" "322x25" "20x4" "14018x7" ...
##  $ Title  : chr  "Arthritis Treatment Data" "Baseball Data" "Broken Marriage Data" "Ergebnisse der Fussball-Bundesliga" ...
## 'data.frame':    76 obs. of  5 variables:
##  $ Package: chr  "vcd" "vcd" "vcd" "vcd" ...
##  $ Item   : chr  "Arthritis" "Baseball" "BrokenMarriage" "Bundesliga" ...
##  $ class  : chr  "data.frame" "data.frame" "data.frame" "data.frame" ...
##  $ dim    : chr  "84x5" "322x25" "20x4" "14018x7" ...
##  $ Title  : chr  "Arthritis Treatment Data" "Baseball Data" "Broken Marriage Data" "Ergebnisse der Fussball-Bundesliga" ...

head(ds)
##   Package           Item      class     dim
## 1     vcd      Arthritis data.frame    84x5
## 2     vcd       Baseball data.frame  322x25
## 3     vcd BrokenMarriage data.frame    20x4
## 4     vcd     Bundesliga data.frame 14018x7
## 5     vcd  Bundestag2005      table    16x5
## 6     vcd      Butterfly      table      24
##                                     Title
## 1                Arthritis Treatment Data
## 2                           Baseball Data
## 3                    Broken Marriage Data
## 4      Ergebnisse der Fussball-Bundesliga
## 5 Votes in German Bundestag Election 2005
## 6             Butterfly Species in Malaya
  1. Using the structable() function, create a “flat” table that has the Damage Index on the columns and whether the O-ring failed and how many failures on the rows.
Space_table<-structable(Damage~Fail+nFailures,data = SpaceShuttle)
Space_table
##                Damage  0  2  4 11
## Fail nFailures                   
## no   0                15  0  1  0
##      1                 0  0  0  0
##      2                 0  0  0  0
## yes  0                 0  0  0  0
##      1                 0  1  4  0
##      2                 0  0  1  1
##                Damage  0  2  4 11
## Fail nFailures                   
## no   0                15  0  1  0
##      1                 0  0  0  0
##      2                 0  0  0  0
## yes  0                 0  0  0  0
##      1                 0  1  4  0
##      2                 0  0  1  1
  1. Construct the same formatted table that you did in part a, but now use the xtabs() and ftable() functions.
space_table2<-xtabs(~Fail+nFailures+Damage,data = SpaceShuttle)
ftable(space_table2)
##                Damage  0  2  4 11
## Fail nFailures                   
## no   0                15  0  1  0
##      1                 0  0  0  0
##      2                 0  0  0  0
## yes  0                 0  0  0  0
##      1                 0  1  4  0
##      2                 0  0  1  1
##                Damage  0  2  4 11
## Fail nFailures                   
## no   0                15  0  1  0
##      1                 0  0  0  0
##      2                 0  0  0  0
## yes  0                 0  0  0  0
##      1                 0  1  4  0
##      2                 0  0  1  1