0% found this document useful (0 votes)
12 views16 pages

Algorithm

The document outlines various statistical methods and their implementation in R programming, including graphical representations for grouped data, probability distributions, regression and correlation analysis, large and small sample tests, F-tests, Chi-Square tests, and ANOVA. Each section provides an aim, algorithm, coding examples, and results for the respective statistical tests. The document serves as a comprehensive guide for performing statistical analyses using R.

Uploaded by

Udhaya Perumal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views16 pages

Algorithm

The document outlines various statistical methods and their implementation in R programming, including graphical representations for grouped data, probability distributions, regression and correlation analysis, large and small sample tests, F-tests, Chi-Square tests, and ANOVA. Each section provides an aim, algorithm, coding examples, and results for the respective statistical tests. The document serves as a comprehensive guide for performing statistical analyses using R.

Uploaded by

Udhaya Perumal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

3.

Graphics for grouped data

Aim
To create graphical representations for grouped data using a Bar Graph and a Line Graph in R
programming.

Algorithm
1. Start the program.
2. Create a numeric vector A to store the frequencies (number of students).
3. Create a character vector B to store the class intervals (marks range).
4. Use the barplot() function to draw the bar graph.
5. Assign:
o [Link] = B for class intervals
o xlab for X-axis label
o ylab for Y-axis label
o col for bar color
o border for border color
o main for the title

7. Create a numeric vector x to represent time values.


8. Create a numeric vector y to represent cost values.
9. Use the plot() function with type='b' to draw both points and lines.
10. Set:
o main for title
o xlab for X-axis label
o ylab for Y-axis label
o lwd for line width

11. Display the bar and line graph.

12. Stop the program.

Coding:

#Bar graph
A <- c(17, 11, 7, 13, 5, 10, 18, 25, 20, 18, 30, 19 )
B <- c("0-5","5-10","10-15","15-20","20-25","25-30","30-35",
"35-40","40-45","45-50","50-55","55-60")
barplot(A, [Link] = B, xlab="Marks", ylab="No. of Students",
col = "green", border = "black",
main ="Graphics for grouped data")
#line graph
x = c(1,2,3,4,5,6,7,8,9,10,11)
y = c(22,13,5,9,25,22,26,1,9,10,2)

plot(x, y, type='b', main="Graphics for grouped data",


xlab="Time", ylab="Cost", asp=1, lwd=2)

Output:
4. Graphical Display of distributions

Aim
To graphically display different probability distributions such as Binomial, Poisson, Beta, and
Normal distributions using R programming.

Algorithm
Step 1: Start the Program

Binomial Distribution

1. Create a sequence success <- 0:20 to represent possible number of successes.


2. Use the function dbinom(success, size=20, prob=0.3) to calculate binomial
probabilities.
3. Use the plot() function with type='l' to draw the line graph.
4. Add title and axis labels using main, xlab, and ylab.
5. Display the Binomial distribution graph.

Poisson Distribution

6. Use the same success <- 0:20 values.


7. Calculate Poisson probabilities using dpois(success, lambda=5).
8. Use the plot() function with type='l' to draw the graph.
9. Add title and axis labels.
10. Display the Poisson distribution graph.

Beta Distribution

11. Create a sequence p = seq(0,1,length=100) to represent probability values.


12. Compute Beta density using dbeta(p, 2, 10).
13. Use the plot() function with type='l' to draw the curve.
14. Add title and axis labels.
15. Display the Beta distribution graph.

Normal Distribution

16. Create a sequence x <- seq(-10, 10, by=0.1).


17. Calculate normal density using dnorm(x, mean=0, sd=1).
18. Use the plot() function with type='l' to draw the curve.
19. Add title and axis labels.
20. Display the Normal distribution graph.

Step 2: Stop the Program

Coding

#Binomial Distribution
success <- 0:20

plot(success,dbinom(success,size=20,prob=.3),
type='l',
main='Binomial Distribution (n=20, p=0.3)',
xlab ="Successes",
ylab="Probability",
lwd=2)

#Poisson Distribution
success <- 0:20
plot(success, dpois(success, lambda=5),
type='l',
main="Poisson Distribution (lambda = 5)",
xlab ="Successes",
ylab="Probability",
lwd=2)

#Beta Distribution
p = seq(0,1, length=100)

plot(p, dbeta(p, 2, 10), xlab="p", ylab="Density",


type ='l', main="Beta Distribution", lwd=2)

#Normal Distribution
x <- seq(-10, 10, by = .1)

plot(x,dnorm(x, mean = 0, sd = 1), type='l',


main = "Normal Distribution",
xlab="x", ylab="y")

[Link] and Correlation - Linear Models

Aim
To find the Correlation Coefficient between Height and Weight and to obtain the Regression
Equations of

1. yyy on xxx
2. xxx on yyy
using R programming.

Algorithm
Step 1: Start the Program

Step 2: Input the Data


1. Create a numeric vector x to store Height values.
2. Create a numeric vector y to store Weight values.

Step 3: Find Correlation Coefficient

3. Use the function cor(x, y) to calculate the correlation coefficient rrr.


4. Store the result in variable r.
5. Print the value of r using print(r).

Step 4: Regression Equation of y on x

6. Use the function lm(y ~ x) to create a linear regression model.


7. Store it in variable model1.
8. Print the model using print(model1) to obtain the regression equation of y on x.

Step 5: Regression Equation of x on y

9. Use the function lm(x ~ y) to create another regression model.


10. Store it in variable model2.
11. Print the model using print(model2) to obtain the regression equation of x on y.

Step 6: Stop the Program

Result
Thus, the correlation coefficient and the regression equations of

 y on x
 x on y

are obtained successfully using R.

8. Large Sample Tests

Aim
To perform large sample tests in R for:

1. Test of significance of a single proportion


2. Test of significance of two proportions
3. Test of significance of a single mean
4. Test of significance of difference between two means
Algorithm

1. Test of Significance of Single Proportion


Step 1: Start the program.

Step 2: Set significance level alpha = 0.05.

Step 3: State hypotheses:

 H0:P=P0H_0: P = P_0H0:P=P0
 H1:P≠P0H_1: P \neq P_0H1:P P0

Step 4: Use [Link](x, n, p, alternative="[Link]") to compute test statistic.

Step 5: Store result in Zcal.

Step 6: Compare p-value (Zcal[3]) with alpha.

 If p-value < alpha → Reject H0H_0H0


 Otherwise → Accept H0H_0H0

2. Test of Significance of Two Proportions


Step 1: Set alpha = 0.05.

Step 2: State hypotheses:

 H0:P1=P2H_0: P_1 = P_2H0:P1=P2


 H1:P1≠P2H_1: P_1 \neq P_2H1:P1 P2

Step 3: Use [Link](x=c(x1,x2), n=c(n1,n2), alternative="[Link]").

Step 4: Store result in Zcal.

Step 5: Compare p-value with alpha.

Step 6: Print decision (Reject or Accept H0H_0H0).

3. Test of Significance for Single Mean (Z-Test)


Step 1: Install and load BSDA package.

Step 2: Set alpha = 0.05.


Step 3: State hypotheses:

 H0:μ=1600H_0: \mu = 1600H0:μ=1600


 H1:μ≠1600H_1: \mu \neq 1600H1:μ 1600

Step 4: Generate sample data using rnorm().

Step 5: Use [Link](x, mu=1600, sigma.x=120) function.

Step 6: Store result in Zcal.

Step 7: Compare p-value (Zcal[2]) with alpha.

4. Test of Significance for Difference of Means


Step 1: Install and load BSDA package.

Step 2: Set alpha = 0.05.

Step 3: State hypotheses:

 H0:μ1=μ2H_0: \mu_1 = \mu_2H0:μ1=μ2


 H1:μ1≠μ2H_1: \mu_1 \neq \mu_2H1:μ1 μ2

Step 4: Generate two samples using rnorm().

Step 5: Use [Link](x, y, mu=25, sigma.x=40, sigma.y=30).

Step 6: Store result in Zcal.

Step 7: Compare p-value with alpha.

Result
Thus, the large sample tests for proportion, two proportions, mean, and difference of means are
performed successfully using R.
9. Small Sample Test – t-Test

Aim
To perform small sample t-tests using R for:

1. Test of significance of a single mean


2. Test of significance of two independent means

Algorithm

1. Test for Significance of Single Mean (One Sample t-test)


Step 1: Start the program.

Step 2: Set the significance level alpha = 0.05.

Step 3: State the hypotheses:

 H0:μ=50H_0: \mu = 50H0:μ=50 Kgs


 H1:μ≠50H_1: \mu \neq 50H1:μ 50 Kgs

Step 4: Enter the sample data into vector x.

Step 5: Use the function [Link](x, mu=50) to calculate the test statistic.

Step 6: Store the result in variable tcal.

Step 7: Compare the p-value (tcal[3]) with alpha.

 If p-value < alpha → Reject H0H_0H0


 Otherwise → Accept H0H_0H0

2. Test for Significance of Two Means (Independent Samples


t-test)
Step 1: Set alpha = 0.05.

Step 2: State the hypotheses:

 H0:μ1=μ2H_0: \mu_1 = \mu_2H0:μ1=μ2


 H1:μ1<μ2H_1: \mu_1 < \mu_2H1:μ1<μ2 (Left-tailed test)
Step 3: Enter the two sample data sets into vectors x and y.

Step 4: Use the function


[Link](x, y, [Link]=TRUE, alternative="less")
to perform independent sample t-test.

(Correct spelling: alternative="less" instead of "lesser")

Step 5: Store result in tcal.

Step 6: Compare the p-value with alpha.

 If p-value < alpha → Reject H0H_0H0


 Otherwise → Accept H0H_0H0

Step 7: Display the result.

Result
Thus, the small sample t-test for single mean and independent two means is performed
successfully using R.

10. F – Test (Test for Equality of Variances)

Aim
To test whether there is any significant difference between the variances of time taken by two
methods using F-test at 5% level of significance.

Algorithm
Step 1: Start the Program

Step 2: State the Hypotheses

 H0H_0H0: There is no significant difference between the variances of time distribution.


 H1H_1H1: There is a significant difference between the variances of time distribution.

Set the level of significance:


alpha = 0.05

Step 3: Enter the Data


1. Store method 1 time values in vector method_1.
2. Store method 2 time values in vector method_2.

Step 4: Calculate Sample Variances

3. Compute variance of method 1 using var(method_1) and store in x.


4. Compute variance of method 2 using var(method_2) and store in y.

Step 5: Perform F-Test

5. Use [Link](method_1, method_2) and store in F1.


6. Use [Link](method_2, method_1) and store in F2.

Step 6: Decision Rule

7. Compare the two variances x and y.


8. Select the F-test result corresponding to the larger variance in the numerator.
9. Compare p-value (F1[3] or F2[3]) with alpha.

 If p-value < alpha → Reject H0H_0H0


 Otherwise → Accept H0H_0H0

Step 7: Display the Result

10. Print the F-test result and conclusion.

Step 8: Stop the Program

Result
Thus, the F-test is performed to determine whether there is a significant difference between the
variances of the two methods.

11. Chi-Square Test (Goodness of Fit Test)

Aim
To test whether accidents are equally likely to occur on all days of the week using the Chi-
Square Goodness of Fit Test at 5% level of significance.

Algorithm
Step 1: Start the Program
Step 2: State the Hypotheses

 H0H_0H0: There is no difference between Observed and Expected values (Accidents are
equally distributed across days).
 H1H_1H1: There is a difference between Observed and Expected values (Accidents are
not equally distributed).

Set level of significance:


alpha = 0.05

Step 3: Enter the Data

1. Store the observed frequencies in vector:


observed <- c(8, 12, 9, 14, 17)
2. Since accidents are equally likely for 5 days, set expected probabilities as:
expected <- c(rep(1/5, 5))

Step 4: Perform Chi-Square Test

3. Use the function:


[Link](observed, p=expected)
4. Store the result in Cal_Value.

Step 5: Decision Rule

5. Extract the p-value from the result.


6. Compare p-value with alpha.

 If p-value < alpha → Reject H0H_0H0


 If p-value ≥ alpha → Accept H0H_0H0

Step 6: Display the Conclusion

7. Print whether H0H_0H0 is accepted or rejected.

Step 7: Stop the Program

Result
Thus, using the Chi-Square test, we determine whether accidents are equally distributed across
the days of the week or not.
12. ANOVA (One-Way Classification)

Aim
To test whether there is any significant difference among the mean outputs of three machines
using One-Way ANOVA at 5% level of significance.

Algorithm
Step 1: Start the Program

Step 2: State the Hypotheses

 H0H_0H0: μ1=μ2=μ3\mu_1 = \mu_2 = \mu_3μ1=μ2=μ3 (All machine means are equal)


 H1H_1H1: At least one mean differs

Set level of significance:


alpha = 0.05

Step 3: Enter the Data

1. Create a matrix containing:


o Machine labels (A, B, C)
o Corresponding output values
2. Assign column names:
o "Machine"
o "Output"
3. Convert the matrix into a data frame using [Link]().

Step 4: Perform One-Way ANOVA

4. Use the function:

aov(Output ~ Machine, data=frame)

5. Store the result in variable anova.


6. Use summary(anova) to display the ANOVA table.

Step 5: Decision Rule

7. Compare the p-value from the ANOVA table with alpha.


 If p-value < alpha → Reject H0H_0H0
 If p-value ≥ alpha → Accept H0H_0H0

Step 6: Display the Result

8. Print the ANOVA summary and state the conclusion.

Step 7: Stop the Program

Result
Thus, One-Way ANOVA is performed to determine whether there is a significant difference
among the mean outputs of the three machines.

13. ANOVA (Two-Way Classification)

Aim
To test whether there is any significant difference in Ash Content with respect to:

1. Varieties
2. Chemists

using Two-Way ANOVA at 5% level of significance.

Null Hypotheses
 H01H_{01}H01: There is no significant difference between Varieties.
 H02H_{02}H02: There is no significant difference between Chemists.

Alternative Hypotheses
 H11H_{11}H11: There is a significant difference between Varieties.
 H12H_{12}H12: There is a significant difference between Chemists.

Algorithm
Step 1: Start the Program

Step 2: Enter the Data

1. Create a data frame containing:


o Variety (A, B, C)
o AshContent (numerical values)
o Chemist (1, 2, 3, 4)
2. Convert it into a proper data frame using [Link]().

Step 3: Set Level of Significance

Set
alpha = 0.05

Step 4: Perform Two-Way ANOVA (Without Interaction)

3. Use:

aov(AshContent ~ Variety + Chemist, data=data)

4. Display result using summary().


5. Compare p-values for:
o Variety
o Chemist

with alpha.

Step 5: Perform Two-Way ANOVA (With Interaction)

6. Use:

aov(AshContent ~ Variety * Chemist, data=data)

7. Display result using summary().


8. Check p-values for:
o Variety
o Chemist
o Variety:Chemist (Interaction)

Step 6: Decision Rule

 If p-value < alpha → Reject corresponding Null Hypothesis


 If p-value ≥ alpha → Accept corresponding Null Hypothesis

Step 7: Conclusion

Based on the p-values:

 Conclude whether there is a significant difference between Varieties.


 Conclude whether there is a significant difference between Chemists.
 Conclude whether interaction effect exists (if using interaction model).
Result
Thus, Two-Way ANOVA is performed to determine the effect of Variety and Chemist on Ash
Content.

You might also like