06.09.2021 Views

Learning Statistics with R - A tutorial for psychology students and other beginners, 2018a

Learning Statistics with R - A tutorial for psychology students and other beginners, 2018a

Learning Statistics with R - A tutorial for psychology students and other beginners, 2018a

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

dan.grump<br />

40 50 60 70 80 90<br />

5 6 7 8 9<br />

dan.sleep<br />

Figure 6.17: A fancy scatterplot drawn using the scatterplot() function in the car package.<br />

.......................................................................................................<br />

+ smooth = FALSE<br />

+ )<br />

The first two arguments should be familiar: the first input is a <strong>for</strong>mula (dan.grump ~ dan.sleep) telling<br />

R what variables to plot, 15 <strong>and</strong> the second specifies a data frame. The third argument smooth I’ve set<br />

to FALSE to stop the scatterplot() function from drawing a fancy “smoothed” trendline (since it’s a bit<br />

confusing to <strong>beginners</strong>). The scatterplot itself is shown in Figure 6.17. As you can see, it’s not only<br />

drawn the scatterplot, but its also drawn boxplots <strong>for</strong> each of the two variables, as well as a simple line<br />

of best fit showing the relationship between the two variables.<br />

6.6.1 More elaborate options<br />

Often you find yourself wanting to look at the relationships between several variables at once. One<br />

useful tool <strong>for</strong> doing so is to produce a scatterplot matrix, analogous to the correlation matrix.<br />

15 You might be wondering why I haven’t specified the argument name <strong>for</strong> the <strong>for</strong>mula. The reason is that there’s a bug<br />

in how the scatterplot() function is written: under the hood there’s one function that expects the argument to be named<br />

x <strong>and</strong> an<strong>other</strong> one that expects it to be called <strong>for</strong>mula. I don’t know why the function was written this way, but it’s not an<br />

isolated problem: this particular kind of bug repeats itself in a couple of <strong>other</strong> functions (you’ll see it again in Chapter 13).<br />

The solution in such cases is to omit the argument name: that way, one function “thinks” that you’ve specified x <strong>and</strong> the<br />

<strong>other</strong> one “thinks” you’ve specified <strong>for</strong>mula <strong>and</strong> everything works the way it’s supposed to. It’s not a great state of affairs,<br />

I’ll admit, but it sort of works.<br />

- 186 -

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

Saved successfully!

Ooh no, something went wrong!