0% found this document useful (0 votes)
76 views106 pages

Business Forecasting Lab Manual - 374

This document is a record of work for the Business Forecasting course at SRM Institute of Science and Technology for the academic year 2024-2025, authored by student Jaivignesh S. It includes a bonafide certificate, a table of contents detailing various forecasting topics and methods, and an introduction to forecasting principles and techniques. The document outlines the importance of forecasting in decision-making, planning, and resource allocation, as well as various qualitative and quantitative forecasting methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views106 pages

Business Forecasting Lab Manual - 374

This document is a record of work for the Business Forecasting course at SRM Institute of Science and Technology for the academic year 2024-2025, authored by student Jaivignesh S. It includes a bonafide certificate, a table of contents detailing various forecasting topics and methods, and an introduction to forecasting principles and techniques. The document outlines the importance of forecasting in decision-making, planning, and resource allocation, as well as various qualitative and quantitative forecasting methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 106

SRM INSTITUTE OF SCIENCE AND TECHNOLOGY

Kattankulathur, Chennai-603 203

FACULTY OF MANAGEMENT

MB20BA02 BUSINESS FORECASTING

RECORD WORK
Academic Year 2024-2025

Name of the Student JAIVIGNESH.S

Register Number RA2352001010374

Name of the Programme M.B.A

Year & Semester II YEAR & III SEMESTER

Section G - SECTION

Page | 1
SRM INSTITUTE OF SCIENCE AND TECHNOLOGY
FACULTY OF MANAGEMENT

BONAFIDE CERTIFICATE

This is to certify that this is the Bonafide record of work done


by_____________________JAIVIGNESH.S____________________________
Registration No.__RA2352001010374_ of SecondYear M.B.A.Degree Course
MBG23B02L BUSINESSFORECASTINGin the Faculty of Management,
SRM Institute of Science and Technology, Kattankulathur during the Academic
Year 2024-2025.

Dr.P.Poonguzhali Dr.V.M.Shenbagaraman
Faculty In-charge Chair – IS & Tech Mgt

Dr.Shivganesh Bhargava
Dean

Submitted for the University Examination held on ____________, SRM


Institute of Science and Technology, Kattankulathur.

Internal Examiner I Internal Examiner II

Page | 2
Table of Contents

Ex. Page
Date Topic Signature
No. No.

1 22.7.24 Forecasting – Introduction 5

2 24.7.24 Forecasting Methods 11


29.7.24 Forecasting through MS-Excel Spreadsheets
3 17
by using naive method
30.7.24 Forecasting through MS-Excel Spreadsheets
4 19
by using Excel function
2.8.24 Forecasting through MS-Excel Spreadsheets
5 21
by using moving average method
5.8.24 Forecasting through MS-Excel Spreadsheets
6 23
by using weighted average method
8.8.24 Forecasting through MS-Excel Spreadsheets
7 26
by using weighted moving average method
9.8.24 Forecasting through MS-Excel Spreadsheets
8 29
by using exponential smoothing method
12.8.24 Forecasting through MS-Excel Spreadsheets
9 by using moving average method through 31
data analysis option
14.8.24 Forecasting through MS-Excel Spreadsheets
10 by using exponential smoothing method 33
through data analysis option
18.8.24 Forecasting through MS-Excel Spreadsheets
by using regression method with one
11 35
varying variable through data analysis
option
20.8.24 Forecasting through MS-Excel Spreadsheets
by using regression method with two
12 37
varying variables through data analysis
option
27.8.24 Computation of Forecasting errors – Mean
13 Square Deviation (MSD), Mean Absolute 39
Deviation (MAD) and Tracking Signal (TS)
29.8.24 Forecasting through MS-Excel Spreadsheets
14 by using Auto-Regressive Integrated 41
Moving Average (ARIMA) method

Page | 3
15 30.8.24 IntroductionToR Program and its Packages 44
16 2.9.24 Basics of R Program 57
17 4.9.24 Data Slicing inR Program 69
18 6.9.24 Importing and Exporting of Data in R Program 73
19 11.9.24 Basics of R Program through SWIRL Package 75
12.9.24 Handling of Data using Matrices and Data
20 76
frame in R Program
21 14.9.24 Time and Date Function in R Program 78
16.9.24 Exploring the Data using DPLYR Package
22 79
in R Program
20.9.24 R Commander Package – File and Data in R
23 81
Program
24 22.9.24 Data Frame in R Commander 83
24.9.24 Forecasting by using Regression Method in
25 85
R Program for New Data
26.9.24 Forecasting by using Regression method
26 87
through R Commander
4.10.24 Forecasting by using Regression Method
27 88
with Categorical Variables in R Program
6.10.24 Forecasting by using Naïve method in R
28 89
Program
14.10.24 Forecasting by using Bivariate Regression
29 91
in R Program
16.10.24 Forecasting by using Multiple Regression in
30 93
R Program
21.10.24 Forecasting by using Moving Average
31 95
Method in R Program
24.10.24 Forecasting by using Auto Regressive
32 Integrated Moving Average (ARIMA) 96
Method in R Program

Ex. No. 1
Forecasting - Introduction
Page | 4
Aim:
To learn about basics of forecasting

Forecasting - Meaning

Forecasting is a process of making predictions or estimates about future events based


on past and present data and analysis. It plays a crucial role in various fields and industries,
helping organizations and individuals plan for the future.

Forecasting involves predicting future trends, outcomes, or events based on historical


data, analysis of current conditions, and the application of various forecasting methods. It is
essentially an attempt to reduce uncertainty about the future and make informed decisions.

Objectives of Forecasting

 Planning: Forecasting helps in planning for the future by providing insights into
potential developments and trends.
 Decision-Making: It aids decision-makers by providing them with information to
anticipate and respond to future changes effectively.
 Resource Allocation: Forecasting assists in allocating resources such as manpower,
funds, and materials efficiently.
 Risk Management: By anticipating future events, organizations can better manage
risks and uncertainties.
 Performance Evaluation: Forecasting allows for the comparison of actual results
with predicted outcomes, facilitating performance evaluation.

Characteristics of Forecasting

 Based on Historical Data: Forecasting relies on past data and trends to make
predictions about the future.

Page | 5
 Subject to Uncertainty: The future is uncertain, and forecasting acknowledges this by
providing probability estimates rather than absolute predictions.
 Continuous Process: Forecasting is an ongoing process that requires regular updates
as new information becomes available.
 Informed Decision-Making: The primary purpose of forecasting is to support
decision-making processes by providing valuable insights.
 Method Diversity: Various methods, including qualitative and quantitative
approaches, can be used for forecasting, depending on the nature of the data and the
context.

Forecasting Principles

 Rarely perfect: Since it is future expected value, nobody could able to calculate exact
value. All the available methods will give approximate value, but expected to very
close to actual value, and hence calculated forecasted values are expected as rarely
perfect.
 Closely accurate for grouped data than individual data: As per the universal thumb
rule of statistics and future predictions, more data will give more accurate results. As
per this logic, increased number of data will yield close accurate data towards actual
value than the minimal or individual data.
 Closely accurate for shorter period’s data than longer period data: Old data and
very old data are considered as outdated data and hence these data may not give
logical results. In other hand, latest data are considered as fresh data and they will
yield close accurate data towards actual value than the longer period or older period
outdated data.
 Patterns and methods are customised based on products and industry: Nature and
demand of every individual products will vary based on several factors. Hence no one
could expect standard patterns or methods to be adopted for the purpose of forecasting
any individual products. It will be varied product to product, sometimes for the same
product it will be varied to period to period in customized manner.
Overview of forecasting process

• These seven steps can generate forecasts.


• Determine what the forecast is for.

Page | 6
• Select the items for the forecast.
• Select the time horizon.
• Select the forecast model type.
• Gather data to be input into the model.
• Make the forecast.
• Verify and implement the results.

Factors affecting demand forecasting

• Technological update
• Consumer preferences
• Government policies and regulations
• Product life cycle
• Economic condition of both company and country
• Market competitions
• Price of the product
• Sales effort
• Advertising and promotions
• Credit policies
• Product / service quality

Types of Forecasting

• Based on use
• Demand Forecasting
• Economic Forecasting
• Technological Forecasting
• Sales Forecasting
• So on….
• Based on Time period
• Short term Forecasting
• Medium term Forecasting
• Long term Forecasting

Forecasting Patterns

Page | 7
• Trend (Straight line) pattern

• Seasonal pattern

• Cyclical pattern

Page | 8
• Historical (stationary) pattern

• Random pattern

Page | 9
Some examples of forecasting decisions

• Marketing
• Demand
• Market share
• Price trend
• Production
• Materials requirements
• Materials cost
• Labour costs
• Maintenance requirements
• Plant capacity
• Finance
• Cash flow
• Rates of expenses
• Revenues
• Personnel
• Number of workers in each category
• Labour turnover
• Absenteeism
Conclusion:

Learned some basic aspects of forecasting

Ex. No. 2
Forecasting Methods
Date:

Page | 10
Aim:

To understand various methods of forecasting techniques

Methods of Forecasting:

There are numerous methods of forecasting. All the methods can be categorized under
two categories such as qualitative methods and quantitative methods. Some of those methods
are given below.

Qualitative forecasting

Qualitative forecasting methods rely on subjective judgment, expert opinions, and


qualitative information rather than historical data and quantitative analysis. These methods
are particularly useful when historical data is scarce, unreliable, or when forecasting for a
new product or in an emerging market. Here are some common qualitative forecasting
methods:

Expert opinion / Delphi technique:

 Panel discussions:Involves gathering opinions from a group of experts in the relevant


field. The Delphi method is a structured approach to achieving consensus among a
panel of experts through multiple rounds of surveys and feedback.

Market Research:

 Surveys and Questionnaires: Collecting data through surveys and questionnaires to


gather opinions and insights from potential customers, industry experts, or other
stakeholders.

 Focus Groups: Bringing together a small group of individuals to discuss and provide
qualitative feedback on a product or service.

Market Feedback from Sales Force:

 Sales force opinion:Incorporating the opinions and insights of the sales team, who are

Page | 11
in direct contact with customers and have a good understanding of market conditions.

Customer Feedback:

 Customer interviews:Directly interviewing customers to gather insights into their


preferences, expectations, and future behavior.

 Feedback forms:Collecting feedback from customers through various channels, such


as online forms, comments, and reviews.

Technology Assesment:

 Technology Forecasting:Examining technological trends and advancements to


predict how they might impact a particular industry or market.

Economic indicators:

 Economic Forecasting: Analyzing economic indicators and trends to make


qualitative predictions about the future state of the economy and its impact on a
business.

Analogies and Historical Analogs:

 Analog Forecasting: Making predictions based on analogies to similar situations or


historical patterns.

Scenario analysis:

 Scenario planning:Developing multiple scenarios or stories about potential future


events and conditions. This helps in identifying a range of possible outcomes and their
associated probabilities.

Composite Forecasting:

 Combining methods: Combining both quantitative and qualitative methods to take


advantage of the strengths of each approach.

Qualitative forecasting methods are often used in conjunction with quantitative methods
to provide a more comprehensive and accurate picture of future trends. These methods
require careful consideration of the expertise and biases of the individuals providing the
qualitative input and may involve a degree of uncertainty compared to purely data-driven
approaches.

Quantitative forecasting

Page | 12
Quantitative forecasting methods rely on historical data and mathematical models to
make predictions about future values. These methods are particularly useful when there is a
sufficient amount of reliable historical data, and the relationships between variables can be
quantified. Here are some common quantitative forecasting methods:

Naïve method:

The naive method of forecasting is a simple and straightforward approach that


involves using the most recent observation as the forecast for future periods. In other words,
it assumes that future values will be the same as the last observed value. The naive forecast is
easy to implement and serves as a baseline model for more sophisticated forecasting methods.

There are two common variations of the naive forecasting method:

 Single naïve method:The forecast for the next period is simply the value of
the last observed data point.
 Seasonal naïve method:In cases where there is a repeating pattern or
seasonality in the data, the seasonal naive method uses the value from the
same season in the previous year as the forecast for the corresponding period
in the current year.

Average analysis method:

Average analyses in forecasting involve using various techniques to calculate average


values, which can then be used to make predictions or gain insights into trends. Here are
some common types of average analyses in forecasting:

 Weighted average analysis:Assigns different weights to individual data points based


on their significance. This can be particularly useful when certain data points have a
more significant impact on the overall average.
 Seasonal averages / Semi-average analysis:Calculates averages based on specific
seasons or time periods, helping to identify and analyze seasonal patterns in data.

Moving average analysis:

Page | 13
Moving average analysis is a time series forecasting method that involves calculating
averages of subsets of sequential data points to identify trends or patterns. It is particularly
useful for smoothing out short-term fluctuations and highlighting longer-term trends in the
data. Moving averages are commonly used in forecasting, and there are different types of
moving averages, including simple moving averages (SMA) and exponential moving
averages (EMA).

 Simple Moving Average:Averages the data points within a specified window or


period. It smoothens out fluctuations and highlights trends over time.
 Weighted moving average analysis:Assigns different weights to different data points,
giving more importance to recent values.
 Exponential moving average analysis: Exponential moving average analysisgives
more weight to recent observations, and it is calculated using a smoothing parameter
(α)
EMAt = (α x Yt) + [(1- α) x EMAt-1]
 Seasonal moving / Double moving average analysis:Averages the data points within
a specified window or period based on specific seasons or time periods, helping to
identify and analyze seasonal patterns in data.

Regression analysis:

 Linear regression analysis:Involves a single independent variable. Models the


relationship between a dependent variable and one variable using a linear equation.
The relationship between the dependent variable ‘Y’ and the independent variable ‘X’
is represented by a straight line equation:
Y=a+bX

where, ‘a’ is intercept value, ‘b’ is future time period and ‘X’ is independent variable
value

 Multiple regression analysis:Extends linear regression to include multiple


independent variables in the model. The relationship between the dependent variable
‘Y’ and the independent variables ‘X1, X2, X3, ……… Xn’ is represented by a straight
line equation:
Y = a + b1 X1 + b2 X2 + b3 X3 + ………….. bnXn

Page | 14
 Auto-regressive Integrated Moving Average (ARIMA) Regression: Combines
regression analysis with the ARIMA time series forecasting model to account for both
the trend and seasonality in the data.
Exponential smoothing analysis:

Exponential smoothing models are time series forecasting methods that use weighted
averages of past observations to predict future values. These models are particularly useful
when there is a trend or seasonality in the data. Exponential smoothing methods assign
exponentially decreasing weights to past observations, giving more importance to recent data
points. Here are some common types of exponential smoothing models:

 Simple (single) exponential smoothing analysis:Simple Exponential Smoothing is


suitable for time series data with no clear trend or seasonality. The forecast is based
on a weighted average of past observations, and it is updated with each new data
point.
The formula for the forecast (Ft) is, Ft = [α x Yt]+ [(1 – α) x Ft – 1]

Where, where Yt is the actual observation at time t, α is the smoothing parameter (0<
α <1), and Ft – 1 is the forecast for the previous period.

 Double exponential smoothing analysis (Holt’s Method):Double Exponential


Smoothing is suitable for time series data with a linear trend. It extends simple
exponential smoothing by incorporating a trend component. There are two smoothing
parameters: one for the level (α) and one for the trend (β).
The formulas for the level (Lt) and trend (Tt) are:

Lt = [α x Yt]+ [(1 – α) x (Lt – 1+ Tt – 1)]

Tt = [β x Lt - Lt-1]+ [(1 – β) x Tt – 1]

The forecast (Ft) is the sum of the level and trend components: Ft = Lt + Tt

 Triple Exponential Smoothing (Holt Winter’s Method): Triple Exponential


Smoothing is suitable for time series data with both trend and seasonality. It extends
double exponential smoothing by incorporating a seasonality component. There are
smoothing parameters for the level (α), trend (β), and seasonality (γ).
The formulas for the level (Lt) and trend (Tt), and seasonality (St) are:

Page | 15
The forecast (Ft) is the product of the level, trend, and seasonality components:

Ft = (Lt + Tt) X St-m+1

These exponential smoothing models provide a balance between simplicity and


effectiveness. The choice of the smoothing parameters (α, β, γ) depends on the characteristics
of the data, and they are often determined through optimization or cross-validation.
Exponential smoothing models are widely used in various industries for short- to medium-
term forecasting.

As a concluding remarks on quantitative forecasting methods, when choosing a


quantitative forecasting method, it's crucial to consider the characteristics of the data, the
assumptions of the chosen method, and the specific requirements of the forecasting task.
Additionally, regular model evaluation and refinement are essential for maintaining the
accuracy of forecasts over time.

Conclusion:

Learned the various forecasting methods under both qualitative and quantitative
categories.

Page | 16
Ex. No. 3 Forecasting through MS-Excel Spreadsheets by
using naive method
Date:

Aim:
To determine forecasting values through MS-Excel Spreadsheets by using naive method

Problem:
Following data represents the last one year sales of a company on weekly basis. Compute the
next one month forecasting values by using naïve method and draw the line graph for all 56 values
which comprises actual sales values of 52 weeks and forecasting values of next 4 weeks.

Week Sales Week Sales Week Sales Week Sales


No. No. No. No.
1 6109 14 558 210 882 40 888
2 662 15 568 28 662 41 1232
3 1099 16 1015 29 818 42 13810
4 680 110 10109 30 851 43 1420
5 583 18 8106 31 1119 44 1146
6 1098 19 862 32 1129 45 1361
10 608 20 884 33 10100 46 1498
8 1030 21 1052 34 1082 410 1100
9 544 22 858 35 1089 48 1339
10 608 23 863 36 1061 49 10106
11 1058 24 1056 310 1102 50 1424
12 562 25 858 38 869 51 1310
13 681 26 689 39 1025 52 1066

Procedure:
Step 1: Transfer all the given sales values in to MS-Excel spreadsheet along with week numbers in
first row first column of spreadsheet along with title row.
Step 2: Open the new third column (C column) and provide title as “forecasting value by using naïve
method”.
Step 3: Choose C3 column and put following formula as “=B2”

Page | 17
Step 4: Drag this formula till 52 weeks.
Step 5: The arrived value of 53rd week considered as forecast value of 54th week.
Step 6: Since we have to calculate one month (4 weeks) forecast value, drag the A column from A52
to A56 and similarly C column from C52 to C56.

Step 10: Use 53rd forecast value as actual value of 53rd week and find out forecast value of 54th week.
Repeat this step till 56 weeks.

Step 8: Declare the result for next one month forecast values with the help of last four values of B
column.
(Note: Naïve method is very simple and not involving and calculation instead considering previous
value as next duration’s forecast value)
Step 9: Select all the naïve forecasted values and insert line graph.
Step 10: Provide X axis title as “Week No.” and Y axis title as “Sales values” and chart title as
“Forecasting chart by using naïve method”.
Output:

Result:
Calculated the forecasting values of next one month which comprises of four weeks by using
naïve method and given the result below.Similarly line graph has been drawn and pasted for the
actual values along with calculated forecasting values.

Page | 18
Ex. No. 4 Forecasting through MS-Excel Spreadsheets by
using Excel function
Date:

Aim:
To determine forecasting values through MS-Excel Spreadsheets by using Excel function

Problem:
Following data represents the last one year sales of a company on weekly basis. Compute the
next one month forecasting values by using Excel function and draw the line graph for all 56 values
which comprises actual sales values of 52 weeks and forecasting values of next 4 weeks

Week Sales Week Sales Week Sales Week Sales


No. No. No. No.
1 6109 14 558 210 882 40 888
2 662 15 568 28 662 41 1232
3 1099 16 1015 29 818 42 13810
4 680 110 10109 30 851 43 1420
5 583 18 8106 31 1119 44 1146
6 1098 19 862 32 1129 45 1361
10 608 20 884 33 10100 46 1498
8 1030 21 1052 34 1082 410 1100
9 544 22 858 35 1089 48 1339
10 608 23 863 36 1061 49 10106
11 1058 24 1056 310 1102 50 1424
12 562 25 858 38 869 51 1310
13 681 26 689 39 1025 52 1066

Procedure:
Step 1: Transfer all the given sales values in to MS-Excel spreadsheet along with week numbers in
first row first column of spreadsheet along with title row.
Step 2: Choose the B54 cell and put-up the forecast function as below
=FORECAST(A54,B2:B53,A2:A53)
(Note: The function format is “FORECAST(Unknown X, Known Ys, Known Xs)
Page | 19
Note: By default, all the first column values (Duration) are considered as Xs and second column
values (Sales) are Ys.
Step 3: Enter and get the result
Step 4: Since we have to calculate one month (4 weeks) forecast value, consider this calculated
forecast value as actual value of next week and repeat the process for 56 weeks.
Step 5: For this purpose, drag the A column from A52 to A56 and similarly B column from B52 to
B56.
Step 6: Declare the result for next one month forecast values with the help of last four values of B
column.
Step 10: Put-up all the 56 values (actual sales values of 52 weeks and forecasted values of next 4
weeks) in separate column and insert line graph.
Step 8: Provide X axis title as “Week No.” and Y axis title as “Sales values” and chart title as
“Forecasting chart by using Excel function”.
Output:

Result:
Calculated the forecasting values of next one month which comprises of four weeks by using
Excel function and given the result below.Similarly line graph has been drawn and pasted for the
actual values along with calculated forecasting values.

11 44800
12 458102.102
13 46945.45

Page | 20
Ex. No. 5 Forecasting through MS-Excel Spreadsheets by
using moving average method
Date:

Aim:
To determine forecasting values through MS-Excel Spreadsheets by using moving average
method

Problem:
Following data represents the last one year sales of a company on weekly basis. Compute the
next one month forecasting values by using moving average method, by using 4 weeks moving
average period and draw the line graph for all 56 values which comprises actual sales values of 52
weeks and forecasting values of next 4 weeks

Week Sales Week Sales Week Sales Week Sales


No. No. No. No.
1 6109 14 558 210 882 40 888
2 662 15 568 28 662 41 1232
3 1099 16 1015 29 818 42 13810
4 680 110 10109 30 851 43 1420
5 583 18 8106 31 1119 44 1146
6 1098 19 862 32 1129 45 1361
10 608 20 884 33 10100 46 1498
8 1030 21 1052 34 1082 410 1100
9 544 22 858 35 1089 48 1339
10 608 23 863 36 1061 49 10106
11 1058 24 1056 310 1102 50 1424
12 562 25 858 38 869 51 1310
13 681 26 689 39 1025 52 1066

Procedure:
Step 1: Transfer all the given sales values in to MS-Excel spreadsheet along with week numbers in
first row first column of spreadsheet along with title row.
Step 2: Open the new third column and provide title as “forecasting value by using moving average

Page | 21
method”.
Step 3: Choose the week number 4th cell of column 3 and calculate the average of first four values by
using following formula: =AVERAGE(B2:B5)
Step 4: Drag this formula downward till 56 weeks, because we need to calculate next one month
forecasting value, one month comprises of 4 weeks; so 52 + 4 = 56 weeks.
Step 5: Last value is considered as forecasting value of 53 rd week and assume this value as actual
sales value of 53rd week. So transfer the 52nd week’s forecasting value (C53) in to 53 rd week’s actual
sales value (B54).
Step 6: Repeat this transfer (Previous week’s forecasting value is considered as next week’s actual
value) of values till 56 weeks
Step 10: Last 4 forecasting values are considered as next month forecast value.
Step 8: Put-up all the 56 values (actual sales values of 52 weeks and forecasted values of next 4
weeks) in separate column and insert line graph.
Step 9: Provide X axis title as “Week No.” and Y axis title as “Sales values” and chart title as
“Forecasting chart by using Excel function”.
Output:

Result:
Calculated the forecasting values of next one month which comprises of four weeks by using
4 weeks moving average period and given the result below.Similarly line graph has also been drawn
and pasted for the actual values along with calculated forecasting values.

Week No. Forecast values based on 4 weeks moving average period

53 1626

54 1635

55 1640

56 1650

Page | 22
Ex. No. 6 Forecasting through MS-Excel Spreadsheets by
using weighted average method
Date:

Aim:
To determine forecasting values through MS-Excel Spreadsheets by using weighted average
method

Problem:
Following data represents the last one year sales of a company on weekly basis. Compute the
next one month forecasting values by using weighted average method.Assign 60% weightage for
latest three months, 30% weightage for next latest three months, and 10% weightage for rest of the
weeks. Draw the line graph for all 56 values which comprises actual sales values of 52 weeks and
forecasting values of next 4 weeks

Week Sales Week Sales Week Sales Week Sales


No. No. No. No.
1 6109 14 558 210 882 40 888
2 662 15 568 28 662 41 1232
3 1099 16 1015 29 818 42 13810
4 680 110 10109 30 851 43 1420
5 583 18 8106 31 1119 44 1146
6 1098 19 862 32 1129 45 1361
10 608 20 884 33 10100 46 1498
8 1030 21 1052 34 1082 410 1100
9 544 22 858 35 1089 48 1339
10 608 23 863 36 1061 49 10106
11 1058 24 1056 310 1102 50 1424
12 562 25 858 38 869 51 1310
13 681 26 689 39 1025 52 1066

Procedure:
Step 1: Transfer all the given sales values in to MS-Excel spreadsheet along with week numbers in
first row first column of spreadsheet along with title row.

Page | 23
Step 2: Open the new third column (Column C) and name it as “weightage”.

Step 3:Assign the weights for each value in the third column as below

Latest three months (12 weeks - from 41st week to 52nd week) as 0.6 [60% = 60/100 = 0.6],

Next latest three months (12 weeks - from 29th week to 40th week) as 0.3 [30% = 30/100 = 0.3]

Rest of the values (28 weeks – from 1st to 28th week) as 0.1 [10% = 10/100 = 0.1]

Step 4: Open the new fourth column (Column D) and name it as “weighted values”.
Step 5: Multiply the each sales values along with its weightage by using following formula in the
fourth column (Column D).
=B2*B3 and drag down the same till 56 weeks
Step 6: Calculate the total sum of both 3 rd and 4th column (Column C and D) by using following
formula
=SUM(C2:C53) – For C column
=SUM(D2:D53) – For D column
Step 10: Expand the week number column till 56 weeks by putting the numbers such as 53, 54, 55
and 56
Step 8: Select the 53rd week second column cell (B54) and enter the following formula
=D54/C54 (Note: Total weighted value / Total weight)
Step 9: Assume arrived forecasting value of 53rd week as actual sales value of 53rd week and assign
weights accordingly
Latest three months (12 weeks - from 42nd week to 53rd week) as 0.6 [60% = 60/100 = 0.6],

Next latest three months (12 weeks - from 30th week to 41st week) as 0.3 [30% = 30/100 = 0.3]

Rest of the values (29 weeks – from 1st to 29th week) as 0.1 [10% = 10/100 = 0.1]
Step 10: Repeat the step 5 to step 8 and calculate 54th week forecasting value.
Step 11: Repeat the step 9 till you arrive 56 weeks forecasting values.
Step 12: Declare the result of next one month (Last calculated 4 weeks) forecasting values in the
below result section.
Step 13: Put-up all the 56 values (actual sales values of 52 weeks and forecasted values of next 4
weeks) in separate column and insert line graph.
Step 14: Provide X axis title as “Week No.” and Y axis title as “Sales values” and chart title as
“Forecasting chart by using Excel function”.

Page | 24
Output:

Result:
Calculated the forecasting values of next one month which comprises of four weeks by using
weighted average method and given the result below.Similarly line graph has also been drawn and
pasted for the actual values along with calculated forecasting values.

Week No. Forecast values based on weighted average method

53 1533

54 2455

55 4500

56 45105

Ex. No. 7 Forecasting through MS-Excel Spreadsheets by


Page | 25
using weightedmoving average method
Date:

Aim:
To determine forecasting values through MS-Excel Spreadsheets by using weighted moving
average method

Problem:
Following data represents the last one year sales of a company on weekly basis. Compute the
next one month forecasting values by using weighted moving average method.Assume 3 weeks
moving average period and 60% weightage for latest week, 30% weightage for next latest week, and
10% weightage for rest of the weeks. Draw the line graph for all 56 values which comprises actual
sales values of 52 weeks and forecasting values of next 4 weeks

Week Sales Week Sales Week Sales Week Sales


No. No. No. No.
1 6109 14 558 210 882 40 888
2 662 15 568 28 662 41 1232
3 1099 16 1015 29 818 42 13810
4 680 110 10109 30 851 43 1420
5 583 18 8106 31 1119 44 1146
6 1098 19 862 32 1129 45 1361
10 608 20 884 33 10100 46 1498
8 1030 21 1052 34 1082 410 1100
9 544 22 858 35 1089 48 1339
10 608 23 863 36 1061 49 10106
11 1058 24 1056 310 1102 50 1424
12 562 25 858 38 869 51 1310
13 681 26 689 39 1025 52 1066

Page | 26
Procedure:
Step 1: Transfer all the given sales values in to MS-Excel spreadsheet along with week numbers in
first row first column of spreadsheet along with title row.

Step 2: Open the new third column (Column C) and name it as “Forecasting values by using weighted
moving average”.

Step 3: Drag down the A column from A53 to A510 for the next 4 weeks (i.e. till 56 - for the purpose
of next one month)

Step 4: Enter the formula in C4 cell as below with respect to weighted moving average,

=(B4*0.6)+(B3*0.3)+(B2*0.1)/(0.6+0.3+0.1)

Step 5: Drag down this cell formula till 56 weeks.

Step 6: Transfer the calculated value of 52nd week in to actual value of 53 rd week similarly from 53 to
54, 54 to 55 and 55 to 56th week.

Step 10: Declare the result by using last 4 values of C column as next one month forecasted values.

Step 8: Put-up all the 56 values (actual sales values of 52 weeks and forecasted values of next 4
weeks) in separate column and insert line graph.
Step 9: Provide X axis title as “Week No.” and Y axis title as “Sales values” and chart title as
“Forecasting chart by using Excel function”.

Page | 27
Output:

Result:
Calculated the forecasting values of next one month which comprises of four weeks by using
weightedmoving average method and given the result below.Similarly line graph has also been drawn
and pasted for the actual values along with calculated forecasting values.

Week No. Forecast values based on weighted moving average method


53 11105
54 1155
55 1152
56 1160

Page | 28
Ex. No. 8 Forecasting through MS-Excel Spreadsheets by
using exponential smoothing method
Date:

Aim:
To determine forecasting values through MS-Excel Spreadsheets by using regression method
with single variable factor

Problem:

Following table given the one year profit value in terms of lakhs. Calculate the 13 th month forecast
value by using exponential smoothing method by assuming first week profit value as forecast value of
second week and smoothing constant value as 0.8.

Month No. Profit


1 6
2 8
3 11
4 23
5 29
6 34
10 40
8 45
9 56
10 60
11 65
12 102

Procedure:
Step 1: Transfer all the given table values in to MS-Excel spreadsheet along with month numbers in
first column (Column A) and profit values in the second column (Column B) of spreadsheet along
with title row.
Step 2: Open 3rd column (Column C) and name it as Exponential smoothing forecast value
Step 3: Place the cursor at C3 cell and enter the forecast value of 2 nd month as 6 which is given in the
problem. (Note: Assume first week profit value as forecast value of second week)

Page | 29
Step 4: Formula for the exponential smoothing forecast method is
Forecast = Previous period forecast value + [Smoothing constant x (Difference between previous
period demand and forecast values)]
Ft = Ft-1+ [a (Dt-1 - Ft-1)]
Step 5: Place the cursor at C4 cell and enter the below formula as per the formula mentioned in step 4
by considering smoothing constant value as 0.8 which is given in problem.

=C3+(0.8*(B3-C3))

Step 6: Drag the C4 cell value/formula till C14.


Step 10: Declare the result as forecasted profit value of 13th month is as the value in C14 cell.
Step 8: Draw the line graph for all the 12 months profit value and 13th month forecasted value.

Output:

Result
Exponential smoothing forecast value of 13th month is 100.34

Page | 30
Ex. No. 9 Forecasting through MS-Excel Spreadsheets by
using moving average method through data
Date:
analysis option

Aim: To forecast sales using the moving average method through MS-Excel Spreadsheets.

Problem: The following data represents the sales of a company over the past 12 months. Compute
the next 3 months' forecasting values using the moving average method and the Data Analysis option
in Excel.

Sales Data:

Month Sales
Jan 12000
Feb 12500
Mar 13000
Apr 11000
May 13600
Jun 15500
Jul 14000
Aug 16500
Sep 13500
Oct 12893
Nov 15654
Dec 13000

Procedure:

1. Step 1: Transfer all the given sales values and corresponding months into an MS-Excel
spreadsheet.
2. Step 2: Go to the "Data" tab in the Excel ribbon.
Page | 31
3. Step 3: Click on "Data Analysis" (Note: If Data Analysis is not visible, you might need to
install the Analysis ToolPak. You can do this by going to "File" > "Options" > "Add-Ins" >
"Excel Add-Ins" > "Analysis ToolPak" > "Go").
4. Step 4: Select "Moving Average" from the list of available analysis tools.
5. Step 5: Click "OK".
6. Step 6: In the "Moving Average" dialog box, select the range of sales data (excluding the
months).
7. Step 10: Set the "Number of periods" to 3 for a 3-month moving average.
8. Step 8: Choose an output range for the moving averages.
9. Step 9: Click "OK" to generate the moving average values.
10. Step 10: The last 3 values in the moving average column are the forecasted sales for the next
3 months.

Output:

Result:

Calculated the next 3 months' forecasted sales using the moving average method through the
Data Analysis option in Excel. Results are arrived as below

Ex. No. 10 Forecasting through MS-Excel Spreadsheets by


using exponential smoothing method through data
Date:
analysis option

Page | 32
Aim: To forecast sales using the exponential smoothing method through MS-Excel Spreadsheets.

Problem: The following data represents the sales of a company over the past 12 months. Compute
the next 3 months' forecasting values using the exponential smoothing method and the Data Analysis
option in Excel.

Sales Data:

Month Sales
Jan 11000
Feb 12500
Mar 6000
Apr 125000
May 14000
Jun 16500
Jul 11500
Aug 18000
Sep 14600
Oct 110000
Nov 20000
Dec 14000

Procedure:

1. Step 1: Transfer all the given sales values and corresponding months into an MS-Excel
spreadsheet.

2. Step 2: Go to the "Data" tab in the Excel ribbon.

3. Step 3: Click on "Data Analysis" (Note: If Data Analysis is not visible, you might need to
install the Analysis ToolPak. You can do this by going to "File" > "Options" > "Add-Ins" >
"Excel Add-Ins" > "Analysis ToolPak" > "Go").

Page | 33
4. Step 4: Select "Exponential Smoothing" from the list of available analysis tools.

5. Step 5: Click "OK".

6. Step 6: In the "Exponential Smoothing" dialog box, select the range of sales data (excluding
the months).

7. Step 10: Set the "Alpha" value. This parameter controls the weight given to the most recent
observation. For example, you can start with an Alpha value of 0.2.

8. Step 8: Choose an output range for the smoothed values.

9. Step 9: Click "OK" to generate the smoothed values.

10. Step 10: The last 3 values in the smoothed column are the forecasted sales for the next 3
months.

Output:

Result:

Calculated the next 3 months' forecasted sales using the exponential smoothing method
through the Data Analysis option in Excel. Results are arrived as below.

Ex. No. 11 Forecasting through MS-Excel Spreadsheets by


using regression method with one varying variable
Date:
through data analysis option

Page | 34
Aim: To forecast sales using the regression method with one varying variable through MS-Excel
Spreadsheets.

Problem: The following data represents the sales of a company over the past 12 months of 2022.
Compute the forecasted sales value of next month January 2023 using the regression method.

Sales Data:

Month Sales (in Rs. Lakhs)


1 12
2 15
3 11.5
4 19
5 15.5
6 14.2
10 110
8 11
9 16
10 14
11 20
12 14.5

Procedure:

1. Step 1: Transfer all the given sales and advertising budget values along with the
corresponding months into an MS-Excel spreadsheet.

2. Step 2: Go to the "Data" tab in the Excel ribbon.

3. Step 3: Click on "Data Analysis" (Note: If Data Analysis is not visible, you might need to
install the Analysis ToolPak. You can do this by going to "File" > "Options" > "Add-Ins" >
"Excel Add-Ins" > "Analysis ToolPak" > "Go").

4. Step 4: Select "Regression" from the list of available analysis tools.


Page | 35
5. Step 5: Click "OK".

6. Step 6: In the "Regression" dialog box, set the Y Range as the sales data and the X Range as
the month data.

7. Step 10: Check the box for "Labels in First Row" if your data has column headers.

8. Step 8: Choose an output range for the regression analysis.

9. Step 9: Click "OK" to generate the regression results.

10. Step 10: The regression results will include coefficients that can be used to form a regression
equation. You can use below equation to forecast the sales value of next month January 2023.

11. Step 11: Regression equation is Y = a + b X

12. Step 12: Place the cursor in to where the forecast value to be calculated and enter the formula
as below.

= <intercept value> + <X variable value>*13

13. Declare arrived value from step 12 as forecasted value of 13th month January 2023.

Output:

Result:

Forecasted sales value of next month January 2023 is 13

Ex. No. 12 Forecasting through MS-Excel Spreadsheets by


using regression method with two varying variable
Date:
through data analysis option

Page | 36
Aim: To forecast sales using the regression method with two varying variable through MS-Excel
Spreadsheets.

Problem: The following data represents the sales of a company over the past 12 months of 2022
along with an advertising budget for each month. Compute the forecasted advertising budgetvalue of
next month January 2023 if sales is 18 lakhs using the regression method.

Sales Data:

Advertising Budget
Sales
(in Rs. Lakhs)
Month (in Rs. Lakhs)
Jan 5
12
Feb 4
15
Mar 8
11.5
Apr 6.5
19
May 9
15.5
Jun 5
14.2
Jul 10
110
Aug 2.5
11
Sep 4.5
16
Oct 5
14
Nov 10
20
Dec 8.5
14.5

Procedure:

Page | 37
1. Step 1: Transfer all the given sales and advertising budget values along with the
corresponding months into an MS-Excel spreadsheet.

2. Step 2: Go to the "Data" tab in the Excel ribbon.

3. Step 3: Click on "Data Analysis" (Note: If Data Analysis is not visible, you might need to
install the Analysis ToolPak. You can do this by going to "File" > "Options" > "Add-Ins" >
"Excel Add-Ins" > "Analysis ToolPak" > "Go").

4. Step 4: Select "Regression" from the list of available analysis tools.

5. Step 5: Click "OK".

6. Step 6: In the "Regression" dialog box, set the Y Range as the advertisement budget data and
the X Range as the sales data.

7. Step 10: Check the box for "Labels in First Row" if your data has column headers.

8. Step 8: Choose an output range for the regression analysis.

9. Step 9: Click "OK" to generate the regression results.

10. Step 10: The regression results will include coefficients that can be used to form a regression
equation. You can use below equation to forecast the sales value of next month January 2023.

11. Step 11: Regression equation is Y = a + b X

12. Step 12: Place the cursor in to where the forecasted advertisement budget value to be
calculated for 18 lakhs sales and enter the formula as below.

= <intercept value> + <X variable value>*18

13. Declare arrived value from step 12 as forecasted value of advertisement budget for the 18
lakhs sales.

Output:

Page | 38
Result:

Forecasted advertisement budgetvalue of 18 lakhs sales is 22.9 lakhs

Ex. No. 13 Computation of Forecasting errors – Mean Square


Deviation (MSD), Mean Absolute Deviation (MAD)
Date:
Page | 39
and Tracking Signal (TS)

Aim: To compute forecasting errors – Mean Square Deviation (MSD), Mean Absolute Deviation
(MAD), and Tracking Signal (TS) using MS-Excel.

Problem: Given actual and forecasted sales data for a product over 12 months, calculate the Mean
Square Deviation (MSD), Mean Absolute Deviation (MAD), and Tracking Signal (TS) to evaluate
the accuracy of the forecast.

Sales Data:

Month Actual Sales Forecasted Sales


Jan 1200 1000
Feb 1300 1320
Mar 1000 1450
Apr 1450 1500
May 1250 1362
Jun 1500 1452
Jul 1460 1200
Aug 1563 1460
Sep 1600 1560
Oct 1381 1431
Nov 1562 1698
Dec 1460 1520

Procedure:

1. Step 1: Transfer the given data into an Excel spreadsheet.


Page | 40
2. Step 2: Calculate the Mean Square Deviation (MSD):

=SUM((B2:B13-C2:C13)^2)/COUNT(B2:B13)

3. Step 3: Calculate the Mean Absolute Deviation (MAD):

=SUM(ABS(B2:B13-C2:C13))/COUNT(B2:B13)

4. Step 4: Calculate the Cumulative Error (CE):

=SUM(B2:B13-C2:C13)

5. Step 5: Calculate the Mean Absolute Deviation (MAD) of the forecast errors:

=SUM(ABS(B2:B13-C2:C13))/COUNT(B2:B13)

6. Step 6: Calculate the Tracking Signal (TS):

=CE/MAD

Output:

Results:

 Mean Square Deviation (MSD):

 Mean Absolute Deviation (MAD):

 Tracking Signal (TS)

Ex. No. 14 Computation of Forecasting errors – Mean Square


Deviation (MSD), Mean Absolute Deviation (MAD)
Date:
and Tracking Signal (TS)
Page | 41
Aim: To forecast future sales using the Auto-Regressive Integrated Moving Average (ARIMA)
method through MS-Excel Spreadsheets.

Problem: You have been provided with monthly sales data for a product over the last two years.
Utilize the ARIMA model to forecast sales for the next six months.

Sales Data:

Month Sales (in Rs. Lakhs)


Jan-20 120
Feb-20 140
Mar-20 115
Apr-20 130
May-20 155
Jun-20 165
Jul-20 130
Aug-20 105
Sep-20 165
Oct-20 1105
Nov-20 155
Dec-20 200
Jan-21 215
Feb-21 190
Mar-21 185
Apr-21 200
May-21 1105
Jun-21 250
Jul-21 235
Aug-21 205
Sep-21 195
Oct-21 180
Nov-21 1100
Dec-21 200
Procedure:

Page | 42
1. Step 1: Open Excel and Transfer Data

 Open a new Excel sheet and input the provided sales data.

2. Step 2: Identify Parameters for ARIMA

 Analyze the data to identify the appropriate parameters (p, d, q) for the ARIMA
model. This includes determining the order of differencing (d) and the lag values for
auto-regression (p) and moving average (q).

3. Step 3: Install Data Analysis ToolPak (if not installed)

 Go to "File" > "Options" > "Add-Ins" > "Excel Add-Ins" > Check "Analysis ToolPak"
> Click "OK".

4. Step 4: Use Data Analysis ToolPak for ARIMA

 Go to "Data" > "Data Analysis" > Choose "Regression" > Click "OK".

 Input the "Input Y Range" (sales data) and "Input X Range" (time periods, 1 to n).

5. Step 5: Interpret ARIMA Output

 Analyze the output to understand the model coefficients and diagnostic statistics.

6. Step 6: Forecast Future Sales

 Utilize the ARIMA model to forecast sales for the next six months by inputting the
future time periods.

7. Step 10: Create a new column as Forecasted Value, Choose Cell D5 and put up the function
as following

=SUM(B2:B4*Coefiicient of sales)/3

and the formula down and declare the forecasted sales value for next 6 month

Output:

Page | 43
Results:

Using the ARIMA model, the forecasted sales results were arrived

Ex. No. 15
IntroductionToR Program and its Packages
Date:

Page | 44
Introductionto R:
✓ Risaprogramminglanguageandsoftwareenvironmentforstatisticalanalysis,graphicsrepresent
ationandreporting.
✓ RisfreelyavailableundertheGNUGeneralPublicLicense,andpre-compiled binary
versionsareprovidedforvariousoperatingsystemslikeLinux,WindowsandMac.
✓ ThisprogramminglanguagewasnamedR,basedonthefirstletteroffirstnameofthe
twoRauthors(RobertGentlemanandRossIhaka).
✓ RallowsintegrationwiththeprocedureswrittenintheC,C++,.Net,Pythonor
FORTRANlanguagesforefficiency.

FeaturesofR:
 Risawell-
developed,simpleandeffectiveprogramminglanguagewhichincludesconditionals,loops;
userdefinedrecursivefunctionsandinputandoutputfacilities.
 Rhasaneffectivedatahandlingandstoragefacility,

 Rprovidesasuiteofoperatorsforcalculationsonarrays,lists,vectorsandmatrices.

 Rprovidesalarge,coherentandintegratedcollectionoftoolsfordataanalysis.

 Rprovidesgraphicalfacilitiesfordataanalysisanddisplayeitherdirectlyatthecomputerorprinting
atthepapers.

Data types:
ThevariablesareassignedwithR-ObjectsandthedatatypeoftheR-
objectbecomesthedatatypeofthevariable.TherearemanytypesofR-objects.Thefrequentlyusedonesare,

 Vectors
 Lists
 Matrices
 Arrays
 Factors
 Data Frames
Variables:
Avariableprovidesuswithnamedstoragethatourprogramscanmanipulate.AvariableinRcanstor
eanatomicvector,groupofatomicvectorsoracombinationofmanyRobjects.
Avalidvariablenameconsistsofletters,numbersandthedotorunderlinecharacters.Thevariablename
Page | 45
startswithaletterorthedotnotfollowedbyanumber.

VariableName Validity Reason

var_name2. valid Hasletters,numbers,dotandunderscore

var_name% Invalid Hasthecharacter'%'.Onlydot(.)andunderscoreallowed.

2var_name invalid Startswithanumber

.var_name, Canstartwithadot(.)butthedot(.)shouldnotbefollowedbyan
valid
var.name umber.

.2var_name invalid Thestartingdotisfollowedbyanumbermakingitinvalid.

_var_name invalid Startswith_whichisnotvalid

TypesofOperators
Anoperatorisasymbolthattellsthecompilertoperformspecificmathematicalorlogicalmanipulatio
ns.Rlanguageisrichinbuilt-inoperatorsandprovidesfollowingtypesofoperators.

We have thefollowing types ofoperatorsin R programming−

 ArithmeticOperators

 RelationalOperators

 LogicalOperators

 AssignmentOperators

 MiscellaneousOperators

DecisionMaking:

Rprovides the following types of decision making statements. Click the following links to
check their detail.

S. No Statement&Description

Page | 46
ifstatement-
1
AnifstatementconsistsofaBooleanexpressionfollowedbyoneormorestatements
.if...elsestatement-
2 Anifstatementcanbefollowedbyanoptionalelsestatement,whichexecuteswhent
heBooleanexpressionisfalse.
switchstatement-
3
Aswitchstatementallowsavariabletobetestedforequalityagainstalistofvalues.

Loop:

Rprogramming language provides the following kind so flop to handle looping


requirements .Click the following links to check their detail.

S.No. LoopType&Description

Repeatloop-
1 Executesasequenceofstatementsmultipletimesandabbreviatesthecodethatmanagesth
eloopvariable.
Whileloop-
2 Repeatsastatementorgroupofstatementswhileagivenconditionistrue.Itteststhecondition
beforeexecutingtheloopbody.

3 forloop-Likeawhilestatement,exceptthatitteststheconditionattheendoftheloopbody.

Function:

FunctionDefinition:
AnRfunctioniscreatedbyusingthekeywordfunction.ThebasicsyntaxofanRfunctiondefinitionisasf
ollows–
function_name<-function(arg_1,arg_2,...)
{Functionbody
}

FunctionComponents
The different parts of a function are −

 FunctionName−Thisistheactualnameofthefunction.ItisstoredinRenvironment
asanobjectwiththisname.
Page | 47
 Arguments−Anargument is a placeholder.When a function is involed ,you pass a value to
the argument .Arguments are optional; that is ,a function may contain no arguments.Also
arguments can have default values.

 FunctionBody−The function body containsacollectionofstatementsthat


defineswhatthefunctiondoes.

 ReturnValue−Thereturnvalueofafunctionisthelastexpressioninthefunctionbodytobeevaluate
d.

Rhasmanyin-
builtfunctionswhichcanbedirectlycalledintheprogramwithoutdefiningthemfirst.Wecanalsocreatea
nduseourownfunctionsreferredasuserdefinedfunctions.
Built-inFunction

Simpleexamplesofin-
builtfunctionsareseq(),mean(),max(),sum(x)andpaste(...)etc.Theyaredirectlycalledbyuserwritte
nprograms.YoucanrefermostwidelyusedRfunctions.

String:
AnyvaluewrittenwithinapairofsinglequoteordoublequotesinRistreatedasastring.InternallyR
storeseverystringwithindoublequotes,evenwhenyoucreatethemwithsinglequote.

RulesAppliedinStringConstruction

 Thequotesatthebeginningandendofastringshouldbeeitherdoublequotesorbothsinglequo
tes.Theycannotbemixed.

 Doublequotescanbeinsertedintoastringstartingandendingwithsinglequote.

 Singlequotecanbeinsertedintoastringstartingandendingwithdoublequotes.

 Doublequotescannotbeinsertedintoastringstartingandendingwithdoublequotes.

 Singlequotecannotbeinsertedintoastringstartingandendingwithsinglequote.

Vector:

VectorsarethemostbasicRdataobjectsandtherearesixtypesofatomicvectors.Theyarelogical,i
nteger,double,complex,characterandraw.

Page | 48
List: ListsaretheRobjectswhichcontainelementsofdifferenttypeslike−numbers,strings,vectorsandano
therlistinsideit.Alistcanalsocontainamatrixorafunctionasitselements.Listiscreatedusinglist()fun
ction.

Matrix:

MatricesaretheRobjectsinwhichtheelementsarearrangedinatwo-
dimensionalrectangularlayout.Theycontainelementsofthesameatomictypes.Thoughwecancreatea
matrixcontainingonlycharactersoronlylogicalvalues,theyarenotofmuchuse.Weusematricescontain
ingnumericelementstobeusedinmathematicalcalculations.AMatrixiscreatedusingthematrix()funct
ion.

Syntax

The basic syntax for creating a matrix in R is −

matrix(data,nrow,ncol,byrow,dimnames)

Following is the description of the parametersused −

 dataistheinputvectorwhichbecomesthedataelementsofthematrix.

 nrowisthenumberofrowstobecreated.

 ncolisthenumberofcolumnstobecreated.

 byrowisalogicalclue.IfTRUEthentheinputvectorelementsarearrangedbyrow.

 dimnameisthenamesassignedtotherowsandcolumns.

Array:

ArraysaretheRdataobjectswhichcanstoredatainmorethantwodimensions.Forexample−Ifwecr
eateanarrayofdimension(2,3,4)thenitcreates4rectangularmatriceseachwith2rowsand3columns.Arr
ayscanstoreonlydatatype.

Anarrayiscreatedusingthearray()function.Ittakesvectorsasinputandusesthevaluesinthedimparamet
ertocreateanarray.

Page | 49
Factor:

Factors are the data objects which areused to categorize the data and store itas levels.
Theycanstorebothstringsandintegers.Theyareusefulinthecolumnswhichhavealimitednumberofuniqu
evalues.Like"Male,"Female"andTrue,Falseetc.Theyareusefulindataanalysisforstatisticalmodeling.

Factorsarecreatedusingthefactor()functionbytakingavectorasinput.

DataFrames:

Adataframeisatableoratwo-dimensionalarray-
likestructureinwhicheachcolumncontainsvaluesofonevariableandeachrowcontainsonesetofvaluesfr
omeachcolumn.

Followingarethecharacteristicsofadataframe.
 Thecolumnnamesshouldbenon-empty.
 Therownamesshouldbeunique.
 Thedatastoredinadataframecanbeofnumeric,factororcharactertype.
 Eachcolumnshouldcontainsamenumberofdataitems.

R Studio

RStudioisafreeandopen-
sourceintegrateddevelopmentenvironment(IDE)forR,aprogramminglanguageforstatisticalcomputinga
ndgraphics.

Howto openR Studio:

OpeninRprogramming forthefollowingsteps

1. Firstgotostartmenu&openRStudio

Page | 50
2. Inthenextsteptobedisplayfourwindows.

 SourceWindow/EditorWindow
 Consolewindow
 GlobalEnvironment/History

 Home(files,plot,packages,help,viewer)

SourceWindow/EditorWindow GlobalEnvironment/HistoryWindows

Home(Files/Plots/Packages/Help/Viewer)
RConsole

Editorwindow:

Page | 51
The top leftquadrant is the editor. It’s where youwrite R codeyou want to keep for later


functions,classes,packages,etc.Thisis,forallintentsandpurposes,identicaltoeveryothercodeeditor’smain
window.
Apartfromsome self-explanatorybuttons, and othersthat needn’t concernyouat thisstarting
point,there is also a “Sourceon Save” checkbox.
This means “Load contents of file into my console’s runtimeevery time I save the file”.
Youshouldhavethisonatalltimes;itmakesyourdevelopmentflowfasterbyoneclick.

Page | 52
ConsoleWindow:

1. Thelowerleftquadrantistheconsole.It’saREPLforRinwhichyoucantestoutyourideas,datasets
,filters,andfunctions.
2. Thisiswhereyou’llbespendingmostofyourtimeinthebeginning–
it’sherethatyouverifyanideayouhadworksbeforecopyingitoverintotheeditorabove.
3. ThisisalsotheenvironmentintowhichyourRfileswillbesourcedonsave(seeabove),sowhenever
youdevelopanewfunctioninanRfileabove,itautomaticallybecomesavailableinthisREPL.We’
llbespendingalotoftimeintheREPLintheremainderofthisarticle.

GlobalEnvironment/History:
Thetoprightquadranthastwotabs:environmentandhistory.
Environmentreferstotheconsoleenvironment(seeabove)andwilllist,indetail,everysinglesym
bol youdefined intheconsole (whethervia sourcing ordirectly).
Thatis,ifyouhaveafunctionavailableintheREPL,itwillbelistedintheenvironment.Ifyouhaveavariable
,oradataset,itwillbelistedthere.
Thisiswhereyoucanalsoimportcustomdatasetsmanuallyandmaketheminstantlyavailableinth
econsole,ifyoudon’tfeelliketypingoutthecommandstodoso.Youcanalsoinspecttheenvironmentofoth
erpackagesyouinstalledandloaded(moreonpackagesatalatertime).Playaroundwithit–
youcan’tbreakanything.
Historylistseverysingleconsolecommandyouexecutedsincethelastprojectstarted.Itissavedint
oahidden.Rhistoryfileinyourproject’sfolder.Ifyoudon’tchoosetosaveyourenvironment after a
session, the history won’t besaved.
Page | 53
Home(files,plot,packages,help,viewer)
Thebottomrightpanelisthemiscpanel,andcontainsfiveseparatetabs.The
firstone,Files,isself-explanatory.

ThePlotstabwillcontainthegraphsyougeneratedwithR.Itisthereyoucanzoom,export,co
nfigureandinspectyourchartsandplots.
ThePackagestabletsyouinstalladditionalpackagesintoR.

TheHelptabletsyousearchtheincrediblyextensivehelpdirectoryandwillautomaticallyopenwhe
neveryoucallhelponacommandintheconsole(helpiscalledbypretendingacommandnamewithaquestio
nmark,likeso:?data.frame).

Packages:
RpackagesareacollectionofRfunctions,compliedcodeandsampledata.Theyarestoredundera
directorycalled"library"intheRenvironment.Bydefault,Rinstallsasetofpackagesduringinstallation
.Morepackagesareaddedlater,whentheyareneededforsomespecificpurpose.WhenwestarttheRconso
le,onlythedefaultpackagesareavailablebydefault.Otherpackageswhicharealreadyinstalledhavetobe
loadedexplicitlytobeusedbytheRprogramthatisgoingtousethem.
AllthepackagesavailableinRlanguagearelistedatRPackages.

Belowisalistofcommandstobeusedtocheck,verifyandusetheRpackages.

InstallingPackages
Thereareworld-
wideRpackagerepositoriesorComprehensiveRArchiveNetwork(CRAN)sitesthatallowpackagestobed
ownloadedandinstalled.Youalmost
neverhavetodirectlyworkwiththemsinceRStudiomakesiteasytoinstallthepackagesasshowninthefigur
ebelow,wherewehaveclickedonthePackagestabandclickedontheInstallbutton.Notehowasyoutype
thenameof apackage,you getauto-completion.(In fact,RStudioprovidesauto-
completionevenasyoutypeRcommands,showingyouvariousoptionsyoucanuseforthecommands)

Page | 54
RecommendedPackages

ManyusefulRfunctioncomeinpackages,freelibrariesofcodewrittenbyR'sactiveusercommunity.Toins
tallanRpackage,openanRsessionandtypeatthecommandlineinstall.packages("<thepackage'sname
>")
RwilldownloadthepackagefromCRAN,soyou'llneedtobeconnectedtotheinternet.Onceyouhaveapack
ageinstalled,youcan makeitscontentsavailabletouseinyourcurrentRsessionbyrunning

library("<thepackage'sname>")

TherearethousandsofhelpfulRpackagesforyoutouse,butnavigatingthemallcanbeachallenge.Tohelpy
ouout,we'vecompiledthisguidetosomeofthebest.We'veusedeachofthese,andfoundthemtobeoutstandi
ng–
we'veevenwrittensomeofthem.Butyoudon'thavetotakeourwordforit,thesepackagesarealsosomeofthet
opmostdownloadedRpackages.

Toloaddata

DBI-
ThestandardforforcommunicationbetweenRandrelationaldatabasemanagementsystems.Packagestha
tconnectRtodatabasesdependontheDBIpackage.
odbc-
UseanyODBCdriverwiththeodbcpackagetoconnectRtoyourdatabase.Note:RStudioprofessionalprod
uctscomewithprofessionaldriversforsomeofthemostpopulardatabases.
RMySQL,RPostgresSQL,RSQLite-
Ifyou'dliketoreadindatafromadatabase,thesepackagesareagoodplacetostart.Choosethepackagethatfit
syourtypeofdatabase.
XLConnect,xlsx-
ThesepackageshelpyoureadandwriteMicorsoftExcelfilesfromR.Youcanalsojustexportyourspreadsh
eetsfromExcelas.csv's.
foreign-WanttoreadaSASdatasetintoR?OranSPSSdataset?
Page | 55
ForeignprovidesfunctionsthathelpyouloaddatafilesfromotherprogramsintoR.
haven-EnablesRtoreadandwritedatafromSAS,SPSS,andStata.

Rcanhandleplaintextfiles–
nopackagerequired.Justusethefunctionsread.csv,read.table,andread.fwf.Ifyouhaveevenmoreexoticd
ata,consulttheCRANguidetodataimportandexport

Page | 56
Tomanipulatedata
dplyr-
Essentialshortcutsforsubsetting,summarizing,rearranging,andjoiningtogetherdatasets.dplyris
ourgotopackageforfastdatamanipulation.
tidyr-
Toolsforchangingthelayoutofyourdatasets.Usethegatherandspreadfunctionstoconvertyourda
taintothetidyformat,thelayoutRlikesbest.
stringr-Easytolearntoolsforregularexpressionsandcharacterstrings.
lubridate-Toolsthatmakeworkingwithdatesandtimeseasier.

Tovisualizedata
ggplot2-
R'sfamouspackageformakingbeautifulgraphics.ggplot2letsyouusethegrammarofgraphicstob
uildlayered,customizableplots.
ggvis-Interactive,webbasedgraphicsbuiltwiththegrammarofgraphics.
rgl-Interactive3DvisualizationswithR
htmlwidgets-
Afastwaytobuildinteractive(javascriptbased)visualizationswithR.Packagesthatimplementht
mlwidgetsinclude:
leaflet(maps)
dygraphs(timeseries)
DT(tables)
diagrammeR(diagrams)netw
ork3D(network graphs)

threeJS(3Dscatterplotsandglobes).

googleVis-
Let'syouuseGoogleCharttoolstovisualizedatainR.GoogleCharttoolsusedtobecalledGapmind
er,thegraphingsoftwareHansRoslingmadefamousinhieTEDtalk.

Tomodeldata
car-car'sAnovafunctionispopularformakingtypeIIandtypeIIIAnovatables.
mgcv-GeneralizedAdditiveModels
lme4/nlme-LinearandNon-linearmixedeffectsmodels
randomForest-Randomforestmethodsfrommachinelearning
multcomp-Toolsformultiplecomparisontesting
Page | 57
vcd-Visualizationtoolsandtestsforcategoricaldata
glmnet-Lassoandelastic-netregressionmethodswithcrossvalidation
survival-Toolsforsurvivalanalysis

caret-Toolsfortrainingregressionandclassificationmodels
Toreportresults
shiny-
Easilymakeinteractive,webappswithR.Aperfectwaytoexploredataandsharefindingswithnon
-programmers.
R Markdown–The perfect workflow forreproducible reporting. Write R code
inyourmarkdownreports.Whenyourunrender,RMarkdownwillreplacethecode
withitsresultsandthenexportyourreportasanHTML,pdf,orMSWorddocument,oraHTMLor
pdfslideshow.Theresult?
Automatedreporting.RMarkdownisintegratedstraightintoRStudio.xtable-
ThextablefunctiontakesanRobject(likeadataframe)andreturnsthelatexorHTMLcodeyouneedt
opasteaprettyversionoftheobjectintoyourdocuments.Copyandpaste,orpairupwithRMarkdow
n.

ForSpatialdata
sp,maptools-Toolsforloadingandusingspatialdataincludingshapefiles.
maps-Easytousemappolygonsforplots.
ggmap-
DownloadstreetmapsstraightfromGooglemapsandusethemasabackgroundinyourggplots.

ForTimeSeriesandFinancialdata
zoo-ProvidesthemostpopularformatforsavingtimeseriesobjectsinR.
xts-Veryflexibletoolsformanipulatingtimeseriesdatasets.
quantmod-
Toolsfordownloadingfinancialdata,plottingcommoncharts,anddoingtechnicalanalysis.

TowritehighperformanceRcode
Rcpp-WriteRfunctionsthatcallC++codeforlightningfastspeed.
data.table-Analternativewaytoorganizedatasetsforvery,veryfastoperations.Usefulforbigdata.
parallel-UseparallelprocessinginRtospeedupyourcodeortocrunchlargedatasets.

Page | 58
To work withthe web
XML-ReadandcreateXMLdocumentswithR
jsonlite-ReadandcreateJSONdatatableswithR
httr-Asetofusefultoolsforworkingwithhttpconnections

Towrite yourownRpackages
devtools-AnessentialsuiteoftoolsforturningyourcodeintoanRpackage.
testthat-testthatprovidesaneasywaytowriteunittestsforyourcodeprojects.
roxygen2-
AquickwaytodocumentyourRpackages.roxygen2turnsinlinecodecommentsintodocumentati
onpagesandbuildsapackagenamespace.
YoucanalsoreadabouttheentirepackagedevelopmentprocessonlineinHadleyWickham'sRPack
agesbook

Inference:

Learnt the basic introductory content of R Program

Page | 59
Ex. No. 16
Basics ofR Program
Date:

AIM:

TounderstandthebasiccommandsinRprogramming.

PROCEDURE:

Rasacalculator
>1+2
[1]3

VariableAssignment
Weassignvaluestovariableswiththeassignmentoperator"=".Justtypingthevariablebyitselfatthepromp
twillprintoutthevalue.Weshouldnotethatanotherformofassignmentoperator"<-"isalsoinuse.
>x=1
>x
[1]1

Functions
Rfunctionsareinvokedbyitsname,thenfollowedbytheparenthesis,andzeroormorearguments.Thefoll
owingapplythefunctionctocombinethreenumericvaluesintoavector.

>c(1,2,3)
[1]1 2 3

Comments
Alltextafterthepoundsign"#"withinthesamelineisconsideredacomment.
>1+1 #thisisacomment[1]2
Page | 60
RDataType

➢ Numeric
➢ Integer
➢ Complex
➢ Logical
➢ Character

Numeric:
DecimalvaluesarecallednumericsinR.Itisthedefaultcomputationaldatatype.Ifweassignadecimalvalu
etoavariablexasfollows,xwillbeofnumerictype.

>x=10.5 #assignadecimalvalue

>x

#printthevalueofx[1]10.5

>class(x)

#printtheclassnameofx[1]"numeric"

Furthermore,evenifweassignanintegertoavariablek,itisstillbeingsavedasa numericvalue.
>k=1

>k

#printthevalueofk[1]1

>class(k)

#printtheclassnameofk[1]"numeric"

Integer:
InordertocreateanintegervariableinR,weinvoketheas.integerfunction.Wecanbeassuredthatyisinde
Page | 61
edanintegerbyapplyingtheis.integerfunction.
>y=as.integer(3)

>y

#printthevalueofy[1]3

>class(y)

#printtheclassnameofy[1]"integer"

>is.integer(y)#isyaninteger?
[1]TRUE

Incidentally,wecancompelanumericvalueintoanintegerwiththesameas.integerfunction.
>as.integer(3.14)

#coerceanumericvalue[1]3

Andwecanparseastringfordecimalvaluesinmuchthesameway.
>as.integer("5.210")#coerceadecimalstring[1]5

Ontheotherhand,itiserroneoustryingtoparseanon-decimalstring.

>as.integer("Joe")#coerceannon−decimalstring

[1]NA

Warningmessage:

NAsintroducedbycoercion

Often,itisusefultoperformarithmeticonlogicalvalues.LiketheClanguage,TRUEhasthevalue1,whileF
ALSEhasvalue0.
>as.integer(TRUE) #thenumericvalueofTRUE[1]1

Page | 62
>as.integer(FALSE)#thenumericvalueofFALSE[1]
0

Complex:

ComplexvalueinRisdefinedviathepureimaginaryvaluei.

>z=1+2i #createacomplexnumber

>z

#printthevalueofz[1]1+2i

>class(z)

#printtheclassnameofz[1]"complex"

The following gives anerror as −1is not a complexvalue.

>sqrt(−1) #squarerootof−1

[1]NaN

Warningmessage:

Insqrt(−1):NaNsproduced

Instead, wehave to use thecomplex value −1 + 0i.


>sqrt(−1+0i) #squarerootof−1+0i

[1]0+1i

Analternative isto coerce −1into a complex value.

>sqrt(as.complex(−1))

Page | 63
[1]0+1i

Logical:

Alogicalvalueisoftencreatedviacomparisonbetweenvariables.
>x=1;y=2#samplevalues
>z=x>y #isxlargerthany?
>z

#printthelogicalvalue[1]FALSE
>class(z)

#printtheclassnameofz[1]"logical"

Standardlogicaloperationsare"&"(and),"|"(or),and"!"(negation).
>u= TRUE; v= FALSE
>u&v # u AND
v[1]FALSE
>u |v # u OR
v[1]TRUE
>!u #negationofu
[1]FALSE

Character:
AcharacterobjectisusedtorepresentstringvaluesinR.Weconvertobjectsintocharactervalueswiththeas.
character()function:
>x=as.character(3.14)
>x

#printthecharacterstring[1]"3.14"
>class(x)

#printtheclassnameofx[1]"character"

Page | 64
Twocharactervaluescanbeconcatenatedwiththepastefunction.
>fname="Joe";lname="Smith"
>paste(fname,lname)
[1]"JoeSmith"

However,itisoftenmoreconvenienttocreateareadablestringwiththesprintffunction,whichhasaClangua
gesyntax.
>sprintf("%shas%ddollars","Sam", 100)
[1]"Samhas100dollars"

Toextractasubstring,weapplythesubstrfunction.Hereisanexampleshowinghowtoextractthesubstring
betweenthethirdandtwelfthpositionsinastring.
>substr("Maryhasalittlelamb.",start=3,stop=12)[1]"ry
has al"

Andtoreplacethefirstoccurrenceoftheword"little"byanotherword"big"inthestring,weapplythesubfun
ction.
>sub("little","big","Maryhasalittlelamb.")
[1]"Maryhasabiglamb."

Vector:

Avectorisasequenceofdataelementsofthesamebasictype.Membersinavectorareofficiallycalledcompo
nents.Nevertheless,wewilljustcallthemmembersinthissite.
Hereisavectorcontainingthreenumericvalues2,3and5.
>c(2,3,5)
[1]2 3 5

Andhereisavectoroflogicalvalues.
>c(TRUE,FALSE,TRUE,FALSE,FALSE)
[1]TRUEFALSETRUEFALSEFALSE

Avectorcancontaincharacterstrings.

Page | 65
>c("aa","bb","cc","dd","ee")
[1]"aa""bb""cc""dd""ee"
Incidentally,thenumberofmembersinavectorisgivenbythelengthfunction.
>length(c("aa","bb","cc","dd","ee"))[1]5

Combiningvectors:

Vectorscanbecombinedviathefunctionc.Forexamples,thefollowingtwovectorsnandsarecombinedi
ntoanewvectorcontainingelementsfrombothvectors.
>n=c(2,3, 5)
>s=c("aa","bb","cc", "dd","ee")
>c(n,s)
[1]"2" "3" "5""aa""bb""cc""dd" "ee"

VectorArithmetic:

Arithmeticoperationsofvectorsareperformedmember-by-

member,i.e.,memberwise.Forexample,supposewehavetwovectorsaandb.

>a=c(1,3,5,10)
>b=c(1,2, 4,8)

Then,ifwemultiplyaby5,wewouldgetavectorwitheachofitsmembersmultipliedby5.
>5*a
[1]5 15 25 35

Andifweaddaandbtogether,thesumwouldbeavectorwhosemembersarethesumofthecorresponding
membersfromaandb.
>a+b
[1]259 15
Similarlyfor subtraction,multiplication and division,weget new vectors
viamemberwiseoperations.
>a-b
[1]011 -1
Page | 66
>a*b
[1]1 6 20 56
>a/b
[1]1.000 1.500 1.250 0.8105
RecyclingRule
Iftwovectorsareofunequallength,theshorteronewillberecycledinordertomatch
thelongervector.Forexample,thefollowingvectorsuandvhavedifferentlengths,andtheirsumiscomput
edbyrecyclingvaluesoftheshortervectoru.
>u=c(10,20,30)
>v =c(1, 2,3,4, 5,6, 10,8, 9)
>u+v
[1]11 22 33 14 25 36 110 28 39

VectorIndex:

Weretrievevaluesinavectorbydeclaringanindexinsideasinglesquarebracket"[]"operator.
Forexample,thefollowingshowshowtoretrieveavectormember.Sincethevectorindexis1-
based,weusetheindexposition3forretrievingthethirdmember.
>s=c("aa","bb","cc", "dd","ee")
>s[3]
[1]"cc"

Unlikeotherprogramminglanguages,thesquarebracketoperatorreturnsmorethanjustindividual
members. In fact, theresult ofthe squarebracketoperator is anothervector,ands[3]
isavectorslicecontainingasinglemember"cc".

NegativeIndex
Iftheindexisnegative,itwouldstripthememberwhosepositionhasthesameabsolutevalueasthenegativei
ndex.Forexample,thefollowingcreatesavectorslicewiththethirdmemberremoved.
>s[-3]
[1]"aa""bb""dd""ee"

Page | 67
Out-of-RangeIndex
Ifanindexisout-of-range,amissingvaluewillbereportedviathesymbolNA.
>s[10]
[1] NA

NumericIndexvector:

Anewvectorcanbeslicedfromagivenvectorwithanumericindexvector,whichconsistsofmemberpositi
onsoftheoriginalvectortoberetrieved.

Hereitshowshowtoretrieveavectorslicecontainingthesecondandthirdmembersofagivenvectors.
>s=c("aa","bb","cc", "dd","ee")
>s[c(2, 3)]
[1]"bb""cc"

DuplicateIndexes
Theindexvectorallowsduplicatevalues.Hencethefollowingretrievesamembertwiceinoneoperation.
>s[c(2, 3,3)]
[1]"bb""cc""cc"

Out-of-OrderIndexes
Theindexvectorcanevenbeout-of-order.Hereisavectorslicewiththeorderoffirst
andsecondmembersreversed.
>s[c(2, 1,3)]
[1]"bb""aa""cc"

RangeIndex
Toproduceavectorslicebetweentwoindexes,wecanusethecolonoperator":".Thiscanbeconvenientfors
ituationsinvolvinglargevectors.
>s[2:4]

Page | 68
[1]"bb""cc""dd"

LogicalIndexvector:

Anewvectorcanbeslicedfromagivenvectorwithalogicalindexvector,whichhasthesamelengthastheori
ginalvector.
ItsmembersareTRUEifthecorrespondingmembersintheoriginalvectoraretobeincludedintheslice,and
FALSEifotherwise.
Forexample,considerthefollowingvectorsoflength5.
>s=c("aa","bb","cc", "dd","ee")

Toretrievethethesecondandfourthmembersofs,wedefinealogicalvectorLofthesamelength,andhaveit
ssecondandfourthmemberssetasTRUE.
>L=c(FALSE,TRUE,FALSE,TRUE,FALSE)
>s[L]
[1]"bb""dd"

Thecodecanbeabbreviatedintoasingleline.
>s[c(FALSE,TRUE,FALSE,TRUE,FALSE)]
[1]"bb""dd"

List
Alistisagenericvectorcontainingotherobjects.
Forexample,thefollowingvariablexisalistcontainingcopiesofthreevectorsn,s,b,andanumericv
alue3.

>n=c(2,3, 5)

> s= c("aa","bb","cc", "dd","ee")

>b=c(TRUE,FALSE,TRUE,FALSE,FALSE)

> x=list(n,s,b,3) #x containscopiesofn, s,b

Page | 69
ListSlicing
Weretrievealistslicewiththesinglesquarebracket"[]"operator.Thefollowingisaslicecontainingtheseco
ndmemberofx,whichisacopyofs.

>x[2]
[[1]]

[1]"aa""bb""cc""dd""ee"

Withanindexvector,wecanretrieveaslicewithmultiplemembers.Hereaslicecontainingtheseco
ndandfourthmembersofx.

>x[c(2,4)]

[[1]]

[1]"aa""bb""cc""dd""ee"

[[2]]

ListMemberReference

Inordertoreferencealistmemberdirectly,wehavetousethedoublesquarebracket"[[]]"operator.T
hefollowingobjectx[[2]]isthesecondmemberofx.Inotherwords,x[[2]]isacopyofs,butisnotaslice
containingsoritscopy.

>x[[2]]

[1]"aa""bb""cc""dd""ee"

Wecanmodifyitscontentdirectly.

>x[[2]][1] = "ta"

>x[[2]]

[1]"ta" "bb" "cc""dd""ee"

>s

[1]"aa""bb""cc""dd""ee" #sis unaffected

Page | 70
DataFrame

Adataframeisusedforstoringdatatables.Itisalistofvectorsofequallength.Forexample,thefollow
ingvariabledfisadataframecontainingthreevectorsn,s,b.

>n=c(2,3, 5)

> s=c("aa","bb","cc")

>b=c(TRUE,FALSE,TRUE)

> df=data.Frame(n,s,b) #dfisadataframe

Build-inDataFrame

Weusebuilt-indataframesinRforourtutorials.Forexample,hereisabuilt-
indataframeinR,calledmtcars.

>mtcars

mpgcyldisphpdratwt...

MazdaRX4 21.0 6 160 110 3.90 2.62 ...

MazdaRX4 Wag21.061601103.902.88...

Datsun 710 22.8 4 108 93 3.85 2.32 ...


Thetoplineofthe table,calledthe header, contains thecolumn names.Each horizontal
lineafterwarddenotesadatarow,whichbeginswiththenameoftherow,and
thenfollowedbytheactualdata.Eachdatamemberofarowiscalledacell.

Toretrievedatainacell,wewouldenteritsrowandcolumncoordinatesinthesinglesquarebracket"[]
"operator.Thetwocoordinatesareseparatedbyacomma.Inotherwords,thecoordinatesbeginswith
rowposition,thenfollowedbyacomma,andendswiththecolumnposition.Theorderisimportant.

Hereisthecellvaluefromthefirstrow,secondcolumnofmtcars.
>mtcars[1,2]

[1]6

Moreover,wecanusetherowandcolumnnamesinsteadofthenumericcoordinates.
➢ Mtcars(“Mazda RX4”,”cyl”)

[1]6

Page | 71
Lastly,thenumberofdatarowsinthedataframeisgivenbythenrowfunction.

>nrow(mtcars)

#numberofdatarows[1]32

Andthenumberofcolumnsofadataframeisgivenbythencolfunction.

>ncol(mtcars)

#numberofcolumns[1]11
FurtherdetailsofthemtcarsdatasetisavailableintheRdocumentation.

> help(mtcars)

Insteadofprintingouttheentiredataframe,itisoftendesirabletopreviewitwiththeheadfunctionbef
orehand.

>head(mtcars)

mpgcyldisphpdratwt...

MazdaRX4 21.0 6 160 110 3.90 2.62 ...

Page | 72
Output:

Inference:

Learnt the basic coding aspects of R Progra

Page | 73
Ex. No. 17
Data Slicing inR Program
Date:

Aim:

To implement Data Slicingin R programming for the following commands.

DataFrameColumnVector

Wereferenceadataframecolumnwiththedoublesquarebracket"[[]]"operator.

Forexample,toretrievetheninthcolumnvectorofthebuilt-indatasetmtcars,wewritemtcars[[9]].

>mtcars[[9]]

[1]1 1 1 0 0 0 0 0 0 0 0 ...

Wecanretrievethesamecolumnvectorbyitsname.
>mtcars[["am"]]

[1]1 1 1 0 0 0 0 0 0 0 0 ...

Wecanalsoretrievewiththe"$"operatorinlieuofthedoublesquarebracketoperator.
>mtcars$am

[1]1 1 1 0 0 0 0 0 0 0 0 ...

Yetanotherwaytoretrievethesamecolumnvectoristousethesinglesquarebracket"[]"operat
or.Weprependthecolumnnamewithacommacharacter,whichsignalsawildcardmatchforth
erowposition.

>mtcars[,"am"]

[1]1 1 1 0 0 0 0 0 0 0 0 ...
Page | 74
DataFrameColumnSlice

Weretrieveadataframecolumnslicewiththesinglesquarebracket"[]"operator.

NumericIndexing

Thefollowingisaslicecontainingthefirstcolumnofthebuilt-indatasetmtcars.

>mtcars[1]

mpg

MazdaRX4 21.0

MazdaRX4Wag 21.0

NameIndexing

Wecanretrievethesamecolumnslicebyitsname.

>mtcars["mpg"]

mpg

MazdaRX4 21.0

MazdaRX4Wag 21.0

Toretrieveadataframeslicewiththetwocolumnsmpgandhp,wepackthecolumnnamesinani
ndexvectorinsidethesinglesquarebracketoperator.
>mtcars[c("mpg","hp")]
mpghp
Mazda RX421.0110
MazdaRX4 Wag21.0110

Page | 75
Datsun1010 22.8 93

DataFrameRowSlice

Weretrieverowsfromadataframewiththesinglesquarebracketoperator,justlikewhatwedid
withcolumns.However,inadditionaltoanindexvectorofrowpositions,weappendanextraco
mmacharacter.Thisisimportant,astheextracommasignalsawildcardmatchforthesecondco
ordinateforcolumnpositions.

NumericIndexing

Forexample,thefollowingretrievesarowrecordofthebuilt-
indatasetmtcars.Pleasenoticetheextracommainthesquarebracketoperator,anditisnotatypo
.Itstatesthatthe19104CamaroZ28hasagasmileageof13.3milespergallon,andaneightcylin
der245horsepowerengine,,etc.

>mtcars[24,]

mpgcyldisphpdratwt...

CamaroZ2813.383502453.733.84...

Toretrievemorethanonerows,weuseanumericindexvector.
>mtcars[c(3,24),]

mpgcyldisphpdratwt...

Datsun 710 22.8 4 108 93 3.85 2.32 ...

Camaro Z2813.3 83502453.733.84...


NameIndexing

Wecanretrievearowbyitsname.

>mtcars["CamaroZ28",]

mpgcyldisphpdratwt...

CamaroZ2813.383502453.733.84...

Page | 76
Andwecanpacktherownamesinanindexvectorinordertoretrievemultiplerows.

>mtcars[c("Datsun710","CamaroZ28"),]

mpgcyldisphpdratwt...

Datsun 710 22.8 4 108 93 3.85 2.32 ...

CamaroZ2813.383502453.733.84...

LogicalIndexing

Lastly,wecanretrieverowswithalogicalindexvector.InthefollowingvectorL,thememberv
alueisTRUEifthecarhasautomatictransmission,andFALSEifotherwise.

>L=mtcars$am==0

>L

[1]FALSEFALSEFALSETRUE...

Hereisthelistofvehicleswithautomatictransmission.

>mtcars[L,]

mpgcyldisphpdrat wt ...

Hornet4Drive 21.4 6 258.0 110 3.08 3.215 ...

HornetSportabout18.78360.01753.153.440...

Andhereisthegasmileagedataforautomatictransmission.

Page | 77
>mtcars[L,]$mpg

[1]21.4 18.7 18.1 14.3 24.4 ...

Output:

Inference:

Learnt the basic slicing aspects of R Program

Page | 78
Ex. No. 18
Importing and Exporting of Data inR Program
Date:

Aim:

ToimportingandexportingthedatausingRProgramming

Procedure:

Thesampledatacanalsobeincommaseparatedvalues(CSV)format.Eachcellinsidesuchd
atafileisseparatedbyaspecialcharacter,whichusuallyisacomma,althoughothercharactersc
anbeusedaswell.

Thefirstrowofthedatafileshouldcontainthecolumnnamesinsteadoftheactualdata.Hereisa
sampleoftheexpectedformat.

Col1,Col2,
Col3100,a1,b1

200,a2,b2

Afterwecopyandpastethedataaboveinafilenamed"mydata.csv"withatexteditor,wecanrea
dthedatawiththefunctionread.csv.
>mydata=read.csv("mydata.csv")#readcsvfile
>mydata
Col1Col2Col3

1 100 a1 b1
2 200 a2 b2
3 300 a3 b3

Page | 79
#WriteCSVinR

>write.csv(MyData,file="MyData.csv",row.names=FALSE)

Output:

Inference:

Learnt the importing and exporting of data in R Program successfully.

Page | 80
Ex. No. 19
Basics of R Program through SWIRL Package
Date:

Aim:
TolearnR basicsusing swirlpackage

Procedure:
Step1:InstallR, RStudioandSWIRLpackage
Step2:Call SWIRL package (command: library
(swirl))Step3:Learnbasicbuilding blocksusing Swirl
Step 4:Typethe nameand thenselectthecourse and startlearning thecourse

Output:

Result:

Learntthebasics of R Programming through Swirl Package


Page | 81
Ex. No. 20 Handling of Data using Matrices and Data frame in
Date: R Program

Aim:
TolearnthehandlingofdatausingMatrices andDataframe

Procedure:

Step1:LoadtheswirlpackageinRstudio
Step2:Choosethecourse onmatrices anddataframe
Step 3: Learn about the handling of data using matrices and data frame

Output:

Page | 82
Result:
Learntthemethod ofcreatingandusingMatrices andDataFrame

Page | 83
Ex. No. 21
Time and Date Function in R Program
Date:

Aim:
TounderstandusageoftimeandDatefunctions inR

Procedure:

Step1:Usingswirlpackage, under RprogrammingselectthecourseTimeanddatefunctions


Step2: Followthecommandsgivenbyswirl.
Step3: Completethecourse

Output:

Result:
LearnttheDateand TimehandlingfunctionsofR.

Page | 84
Ex. No. 22 Exploring the Data using DPLYR Package in R
Date: Program

Aim:
Toexplorethenature ofDatausingRpackage dplyr

Procedure:

Step 1:To install the dplyr package, type the following command install.
Packages(“dplyr”)
Step 2:Toload dplyrpackage,typethecommandbelowlibrary(dplyr)
Step 3:Change thefile pathto the code belowmydata =read.csv(“C:\\Users\\Documents\\
sampledata.csv”)
Step 4:The function sample_n selects random rows from a data frame. The code
issample_n(mydata,3)
Step 5:The function sample_frac selects random fraction of rows from a data frame. The code
issample_frac(mydata,0.1)
Step 6:Thedistinctfunctionisusedtoeliminateduplicates. X1=distinct(mydata)
Step 10:Remove duplicate rows based on a variable. X2 = distinct(mydata, Index,. keep_all
=TRUE

Page | 85
Output:

Result:
Learn the functions of dplyr library in handling data.
Page | 86
Page | 87
Ex. No. 23 R Commander Package – File and Data in R
Date: Program

Aim:
Toinstallandlearnusageof RcommanderinRenvironment

Procedure:

Step 1: Typethefollowinginstall.packages(“Rcmdr”,dep=TRUE)2.)
Step 2: Tobring RcmdrintoR type library(Rcmdr)
Step 3: IntheRcmdrmenuclickonDatathenImportdatathentextfile
Step 4: UseSP500Daily.csvfile.
Step 5: Save itas Dataset using Data then Active data set then Save Active
Step 6: Nowload the dataset
Step 10: Type str(Dataset) and click submit
Step 8: Type head(Dataset) and click submit
Step 9: Type tail(Dataset) and click submit
Step 10: Type Dataset[1:10,] and click submit
Step 11: TypeDataset[,2:3]andclick submit
Step 12: Type attributes(Dataset) and click submit
Step 13: Typesummary(Dataset)andclicksubmit
Output:

Page | 88
Result:

TheFileandData functionality isexploredforRcommanderpackage.

Page | 89
Ex. No. 24
Data Frame in R Commander
Date:

Aim:
To usethe DataframefeaturefordatausingRcommander

Procedure: Step1: Before you start, type in the following


statementrm(list=ls())Step2:ReadinSP500Daily.csv.xlsx-
SP500Daily.csv.csv

Step3: Check the Data using the commands str (Dataset),


names(Dataset),row.names(Dataset), and attributes(Dataset) and present the results
below.Step4:Viewthetop6rowsbytyping head(Dataset)andthe bottom6rows
bytypingtail(Dataset)
Step 5:Many times , we want to take just a few rows or a few columns from the data
frame.Suppose we create a new object Dataset.sub1 by the following. Dataset.sub1 <-
Dataset[1:10,]Dataset.sub1
Step6:Similarly , we can take just a few of the columns using Dataset.sub2 <-
Dataset[1:10,1:2]
Dataset.sub2

Step7 : And we can take just a few of the columns using their names: Dataset.sub3 <-
Dataset[1:10,c(“caldt”,”vwretd”)]Dataset.sub3

Page | 90
Output:

Result:

We learntthedataframeoperationsincludingsub-settingofdatafromthedataframeinR-
commander.
Page | 91
Ex. No. 25 Forecasting by using Regression Method in R
Date: Program for New Data

Aim:To forecast the future value by using regression method through R Program

Problem: Calculate the Forecast sales value of 9th month for the below data

Month (X) 1 2 3 4 5 6 10 8

Sales (Y) 10 10 11 15 14 18 110 20

Procedure:

Step 1: Create the data table in Excel spread sheet with the required column title for example first
column title as X and second column title as Y and give the file name as you required for example
FC1.

Step 2: Import the data from excel to R program by using ‘import dataset’ option either from
environment/history window or from file option.

Step 3: Generate new separate R Script window by using File > New file > R script or
Cntrl+shif+N

Step 4: In this source window, type FC1 and Run for ensuring dataset has been successfully
imported.

Step 5: Type the below code in next few lines

lm(formula = Y ~ X, data = FC1)

RegFC<- lm(Y ~ X, data = FC1)

summary(RegFC)

Step 6: Select the entire coding and run the program.

Step 10: You will receive the output in console window

Page | 92
Step 8: Use intercept value and X variable value in linear equation Y = mX + C where C is
intercept value, X is X variable value and m is the variable for which you want to calculate forecast
value. (Note: This could be calculated in source window itself by entering the values.

Output:

Page | 93
Ex. No. 26 Forecasting by using Regression method through R
Date: Commander

Aim:

Using R-commander create regression modelling of data

Procedure:

Step 1:StartR/RStudio and Rcmdr

Step 2:GotoFile >Changeworking directorytoselectwhereyouwantthedatatobe


placed and whereyouwantRto look.
Step 3:Importor load adataset
Step 4:To run a Regression type: RegModel<- lm(Y~X, data= mydataset) and
thentypesummary(RegModel).
Output:

Result:

LearnedtheregressionfunctionusingR-commander.

Page | 94
Ex. No. 27 Forecasting by using Regression Method with
Date: Categorical Variables in R Program

Aim:
Understand the data using scatterplots and create a regression model to handleindependent
categoricalvariables.

Procedure:

Step 1: Retrieve the Data by using


"C:/Users/USER/Downloads/SP500Daily.csv.xlsx - SP500Daily.csv.csv
Step 2:Install psych package using Rstudio
Step 3:Read in the data and let us keep a smaller subset of
variablesRm(list=ls())
str(dat)

Step 4:Our variable of interest is RXEXP09, drug expenditures in 2009.5.)


NAs<-(dat<0)
Dat[NAs]<-NA
Output:

Result:

Thuswelearntregressionwithcategoricalvariables
Page | 95
Page | 96
Ex. No. 28 Forecasting by using Naïve method in R
Date: Program

Aim:
Tounderstandthetimeseriesdataandforecastusingnaïvemethod

Procedure:

Steps 1: load required libraries. If not installed so far, install


themlibrary(readr)
library(ggpl
ot2)library(f
orecast)libra
ryfpp2)libra
ry(TTR)libr
ary(dplyr)
Step 2:Readindatadat="C:/Users/USER/Downloads/SP500Daily.csv.xlsx-
SP500Daily.csv.csv”

Step 3: Usehead(dat)topreviewdatacontents
Step 4:dat_ts<-ts(dat[,2],start= c(1959,1,1),
end=c(2022,1,10),frequency=12)
Step 5: naïve_mod<-naïve(dat_ts,h=12)
Step 6: summary(naïve_mod)

Page | 97
Output:

Result:
Thus we did time series analysis using naïve method.

Page | 98
Ex. No. 29 Forecasting by using Bivariate Regression in R
Date: Program

Aim:

ToanalyzetheBivariateRegressionmodelusingRProgrammingcommands.

Procedure:
>data(trees)

>trees[1:5,]#Showfirst5lines
GirthHeightVolume
1 8.3 100 10.3
2 8.6 65 10.3
3 8.8 63 10.2
4 10.5 102 16.4
5 10.10 81 18.8
>reg1<-lm(Height~Girth,data=trees)
>reg1

Call:
lm(formula=Height~Girth,data=trees)

Coefficients:
(Intercept)

Girth
62.031 1.054
>summary(reg1)
Call:
lm(formula=Height~Girth,data=trees)
Residuals:
Min 1Q Median 3Q Max
-12.5816 -2.10686 0.3163 2.41028 9.9456

Page | 99
Coefficients:
EstimateStd.ErrortvaluePr(>|t|)
(Intercept) 62.0313 4.383314.1521.49e-14***
Girth 1.0544 0.3222 3.2102 0.002106 **
---
Signif. codes:0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1‘ ’ 1
Residualstandarderror:5.538on29degreesoffreedomMultiple
R-squared:0.26910, AdjustedR-squared:0.2445
F-statistic:10.101on1and29DF,p-value:0.0021058
Use linear equation Y = mX + c and calculate the forecast value

Output:

Page | 100
Ex. No. 30 Forecasting by using Multiple Regression in R
Date: Program

Aim:

ToAnalyzeaMultipleRegressionModelusingRprogrammingcommands.

Procedure:
>data(trees)
>trees[1:5,]#Showfirst5lines
GirthHeightVolume
1 8.3 100 10.3
2 8.6 65 10.3
3 8.8 63 10.2
4 10.5 102 16.4
5 10.10 81 18.8
>reg1<-lm(Height~Girth+Volume,data=trees)
>reg1

Call:
lm(formula=Height~Girth+Volume,data=trees)

Coefficients:
(Intercept) Girth

Volume83.2958 -
1.8615 0.51056
>summary(reg1)

Call:

lm(formula=Height~Girth+Volume,data=trees)

Residuals:

Min 1QMedian 3Q Max

Page | 101
-9.10855 -3.3649 0.5683 2.310410 11.6910
Coefficients:

EstimateStd.ErrortvaluePr(>|t|)
(Intercept)83.2958 9.08669.16106.33e-10
***
Girth -1.8615 1.15610 -1.609 0.1188

Volume 0.51056 0.2208 2.6010 0.0145 *

---

Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1‘ ’ 1

Residualstandarderror:5.056on28degreesoffreedomMulti
pleR-squared:0.4123,AdjustedR-squared:0.31003
F-statistic:9.82on2and28DF,p-value:0.0005868
Use linear equation Y = mX + c and calculate the forecast value

Output:

Page | 102
Ex. No. 31 Forecasting by using Moving Average Method in R
Date: Program

Aim:
To forecast future quantity using the Moving Average method

Procedure:
1.) Use the following libraries and then read in the
data .library(tidyverse)
library(lubridate)lib
rary(fpp2)library(z
oo)
2.)Read in theSP500Daily.csvdata into dat as data
frame3.)UsetheCenteredMA method asfollows

Savings <- dat %>


%Select(date,srate=psavert)
%>%
4.)plotthesevalues andcomparethe actualdata tothedifferentmovingaveragesmoothers
Output:

Result:
Thus we have forecasted future quantity using moving average method.

Page | 103
Ex. No. 32 Forecasting by using Auto Regressive Integrated
Date: Moving Average (ARIMA) Method in R Program

Aim:
Tocalculate forecastvalues usingARIMAtechnique

Procedure:

1.)Usethelibrary(zoo)

2.)ReadintheSP500Daily.csv dataintodatasdataframe
3.) Convertourdatatoatimeseries objectthentoazooobject
dat_ts<-ts(dat[,2], start=c(1959,1,1),end=c(2022,1,10),frequency=12)
4.)Converttozoo objectandcheck data
df.ts1=as.zoo(dat_t
s)head(df.ts1)
5.)Detrendusingdifffunctionandchecktheplot

diff.ts1=diff(df.ts1)
plot(diff.ts1).Nowthedataisdetrended.
6.) Nowusetheoriginal time seriesdat.tsobjectand usetheautoarima.
Output:

Result:
Thuswe learntforecastingusingARIMAmethod.

Page | 104
Page | 105
Page | 106

You might also like