1. Home
  2. Explore
  3. Computer Science
  4. Introduction to R

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Introduction to R

Transcript

Welcome to our an introduction. I'm Barton  Paulson. And my goal in this course is to   introduce you to our This is our, but also,  this is our. And then finally, this is our,   it's arguably the language of data science. And  just so you don't think I'm making stuff up off   the top of my head, I have some actual data. This  is a ranking from a survey of data mining experts   on the software that they use most often in  their work. And take a look here at the top

are is first. In fact, it's 50% more than Python,  which is another major tool in data science. So   both of them are important. But you can see why  I personally am fond of R. And why is the one   that I want to start with introducing you to  data science. Now there's a few reasons that R   is especially important. Number one, it's free.  And it's open source compared to other software   packages can be 1000s of dollars per year. Also,  R is optimized for vector operations, which means

you can go through an entire row, or an entire  table of data without you having to explicitly   write for loops. If you've ever had to do that,  then you know, it's a pain. And so this is a nice   thing. Also, R has an amazing community behind it  where you can find supportive people. And you can   get examples of whatever it is you need to do.  And you can get new developments all the time.   Plus our has over 9000 contributed or third party  packages available, make it possible to basically do anything. Or if you want to put it in the words  of Yoda. You can say this, this is our there is no   if only how, and in this case, I'm quoting our  user Simon Blomberg. So very briefly, in some,   here's why I want to introduce you to our number  one, because r is the language of data science,   because it's free, and it's open source.  And because of the free packages that you   can download, install r makes it possible to do  nearly anything when you're working with data. So

I'm really glad you're here. And then I'll have  this chance to show you how you can use R to do   your own work with data in a more productive,  more interesting and more effective way. Thanks   for joining me. The first thing that we need to  do for our an introduction is to get set up. More   specifically we need to talk about installing  are, the way you do this is you can download it,   you just need to go to the home page for the our  project for statistical computing. And that's at our dash project.org. When you get there, you  can click on this link in the first paragraph   that says download our and then I'll bring you to  this page that lists all the places that you can   download it. Now I find the easiest is to simply  go to this top one, this has cloud because that'll   automatically direct you to whichever of the  below mirrors is best for your location. When   you click on that, you'll end up at this page,  the comprehensive our archive network, or CRAN,

which we'll see again, in this course, you need to  come here and click on your operating system. If   you're on a Mac, it'll take you to this page.  And the version you're going to want to click   on is just right here, it's a package file,  that's a zipped application installation file,   click on that, download it and follow the  standard installation directions. If you're   on a Windows PC, then you're probably going to  want this one base again, click on it, download it and go through the standard installation  procedure. And if you're on a Linux computer,   you're probably already familiar with what you  need to do. So I'm not going to run through   that. Now before we get a look at what is actually  like when you open it, there's one other thing you   need to do. And that is to get the files that  we're going to be using in this course. on the   page that you found this video, there's a link  that says download files. If you click on that,

then you'll download a zipped folder called  our oh one underscore entro underscore files,   download that unzip it. And if you want to put  it on your desktop, when you open it, you're   going to see something like this a single folder  that's on your desktop. And if you click on it,   then it opens up a collection of scripts. The  dot r extension is for an R source or a script   file. I also have a folder with a few data files  that we'll be using in one of these videos. If you simply double click on this first file, whose  full name is this, that'll open up in our and let   me show you what that looks like. When you open  up the application Are you will probably get a   setup of windows that look like this. On the left  is the source window or the script window where   you actually do your programming. On the right is  the console window that shows you the output and

right now it's got a bunch of boilerplate text.  Now coming over here again on the left, any line   that begins with a pound sign or hashtag or aka  Thorpe is a commented line. That's not right. On   these other lines or code that can be run, by the  way, you may notice a red warning just popped up   on the right side, that's just telling us about  something that has to do with changes in our and it doesn't affect us. What I'm going to do right  here is I'm going to put the cursor in this line,   and then I'm going to hit Command or Control and  then enter, which will run that line. And you can   see now, that is opened up over here. And what  I've done is I've made available to the program,   a collection of data sets. Now I'm going to pick  one of those data sets is the iris data sets very

well known as a measurement of three species of  the iris flower. And we're going to do head to see   the first six lines. And there we have the sepal  length, sepal width, petal length and petal width   of in this case, it's also Tosa. But if you want  to see a summary of the variables, get some quick   descriptive statistics, we can run this next line  over here. And now I get the quartiles. The mean, as well as the frequency of the three different  species of Iris, on the other hand, is really   nice to get things visually. So I'm going to run  this basic plot command for the entire dataset.

And it opens up a small window, I'm  gonna make it bigger. And it's a   scatterplot of the measurements or the three kinds  of viruses, as well as a funny one where it's   including the three different categories, they're  gonna close that window. And so that is basically   what our looks like and how our works in its  simplest possible version. Now, before we leave,   I'm actually going to take a moment to clean up  the application in the memory, I'm going to detach or remove the datasets package that I added.  I already closed the plot. So I don't need to   do this one separately. But what I can do is come  over here to clear the console, I'm actually going   to come up to edit and come down to clear console.  And that cleans it out. And this is a very quick   run through of what our looks like in its native  environment. But in the next movie, I'm going to

show you another application we can install called  our studio that lays on top of this, and makes   interacting with our a lot easier and a lot more  organized and really a lot more fun to work with. The next step and are an introduction and  setting up is about something called our   studio. Now. This is our studio. And what it is  is a piece of software that you can download,   in addition to our what you've already installed,  and its purpose is really simple. It makes working   with our easier. Now there's a few different ways  that it does is number one is it has consistent   commands. What's funny is, the different operating  systems have slightly different keyboard commands

for the same operations. And our, our studio  fixes that. And it makes it the same whether   you're on Mac, Windows or Linux. Also, there's  a unified interface instead of having two,   three or 17. windows open, you have one window  with the information organized, and also makes   it really easy to navigate with the keyboards  and to manage the information that you have in   our and let me show you how to do this. But first  we have to install it, where you're going to need to do is to go to our studios website, which is at  our studio.com. From there, click on download our   studio. Now bring it to this page or something  like it. And you're going to want to choose   the desktop version. Now, when you get there,  you're going to want to download the free sort of   community version as opposed to the $1,000 a year  version. And so click here on the left. And then

you're going to come to the list of installers for  supported platforms, it's down here on the left,   this is where you get to choose your operating  system. Click the top one if you have windows. The   next one if you have a Mac and then we have lots  of different versions of Linux, whichever one you   get, click on it, download it and go through the  standard installation process, then open it up. And then let me show you what it's like working  in our studio. To do this, open up this file and   we'll see what it's like in our studio. When you  open up our studio, you get this one window that   has several different panes in it. At the top, we  have the script or the source window. And this is   where you do your actual programming. And you'll  see that it looks really similar to what we did

when I opened up the our application. The color is  a little different. But that's something that you   can change in preferences or options. The console  is down here at the bottom. And that's where you   get the text output. Over here is the environment  that saves the variables if you're using any and   then plots and other information show up here  in the bottom right. Now you have the option   of rearranging things and changing what's there  as much as you want. Our studio is a flexible environment. And you can resize things by simply  dragging the divider between the areas. So let me   show you quick example, using the exact same code  that I did in my previous example. So you can see   how it works in our studio as opposed to the  regular our app that we use first time. First,   I'm going to load some data, that's by using  the datasets package, I'm going to do a Command   or Ctrl N, enter to load that one. And you  can see right here, it's run the command.

And then I want to do the quick summary of  data I'm going to do head Irish shows the   first six lines. And then here it is down here,  I can make that a little bit bigger if I want. Then I can do a summary by just coming back  here, and clicking Command or Control Enter.   And actually, I'm going to do a keyboard command  to make the console bigger now. And then we can   see all of that, I have the same basic descriptive  statistics and the same frequencies there. And go   back to how it was before. And make this bring  this one down a little. And now we can do the   plot. Now this time, you see it shows up in this  window here on the side, which is nice. It's not

a standalone window. Let me make that one bigger,  it takes a moment to adjust. And there we have the   same information that we had in the our app.  Right here, it's more organized in a cohesive   environment. And you see that I'm using keyboard  shortcuts to move around. And it makes life really   easy for dealing with the information that I have  in our I'm going to do the same cleanup, I'm going to detach the package that I had, this is actually  a little command to clear the plots. And then here   in our studio, I can run a funny little command  that'll do the same as doing Ctrl l to clear the   console for me. And that is a quick run through  of how you can do some very basic coding in our   studio, again, which makes working with our more  organized more efficient and easier to do overall.

In our very basic introduction to our and setting  up, there's one more thing I want to mention that   makes working with are really amazing. And that's  the packages that you can download install.   Basically, you can think of them as giving you  have superpowers when you're doing your analysis,   because you can basically do anything with  the packages that are available. Specifically,   packages are bundles of code. So it's more  software that add new function to our makes it so we can do new things. Now, there are two kinds  of package two general categories. There are base   packages, these are packages that are installed  with our so they're already there. But they're   not loaded by default. That way, our doesn't use  maybe as much memory as it might otherwise. But   more significant than that are the contributed  or third party packages. These are packages that   need to be downloaded, installed, and then  loaded separately. And when you get those,

it makes things extraordinary. And so you may  ask yourself, where to get these marvelous   packages that make things so superduper?  Well, you have a few choices. Number one,   you can go to CRAN. That's the comprehensive  our archive network, that's an official,   our site that has things listed with the official  documentation, too, you can go to a site called   CRAN tastic, which really is just a way of listing  these things. And when you click on the links, it redirects you back to CRAN. And then third,  you can also get our packages from GitHub,   which is an entirely different process. If  you're familiar with GitHub, it's not a big   deal. Otherwise, you don't usually need to deal  with it. But let's start with this first one,   the comprehensive our archive network,  or CRAN. Now, we saw this previously,   when we were just downloading our This time,  we're going to CRAN dot r dash project.org.

And we're specifically looking for this one,  the CRAN packages, that's gonna be right here   on the left click on packages. And when you open  that, you're gonna have an interesting option.   And that's to go to task views. And that breaks  it down by topic. So we have here, packages that   deal with Bayesian inference packages that deal  with chemo metrics, and computational physics,   so on and so forth. If you click on any one of  those, it'll give you a short description of the packages that are available and what they're  designed to do. Now another place to get packages,   I said, is CRAN tastic, at CRAN tastic.org. And  this is one that lists the most recently updated   the most popular packages. And it's a nice way of  getting some sort of information about what people   use most frequently, although it does redirect  you back to CRAN to do the actual downloading.   And then finally@github.com if you go to slash  trending slash R, you'll see the most common

are most frequently downloaded packages on GitHub  for use and are now regardless of how you get it,   let me show you the ones that I use most often and  I find these Make working with are really a lot   more effective and what easier. Now they have kind  of cryptic names. The first one is d plier, which   is for manipulating data frames, then there's  tidy or for cleaning up information, stringer   for working with strings or text information.  lubra date for manipulating date information.

h TT er for working with website data. GG ww is  where the GG stands for grammar of graphics. This   is for interactive visualizations. GG, plot two  is probably the most common package for creating   graphics or data visualizations in our SHINee is  another one that allows you to create interactive   applications that you can install on websites.  reo is for our input output is for importing and exporting data. And then our markdown allows you  to create what are called interactive notebooks or   rich documents for sharing your information. Now,  there are others, but there's one in particular,   that thing's useful. I call it the one package  to load them all. And it's Pac Man, which not   surprisingly, stands for package manager. And  I'm going to demonstrate all of these in another   course that we have here. But let me show you very  quickly how to get them working. He just tried

an R. If you open up this file from the course  files, let me show you what it looks like. What   we have here in our studio is the file for this  particular video. And I say that I use Pac Man,   if you don't have it installed already, then run  this one installation line. This is the standard   installation command in R. And now add Pac Man,  and then it will show up here and packages. Now   I already have it installed. So you can see it  right there. But it's not currently loaded. See

because installing means making it available  on your hard drive. But loading means actually   making it accessible to your current routines.  So then I need to load it or import it. And I   can do it with one of two ways. I can use the  require, which gives a confirmation message,   I can do it like this. And you see it's got that  little sentence there. Or I can do library which   simply loads it without saying anything. You  can see now by the way that it's checked off, so we know it's there. Now, if you have  Pac Man installed, even if it's not loaded,   then you can actually use Pac Man to install other  packages. So what I actually do is because I have   Pac Man installed, I just go straight to this one  you do Pac Man and then the two colons. It says,   use this command, even though this package isn't  loaded. And then I load an entire collection,   all the things that I showed you starting with Pac  Man itself. So now I'm going to run this command.

And what's nice about Pac Man is, if you don't  have the package, it will actually install it,   make it available and load it. And I gotta tell  you, this is a much easier way to do it than the   standard r routine. And then, for base packages,  that means the ones that come with are natively   like the data sets package, you still want to do  it this way you load and unload them separately.   So now I've got that one available. And then I can  do the work that I want to do. Now I'm actually not going to do it right now, because I'm going to  show it to you in future videos. But now I have a   whole collection of packages available, they're  going to give me a lot more functionality and   make my work more effective. I'm going to finish  by simply unloading what I have here. Now if   you want to with Pac Man, you can unload specific  packages, or the easiest way is to do p underscore unload all. And what that does is it unload all  of the add on or contributed third party packages.

And you can see I've got the full list  here of what is unloaded. However,   for the base packages like data sets, you  need to use the standard r command detach,   which I'll use right here. And then I'll clear  my console. And that's a very quick run through   of how packages can be found online installed into  our and loaded to make your code more available.   And I'll demonstrate how those work in basically  every video from here on out. So you'll be able to see how to exploit their functionality to  make your work a lot faster and a lot easier.

Probably the best place to start when you're  working with any statistics program is basic   graphics so you can get a quick visual impression  of what you're dealing with. And the command and   are the next simplest of all, is the default plot  command is also known as basic x, y plotting for   the x and y axes on a graph. And what's neat about  RS plot command is that it adapts to data types and to the number of variables that you're dealing  with. Now, it's going to be a lot easier for me to   simply show you how this works. So let's try it  in our just open up the script file and we'll see   how we can do some basic visualizations in our  The first thing that we're going to do is load   some data Data Sets from the data sets package  that comes with our, we simply do library data   sets. And that loads it up, we're gonna use the  iris data, which I've showed you before. And you'll get to see many more times. Let's look  at the first few lines. I'll zoom in on that.

And what this is, is the measurement of the Siebel  m petal length and width for three species of   viruses is a very famous data set about 100 years  old. And it's a great way of getting a quick feel   for what we're able to do and are, I'll come back  to the full window here. And what we're going to   do is first get a little information about the  plot command to get help on something in our just do the question mark, and the thing you want  help for. Now we're in our studio. So this opens   up right here in the help window. And you see  we've got the whole set of information here,   all the parameters and additional links, you can  click on and then examples here at the bottom.

I'm going to come over here and I'm going to use  the command for a categorical variable first.   And that's the most basic kind of data that we  have. And so species, which is three different   species is what I want to use right here. So I'm  going to do plot, and then in the parentheses,   you put what it is you want to plot. And what I'm  doing here is I'm saying it's in the data set,   Iris, that's our data frame, actually. And then  the dollar sign says use this variable that's in that data. So that's how you specify the whole  thing. And then we get an extremely simple three   bar chart, I'll zoom in on it. And what it tells  you is that we have three species of Iris setosa,   versicolor, and virginica, and then we have 50 of  each. And so it's nice now that we have balanced   group that we have three groups because that  might affect some of the analyses that you do.   And it's an extremely quick and easy way to  begin looking at the data all zoom back out.

Now let's look at a quantitative variable, so  one that's on an interval or nominal level of   measurement. For this one, I'll do petal length.  And you see I do the same thing plot and then Iris   and then peddling. Please note I'm not telling  are that this is now a quantitative variable. On   the other hand, it's able to figure that one out  and by itself. Now, this one's a little bit funny,   because it's a scatterplot, I'm going to zoom in  on it. But the x axis is the index number or the row number in the dataset. So that one's really  not helpful. It's the variable that's going on the   Y, that's the petal length that you get to see the  distribution. On the other hand, you know that we   have 50 of each species. And we have the setosa.  And then we have the versicolor. And then we have   the virginica. And so you can see that there  are group differences on these three things.

Now, what I'm going to do is I'm going to  ask for a specific kind of plot to break it   down more explicitly between the two categories.  That is, I'm going to put in two variables now,   where I have my categorical species, and  then a comma, and then the petal length,   which is my quantitative measurement. I'm  going to run that again, you just hit Ctrl,   or command and Enter. And this is one that I'm  looking for here. Let's zoom in on that. Again, you see that it's adapted. And it knows, for  instance, that the first variable I gave it   is categorical. The second was quantitative, and  the most common chart for that is a box plot. And   so that's what it automatically chooses to do.  And you can see, it's a good plot here, we can   see very strong separation between the groups on  this particular measurement. I'll zoom back out.

And then let's try a quantitative pair. So now  I'll do petal length and petal width, so it's   gonna be a little bit different. I'll run that  command. And now this one is a proper scatterplot,   where we have a measurement across the bottom,  and a measurement of the side. But you can see   that there's a really strong positive association  between these two. So not surprisingly, as a petal   gets longer, it generally also gets wider, so it  just gets bigger overall. And then finally, if I want to run the plot command on the entire data  set the entire data frame, this is what happens,   we do plot and then Iris. Now we've seen this one  in previous examples, but let me zoom in on it.

And what it is, is an entire matrix of scatter  plots of the four quantitative variables. And then   we have species, which is kind of funny because  it's not labeling them. But it shows us a dot   plot for the measurements of each species. And  this is a really nice way if you don't have too   many variables of getting a very quick holistic  impression of what's going on in your data. And so the point of this is that the default plot command  is able to adapt to the number of variables I gave   it, and to the kind of variables I give it, and  it makes life really easy. Now, I want you to   know that it's possible to change the way that  these look. I'm going to specify some options.   I'm going to do the plot again, this scatterplot  where I say plot, and then in parentheses,   I give these two arguments, or saying what I  want in it, I'm gonna say, do the petal length,

and do the petal width. And then I'm gonna go  to another line, I'm just separating with comma.   Now if you want to, you can write this all as one  really long line, I break it up, because I think   it makes a little more readable. I'm going to  specify the color, a new with call for color, and   then I use a hex code. And that code is actually  for the red that is used on the data lab homepage.   And then PCH is four point character, and that  is a 19 is a solid circle. Now I'm going to main title on it, and then I'm gonna put a label on the  x axis and a label on the y axis. So I'm actually   going to run those now by doing Command or Control  Enter for each line, and you can see it builds up.

And when we finished, we got the whole thing, I'll  zoom in on it again. And this is the kind of plot   that you could actually use in a presentation  or possibly in a publication. And so even what   the base command, we're able to get really  good looking, informative and clean graphs. Now, what's interesting is that the plot command  can do more than just show data, we can actually   feed it in formulas, if you want, for instance,  to get a cosine, I do plot and then coast is   for cosine. And then I give the limit, I go from  zero to two times pi, because that's relevant for   cosine. I click on that, and you can see the graph  there, it's doing our little cosine curve, I can

do an exponential distribution from one to five.  And there it is curving up. And I can do D norm,   which is for a density of a normal distribution  from minus three to plus three. And there's the   good old bell curve there in the bottom right.  And then we can use the same kind of options   that we used earlier for our scatterplot. Here  to say, do a plot of D norm, so the bell curve   from minus three to plus three on the x axis. And  now we're going to change the color to red l WD is

for linewidth, make it thicker, give it a title on  the top, a label on the x axis and a label on the   y axis. We'll zoom in on that. And so there is my  new and improved prettier and presentation ready   bell curve that I got with a default plot, command  and R. And so this is a really flexible and   powerful command. Also, it's the base package. And  you'll see that we have a lot of other commands that can do even more elaborate things. But this  is a great way to start and get a quick impression   of your data, see what you're dealing with, and  shape the analyses that you do subsequently.

The next step in our introduction, and our  discussion of basic graphics, is bar charts.   And the reason I like to talk about bar charts  is this, because simple is good. And when it   comes to bar charts, bar charts are the most basic  graphic for the most basic data. And so they're a   wonderful place to start in your analysis. Let me  show you how this works. Just try it in our open   up this script. And let's run through and see how  it works. When you open up the file in our studio, the first thing we're going to want to do is  come down here and open up the datasets package.

And then we're going to scroll down a little bit  and we're going to use a dataset called empty   cars. Let's get a little bit of information about  this do the question mark and the name of the data   set. This is Motor Trend. That's a magazine  car road test from 1974. So you know they're   42 years old. Let's take a look at the first few  rows of what's in empty cars by doing head. I'm   going to zoom in on this. And what you can see is  that we have a list of cars the Mazda RX four and the wagon the Datsun 710, the AMC Hornet and I  actually remember these cars and we have several   variables on each of them we have the mpg MPG,  we have the number of cylinders the displacement   and cubic inches, the horsepower the final drive  ratio which has to do with the axle, and then we   have the weight in tons the quarter mile time in  seconds. And these are a bunch of really really

slow cars. V S is for whether the cylinders are in  a V, or whether they are in a straight or in line.   And then the am is for automatic or manual. Then  we go into the next line we have gear which is the   number of gears in the transmission and carb for  how many carburetor barrels they have, which is   we don't even use carburetors anymore. Anyhow. So  that's what's in the data set. I'll zoom back out. Now if we want to do a really basic bar chart,  you might think that the most obvious thing to   do would be to use RS bar plot command. That's,  it's named for the bar chart. And then to specify   the data set empty cars, and then the dollar sign,  and then the variable that we want cylinders. So   you think that would work, but unfortunately,  it doesn't. Instead, what we get is this,   which is just kind of going through all the  cases on a one by one by one row and telling

us how many cylinders are in that case, that's  not a good one. That's not what we want. And   so what we need to do is we actually need to  reformat the data a little bit, by the way,   you would have to do the exact same thing, if  you wanted to make a bar chart in a spreadsheet,   like Excel or Google Sheets, you can't do it with  the raw data, you first need to create a summary   table. And so what we're going to do here is we're  going to use the command table, we're gonna say, take this variable from this data set and make a  table of it, and feed it into an object, you know,   a data thing, data container called cylinders,  I'm going to run that one. And then you see that   just showed up in the top left, let me zoom in  on that one. So now I have in my environment,   a data object called cylinders, it's a table,  it's got a length of three, it's got a size of   1000 bytes, and it gives us a little bit more  information. Let's go back to where we were.

But now I've saved that information into  cylinders, which just has the number of cylinders,   I can run the bar plot command. And now I get  the kind of plot I expected to see. From this, we   see that we have a fair number of cars with four  cylinders, a smaller number was six. And because   this is in 74, we've done a lot of eight cylinder  cars in this particular data set. Now, we can also   use the default plot command, which I showed you  previously, on the same data, we're just going to do something a little different, it's actually  going to make a line chart where the lines are the   same length of each bars, I'd probably use the bar  plot instead, because it's easier to tell what's   going on. But this is a way of making a default  chart that gives you the information you need   for the categorical variables. Remember, simple  is good. And that's a great way to start. In our

last video, on basic graphics, we talked about bar  charts. If you have a quantitative variable, then   the most basic kind of chart is a histogram. And  this is for data that is quantitative or scaled or   measured, or interval or ratio level, all of those  are referring to basically the same thing. And   in all of those, you want to get an idea of what  you have. And a histogram allows you to see what you have. Now there's a few things you're going  to be looking for with a histogram. Number one,   you're going to be looking for the shape of the  distribution, is it symmetrical, is it skewed is a   uni modal by modal, you're going to look for gaps  or big empty spaces in the distribution. You're   also going to look for outliers, unusual scores,  because those can distort any of your subsequent

analyses. He'll look for symmetry to see whether  you have the same number of high and low scores or   whether you have to do some sort of adjustment  to the distribution. But this is going to be   easier if we just try it in R. So open up this R  script file. And let's take a look at how we can   do histograms in R. When you open up the file, the  first thing we need to do is come down here and load the data sets. We'll do this by running the  library command, I just do Ctrl or Command Enter.

And then we can do the iris data set. Again, we've  looked at it before. But let's get a little bit of   information from it by asking for help on Iris.  And there we have Edgar Anderson's Iris data,   also known as Fisher's Iris data, because he  published an article on it. And here's the full   set of information available on it from 1936. So  it's 80 years old. Let's take a look at the first   few rows. Again, we've seen this before, Siebel  and petal length and width for three species of

Iris. We're gonna do a basic histogram on the  four quantitative variables that are in here.   And so I'm going to use just the hist command.  So hist and then the dataset Iris and then the   dollar sign to say which variable and then Siebel  dot length. I run that I get my first histogram.   Let's zoom in on a little bit. And what happens  here is of course, it's a basic sort of black line   on white background, which is fine for exploratory  graphics. And it gives us a default title that

says histogram of the variable and it gives us the  the clunky name which is also on the x axis on the   bottom, it automatically adjusts the x axis and it  chooses about seven or nine bars, which is usually   the best choice for a histogram. And then on the  left, it gives us the frequency or the count of   how many offs revisions are in that group. So  for instance, we have only five irises whose sepal length is between four and four and a half  centimeters, I think it is. Let's zoom back out.

And let's do another one. Now, this time for  a simple width, you can see that's almost a   perfect bell curve. And we do petal length, we get  something different. Let me zoom in on that one. And this is where we see a big gap, we've got a  really strong bar there at the low end. In fact,   it goes above the frequency axis. And then we  have a gap. And then sort of a bell curve that   lets us know that there's something interesting  going on with the data that we're going to want   to explore a little more fully. And then  we'll do another one for petal width,   I'll just run this command. And you can see  the same kind of pattern here where there's a big clump at the low end, there's a gap. And  then there's sort of a bell curve beyond that.

Now, another way to do this is to do the  histograms by groups. And that would be an   obvious thing to do here, because we have three  different species of Iris. So what we're going   to do here is we're going to put the graphs into  three rows, one above another in one column. I'm   going to do this by changing a parameter pa RS  for parameter, and I'm giving it the number of   rows that I want to have in my output. And  I need to give it a combination of numbers, I do this C, which is for concatenate, it means  treat these two numbers as one unit, where three   is the number of rows, and then the one is the  number of columns. So I run that it doesn't show   anything just yet. And then I'm going to come down  and I'm going to do this more elaborate command,   I'm going to do hist. That's the histogram that  we've been doing. I'm going to do petal length,

except this time in square brackets, I'm going to  put a selector is this means use only these rows.   And the way I do this is by saying I want to do it  for this atossa irises. So I say, Iris, that's the   data set, and then dollar sign. And then species  is the variable. And then two equals because in   computers, that means is equivalent to and then  in quotes, and they have to spell it exactly the same with the same capitalization and do setosa.  So this is the variable and the row selection.   I'm also going to put in some limits for the  x, because I want to manually make sure that   all three of the histograms I have have the same  x scale. So I'm going to specify that breaks is   for how many bars I wanted the histogram. And and  actually, what's funny about this is it's really   only a suggestion that you give to the computer,  then I'm going to put a title above that one,

I'm going to have no x label, and I'm going to  make it read somebody would do all of that right   now. I'll just run each line. And then you see  I have a very skinny chart, let's zoom in on it. So it's very short. But that's because I'm gonna  have multiple charts, it's gonna make more sense   when we look at them all together. But you can see  by the way that the petal width for this atossa   irises is on the low end. Now let's do the same  thing for versicolor. I'm going to run through all   that. It's all gonna be the same, except we're  gonna make it purple. There's versicolor. And   then let's do virginica last. And we'll make  those blue. And now I can zoom in on that.

And now when we have our three histograms, it's  the same variable petal width, but now I'm doing   it separately for each of the three species. And  it's really easy to see what's going on here.   Now. setosa is really low versicolor and virginica  overlap, but they're still distinct distributions.   This approach, by the way, is referred to as small  multiples, making many versions of the same chart on the same scale. So it's really easy to compare  across groups are across conditions, which is what   we're able to do right here. Now, by the way,  anytime you change the graphical parameters,   you want to make sure to change them back to what  they were before. So here, I'm going par, and then   going back to one column and one row. And that's  a good way of doing histograms for examining   quantitative variables, and even for exploring  some of the complications that can arise when you

have different categories with different scores  on those variables. In our two previous videos,   we looked at some basic graphics for one variable  at a time, we looked at bar charts for categorical   variables, and we looked at histograms for  quantitative variables. While there's a lot more   you can do with univariate distributions. You also  might want to look at by various distributions,   we're gonna look at scatter plots as the most  common version of that you do a scatter plot when what you want to do is visualize the association  between two quantitative variables. Now,   I actually know it's more flexible than that. But  this is the canonical case for a scatterplot. And   when you do that, what sorts of things do you want  to look for in your scatterplot? I mean, there's a   purpose in it. Well, number one, you want to see  if the association between your two variables is   linear, or if it can be described by a straight  line, because most of the procedures that we do

assume linearity. You also want to check if you  have consistent spread across the scores as you go   from one end to the x axis to another, because if  things fan out considerably, then you have what's   called heteroscedasticity. And it can really  complicate some of the other analyses. As always,   you want to look for outliers, because an unusual  score, or especially an unusual combination of   scores, can drastically throw off some of your  other interpretations. And then you want to look for the correlation is there an association  between these two variables. So that's what we're   looking for it, let's try it in our simply open up  this file, and let's see how it works. The first   thing we need to do in our is come down and open  up the datasets package just to command or control   and Enter. And we'll load the data sets, we're  going to use empty cars, we looked at that before,

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent