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 282The value of t* comes from the t-distribution with n−2 degrees of freedom.The prediction interval holds <strong>for</strong> all x simultaneously. It is often plotted using twolines on the scatterplot to show the upper and lower limits.The predict () function will return the lower and upper endpoints <strong>for</strong> each value of thepredictor. We specify interval="prediction" (which can be shortened) and a confidencelevel with level=. (The default is 0.95.)For the heart-rate example we have:> pred.res = predict(res, int = "pred")> pred.resfit lwr upr1 193.2 185.0 201.42 189.4 181.3 197.5...A matrix is returned with columns giving the data we want. We cannot access these withthe data frame notation pred. res$lwr, as the return value is not a data frame. Rather wecan access the columns by name, like pred. res [, ’lwr’ ] or by column number, as in> pred.res[,2] # the ’lwr’ column1 2 3 4 5 6 7 8 910185.0 181.3 177.6 173.9 170.1 166.3 162.4 158.5 154.6185.0...We want to plot both the lower and upper limits. In our example, we have the predictedvalues <strong>for</strong> the given values of age. As the age variable is not sorted, simply plotting willmake a real mess. To remedy this, we specify the values of the age variable <strong>for</strong> which wemake a prediction. We use the values sort (unique (age)), which gives just the x values inincreasing order.> age.sort = sort(unique(age))> pred.res = predict(res.mhr, newdata = data.frame(age= age.sort),+ int="pred")> pred.res[,2]1 2 3 4 5 6 7 8 9185.0 181.3 177.6 173.9 170.1 166.3 162.4 158.5 154.6Now we can add the prediction intervals to the scatterplot with the lines() function(matlines () offers a one-step alternative). The result is Figure 10.7.> plot(mhr ~ age); abline(res)> lines(age.sort,pred.res[,2] , lty=2) # lower curve> lines(age.sort,pred.res[,3], lty=2) # upper curve

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

Saved successfully!

Ooh no, something went wrong!