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.

Multivariate data 123appropriate. The boxplot allows us to compare centers and spreads much more easily. Asthere are nine levels to the income variable, we wouldn’t want to specify the data withcommands like gestation [inc == 1]. Instead, we can use the model <strong>for</strong>mula gestation ~inc with boxplot(). We read the <strong>for</strong>mula as gestation is modeled by inc, which isinterpreted by boxplot() by splitting the variable gestation into pieces corresponding tothe values of inc and creating boxplots <strong>for</strong> each.We use this approach three times. The last appears in Figure 4.6, where the argumentvarwidth=TRUE is specified to show boxes with width depending on the relative size ofthe sample.> boxplot(gestation ~ inc, data=babies) # not yet> boxplot(gestation ~ inc, subset=gestation != 999 &inc != 98,+ data=babies) # better> boxplot(gestation ~ inc, subset=gestation != 999 &inc != 98,+ data=babies,varwidth=TRUE, # variable width to seesizes+ xlab="income level”, ylab="gestation (days)")Figure 4.6 Boxplot of gestation times<strong>for</strong> income levels4.3.2 The plot () function with model <strong>for</strong>mulaBoth boxplot() and plot() are generic functions allowing the programmers of R to writemodel-<strong>for</strong>mula interfaces to them. The boxplot() function always draws boxplots, butwe’ve seen already that the plot() function can draw many types of plots (depending onits first argument). In Example 3.6 the plot() command was called with a model <strong>for</strong>mulaof the type numeric ~ numeric, resulting in a scatterplot. If x and y are paired, numericdata vectors, then the model <strong>for</strong>mula y ~ x represents the model y i =β 0 +β 1 x i +ε i . Thetypical plot <strong>for</strong> viewing this type of model is the scatterplot.

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

Saved successfully!

Ooh no, something went wrong!