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

Business Forecasting - Lab Manual

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

Business Forecasting - Lab Manual

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

DIRECTORATE OF ONLINE EDUCATION

SRM INSTITUTE OF SCIENCE AND TECHNOLOGY


KATTANKULATHUR, CHENNAI

LAB MANUAL

Course: MBA (BA)

SEMESTER- III

SUBJECT TITLE
Business Forecasting

SUBJECT CODE
V20PBBA03
Basic Operations in R

Basic Commands:

• To get current working directory: getwd()


• List all the objects in your local workspace using ls() command.
• List all the files in your working directory using list.files() or dir().
Examples: Arithmetic Operations in R:

(i) 4+4 # Addition

## [1] 8

(ii) 4-4 # Subtraction

## [1] 0

(iii) 4*4 # Multiplication

## [1] 16

(iv) 4/4 # Division

## [1] 1

Experiment 1. R program to calculate factorial value

R Programming Language offers a factorial() a function that can compute the factorial of a
number without writing the whole code for computing factorial.

Algorithm:

Syntax: factorial(x)

Parameters: x: The number whose factorial has to be computed.

Returns: The factorial of the desired number.

Method 1:

# Using factorial() method

answer1 <- factorial(4)


answer2 <- factorial(3)

answer3 <- factorial(0)

print(answer1)

print(answer2)

print(answer3)

Output:

24

Method 2: Compute the factorial of multiple values

# R program to calculate factorial value

# Using factorial() method

answer1 <- factorial(c(0, 1, 2, 3, 4))

print(answer1)

Output:

1 1 2 6 24

Experiment 2. R Program to Print the Fibonacci Sequence

The Fibonacci sequence is a series of numbers in which each number (known as a Fibonacci
number) is the sum of the two preceding ones. The sequence starts with 0 and 1, and then each
subsequent number is the sum of the two previous numbers. The Fibonacci sequence has many
applications in various fields, including mathematics, computer science, and nature.
To generate the Fibonacci sequence in R Programming Language, we will use a loop. The
sequence is defined as follows:

• The first two numbers are 0 and 1.


• The next number is the sum of the two preceding numbers.

Steps:

To print the Fibonacci sequence in R, follow these steps:

1. Take the input for the number of terms (n) to be generated in the sequence.
2. Use a loop to generate the Fibonacci numbers.
3. Print the numbers in the sequence.

# Function to print the Fibonacci sequence using a loop

print_fibonacci <- function(n) {

a <- 0

b <- 1

cat("Fibonacci Sequence:")

for (i in 1:n) {

cat(a, " ")

next_num <- a + b

a <- b

b <- next_num

# Example usage

number_of_terms <- 10

print_fibonacci(number_of_terms)
Output:

Fibonacci Sequence:0 1 1 2 3 5 8 13 21 34

Experiment 3. R – Pie Charts

A pie chart is a circular statistical graphic, which is divided into slices to illustrate numerical
proportions. It depicts a special chart that uses “pie slices”, where each sector shows the relative
sizes of data. A circular chart cuts in the form of radii into segments describing relative
frequencies or magnitude also known as a circle graph.

R Programming Language uses the function pie() to create pie charts. It takes positive
numbers as a vector input.

Syntax: pie(x, labels, radius, main, col, clockwise)

Parameters:

• x: This parameter is a vector that contains the numeric values which are used in the pie
chart.
• labels: This parameter gives the description to the slices in pie chart.
• radius: This parameter is used to indicate the radius of the circle of the pie chart.(value
between -1 and +1).
• main: This parameter is represents title of the pie chart.
• clockwise: This parameter contains the logical value which indicates whether the slices
are drawn clockwise or in anti clockwise direction.
• col: This parameter give colors to the pie in the graph.

To create a simple R pie chart:

• By using the above parameters, we can draw a pie chart.


• It can be described by giving simple labels.

# Create data for the graph.

geeks<- c(23, 56, 20, 63)


labels <- c("Mumbai", "Pune", "Chennai", "Bangalore")

# Plot the chart.

pie(geeks, labels)

Output:

Experiment 4. R – Histograms

A histogram contains a rectangular area to display the statistical information which is


proportional to the frequency of a variable and its width in successive numerical intervals. A
graphical representation that manages a group of data points into different specified ranges. It
has a special feature that shows no gaps between the bars and is similar to a vertical bar graph.

We can create histograms in R Programming Language using the hist() function.

Syntax: hist(v, main, xlab, xlim, ylim, breaks, col, border)

Parameters:

• v: This parameter contains numerical values used in histogram.


• main: This parameter main is the title of the chart.
• col: This parameter is used to set color of the bars.
• xlab: This parameter is the label for horizontal axis.
• border: This parameter is used to set border color of each bar.
• xlim: This parameter is used for plotting values of x-axis.
• ylim: This parameter is used for plotting values of y-axis.
• breaks: This parameter is used as width of each bar.

Creating a simple histogram chart by using the above parameter. This vector v is plot using
hist().

# Create data for the graph.

v <- c(19, 23, 11, 5, 16, 21, 32,

14, 19, 27, 39)

# Create the histogram.

hist(v, xlab = "No.of Articles ",

col = "green", border = "black")

Output:

Experiment 5. R – Bar Charts

A bar chart also known as bar graph is a pictorial representation of data that presents categorical
data with rectangular bars with heights or lengths proportional to the values that they represent.
In other words, it is the pictorial representation of the dataset. These data sets contain the
numerical values of variables that represent the length or height.

R uses the barplot() function to create bar charts.

Syntax:

barplot(H, xlab, ylab, main, names.arg, col)

Parameters:

• H: This parameter is a vector or matrix containing numeric values which are used in
bar chart.
• xlab: This parameter is the label for x axis in bar chart.
• ylab: This parameter is the label for y axis in bar chart.
• main: This parameter is the title of the bar chart.
• names.arg: This parameter is a vector of names appearing under each bar in bar chart.
• col: This parameter is used to give colors to the bars in the graph.

Creating a Simple Bar Chart in R

In order to create a Bar Chart:

A vector (H <- c(Values…)) is taken which contains numeral values to be used.

This vector H is plot using barplot().

# Create the data for the chart

A <- c(17, 32, 8, 53, 1)

# Plot the bar chart

barplot(A, xlab = "X-axis", ylab = "Y-axis", main ="Bar-Chart")


Output:

You might also like