You are on page 1of 21

Department of Statistics

Program: M.Sc. Statistics

Course Number 1569

Data Analysis and Statistical


Course Title
Packages
Semester/Year 3rd / 2019 Autumn

Instructor Sir. Zahoor Ahmad

02
ASSIGNMENT No.

Submission Date

Due Date

Student Name Muhamamd Hashim Javed


Student ID BT-588221

Signature*
Name: M. Hashim Javed Roll: BT-588221
Q1. (a). What are macros and what are there types.
Macros
Minitab macros are collections of Minitab code that allow the user to implement in a single command
procedures and techniques which would otherwise require many separate Minitab commands to be
entered. A macro has a name which is the means by which the user invokes the macro.

Types of macros

Three types of macros have been developed in Minitab to perform various repetitive tasks easily and
effectively:

• Global macros (simple macros, simplest form of macros)

• Local macros (advanced macros)

• Execs (an older form of Minitab macros)

Global Macros
Allow you to analyze and manipulate data in the active worksheet. However, the identity of any
columns, constants, and matrices that are to be accessed must be specified within the macro; therefore,
the worksheet must be configured the same way each time the macro is run.

Example of a Global Macro


Here is a simple example of a macro file named ANALYZE.MAC. Indenting is not necessary, but may be
done to improve readability as illustrated here.

GMACRO (Marks the beginning of the global macro)

Analyze (The template, or the name, of this macro)

NAME C1 "Yield" C2 "Chem1" & C3 "Chem2" C5 "Ln.Yield" (Body of the macro)


PRINT C1-C3
DESCRIBE C1-C3
LET C5 = LOGE('Yield')
REGRESS;
RESPONSE C5;
CONTINUOUS C2 C3;
TERMS C2 C3.

ENDMACRO

Invoking a Global Macro

Name: M. Hashim Javed Roll: BT-588221


To invoke, or process, a global macro from Minitab, enter the symbol % followed by the macro file
name. For example, to invoke a macro file named ANALYZE.MAC, enter the command: %ANALYZE

Advanced Macros (Local Macros)


Local macros are more complex than global macros, and thus harder to write. However, they are
more powerful and flexible. If you need to write a fairly complex macro, or if you want a macro which
you can execute like a Minitab command, then you should write a local macro.
Local macros can use temporary variables, arguments, and subcommands to enhance the
processing capabilities of the macro. Local macros also have a different structure that allows you to
include areas for defining the common commands and the variables.

Local Macro Elements


Local macros have the capability to handle several elements which improve the processing
capabilities of your macro. These three elements are explained further:
i. Variables
ii. Arguments
iii. Subcommands

i. Variables
A variable is an alias that can refer to some piece of data: a number, text string, column,
constant, or matrix. For example, a variable named "Test1" could represent any of the following:
a column of test scores, a constant that is the mean of the test scores, or a text string that is the
name of the test.
ii. Arguments
Arguments are variables that are passed into and out of a macro when it is invoked. The
variables are listed on the main command line and subcommand lines of the macro. If you pass
a global worksheet variable (a column, constant, or matrix) to a macro and the macro changes
the value of that variable, the global worksheet variable will contain that changed value after
the macro executes. An argument can be a variable which represents:

• a stored column, constant, or matrix from a global worksheet: 'Sales', C1, K2,
or M1
• a number such as 2.3

iii. Variables
Local macros can also have subcommands that can modify the behavior of the macro –
just as subcommands in interactive Minitab can change the behavior of a command.
Subcommands can have their own arguments. You can also choose to include or not include the
subcommand when invoking the local macro.
For example, the scatter plot macro described above could be made more flexible by
including a subcommand that lets you decide at what level the confidence bands should be
drawn

Example of a Local Macro


The macro TRIM calculates a 10% trimmed mean--5% trimmed from each end of the data--for a
column of data from the global worksheet and stores it in a constant in the global worksheet.

Name: M. Hashim Javed Roll: BT-588221


(1) MACRO

(2) TRIM X XBAR


#
# TRIM takes one column, X, as input. It orders the data, trims 5%
# from each end, calculates the mean of the remaining data, and
# stores it in the constant XBAR.
#

MCONSTANT N T1 T2 XBAR
MCOLUMN X XSORT XTRIM

(4) #
# first we calculate the trimming points T1 and T2
LET N = COUNT(X)
LET T1 = ROUND(N*0.05)
LET T2 = N-T1+1
# next we check for the case when T1 = 0 and nothing is trimmed
IF T1 = 0
LET XTRIM = X
# otherwise, we sort X, trim the ends and calculate the mean
ELSE
LET XSORT =SORT(X)
COPY XSORT XTRIM;
OMIT 1:T1 T2:N.
ENDIF
LET XBAR = MEAN(XTRIM)

(5) ENDMACRO

Key
Here is what each line in the macro means:
1. MACRO marks the beginning of a local macro.
2. Template. Says to invoke this macro with two arguments: argument 1 is the column of data to be
trimmed, and argument 2 is the constant where the trimmed mean is to be stored
3. Declaration statements:
• MCONSTANT declares four constants (N, T1, T2, and XBAR) to be used as variables by the local
macro. One of these constants, XBAR, is an argument which corresponds to the constant that is
passed into the macro when the user invokes the macro.
• MCOLUMN declares three columns (X, XSORT, and XTRIM) to be used as variables by the local
macro. One of these columns, X, is an argument which corresponds to the column that is passed
into the macro when the user invokes the macro.
4. Body of the macro.
5. ENDMACRO marks the end of the macro.

Name: M. Hashim Javed Roll: BT-588221


Execs (overview)
Execs are stored commands that you use over and over, so that you do not have to retype the
commands each time. You can even write an interactive Exec, which pauses during execution, prompts
the user for information, then continues with execution. Execs are useful for many things, including the
following:
• Repeating a block of commands many times, which is useful for simulations
• Looping through columns of the worksheet, doing the same analysis on each block of columns
• Looping through rows of the worksheet, doing the same analysis on each block of rows
• Performing complex operations not provided as stand-alone commands

How Execs are different from global and local macros


Global and local macros (also called %Macros) are more powerful and flexible than Execs. Some
of the other differences
are as follows:
• Exec, with a default extension of .MTB, is invoked by typing the command EXECUTE or by
choosing File > Tools > Run an Exec.
• Global and local macros, with the default file extension of .MAC, are invoked by entering the
symbol % followed by the macro file name. For example, %SALES invokes the macro SALES.MAC.

If you have Execs that were written using previous releases of Minitab, you can continue to use them
with no change, unless, of course, the Execs use deprecated commands.

To convert your Exec to a global macro


1. Add three lines to your Exec file: GMACRO as the first line, ENDMACRO as the last line, and the
template (the macro name) as the second line of the file.
2. Check for Minitab commands that work differently in %macros (below).
3. Save the macro as a text file, with the extension .MAC.

Name: M. Hashim Javed Roll: BT-588221


Q1. (b) Write a minitab macro to simulate tossing of a die 2000 times. Find the proportion of
even numbers appeared.

MACRO

diethrow

mcolumn a
mconstant n x y z p1 p2

Random 2000 'a';


Integer 1 6.

let n = count(a)

let x = 0
let y = 0

do x = 1 : n
if MOD(a(x),2)=0
let y = y + 1
endif
enddo

let z=(y/n)*100

let p1 = "The Proportion of Even numbers is:"


print p1 z

ENDMACRO

Name: M. Hashim Javed Roll: BT-588221


Q2. (a) Write a minitab macro to find mean deviation of values given in any column of
worksheet.

MACRO
mdev a

mcolumn a b c
mconstant k1 k2

Let k1 = MEAN(a)
Let b = a - K1
Let c = ABSO(b)
Let k2 = MEAN(c)
print k2

endmacrO

Name: M. Hashim Javed Roll: BT-588221


Q2. (b) Write a Minitab macro to draw a systematic random sample of size n from a
population of size N. Population may be stored in a column on Minitab worksheet.

MACRO

systsam a

mcolumn a b
mconstant n np

let Np = count(a)
let n = 50

Sample n 'a' 'b'.

print "Sample of n="


print n
print "From Population of N="
print Np
print 'b'

ENDMACRO

Name: M. Hashim Javed Roll: BT-588221


Q3. (a) Suppose you have data of 9876 students who have appeared in an entry test
examination for admission to M.Sc. Statistics. Data is available in a column of the worksheet.
Write a Minitab macro which calculates the percentile to the marks of a given student.

MACRO

percentile a sc

mcolumn a b
mconstant sc n x y z

let n=count(a)
let b=sort(a)
let y=0
let x=0
do x=1:n
if b(x)<=sc
let y=y+1
endif
enddo

let z=(y/n)*100

print z

ENDMACRO

Name: M. Hashim Javed Roll: BT-588221


Q3. (b) Compare and contrast the coding methods available in SPSS and Minitab.

Comparison of Minitab and IBM SPSS


1. Easy to learn and easy to use. SPSS is menu-driven; the software is very easy to use. Like
Minitab, most of the functionalities in SPSS are organized into pull-down menus in a very
intuitive way. The learning curves for SPSS and Minitab are similar.

2. SPSS is generally stronger in statistical analysis, especially in some specific area, such as
ANOVA-related procedures. The add-on modules give SPSS further flexibility and
potentials to develop its capacities. However, for cutting-edge statistical analysis, SPSS is
stronger than Minitab. So SPSS is most suitable to you if your work involves large dataset,
frequent data management, and intermediate/partially-advanced statistical analysis.

3. SPSS Statistics is loaded with powerful analytic techniques and time-saving features to
help you quickly and easily find new insights in your data, so you can make more accurate
predictions and achieve better outcomes for your organization.

4. View interactive SPSS Statistics output on smart devices (smartphones and tablets) and
Generate presentation-ready output quickly and easily

5. Enhanced Monte Carlo simulation to improve model accuracy with


a. Ability to fit a categorical distribution to string fields
b. Support for Automatic Linear Modeling (ALM)
c. Generate heat maps automatically when displaying scatterplots in which the
target or the input, or both, are categorical
d. Automatically determine and use associations between categorical inputs when
generating data for those inputs
e. Generating data in the absence of a predictive model

6. SPSS Advanced Statistics offers generalized linear mixed models (GLMM), general linear
models (GLM), mixed models procedures, generalized linear models (GENLIN) and
generalized estimating equations (GEE) procedures.

Name: M. Hashim Javed Roll: BT-588221


Q4. (a) Define p-value approach in testing of hypothesis. How will you calculate p-value for a
z-test. Account for problems in calculation of p-value for t-test and F-test without using
statistical software. Write down command in R and Minitab to get p-value for a given value of
't' and 'F' test statistic.

Commands in R
P-value and t-test
t.test({x1},{x2})

P-value and f-test


var.test({x1},{x2})

Commands in Minitab
P-value and t-test
MTB > Onet '{testing-data-variable}';
SUBC> Test {testing-against-mean-value};
SUBC> Confidence 95.0;
SUBC> Alternative 0.

P-value and f-test


MTB > Oneway '{x1}' '{x2}'.

Name: M. Hashim Javed Roll: BT-588221


Q5. (a) What do you know about functions in R. Explain arithmetic operators available in R.

R - Functions
A function is a set of statements organized together to perform a specific task. R has a
large number of in-built functions and the user can create their own functions.
In R, a function is an object so the R interpreter is able to pass control to the function,
along with arguments that may be necessary for the function to accomplish the actions.
The function in turn performs its task and returns control to the interpreter as well as any
result which may be stored in other objects.

Function Definition
An R function is created by using the keyword function. The basic syntax of an R
function definition is as follows −
function_name <- function(arg_1, arg_2, ...) {
Function body
}
Function Components
The different parts of a function are −
• Function Name − This is the actual name of the function. It is stored in R environment as an
object with this name.
• Arguments − An argument is a placeholder. When a function is invoked, you pass a value to
the argument. Arguments are optional; that is, a function may contain no arguments. Also
arguments can have default values.
• Function Body − The function body contains a collection of statements that defines what the
function does.
• Return Value − The return value of a function is the last expression in the function body to be
evaluated.
R has many in-built functions which can be directly called in the program without
defining them first. We can also create and use our own functions referred as user
defined functions.

Built-in Function
Simple examples of in-built functions
are seq(), mean(), max(), sum(x) and paste(...) etc. They are directly called by user
written programs. You can refer most widely used R functions.
# Create a sequence of numbers from 32 to 44.
print(seq(32,44))

Name: M. Hashim Javed Roll: BT-588221


# Find mean of numbers from 25 to 82.
print(mean(25:82))

# Find sum of numbers frm 41 to 68.


print(sum(41:68))
When we execute the above code, it produces the following result −
[1] 32 33 34 35 36 37 38 39 40 41 42 43 44
[1] 53.5
[1] 1526
User-defined Function
We can create user-defined functions in R. They are specific to what a user wants and
once created they can be used like the built-in functions. Below is an example of how a
function is created and used.
# Create a function to print squares of numbers in sequence.
new.function <- function(a) {
for(i in 1:a) {
b <- i^2
print(b)
}
}

Calling a Function
Live Demo

# Create a function to print squares of numbers in sequence.


new.function <- function(a) {
for(i in 1:a) {
b <- i^2
print(b)
}
}

# Call the function new.function supplying 6 as an argument.


new.function(6)

When we execute the above code, it produces the following result −


[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
[1] 36
Calling a Function without an Argument
# Create a function without an argument.
new.function <- function() {
Name: M. Hashim Javed Roll: BT-588221
for(i in 1:5) {
print(i^2)
}
}

# Call the function without supplying an argument.


new.function()
When we execute the above code, it produces the following result −
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
Calling a Function with Argument Values (by position and by name)
The arguments to a function call can be supplied in the same sequence as defined in
the function or they can be supplied in a different sequence but assigned to the names
of the arguments.
# Create a function with arguments.
new.function <- function(a,b,c) {
result <- a * b + c
print(result)
}

# Call the function by position of arguments.


new.function(5,3,11)

# Call the function by names of the arguments.


new.function(a = 11, b = 5, c = 3)

When we execute the above code, it produces the following result −


[1] 26
[1] 58
Calling a Function with Default Argument
We can define the value of the arguments in the function definition and call the function
without supplying any argument to get the default result. But we can also call such
functions by supplying new values of the argument and get non default result.
# Create a function with arguments.
new.function <- function(a = 3, b = 6) {
result <- a * b
print(result)
}

# Call the function without giving any argument.


new.function()

Name: M. Hashim Javed Roll: BT-588221


# Call the function with giving new values of the argument.
new.function(9,5)
When we execute the above code, it produces the following result −
[1] 18
[1] 45
Lazy Evaluation of Function
Arguments to functions are evaluated lazily, which means so they are evaluated only
when needed by the function body.
# Create a function with arguments.
new.function <- function(a, b) {
print(a^2)
print(a)
print(b)
}

# Evaluate the function without supplying one of the arguments.


new.function(6)

When we execute the above code, it produces the following result −


[1] 36
[1] 6
Error in print(b) : argument "b" is missing, with no default

R Arithmetic Operators
These operators are used to carry out mathematical operations like addition and
multiplication. Here is a list of arithmetic operators available in R.

Operator Description

+ Addition

– Subtraction

* Multiplication

/ Division

^ Exponent

Name: M. Hashim Javed Roll: BT-588221


%% Modulus (Remainder from division)

%/% Integer Division

Q5. (b) Create a function in R, that will calculate the number of divisors of any integer given
by the user. It also displays all the divisors.

divisors <- function(x)


{
count <- 0
for (i in x:1)
{
if(x%%i==0)
{
count <- count+1
cat(" ",i,", ")
}
}
return(count)
}

divisors(15)

The user will pass the integer in the function named divisors(x), and the function will print the divisors
and return the number of divisors for that integer.

Name: M. Hashim Javed Roll: BT-588221


Q6. (a) Write a function in R to calculate number of combinations. Values of n and r are
provided by the user.

comb <- function()


{
n <- readline(prompt="In nPr enter the value for n: ")
n <- as.integer(n)
r <- readline(prompt="In nPr enter the value for r: ")
r <- as.integer(r)
combinations <- 0
combinations <- factorial(n) / (factorial(n-r) * factorial(r))

cat("The number of combinations for n=",n," and r=",r,"


is:",combinations)
}

comb()

The user will be prompted to enter the values for r and n in nPr, and then these values will be used in the
calculation of nPr and the function will print the answer as number of combinations.

Name: M. Hashim Javed Roll: BT-588221


Q6. (b) Create a function in R to find that how many times an integer appears in a given
vector.

match <- function(x,y)


{
count <- 0
vctr <- x
int <- y
ln <- length(x)
for (i in 1:ln)
{
if (vctr[i]==int)
{
count <- count+1
}
}
return(count)
}

We will pass the vector as ‘x’ and the integer as ‘y’ to check in the function match(x,y) and the function
will return the number of times integer y appears in the given vector.

Name: M. Hashim Javed Roll: BT-588221


Q7 (a): Write a function in R to print the following pattern using any loop statement
1 2 3 4 5

1 2 3 4

1 2 3

1 2

flag <- function()


{
for (x in 5:1)
{
for (i in 1:x)
{
cat(" ")
cat(i)
cat(" ")
}
cat("\n\n")
}
}

flag()

Upon calling the function flag() the required output will be produced.

Name: M. Hashim Javed Roll: BT-588221


Q7 (b): Implement a fizzbuzz function. It takes a single number as input. If the number is divisible by
three, it returns "fizz". If it's divisible by five it returns "buzz". If it's divisible by three and five, it
returns "fizzbuzz". Otherwise, it returns the number.

fizzbuzz <- function()


{
i <- readline(prompt="Enter an integer: ")
i <- as.integer(i)

if(i%%3 == 0 & i%%5 == 0)


{
print('FizzBuzz')
}
else if(i%%3 == 0)
{
print('Fizz')
}
else if (i%%5 == 0)
{
print('Buzz')
}
else
{
print(i)
}
}

fizzbuzz()

Name: M. Hashim Javed Roll: BT-588221

You might also like