My name is Ashish Mahabal and we will be continuing with R. Today what we are going to do is take a look at packages, how they're installed, and also following so of the things that we see in the best programming practices. What we are going to do is take a look at the debugging, profiling et cetera. So, when it comes to packages, it's pretty straightforward to install them. What you can do is R CMD INSTALL packagename, and that will go ahead and install the package. And if you want to, you can do a check package name also that will run the QA that will run the QA so you would know how Whether the package name has been installed properly. And then if you are going to build a package, a, a useful thing to know is how to, how to do that using R CMD build packagename. And all these instructions will also be on the sister pages that you'll later on see. So, when you give the command library, then that tells you which packages are already in store. Of course that's a useful thing to know, because there are, as we have seen, more than close to 6,000 packages that are there in R. And then when you run library package name, it lowers the specific package for you. Here the example that you see on the screen is about Learn Bayes package about Bayesian Computation using R. So when you say library Learn Bayes, that is the package that's going to be installed for you loaded for you. And then when you run the search command that tells you what are the packages that are loaded. So, these are really basic commands that you get to know easily once you've used a few times. So what we are going to do is we are going to see how one can handle sets using these now. There are of course more automated methods than this, but it is good to know these basic methods as well. So, let's see. Let's try to use a data set called achievement. The way you do it is simply say data achievement, and that should load it for you. And, of course, if you tried to do that, and if you have not loaded the package in which case you're going to be told that it does no where achievement datas is. And then if you say help but you meant of course that still won't tell you much about it because, again the set is not loading. Now in this case, if you know that the data set comes from the package called learn base, you can quickly check whether that package has been loaded for you by giving the search command as you can see here. And output of search tells you immediately that it is in fact not loaded. So you can go ahead and say I'd like to relearn base and it will load it for you. And if you read in the search command, you will see that on number two you can see the learn base package there. So once that is there, then if you say help achievements, you're going to see a little bit about what that particular data set includes. In this case, it is about a set of children in Austria, their achievements. There are 109 observations. One of the columns is the age of the children in months, another column is their IQ. So, if you wanted to for instance go ahead blot them, you could simply say, plot the age versus the IQ. And, so one thing that we're going to see following what we did in the best programming practices is that when you run a program, it is important to save as much time as possible by comparing different methods, by bench marking it. So rather than trying to optimize a program, what we want to do is then bench mark it. And the way to do it is through profiling. So proc.time is an interesting command that tells you about just the current time. So a way to do that would be simply run it at the start of the program. The new program, and then out again. And the difference between the two times will tell you how much time has elapsed in the program. So that is a simple way to do things, but of course there are more interesting things that you can use to, the system of time is required to evaluate a single expression. You can use it to evaluate a single expression. But then, using the Rprof is the way to go if you want to do more complex investigation into where your program is spending time. So, Rprof takes a file name as an argument. And that, in that file name, in that file it is going to put all kinds of information about but processes are being run, and when it is time to stop it, you give the null argument to rprof and run it again. So, the file name then will contain all of the things that R has done in between. But if you look at the file itself, then you'll find that there's not too much you can make out of that file. So, what then you have to do is you have to run the summaryRprof command on that filename. And that is what will summarize for you what has been going on behind the scenes. So, here is the plot that I mentioned earlier. If you simply try to plot age versus IQ, of course those belong to a different namespace and you'll find that you simply get an error that it, R doesn't know about agent IQ. So you could do something like attach a [INAUDIBLE] or that the two variables and achievements are all the seven variables get attached. But of course, as we have seen that is not the best thing to do. Here we are using it because it's a one off command. So, once you do that, attach achievement, agent IQ variable available to you and then if you say, plot h versus IQ, then you just get the part that you see on the left hand side. Now, if you give the same command plot HIQ as an argumentative system of time, then you'll be told how much time is spent in different ways in the user time, system time, and the total elapsed time. And what we also see here is how to save the file, which we haven't seen before. So you can give the PNG commands to save the file the plot has a png command. Okay, so we see how a simple command system.time can be given. But now, within the plot command the the system would have done several different kind of things. So, if my just to see, what it is that running, when that happens. So that is where the Rprof command can come in. So, you can say that Rprof or plotprof, plotprof is the file name that we are using here. And then once you start then give the system.time command with the plot of age versus IQ as the command. As you see that the times that it has returned are somewhat similar not too far different from the earlier times that we have got. And then after that single command, we turn up the profiling by providing the null argument to Rprof. And then, now if you run some of the Rprof on that, you see that there are several lines of output. So, it called the GC and the axis and it gives you the system time for each of them and axis.default and so on. So, of course if this were a real program, then it'll go into many, many different subroutines and functions, and for each of them you'll get to know how much time we'll spend. And then you can figure out which of them is taking much more time than others where you may want to decrease that a little bit. On the right hand side bottom, you see just the output of the plot profile itself. And you can see that it doesn't have anything of which you can make much sense in terms of the time that was was being spent in each of them. So this profiling is a very useful thing in order to figure out where you can be improving your program by benchmarking it. Another important thing that we saw in best programming practices is that you need to do is debugging. And again with R debugging can be done in many different ways. RStudio has very elaborate debugging framework, and I encourage you to use RStudio and take a look at the debugging framework there. But there are also some basic R commands available in the base of, base R that you can use. So trace back. If run trace back, then it's going to tell you not only where the error occurred, but also which subroutine was running when that was called. And go on all the way back, giving you a complete trace. Similarly, you can dump entire frames when an error is encountered by using the options error equal to dump frames command. And then the debugger can be called at that low level to see what all can be done, inspect the entire frame and do what you would like to do with it. Similarly, we saw earlier that many people tend to ignore warnings. If there is no error and there are few warnings, they would simply go ahead and say oh, these are warnings, so I don't need to worry about that and proceed. Which is not a good thing. So there's a way to convert the warnings into errors as well, and that you can do using options(warn=2), there. And then similarly, debug and a file name can be given to get information from whatever was happening in that particular function when they hit a [INAUDIBLE]. Now what we'll see here is accessing some built-in data sets. Remember, when we tried to access achievement there, we could not access it because it belonged to a particular package called on by which was not loaded. And we couldn't get rid of it unless we loaded that. But there are a set of data sets that are already there in R which you can directly access. And one way to find out which these are by just giving the data command with empty parenthesis. And then you'll get a list of such data sets. So here we'll see for instance if we load the AirPassengers data set. And then giving question mark, AirPassengers gives us more details about that data set to, what are the columns involved? What is the length of the data set? And some additional detail on that. And you can even edit such a data set. It'll make a copy for you. And in this particular dataset, for instance there are 144 columns and, and using the usual you can massage it into a 12 by 12 array and, work on that. And you can do a pair spot on that and so on. Another dataset that is useful, to look at because it's very small and, gives only two dimensions, is, cars. So you can simply say data, cars, and the plot cars. It'll just go ahead and plot one column versus the other. Similarly another data set is you should see bad emissions, you should try doing that if you plot using bad emissions you'll find that is a more complex plot that comes about. And we'll return to that particular point soon, because plotting for each different argument can invoke different methods, depending on what it is that you're trying to plot. And there are indirect ways of telling R, what it is that you want to plot, by just a single plot command. So, for plotting complex data set, it is a very useful thing to do. Before that, let's look at some of the plots that you could do. So you can simply do xy plot, or do a histogram or a dot chart by just calling those names. There advantage with R is again, that these are orderly commands, which means that if you give it the minimal arguments, it will do the minimal things. But then it can also take lots and lots of additional arguments which you can find out using help, what it is that they're going to be able to do. So, now, you can see a lot of basic statistics using simply attaching cars and mean of speed which is one of the variable, or max of the speed or summary and doing various things like dotchart of it or doing a barplot of it. And you can give various colors to it or you can get pie(speed). So these are things that you can explore on you own, I'm just showing some of them to you so you can get it. And then it is time that you don't want the variables from cars to be there, you can simply detach those. Here is one such data set which may seem. it, it is a four dimensional array, and when you simply say plot(Titanic), this is the plot that you'll get. And it is about such a plot that we'll see more details, how to attach specific configurations of given data set that you have to simple plot commands. So, the default plot meth, method is what you will define. And then you can associate such methods with the data set. And more generically with the objects to decide what the default behavior should be. And that are, those are some of the things that we'll be getting into, into the next set of the R lectures. So, we'll be looking at something called astRowRap, where we use specific data sets from astronomy, and attach specific plot methods to it. And swirl which is another simple way of learning R for beginners. So we'll see more of that next time.