Business Forecasting Lab Manual - 374
Business Forecasting Lab Manual - 374
FACULTY OF MANAGEMENT
RECORD WORK
Academic Year 2024-2025
Section G - SECTION
Page | 1
SRM INSTITUTE OF SCIENCE AND TECHNOLOGY
FACULTY OF MANAGEMENT
BONAFIDE CERTIFICATE
Dr.P.Poonguzhali Dr.V.M.Shenbagaraman
Faculty In-charge Chair – IS & Tech Mgt
Dr.Shivganesh Bhargava
Dean
Page | 2
Table of Contents
Ex. Page
Date Topic Signature
No. No.
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
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
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.
• 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:
Ex. No. 2
Forecasting Methods
Date:
Page | 10
Aim:
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
Market Research:
Focus Groups: Bringing together a small group of individuals to discuss and provide
qualitative feedback on a product or service.
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:
Technology Assesment:
Economic indicators:
Scenario analysis:
Composite Forecasting:
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:
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.
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).
Regression analysis:
where, ‘a’ is intercept value, ‘b’ is future time period and ‘X’ is independent variable
value
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:
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.
Tt = [β x Lt - Lt-1]+ [(1 – β) x Tt – 1]
The forecast (Ft) is the sum of the level and trend components: Ft = Lt + Tt
Page | 15
The forecast (Ft) is the product of the level, trend, and seasonality components:
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.
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
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
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.
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
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.
53 1533
54 2455
55 4500
56 45105
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
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 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.
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.
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))
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
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.
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.
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.
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.
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:
Procedure:
1. Step 1: Transfer all the given sales and advertising budget values along with the
corresponding months into an MS-Excel spreadsheet.
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").
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.
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.
12. Step 12: Place the cursor in to where the forecast value to be calculated and enter the formula
as below.
13. Declare arrived value from step 12 as forecasted value of 13th month January 2023.
Output:
Result:
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.
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").
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.
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.
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.
13. Declare arrived value from step 12 as forecasted value of advertisement budget for the 18
lakhs sales.
Output:
Page | 38
Result:
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:
Procedure:
=SUM((B2:B13-C2:C13)^2)/COUNT(B2:B13)
=SUM(ABS(B2:B13-C2:C13))/COUNT(B2:B13)
=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)
=CE/MAD
Output:
Results:
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:
Page | 42
1. Step 1: Open Excel and Transfer Data
Open a new Excel sheet and input the provided sales data.
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).
Go to "File" > "Options" > "Add-Ins" > "Excel Add-Ins" > Check "Analysis ToolPak"
> Click "OK".
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).
Analyze the output to understand the model coefficients and diagnostic statistics.
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.
.var_name, Canstartwithadot(.)butthedot(.)shouldnotbefollowedbyan
valid
var.name umber.
TypesofOperators
Anoperatorisasymbolthattellsthecompilertoperformspecificmathematicalorlogicalmanipulatio
ns.Rlanguageisrichinbuilt-inoperatorsandprovidesfollowingtypesofoperators.
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:
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.
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
matrix(data,nrow,ncol,byrow,dimnames)
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.
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:
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"
>sqrt(−1) #squarerootof−1
[1]NaN
Warningmessage:
Insqrt(−1):NaNsproduced
[1]0+1i
>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)
>b=c(TRUE,FALSE,TRUE,FALSE,FALSE)
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]]
>s
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)
Build-inDataFrame
Weusebuilt-indataframesinRforourtutorials.Forexample,hereisabuilt-
indataframeinR,calledmtcars.
>mtcars
mpgcyldisphpdratwt...
MazdaRX4 Wag21.061601103.902.88...
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...
Page | 72
Output:
Inference:
Page | 73
Ex. No. 17
Data Slicing inR Program
Date:
Aim:
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...
Wecanretrievearowbyitsname.
>mtcars["CamaroZ28",]
mpgcyldisphpdratwt...
CamaroZ2813.383502453.733.84...
Page | 76
Andwecanpacktherownamesinanindexvectorinordertoretrievemultiplerows.
>mtcars[c("Datsun710","CamaroZ28"),]
mpgcyldisphpdratwt...
CamaroZ2813.383502453.733.84...
LogicalIndexing
Lastly,wecanretrieverowswithalogicalindexvector.InthefollowingvectorL,thememberv
alueisTRUEifthecarhasautomatictransmission,andFALSEifotherwise.
>L=mtcars$am==0
>L
[1]FALSEFALSEFALSETRUE...
Hereisthelistofvehicleswithautomatictransmission.
>mtcars[L,]
mpgcyldisphpdrat wt ...
HornetSportabout18.78360.01753.153.440...
Andhereisthegasmileagedataforautomatictransmission.
Page | 77
>mtcars[L,]$mpg
Output:
Inference:
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:
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:
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:
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:
Page | 89
Ex. No. 24
Data Frame in R Commander
Date:
Aim:
To usethe DataframefeaturefordatausingRcommander
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
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.
summary(RegFC)
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:
Procedure:
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:
Result:
Thuswelearntregressionwithcategoricalvariables
Page | 95
Page | 96
Ex. No. 28 Forecasting by using Naïve method in R
Date: Program
Aim:
Tounderstandthetimeseriesdataandforecastusingnaïvemethod
Procedure:
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:
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
---
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
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