RCourse-Lecture53-Statistics-Some Examples of R Programming
RCourse-Lecture53-Statistics-Some Examples of R Programming
Lecture 53
Shalabh
Department of Mathematics and Statistics
Indian Institute of Technology Kanpur
1
Steps to write a programme
2
Steps to write a programme
3
Steps to write a programme
We say something in software’s language and software
informs it to computer.
4
Steps to write a programme
Programme in R is written as a function using function.
7
Example 1 n
i
x 2
n
xi
2
i 1
8
Example 1
Input variables : x, y, n (if x and y have different number
of observations, choose different numbers, say n1 and n2)
i
x 2
n
xi
2
Output variables: g, h, g i 1
n
and h
i 1 yi
i
y 2
i 1
9
Example 1
# Remove all data
rm(list = ls())
++++++START OF FUNCTION++++++++
example1 = function(x,y)
#Start of loop
for (i in 1:n)
{
# Define x1, y1 and z1 to store their squares
x1[i] = x[i]^2
y1[i] = y[i]^2
z1[i] = (x[i]/y[i])^2
#End of loop
}
CONTD…
11
Example 1
CONTD…
# Obtain the sum of squared quantities
sum_square_x = sum(x1)
sum_square_y = sum(y1)
sum_square_z = sum(z1)
# Computation of g and h
g = sum_square_x/sum_square_y
h = sum_square_z
14
Example 1
15
Example 1
> x=c(10,20,30)
> y=c(1,2,3)
> example1(x,y)
The value of g and h are 100 and 300 respectively
> x=c(67,87,26,85,6,45)
> y=c(54,64,22,94,20,88)
> example1(x,y)
The value of g and h are 0.8996568 and 5.953203
respectively
Just by changing the values of x and y, one can get required different
outcomes.
16
Example 1
17
Example 1 (Alternative approach)
Input variables : x, y, n
i
x 2
n
xi
2
Output variables: g, h, g i 1
n
and h
i 1 yi
i
y 2
i 1
g = sum(x^2)/sum(y^2)
h = sum(x/y)^2
18
Example 2
Suppose we want to compute
2
x ln y 2
y x ln y 3
f ( x, y )
3 exp
x ln y y
5
y
This can be written as
g ( x, y )
2 2
f ( x, y )
3
exp g ( x , y )
3
5 g ( x, y )
x ln y
where g ( x, y )
y
19
Example 2
Input variables : x, y
Output variables: : f
20
Example 2
# Remove all data
rm(list = ls())
CONTD…
21
Example 2
CONTD…
# define g(x,y)
g = function(x,y) x ln y
g ( x, y )
# Start of function y
{
(x+log(y))/y
# End of function
}
++++++++++++++++++++++++++++++++++++++
g ( x, y )
2 2
# define f(x,y) f ( x, y ) exp g ( x, y ) 3
3
f = function(x,y) 5 g ( x, y )
{
(((g(x,y))^2)/(5+(g(x,y))^3))*(exp(g(x,y)))^(2/3)
} 22
Example 2: At a glance
# define g(x,y)
g = function(x,y)
{
(x+log(y))/y
}
++++++++++++++++++++++++++++++++++++++
# define f(x,y)
f = function(x,y)
{
(((g(x,y))^2)/(5+(g(x,y))^3))*(exp(g(x,y)))^(2/3)
}
# g(x,y) must have been defined earlier.
23
Example 2
24
Example 2
25
Example 2
> x=10
> y=20
> f(x,y)
[1] 0.1234539
> x=1896
> y=23454
> f(x,y)
[1] 0.001394291
x ln(1 x3 )
exp 2 if x 0
x
f ( x) 10 if x 0
3
2 x
if x 0
x
27
Example 3
Input variable : x
Output variable: f
CONTD…
28
Example 3
CONTD…
f = function(x)
{
if(x>0) {exp((x+log(1+x^3))/x^2)}
else {(2+x^3)/x}
}
x ln(1 x 3 ) CONTD…
exp 2 if x 0
x
f ( x) 10 if x 0
3
2 x
if x 0
x
29
Example 3
CONTD…
h = function()
# Start of function
{
# Generation of data on x
x = seq(-1,5,by=0.2)
# Initialization of y to store values of f(x)
y = 0
CONTD…
30
Example 3
CONTD…
# Generation of f(x) values corresponding to x
for(i in 1:length(x))
{
y[i] = f(x[i])
}
# length(x) and length(y) must be same to plot
# y = f(x) with respect to x
plot(x, y, type = "l")
}
31
Example 3: At a glance
f = function(x)
{
if(x>0) {exp((x+log(1+x^3))/x^2)}
else if(x==0) {10}
else {(2+x^3)/x}
}
h = function()
{
x = seq(-1,5,by=0.2)
y = 0
for(i in 1:length(x))
{
y[i] = f(x[i])
}
plot(x,y,type = "l")
32
}
Example 3
33
Example 3
34
Example 3
> f(123)
[1] 1.009126
> f(-123)
[1] 15128.98
> f(0)
[1] 10
> f(8)
[1] 1.249201
> f(-4)
[1] 15.5
> f(0)
35
[1] 10
Example 3
> h()
150
100
y
50
0
-1 0 1 2 3 4 5
x
36