My name is Ashish Mahabal, and I'm going to be talking about the R programming language today. Later on you will be using R in many other models. Here what we'll be doing is looking at only a few basic things in a slightly different, but there are many tutorials available on R as well, but we'll be looking at how to install R, how to. Do some very basic things with R, so that you can hit the ground running. To do that, we'll start with some of the slightly different things about R right away. And then come back to the basic history of it and so on. So, here what you see is a simple linear regression example. And R is of course wonderful for doing lots of statistics. And this is just one of the simpler examples. By the way, you start R by just saying R on the command line prompt once you have R. Then what you see in the next line is that we have assigned a vector which has three numbers, one, two and four to the name of the vector here we are using is y. And in the next line we are using the numbers one, two, three combining them and assigning them to a vector x. So that is how the syntax is. For assignment we use the less than, followed by a dash as the assignment character set. And c is used for combining, and you can combine a large number of different things with that. But that is the main thing you have to remember. So it's not the equal to sign that you use for assignment. Equal to can also be used, we can see that later, but what is really used is less than and dash, so you should better get used to that. Then, in the next line, we see how the linear regression example uses the linear model. And it takes argument y, tilde x. And again this is something you'll see in a non standard thing. So what we are saying here is that y is a dependent variable, and x is the variable on which it depends. And that is what one means by Y wiggle x. And then when you run the linear model on that, then the output of that is being captured in a variable called foo. It's not really a variable, it's an entire structure, though it seems just like a variable. And if you then say simply foo on the command line prompt you'll see what that particular structure contains. So, initially it shows you how the call is. It says that lm can be called with a formula like y wiggle x. And then it goes ahead and tells you what other things there are within the structure, like the coefficients here. The slope is 1.5 and the intercept is minus 0.67. So this is how you will run a simple statistical model. This is how you'll assign two variables and two vectors and so on. And we'll of course be covering more details of that later on. Here is another simple example. In this case it is of a plot. What we're doing here is using the rnorm function, saying that we take 100 numbers in that which have a mean of five and a standard deviation of one, and then assign a vector called a that telling those 100 numbers. In the next line, we similarly assign to b 200 numbers with the same mean and the same standard deviation. So again, the rnorm function can be used just like that. It's all built in and you can use it in a trivial fashion. But equally trivial is to get say a histogram of the b vector. Simply saying hist b is going to give you the plot that you see on the right hand side. And then combine can be used with these vectors as well. So the next line you see that you are combining 100 times the first vector and 100 times the second vector and assigning that to a variable called c. You see here is some kind of overloading c's that combine function but what we are also using the vector name called c so there's a lot of overloading, also with how much you should do. You should decide based on whether you really need to use those kinds of variable names. And then, in the next line you can see that you can simply say length of c to find out that c is now a vector of length 300. And if you simply type c, you'll see what the variable c, or the vector c contains. And of course if you say help(c), it's going to give you help not on the vector, but on the combined command, so you try that out. And yes by the way, so, all these things, various things that we'll be talking about, there'll be examples related to that, there'll be exercises related to that. And you should definitely try many of those out because doing is what will allow you to see how R functions. How the vagaries of R are and how the strengths of R are. So, let's look at a little bit of statistics how it's used in astronomics. So there are as many as 15,000 astronomical studies per year, and as many of 5% have the word statistics in the abstract. Of course many, many more have the word abstract, the word statistics in the rest of the study and they use statistics a lot. Believe in an abstract, as many as 5% have that. And 20% treat variable objects or multivariate datasets. So clearly statistics is more and more increasingly used in astronomy as well as in other sciences. But, if you look at what statistical methods actually get used. You realize that a majority of them are pre World War II. In fact, Fourier transform, that is used very frequently in many, many places, is from the earliest, early 19th century. Similarly, least squares is from the same time, and chi square is from early 20th century. And the two other tests, like Kolmogorov-Smirnov and Principal Component Analysis, they're also from before World War II. Since then, a lot of developments have happened in statistics. Many, many people do not use those but it is languages like R, that allow you to use them in a very easy fashion. And hopefully you will get to learn a lot of that in this module and a couple of other modules that will be talking about R. Most major modules and programming languages and packages have statistics Some examples are Matlab, Mathematica, IDL and so on. So, what is special about R? Why would one try to go and use R in particular? It's mainly because it's excellent for statistics of course, and a survey was done in 2011 where 60 countries were, people from 60 countries were asked about what is it that they use for data mining. And. It turned out that 47% use R. So that's clearly a very big fraction. Given that there are many other options that people have. And the one reason that is so, is because R has been written by statisticians. It also has great layered graphics, in terms of [INAUDIBLE] ggplot. We'll be seeing a little bit of that later on, as well. A variety of GUIs are available too, and interfaces, and of course, most important, is that it is free. So R actually comes from another programming language called S. S was followed by S-plus in 1988, and it was in 1993 that R came about. Ross Ihaka and Robert Gentleman wrote R, the basic parts of it for laying S. And S was a commercial package, and R was free. The current version is 3.1.1. That is what you should install when you run the exercises here. And we'll see how object oriented R is, and how you can use it from command line, as well as from various scripts. So R just like S allows you to do lots and lots of things various statistical tests. But also time series analysis that is becoming more and more important. It can do classification and clustering. So when you install R from the grand package, and we'll come to the grand website soon, there are 15 recommended packages. And those cover a large amount of statistics. And if you familiarize yourself just with those then that'll cover most of your needs. So this a snapshot of how the various recommended packages look. So, you have got smoothing and classification and clustering as we mentioned. So, you should definitely take a look at that. So, CRAN is what I mentioned earlier. It stands for Comprehensive R Archive Network. And there are various mirror sites available for that. But when you go there, you get to see that beside those 15 recommended packages there are as many as 6000 different contributed packages from people. Now, that really gives you a much greater variety. The strength is that these are all people contributed, and of course. One weakness is that there is a bit of organic growth. Some of them get left out and then the development does not continue, but the homogenous whole does allow you to use a large bit of idea of statistical functions. A bioconductor.org's another site where for bioinformatics it has a large number of packages available, and that's another thing that you should definitely take a look at if you are into that kind of thing. The other thing that I would like to mention is that you should definitely get into using an interface and editor, because that. Provides you a lot of boiler plate. Remember we talked earlier in the best programming practices. How we should go to teach sheets and boiler plates. So, that you should [INAUDIBLE] here too for R you can find out what is the best editor that you like to use. Rstudio.com which is mentioned last on the slide is one of the best ones and I would definitely recommend that. More information related to that is available on our sister material on the website, so you should take a look at that. Downloading and installing R is pretty straight forward just like various packages of R and here I've just given you links for different flavors on Mac, how you would use it and on Windows and other uniques a like. So again, it's going to that website, downloading the package and a single click essentially, in most cases allow you to install R in a very easy fashion. Running R is not difficult either. So what I would recommend initially if you've not used R before is to make a directory, say R_work. Go to that directory. And then once you go there if you hit R then you will be inside R and you will have an R prompt there. The most important thing is how to know, to know how to start a package and the next most important thing is of course how to get out of it. And the way you, you get out of R is by pressing Q followed by parentheses. And then it'll give you a chance to save your workspace. That can be quite important if you want to reuse some of the commands that you have used. It can also save some of your data. So, during your session, if you have created some objects and you want those objects to persist. You can also tell it that you would like to use those objects later and those would go into R data. And there are ways to load your earlier R data files later on. A few years ago we had also created a GUI specifically for astronomists, it's still not, it's around. There you can provide input terms of ASCII files or fits files or tables which are like XML. And then, you're allowed to pick various columns from your input files and on those columns, then you can do a variety of statistical functions. Here is a, current interface of that where you see on the left side that you can choose either exploratory functions or advanced functions or various export functions, and how you can provide the input files. Here's a list of functions that you can carry out in that. So there are some simple ones like simply getting a box plot or histogram. But there are also advanced ones like getting correlation matrix and covariance metrics. And then expert ones like H-clustering or survival analysis. So other, another thing that I would like to say is that you should definitely try out various examples that you'll find in the follow up lectures. But you should also explore the net because there's vast amount of material available through such GUIs. And that will help you a great deal. Finally, what you should be aware of is how to get help, and help is available in a variety of ways even in R. So, if you are looking for help on how to solve something, simply saving help solve at the prompt is going to tell you how to get help, what help is available on that. Similarly, doing something like question search is going to provide you options on searching. And then if you want the help to come not on the command line or within your window, but in a separate browser then the way to start that functionality is to do help.start followed by a parenthesis. And what that'll do is it will open a browser window and bring up help material there. And then you can browse through it, click and so on. Just like question, there is the additional help available with question question, keyword. And when you do that, then you can find. Say, if you do question question matrix. Then you will get not just help on matrix. But also other commands which may include the word, matrix. So you get to choose which part of that you want. Because clearly terms like matrix are going to be there in many, many different, functionalities, different packages when R is involved. One other thing that you should remember is that R is case sensitive. So, where capitals are needed, you will want to use capitals. Here, I've shown an example of, getting environmental information on what your home directory for R is. And the Sys keyword that you are to use in such a case. The first S is capital there. So you would say, Sys.getenv. And in codes R_HOME. Whatever you have sited to and then you will be able to get a what that information is. So R_HOME is the standard key word and when if you have set it to something called R_HOME you will get that back with the whole directory of course. And just like we saw that values can be combined when you have assigned to a vector. Other bits like the environment variables, or keywords, can also be similarly combined. The next example shows that if you want to find out what your operating system is and your R_HOME is in one fell swoop, then you can combine them with the c. And pass that on to the Sys.getenv. Now, in R everything in it is an object. And that is why combining various things are very easy. So you can combine different objects, pass them on as arguments and get other objects. We'll be, of course, seeing more about that. Another very important function is the summary function. Maybe you have created something or, if you have a variable vector of various kinds of objects. Then you can provide you can get a summary of what is contained in that by simply saying summary and the name of whatever you're trying to get a summary of. So, again, there is a lot of help available, and you should definitely be taking a look at various help routines, making your life that much easier. Next time, we'll be covering in greater detail the different ways in which you can do assignment and various things about, global variables, et cetera, and about objects. How you create them and what additional things you can do with objects. And one of the important concepts in R is the dataframe, so we will be looking more at that. Will see that next time.