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.

Appendix E 372> ourhist(x)It works fine. But what if we wanted to use a different rule <strong>for</strong> breaks=? It would be niceto be able to override our settings. One way would be to define a breaks= argument:ourhist = function(x,breaks) {hist(x,breaks = breaks, probability = TRUE)lines(density(x))}A typical usage yields> ourhist(x)Error in hist.default(x, breaks = breaks, probability =TRUE) :Argument "breaks" is missing, with nodefaultOops, we haven’t set a default value <strong>for</strong> breaks and we didn’t directly specify one, suchas ourhist (x,"Scott"). Immediately after we list the argument <strong>for</strong> the function, we cansupply a default value in the pattern name=value. Try this:ourhist = function(x,breaks="Scott") {hist(x,breaks=breaks,probability=TRUE)lines(density(x))}Bothourhist(x) and ourhist (x,breaks="Sturges") will now work. The two commandsshow a difference in the number of bins, the second using the Sturges rule to compute thenumber instead of the default.Still, the histogram drawn looks bland. Let’s add some color to it—the color purple.The hist () function has a col= argument to set the color of the boxes. We can make thecolor be purple by default with this modification:ourhist = function(x,breaks="Scott",col="purple”) {hist(x,breaks=breaks,probability=TRUE,col=col)lines(density(x))}Trying it out gives> ourhist(x)> ourhist(x,"Sturges") # use different bin rule> ourhist(x,"Sturges","green") # green be<strong>for</strong>e purple> ourhist(x,"green") # OopsError in match.arg(tolower(breaks), c("sturges", "fd","freedman-diaconis", : ARG should be one of sturges,fd,

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

Saved successfully!

Ooh no, something went wrong!