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.

<strong>Using</strong> R <strong>for</strong> introductory statistics 170The standard deviation isn’t always a better measure of spread. We will repeat thesimulation with exponential data and investigate. Be<strong>for</strong>e doing so, we look at script files,which save a sequence of commands to be executed.6.5.1 Script files and source()R can “read” the contents of a file and execute the commands as though they were typedin at the command line. The command to do this is source(), as insource(file=“filename”). (Most of the GUIs have this ability.)For example, if a file named “sim.R” contains thesecommands## file sim.Rf = function(n) sd(rexp(n))g = function(n) IQR(rexp(n))Figure 6.5 Boxplot of standarddeviation and IQR <strong>for</strong> normal datares.sd = c(); res.iqr = c()<strong>for</strong>(i in 1:200){res.sd[i] = f(100)res.iqr[i] = g(100)}boxplot(list(sd=res.sd, iqr=res.iqr))then the command> source("sim.R")will read and evaluate these commands producing a graph similar to Figure 6.6. Withexponential data, the spread of each statistic is similar. The more skewed or long-tailedthe data is, the wider the spread of the standard deviation compared to the IQR.By using a separate file to type our commands, we can more easily make changes thanwith the command line, and we can save our work <strong>for</strong> later use.

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

Saved successfully!

Ooh no, something went wrong!