0% found this document useful (0 votes)
14 views

RCourse-Lecture53-Statistics-Some Examples of R Programming

This document provides examples of writing programs in R. It discusses the steps to write a program, including defining objectives, identifying inputs and outputs, and translating the problem into R code. It then provides two examples of writing functions to calculate mathematical expressions, demonstrating how to break problems into components and call functions within other functions.

Uploaded by

kavithanjali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

RCourse-Lecture53-Statistics-Some Examples of R Programming

This document provides examples of writing programs in R. It discusses the steps to write a program, including defining objectives, identifying inputs and outputs, and translating the problem into R code. It then provides two examples of writing functions to calculate mathematical expressions, demonstrating how to break problems into components and call functions within other functions.

Uploaded by

kavithanjali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Foundations of R Software

Lecture 53

Some Examples of R Programming

Shalabh
Department of Mathematics and Statistics
Indian Institute of Technology Kanpur

1
Steps to write a programme

 A programme is a set of instructions or commands which are


written in a sequence of operations i.e., what comes first and
what comes after that.

 The objective of a programme is to obtain a defined outcome


based on input variables.

 The computer is instructed to perform the defined task.

2
Steps to write a programme

 Computer is an obedient worker but it has its own language.

 We do not understand computer’s language and computer


does not understand our language.

 The software help us and works like an interpreter between


us and computer.

3
Steps to write a programme
 We say something in software’s language and software
informs it to computer.

 Computer does the task and informs back to software.

 The software translates it to our language and informs us.

4
Steps to write a programme
 Programme in R is written as a function using function.

 Write down the objective, i.e., what we want to obtain as an


outcome.

 Translate it in the language of R.

 Identify the input and output variables.

 Identify the nature of input and output variables, i.e.,


numeric, string, factor, matrix etc. 5
Steps to write a programme
 Input and output variables can be single variable, vector,
matrix or even a function itself.

 The input variables are the component of function which


are reported in the argument of function()

 The output of a function can also be input to another


function.

 The output of an outcome can be formatted as per the need


and requirement. 6
Steps to write a programme
Tips:
 Loops usually slower the speed of programmes, so better is to
use vectors and matrices.

 Use # symbol to write comment to understand the syntax.

 Use the variable names which are easy to understand.

 Don’t forget to initialize the variables.

7
Example 1 n

i
x 2
n
 xi 
2

Suppose we want to compute


i 1
n
and   
i 1  yi 
 i
y 2

i 1

Data x1 , x2 ,..., xn y1 , y2 ,..., yn

x, y: Two data vectors

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

We need summation, so use sum function or alternatively


compute it through vectors.

9
Example 1
# Remove all data
rm(list = ls())

# Define input data vectors, for example


x = c(10,20,30)
y = c(1,2,3)

++++++START OF FUNCTION++++++++
example1 = function(x,y)

# Start of function body


{
# First give all other input variables

# Computation of number of observations


n = length(x)
10
CONTD…
Example 1
CONTD…
#Initialize the values to store squared values
x1 = 0
y1 = 0
z1 = 0

#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

# Format the output


cat("The value of g and h are", g, "and", h,
"\n“, )
}
++++++END OF FUNCTION++++++++
12
Example 1: At a glance
example1 <- function(x,y)
{
n = length(x)
x1 = 0
y1 = 0
z1 = 0
for (i in 1:n)
{
x1[i] = x[i]^2
y1[i] = y[i]^2
z1[i] = (x[i]/y[i])^2
}
sum_square_x = sum(x1)
sum_square_y = sum(y1)
sum_square_z = sum(z1)
g = sum_square_x/sum_square_y
h = sum_square_z
cat("The value of g and h are", g, "and", h,
"respectively", "\n")
} 13
Example 1

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

We break this function in two components –


‐‐ Compute g(x,y) as a function and then
‐‐ compute f(x,y) by calling g(x,y).

20
Example 2
# Remove all data
rm(list = ls())

# Define input data vectors


x
y

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

There is no need to calculate the value of g(x,y).

Just by changing the values of x and y, one can get different


required outcomes.
26
Example 3
Suppose we want to compute

  x  ln(1  x3 ) 
exp  2  if x  0
  x 

f ( x)  10 if x  0
 3
 2  x
if x  0
 x

and plot with line over a values of x as a sequence starting from


‐1 to 5 and increasing it by 0.2.

27
Example 3
Input variable : x
Output variable: f

# Remove all data


rm(list = ls())

# Define input data


x

CONTD…

28
Example 3
CONTD…
f = function(x)
{
if(x>0) {exp((x+log(1+x^3))/x^2)}

else if(x==0) {10}

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

You might also like