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 106in Figure 4.2 shows a definite expected trend: the longer the gestation period the moretime a baby has to increase its birth weight. Do other factors, such as maternal smokingor maternal weight, affect this relationship?To plot with different plot characters, we set the pch=argument using another variableto decide the plot character. First we recede the data with NA, as the data set uses 999 <strong>for</strong>missing data (cf. ?babies).> attach(babies)> gestation[gestation == 999]= NA # 999 is code <strong>for</strong> NA> plot(gestation,wt) # scatterplot> plot(gestation,wt,pch=smoke) # different plotcharacters> table(smoke) # values of plotcharacterssmoke0 1 2 3 9544 484 95 103 10> legend(locator(1),+ legend=c("never","yes","until pregnant","longago","unknown"),+ pch=c(0:3,9))The table() function was used to find out the range of values of smoke. We consulted thehelp page (?babies) to find out what the values mean.Figure 4.2 is a little too crowded to tell if any further relationship exists.Figure 4.2 Scatterplot of gestationversus weight by smoke factorSometimes different colors will help where different plot characters don’t. To changecolors we set the col=argument in plot(). We use rainbow() to create five colors and thenextract these using the values of smoke as indices. We want our indices to be 1, 2, 3, 4, 5,so we change the “9” to a “4” and then add 1. This is done as follows:> smoke[smoke == 9] = 4

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

Saved successfully!

Ooh no, something went wrong!