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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Using</strong> R <strong>for</strong> introductory statistics 72> options("digits"=1) # to fit on the page> prop.table(table(prev,grade),1)gradeprev A A− B+ B B− C+ C D FA 0.54 0.11 0.04 0.14 0.00 0.00 0.11 0.07 0.00…C 0.04 0.00 0.00 0.04 0.04 0.11 0.19 0.33 0.26D 0.00 0.00 0.00 0.11 0.00 0.00 0.44 0.33 0.11F 0.05 0.00 0.00 0.05 0.05 0.05 0.14 0.18 0.50> options("digits"=4) # set back to original> detach(grades) # tidy upFrom comparing the rows, it is apparent that the previous grade has a big influence on thecurrent grade.The opt ions () function is used to set the number of digits that are displayed in theoutput of decimal numbers. It was set to 1 to make the table print without breaking in thespace provided.3.1.5 Graphical summaries of two-way contingency tablesBarplots can be used effectively to show the data in a two-way table. To do this, onevariable is chosen to <strong>for</strong>m the categories <strong>for</strong> the barplot. Then, either the bars <strong>for</strong> eachlevel of the category are segmented, to indicate the proportions of the other variable, orseparate bars are plotted side by side.The barplot () function will plot segmented barplots when its first argument is a twowaytable. Levels of the columns will <strong>for</strong>m the categories, and the sub-bars or segmentswill be proportioned by the values in each column. Segmented bar graphs are the default;use beside=TRUE to get side-by-side bars.If x stores the seat-belt data, we have:> barplot(x, xlab="Parent", main="Child seat-beltusage")> barplot(x, xlab="Parent", main="Child seat-beltusage",beside=TRUE)We can add a legend to the barplot with the argument legend. text=TRUE, or byspecifying a vector of names <strong>for</strong> legend. text=. For example, try| > barplot(x,main="Child seat beltusage",legend.text=TRUE)For the seat-belt data, if we wanted the parents’ distribution (the rows) to be

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

Saved successfully!

Ooh no, something went wrong!