10.07.2015 Views

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Using</strong> R <strong>for</strong> introductory statistics 70122 B B> attach(grades)> table(prev, grade) # also table (grades) worksgradeprev A A− B+ B B− C+ C D FA 15 3 1 4 0 0 3 2 0A− 3 1 1 0 0 0 0 0 0B+ 0 2 2 1 2 0 0 1 1B 0 1 1 4 3 1 3 0 2B− 0 1 0 2 0 0 1 0 0C+ 1 1 0 0 0 0 1 0 0C 1 0 0 1 1 3 5 9 7D 0 0 0 1 a a 4 3 1F 1 0 0 1 1 1 3 4 11A quick glance at the table indicates that the current grade relates quite a bit to theprevious grade. Of those students whose previous grade was an A, fifteen got an A in thenext class; only three of the students whose previous grade was a B or worse received anA in the next class.3.1.3 Marginal distributions of two-way tablesA two-way table involves two variables. The distribution of each variable separately iscalled the marginal distribution. The marginal distributions can be found from the tableby summing down the rows or columns. The sum() function won’t work, as it will add allthe values. Rather, we need to apply the sum() function to just the rows or just thecolumns. This is done with the function apply(). The command apply(x, 1, sum) will sumthe rows, and apply (x, 2, sum) will sum the columns. The margin. table() functionconveniently implements this. Just remember that 1 is <strong>for</strong> rows and 2 is <strong>for</strong> columns.For the seat-belt data stored in x we have:> Xchildparent unbuckled buckledunbuckled 56 8buckled 2 16> margin.table(x,1) # row sum is <strong>for</strong>parents[1] 64 18> margin.table(x,2) # column sum <strong>for</strong> kids[1] 58 24The two marginal distributions are similar: the majority in each case wore seat belts.Alternatively, the function addmargins () will return the marginal distributions byextending the table. For example:> addmargins(x)childparent unbuckled buckled Sum

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!