1
00:00:01,060 --> 00:00:04,180
My name is Ashish Mahabal, and
we'll continue to look at R today.

2
00:00:04,180 --> 00:00:07,140
Today we'll be looking at classes in R.

3
00:00:08,230 --> 00:00:13,410
And as we know, R has come from S, and

4
00:00:13,410 --> 00:00:17,780
the nomenclature that was used in
S has continued with R classes.

5
00:00:17,780 --> 00:00:22,720
So, S3 and S4 are the two main classes,
that are used in R.

6
00:00:22,720 --> 00:00:27,940
Google has a style guide on
how to use our classes, and

7
00:00:27,940 --> 00:00:31,930
they recommend that S3
is much better than S4,

8
00:00:31,930 --> 00:00:37,275
though S4 ones are more rigorous, and
we'll be seeing more details about that.

9
00:00:37,275 --> 00:00:40,500
But one important thing that is
said by everybody respected for

10
00:00:40,500 --> 00:00:45,120
that they say S3 are better than S4 or S4
are better than S3, is that you should try

11
00:00:45,120 --> 00:00:51,210
not to mix the two classes, because
S4 are definitely more rigorous and

12
00:00:51,210 --> 00:00:56,480
S3 are more burdening, but again if you
are not expecting some kind of behavior.

13
00:00:56,480 --> 00:01:00,460
That comes about in S4 and
S3 can lead you to grieve.

14
00:01:00,460 --> 00:01:06,110
So, there are two more blood types of
classes, the base class where typically

15
00:01:06,110 --> 00:01:11,200
C is used, and referenced classes where
in-place modification is possible.

16
00:01:11,200 --> 00:01:15,380
We may not be seeing more details of that,
but the URL that is given here,

17
00:01:15,380 --> 00:01:19,330
will provide you more details if you
are interested in something like that.

18
00:01:21,110 --> 00:01:24,810
So let's start with S3 classes,
they have existed for

19
00:01:24,810 --> 00:01:31,000
long time the coalition [INAUDIBLE]
came about, essentially what

20
00:01:31,000 --> 00:01:37,200
an S3 class has is a list, and some method
and inheritance that can be done at that.

21
00:01:38,640 --> 00:01:41,770
And then it's a bit more generic and

22
00:01:41,770 --> 00:01:45,431
casual very unlike what
a class would be in C++.

23
00:01:45,431 --> 00:01:51,320
In particular there's no way of checking,
so if you have certain variables

24
00:01:51,320 --> 00:01:57,190
that exist in your class and
you make a typo and

25
00:01:57,190 --> 00:02:01,510
during some assignment provide
a different variable name.

26
00:02:01,510 --> 00:02:03,620
It will happily take that and

27
00:02:03,620 --> 00:02:07,620
will not even tell you that, that is not
what you had first given, et cetera.

28
00:02:07,620 --> 00:02:11,330
So, you had to be sure that,
that is what you'll be wanting.

29
00:02:13,770 --> 00:02:16,450
Let's try to define an anastry class.

30
00:02:16,450 --> 00:02:22,010
Let's say that we have a few galaxies and
we want to define the class called Galaxy.

31
00:02:22,010 --> 00:02:25,770
So here,
you see that we have a variable g and

32
00:02:25,770 --> 00:02:29,380
we make it a vector by
assigning a list to it.

33
00:02:29,380 --> 00:02:31,680
We provide some named variables.

34
00:02:31,680 --> 00:02:37,370
Name equal to say NGC 4261, RA such and
such Declination such and such.

35
00:02:37,370 --> 00:02:42,890
RA and Declination are the two variables
that tell you the position of the object.

36
00:02:42,890 --> 00:02:46,610
Galaxy in this case on the sky
just like latitude and longitude.

37
00:02:46,610 --> 00:02:49,070
And then there is a certain magnitude.

38
00:02:49,070 --> 00:02:52,630
And then once you have defined
this variable called g,

39
00:02:52,630 --> 00:02:56,940
then you can say that
class of g is Galaxy.

40
00:02:56,940 --> 00:03:01,710
And just doing these two things
together defines for you that class.

41
00:03:01,710 --> 00:03:04,860
Now you can go ahead and
check the attributes of the classes.

42
00:03:04,860 --> 00:03:08,470
G is an example,
you can say attributes of g, and

43
00:03:08,470 --> 00:03:12,370
then it'll immediately tell you what
are the names of a label with g.

44
00:03:12,370 --> 00:03:16,540
And those are in this case,
Name, RA, Declination and mag.

45
00:03:17,750 --> 00:03:18,840
And then it'll go ahead and

46
00:03:18,840 --> 00:03:24,460
tell you that the class is Galaxy,
that it belongs to the class Galaxy.

47
00:03:26,590 --> 00:03:32,120
And then you can print info
about the particular object.

48
00:03:32,120 --> 00:03:36,220
So, if you were to simply say, g,
which is equivalent to, say print g,

49
00:03:36,220 --> 00:03:41,040
then it will tell you that the name
that is available is NGC 4261.

50
00:03:41,040 --> 00:03:45,310
The RA value is given there,
the declination value is given there, and

51
00:03:45,310 --> 00:03:46,900
the magnitude is given there.

52
00:03:46,900 --> 00:03:49,650
And that the attribute
of the class is Galaxy.

53
00:03:52,290 --> 00:03:56,110
So, one of the most
interesting thing what we

54
00:03:56,110 --> 00:04:01,880
alluded to in an earlier talk is how we
can change some of the default methods.

55
00:04:01,880 --> 00:04:02,640
So, in this case,

56
00:04:02,640 --> 00:04:07,750
if we wanted to change the print method,
then we can do that fairly easily.

57
00:04:07,750 --> 00:04:12,670
Here we saw that if you simply say print
GRG, you get the default print values.

58
00:04:12,670 --> 00:04:14,990
But that's not what we want to print, say.

59
00:04:14,990 --> 00:04:19,700
Suppose we wanted to take a look at where
the Galaxy is, try to get a sky map of it.

60
00:04:19,700 --> 00:04:24,390
One can easily do that in R,
using how the matrix can be overloaded or

61
00:04:24,390 --> 00:04:26,580
modified for this specific case.

62
00:04:26,580 --> 00:04:32,280
In particular, let's look at one
particular, one cla, one method called

63
00:04:32,280 --> 00:04:37,502
browseURL, which takes as an argument,
URL and simply goes to that particularly

64
00:04:37,502 --> 00:04:42,320
URL software, like in other cases,
takes many different arguments, but,

65
00:04:42,320 --> 00:04:45,910
in this case, we'll just worry about
the single argument called the URL.

66
00:04:45,910 --> 00:04:49,360
So, let's define a new print class for
our Galaxy.

67
00:04:50,550 --> 00:04:51,650
To display an image.

68
00:04:51,650 --> 00:04:56,520
So, the definition of the function
is given on the slide.

69
00:04:56,520 --> 00:05:01,560
So, if you say print.galaxy,
that is what our method is going to be.

70
00:05:01,560 --> 00:05:05,370
Print is the default name, and
.galaxy tells us that it is for

71
00:05:05,370 --> 00:05:07,770
the class galaxy that
we are defining that.

72
00:05:07,770 --> 00:05:12,320
And the function takes as
an argument just the object for

73
00:05:12,320 --> 00:05:16,170
which we are going to be talking about,
in this case let's say g.

74
00:05:16,170 --> 00:05:18,850
And then we define within
the function a base URL,

75
00:05:18,850 --> 00:05:21,880
which can be the URL
that we want to go to.

76
00:05:21,880 --> 00:05:25,800
And the URL is going to depend
on two variables in this case,

77
00:05:25,800 --> 00:05:28,540
the RA and declaration for the galaxy.

78
00:05:28,540 --> 00:05:32,670
So then we got the name of the Galaxy
which will get printed on the command,

79
00:05:32,670 --> 00:05:34,210
on the output.

80
00:05:34,210 --> 00:05:41,270
And then we paste the baseurl, the ra
with its value, the dec with its value.

81
00:05:41,270 --> 00:05:44,690
And the last command is also
the last argument that's also very

82
00:05:44,690 --> 00:05:50,170
important separator without any spaces,
because the default is a space.

83
00:05:50,170 --> 00:05:52,170
So when you paste those together and

84
00:05:52,170 --> 00:05:57,730
assign that to g_url, then you can simply
call browseURL with that argument g_url.

85
00:05:57,730 --> 00:06:02,380
And then your define, your print function
has been defined for that class.

86
00:06:02,380 --> 00:06:06,280
Now, if you look at meters of
this particular class Galaxy,

87
00:06:06,280 --> 00:06:10,912
then you'll find that you are told that it
does have one method called plain.galaxy.

88
00:06:10,912 --> 00:06:13,940
And now if you go ahead and
say g, or print of g,

89
00:06:13,940 --> 00:06:19,260
you will see that the gives
you NGC4261 on the output.

90
00:06:19,260 --> 00:06:22,010
But at the same time, it opens a browser.

91
00:06:22,010 --> 00:06:26,170
And in the browser, you in fact
see the position in sky at that

92
00:06:26,170 --> 00:06:29,900
particular that you have said
that the object g belongs to.

93
00:06:29,900 --> 00:06:31,960
And that is what you see
on the right hand side.

94
00:06:31,960 --> 00:06:34,020
This is a slow image of NGC 4261.

95
00:06:34,020 --> 00:06:38,530
So again, this is an S3 class
that we have defined and

96
00:06:38,530 --> 00:06:41,030
a specific print method with it.

97
00:06:41,030 --> 00:06:44,500
If we had misspelled any of the attribute
names, for instance, in declination.

98
00:06:44,500 --> 00:06:46,270
If we had spelled it with.

99
00:06:46,270 --> 00:06:54,020
The lowercase of our RA, lowercase ra,
then the program would have accepted that.

100
00:06:54,020 --> 00:06:55,930
Would not have given us any error.

101
00:06:55,930 --> 00:06:58,940
But just that there wouldn't
have been any useful output.

102
00:06:58,940 --> 00:07:01,080
So that is one thing
that you are to remember.

103
00:07:02,690 --> 00:07:06,320
Now S4 objects, are more rigorous.

104
00:07:06,320 --> 00:07:08,810
Here, the way they are defined
is with a slightly,

105
00:07:08,810 --> 00:07:11,950
writing a slightly different syntax.

106
00:07:11,950 --> 00:07:13,360
Use a set class.

107
00:07:13,360 --> 00:07:15,710
Here, we are using the class
name to be GalaxyS4.

108
00:07:15,710 --> 00:07:18,550
And then you have a representation for it.

109
00:07:18,550 --> 00:07:20,740
Again, we'll use the same variable names.

110
00:07:20,740 --> 00:07:23,590
But here,
we tell what types those variables are.

111
00:07:23,590 --> 00:07:26,840
Remember, earlier, when we used with S3.

112
00:07:26,840 --> 00:07:30,280
When we defined the class, we didn't
have to say anything of that kind.

113
00:07:30,280 --> 00:07:32,850
We just went ahead and defined the list.

114
00:07:32,850 --> 00:07:34,420
Whatever that list contained.

115
00:07:34,420 --> 00:07:37,560
But here, we are to say that
the name is of type character and

116
00:07:37,560 --> 00:07:40,720
the other three variables
are of type numeric.

117
00:07:40,720 --> 00:07:43,460
And that's again, all for the definition.

118
00:07:43,460 --> 00:07:45,630
Then you can say that gS4.

119
00:07:45,630 --> 00:07:47,740
And assign to it a new GalaxyS4,

120
00:07:47,740 --> 00:07:52,300
and then you give it the data through
the names and variables there.

121
00:07:52,300 --> 00:07:55,800
So we're going to see 4260,
same as before, right?

122
00:07:55,800 --> 00:07:59,520
And then that defines for
you an S4 type of class.

123
00:07:59,520 --> 00:08:01,810
In this case GS4 is an example.

124
00:08:03,190 --> 00:08:07,430
So, now if you go in and say gS4,
then it'll tell you that it's an object of

125
00:08:07,430 --> 00:08:12,620
class GalaxyS4, and now what it
uses are what are called slots.

126
00:08:12,620 --> 00:08:14,450
So it says that it has four slots.

127
00:08:14,450 --> 00:08:17,810
One is the name, and RA,
declination and magnitude.

128
00:08:18,810 --> 00:08:23,460
And these slots, in S4 are accessible
using the at character.

129
00:08:23,460 --> 00:08:26,080
Remember in S3,
we had the dollar character.

130
00:08:26,080 --> 00:08:29,417
So, you could have said g$Name and
got back NGC 4261.

131
00:08:29,417 --> 00:08:34,431
To do that with S4 classes,
what you are to do is gS4@Name.

132
00:08:34,431 --> 00:08:36,028
And you'll get the same information.

133
00:08:36,028 --> 00:08:39,846
There are these minor differences, but
the main difference, of course, is error

134
00:08:39,846 --> 00:08:44,630
checking involvement, the rigorousness
with which the class itself is defined.

135
00:08:44,630 --> 00:08:48,720
So, what is print after all which
we have managed to in this fashion.

136
00:08:48,720 --> 00:08:52,680
So, it completely depends on
the context in which it is being used.

137
00:08:52,680 --> 00:08:56,930
So, if you are simply to
say print at the R prompt,

138
00:08:56,930 --> 00:09:00,200
you'll be told that it is
a function which takes x's and

139
00:09:00,200 --> 00:09:03,980
argument and dot, dot, dot indicating
that there are many more arguments.

140
00:09:03,980 --> 00:09:08,140
And all that is happening is the use
method print is being called.

141
00:09:09,260 --> 00:09:13,970
And so, when you says print galaxy
without any arguments despite self,

142
00:09:13,970 --> 00:09:17,650
it does so
how that particular function is defined.

143
00:09:17,650 --> 00:09:19,680
That is another very useful thing in R.

144
00:09:19,680 --> 00:09:23,530
If there is a function and if you simply
give the function name, it provides for

145
00:09:23,530 --> 00:09:27,750
you the party of the function, so
you can take a look at any function that R

146
00:09:27,750 --> 00:09:31,050
is using at any time,
just by typing the name of that function.

147
00:09:31,050 --> 00:09:36,360
So here we see the definition R for
[COUGH] function print Galaxy.

148
00:09:38,060 --> 00:09:42,080
So there are many different prints
available and all you are to do is say

149
00:09:42,080 --> 00:09:47,560
methods print on the R prompt and you will
get to know that there are over 100 or

150
00:09:47,560 --> 00:09:50,480
200 prints that are accessible to you.

151
00:09:51,650 --> 00:09:54,610
The ones with stars are typically
in other named spaces.

152
00:09:54,610 --> 00:09:58,360
So you may have to do something
specific to get access to those.

153
00:09:58,360 --> 00:10:03,512
But so take a look at the let's see,

154
00:10:03,512 --> 00:10:07,796
print.by, which is the 18th one,
the last one in this row.

155
00:10:07,796 --> 00:10:13,640
Our print.aspell which is thirteenth here.

156
00:10:13,640 --> 00:10:17,690
So the aspell comes with an asterisks,
which tells you that if you have to

157
00:10:17,690 --> 00:10:21,330
simply say print out aspell you
won't get imager access to it,

158
00:10:21,330 --> 00:10:23,108
but you have to do something different.

159
00:10:23,108 --> 00:10:25,630
When it's print.by,
it's without an asterisk, so

160
00:10:25,630 --> 00:10:28,354
you can simply say printed by,
and you'll get access to that.

161
00:10:28,354 --> 00:10:32,626
But aspell, the printer aspell you
don't get access to that directly,

162
00:10:32,626 --> 00:10:35,940
you can get access to
aspell itself directly.

163
00:10:35,940 --> 00:10:39,245
What it does here is that R even
has things like spellchecks.

164
00:10:39,245 --> 00:10:45,114
Just under prompt in the command line
you can simply take take a file and

165
00:10:45,114 --> 00:10:48,020
check for its spelling very easily.

166
00:10:48,020 --> 00:10:52,093
The [INAUDIBLE] using R through python,
and

167
00:10:52,093 --> 00:10:58,890
you can simply, you,
you get a package called rpi2 [INAUDIBLE].

168
00:10:58,890 --> 00:11:02,370
And you can load it within Python,

169
00:11:02,370 --> 00:11:05,790
so the way to do it in Python of
course is using the import command.

170
00:11:05,790 --> 00:11:09,420
So if you say import rpy2.robjects
as robjects, then it

171
00:11:09,420 --> 00:11:13,910
becomes accessible to you, and here
a simple example of that has been shown.

172
00:11:13,910 --> 00:11:18,339
So x equal to robjects.IntVector
range ten gives you

173
00:11:18,339 --> 00:11:21,620
first the numbers there and r.rnorm.

174
00:11:21,620 --> 00:11:24,470
Rnorm is the family r function
that we have seen many times, we

175
00:11:24,470 --> 00:11:29,200
are simply taking ten numbers out of that
and calling x element to plot it later on.

176
00:11:29,200 --> 00:11:33,220
So, this functionality of using r
through python is also possible,

177
00:11:33,220 --> 00:11:36,780
and with normpi you can use it
in a very effective manner.

178
00:11:38,630 --> 00:11:43,860
I don't know if some of you noticed, but
when we printed information about the way

179
00:11:43,860 --> 00:11:49,790
these variables in our Galaxy, the input
to RA was given to five decimal places.

180
00:11:49,790 --> 00:11:54,020
But when we printed it out,
then it was only to four decimal places.

181
00:11:54,020 --> 00:11:56,530
So was the fifth decimal place lost?

182
00:11:56,530 --> 00:11:58,080
Is R not precise?

183
00:11:58,080 --> 00:12:03,390
That's certainly not so because, if you
see slot of RA here you get only four

184
00:12:03,390 --> 00:12:09,150
places, but so
where did the last digit vanish?

185
00:12:12,320 --> 00:12:15,750
So, this is something
about the precision in R.

186
00:12:15,750 --> 00:12:19,400
The default precision
is at a certain level.

187
00:12:19,400 --> 00:12:23,990
But you can also insert R to use
a slightly different precision.

188
00:12:23,990 --> 00:12:27,190
So you could say that I
want to use ten digits, and

189
00:12:27,190 --> 00:12:30,430
you can say that by saying options,
digit equal, digits equal to ten.

190
00:12:30,430 --> 00:12:34,110
And that's again, a suggestion,
you can go up to 22.

191
00:12:34,110 --> 00:12:36,700
So, when does it get used and
when does it not get used?

192
00:12:36,700 --> 00:12:38,550
So, look at the next example.

193
00:12:38,550 --> 00:12:42,930
Here, we are saying that
there is a sprint of pi, and

194
00:12:42,930 --> 00:12:45,795
we want it to 100 decimal places, and

195
00:12:45,795 --> 00:12:49,710
output is shown there it becomes
inaccurate after about 15 decimal places.

196
00:12:50,870 --> 00:12:55,525
But does that mean again that R is not
precise after about 15 decimal places?

197
00:12:55,525 --> 00:12:57,250
No.
Again there it depends on whether you

198
00:12:57,250 --> 00:13:00,870
are using large numbers or
you are sing very small numbers.

199
00:13:00,870 --> 00:13:03,580
So the precision can go arbitrary small.

200
00:13:03,580 --> 00:13:10,420
Well, not arbitrary small but up to 308
into the R of 308, which is fairly small.

201
00:13:10,420 --> 00:13:14,230
And then you can see by seeing
what the limit of double is,

202
00:13:14,230 --> 00:13:17,190
the Machine$double that is using.

203
00:13:17,190 --> 00:13:25,520
And then you can use additional tools
within R to go to quite high precision.

204
00:13:25,520 --> 00:13:30,270
But then again, when it is that
you made into problems, and

205
00:13:30,270 --> 00:13:33,160
that is what is shown in the next example.

206
00:13:33,160 --> 00:13:37,240
So if you are trying to
compare very small numbers.

207
00:13:37,240 --> 00:13:41,280
Versus significant digits, then some
things that you have to keep in mind

208
00:13:41,280 --> 00:13:46,510
are that if the differences
are very small, so that the numbers

209
00:13:46,510 --> 00:13:50,710
start looking constant and they're not
close to zero, then you can be in trouble.

210
00:13:50,710 --> 00:13:57,410
So the first example here shows that
we are assigning to x1 50 numbers with.

211
00:13:58,620 --> 00:14:01,970
A mean of one and
a standard deviation that is very small.

212
00:14:03,020 --> 00:14:04,420
One minus 15.

213
00:14:04,420 --> 00:14:04,920
Right.

214
00:14:06,000 --> 00:14:11,480
And why when is where we use something

215
00:14:11,480 --> 00:14:15,920
where we say that again we want
50 numbers and in this case,

216
00:14:15,920 --> 00:14:19,590
the mean is slightly different
with the same standard deviation.

217
00:14:19,590 --> 00:14:25,520
So, if you notice, then one and,
one and, just a small delta.

218
00:14:25,520 --> 00:14:29,900
Now these two numbers are very close
to each other and not close to zero.

219
00:14:29,900 --> 00:14:36,740
So, there comparison is going to look
as if it is a series of constants.

220
00:14:36,740 --> 00:14:38,980
And that is going to throw an error for
you,

221
00:14:38,980 --> 00:14:40,950
because all the numbers seem constant.

222
00:14:40,950 --> 00:14:47,630
But in the second example, what we do is
that we assign to x2 numbers that are,

223
00:14:47,630 --> 00:14:51,700
that have a mean of 0, and again,
a very small sign of deviation.

224
00:14:51,700 --> 00:14:53,150
And to the second vector,

225
00:14:53,150 --> 00:14:57,580
we assign numbers that are very
close to zero, mean, but not zero.

226
00:14:57,580 --> 00:14:59,640
And the same standard deviation.

227
00:14:59,640 --> 00:15:03,340
In this case if you try to
compare them you'll be fine.

228
00:15:03,340 --> 00:15:07,110
So again, it depends very much on what
kind of numbers you are trying to

229
00:15:07,110 --> 00:15:09,580
compare and what kind of
precision that you want with them.

230
00:15:09,580 --> 00:15:12,980
R will allow you to,
use them in a proper fashion.

231
00:15:14,460 --> 00:15:17,750
Next time we'll be seeing ggplot and
some advanced plotting.

