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.

Data 11> x = c(2,3,5,7,11)> xbar = mean(x)> x—xbar # the difference[1] −3.6 −2.6 −0.6 1.4 5.4> (x−xbar)^2 # the squared difference[1] 12.96 6.76 0.36 1.96 29.16> sum((x−xbar)^2) # sum of squareddifferences[1] 51.2> n = length(x)> n[1] 5> sum((x−xbar)^2)/ (n-1)[1] 12.8That is, the variance <strong>for</strong> these numbers is 12.8. ■Functions are like pets A silly analogy: to remember how to use functions in R, thinkof them as being like pets. They don’t come unless we call them by name (spelledproperly). They have a mouth (the parentheses) that likes to be fed (in this case thearguments to the function), and they will complain if they are not fed properly.Finding help<strong>Using</strong> R to do statistics requires knowing a lot of different functions—more than most ofus can keep in our head at any given time. Thankfully, R has excellent built-in helpfacilities. These can be consulted <strong>for</strong> in<strong>for</strong>mation about what is returned by a function, <strong>for</strong>details on additional arguments, and <strong>for</strong> example usages of the function or data set.The help() function is the primary interface to the help system. For exam-pie, help("mean") will find help on the mean() function. A useful shortcut is the ?, as in ?mean, or? "mean". The quoted <strong>for</strong>m is needed <strong>for</strong> some function names. The help page may showup in the terminal, a separate window, or even in a web browser, depending on yoursetup.This works great if we can remember the name of the desired function. If not, there areother ways to search. The function help. search() will search each entry in the helpsystem. For example, help, search ("mean") returns many matches of functions thatmention the word “mean” in certain parts of their help page. To match just functionnames, the well-named apropos () function will search through the available functionnames and variables <strong>for</strong> matches. For example, apropos ("mean") will return alldocumented functions and variables with the word “mean” in their names.If we want to explore the help pages to see what is there, the help. start () function willopen a web browser to an index of all the available documentation, including themanuals, if installed. All subsequent uses of help() or ? will appear in the browser. Astandard installation of R includes a few manuals. These can be viewed in HTML <strong>for</strong>matthrough help. start () or they can be printed out. These manuals may be daunting at first,but they are full of excellent in<strong>for</strong>mation.Most help pages have interesting examples. These can be run one-by-one by cuttingand pasting into the console, or all at once by using the function example (). A typicalusage would be example (mean).

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

Saved successfully!

Ooh no, something went wrong!