0% found this document useful (0 votes)
1 views20 pages

r Program Corrections

The document contains various R programming examples demonstrating matrix operations, graphical displays, statistical tests, and measures of central tendency and dispersion. Key operations include matrix addition, subtraction, and multiplication, as well as visualizations like histograms and pie charts. Additionally, it covers statistical tests such as ANOVA, t-tests, and correlation coefficients, providing outputs for each operation.

Uploaded by

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

r Program Corrections

The document contains various R programming examples demonstrating matrix operations, graphical displays, statistical tests, and measures of central tendency and dispersion. Key operations include matrix addition, subtraction, and multiplication, as well as visualizations like histograms and pie charts. Additionally, it covers statistical tests such as ANOVA, t-tests, and correlation coefficients, providing outputs for each operation.

Uploaded by

Preethi Victor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

01 .

CREATE MATRIX AND PERFORM MATRIX OPERATIONS


PROGRAM :

A=matrix(c(1:9),nrow=3,byrow=TRUE)

B=matrix(c(1,3,4,5,6,7,8,9,1),nrow=3,byrow=TRUE)

print(A+B)

print(A-B)

print(A%*%B)

print(A/B)

print(A%%B)

OUTPUT :

print(A+B)

[,1] [,2] [,3]

[1,] 2 5 7

[2,] 9 11 13

[3,] 15 17 10

print(A-B)

[,1] [,2] [,3]

[1,] 0 -1 -1

[2,] -1 -1 -1
[3,] -1 -1 8

print(A%*%B)

[,1] [,2] [,3]

[1,] 35 42 21

[2,] 77 96 57

[3,] 119 150 93

print(A/B)

[,1] [,2] [,3]

[1,] 1.000 0.6666667 0.7500000

[2,] 0.800 0.8333333 0.8571429

[3,] 0.875 0.8888889 9.0000000

Print(A%%B)

[,1] [,2] [,3]

[1,] 0 2 3

[2,] 4 5 6

[3,] 7 8 0

04. GRAPHICAL DISPLAY OF POISSON DISTRIBUTION


PROGRAM :

# Set the seed for reproducibility

set.seed(123)

# Generate a poisson-distributed dataset

larbda <- 5 # Average rate of events

poisson_data <- rpois(100, larbda)

# Create a bar plot to visualize the probability mass function

barplot(table(poisson_data)/length(poisson_data),

col = "Skyblue",

main = "Poisson Distribution PMF",

xlab = "Number of Events",

ylab = "Probaility",

ylim = c(0,0.15))

#Add a red line reoresention the theoretical poisson PMF

OUT PUT :
02. HISTOGRAM
PROGRAM :

v <- c(19, 23, 11, 5, 16, 21, 32, 14, 19, 27, 39)

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

col = "red", border = "black")

OUTPUT :
3. PIE – CHART
PROGRAM :

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

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

pie(geeks, labels, main = "City pie chart",

col = rainbow(length(geeks)))

OUTPUT :
15. ANOVO – TWO WAY
PROGRAM :

data<-data.frame(

plant=c(“Hibiscus”,”Marigold”,”Rose”),

number=c(5,5,5),

average_span=c(12,16,20)

anova_result<-aov(Average_span~Plant,data=data)

summary(anova_result)

OUTPUT :

Df Sum Sq Mean Sq

Plant 2 32 16

14. SMALL SAMPLE FOR F – TEST


PROGRAM :

set.seed(123)

group1<-rnorm(20,mean=5,sd=2)

group2<-rnorm(20,mean=4,sd=2)

var.test(group1,group2)

OUTPUT :

F test to compare two variances

data: group1 and group2

F = 1.3735, num df = 19, denom df = 19, p-value = 0.4957

alternative hypothesis: true ratio of variances is not equal to 1

95 percent confidence interval:

0.5436557 3.4701298

sample estimates:

ratio of variances

1.37352

13. PAIRED T – TEST


PROGRAM :

Before<-c(23,25,27,29,31)

After<-c(22,24,26,28,30)

Result<-t.test(Before,After,paired=TRUE)

print(Result)

OUTPUT :

Paired t-test

data: Before and After

t = -0.75, df = 4, p-value = 0.495

alternative hypothesis: true mean difference is not equal to 0

95 percent confidence interval:

-14.10578 8.10578

sample estimates:

mean difference

-3

12. TWO SAMPLE T – TEST


PROGRAM :

group1<-c(15,16,18,20,22)

group2<-c(12,14,16,18,20)

t_test_result<-t.test(group1,group2)

print(t_test_result)

OUTPUT :

Welch Two Sample t-test

data: group1 and group2

t = 1.1531, df = 7.9225, p-value = 0.2825

alternative hypothesis: true difference in means is not equal to 0

95 percent confidence interval:

-2.207077 6.607077

sample estimates:

mean of x mean of y

18.2 16.0

11. SMALL SAMPLE FOR T – TEST


PROGRAM :

sample_data<-c(1.8,1.7,1.9,1.6,1.8)

Mu<-2

t_test_result<-t.test(sample_data,mu=mu)

print(t_test_result)

OUTPUT :

One Sample t-test

data: sample_data

t = -4.7068, df = 4, p-value = 0.009262

alternative hypothesis: true mean is not equal to 2

95 percent confidence interval:

1.618429 1.901571

sample estimates:

mean of x

1.76

10. LARGE SAMPLE-TEST FOR EQUALITY OF STANDAR


DEVIATION
PROGRAM :

n1<-500

n2<-600

sd1<-25

sd2<-35

test_result<-bartlett.test(list(x=rep(0,n1),y=rep(1,n2)),list(sd=c(sd1,sd2)))

print(test_result)

OUTPUT :

Bartlett test of homogeneity of variances

data: list(x = rep(0, n1), y = rep(1, n2))

Bartlett's K-squared = NaN, df = 1, p-value = NA

9. LARGE SAMPLE-TEST FOR EQUALITY OF TWO POPULATION


PROPORTION
PROGRAM :

n1<-600

n2<-900

x1<-450

x2<-450

test_result<-prop.test(x=c(x1,x2),n=c(n1,n2),alternative=”two.sided”

print(test_result)

OUTPUT :

Data: c(x1,x2)out of c(n1,n2)

X-squared = 92.711, df = 1, p-value < 2.2e.16

Alternative hypothesis: two.sided

95 percent confidence interval

0.2009925 0.2990075

Sample estimates:

Prop 1 prop 2

0.75 0.50

08. LARGE SAMPLE - TEST FOR SPECIFIED PROTIONAL


PROGRAM :

total_tosses<-900

heads_count<-490

sample_proportion<-heads_count/total_tosses

test_result<-prop.test(x=heads_count,n=total_toses,p=0.5,

alternative=”two.sided”)

print(test_result)

OUTPUT :

1-sample proportions test with continuity correction

data: heads_count out of total_tosses, null probability 0.5

X-squared = 6.9344, df = 1, p-value = 0.008455

alternative hypothesis: true p is not equal to 0.5

95 percent confidence interval:

0.5112311 0.5772736

sample estimates:

0.5444444

07. SPEARMAN’S RANK CORRELATION COEFFICIENT


PROGRAM :

# Vectors with same length

x= c(15, 18, 21, 15, 21)

y = c(25, 25, 27, 27, 27)

# Calculating

# Correlation coefficient

# Using cor() method

result = cor(x, y, method = "spearman")

# Print the result

cat("Spearman correlation coefficient is:", result)

OUTPUT :

Spearman correlation coefficient is: 0.4564355

06. MEASURE OF DISPERSION – STANDARD DEVIATION, MEAN


DEVIATION
PROGRAM :

# R Program to calculate

# Median Absolute Deviation

# Creating a vector

x <- c(1, 4, 2, 3, 7, 3, 7, 3, 8, 9, 2)

# Calling mad() Function

mad(x)

OUTPUT :

[1] 1.4826

05. MEASURE OF CENTRAL TENDENCY – MEAN, MEDIAN MODE


PROGRAM :
# vector of marks

Marks <- c(97, 78, 57, 64, 87)

# calculate average marks

Result <- mean(marks)

Print(result)

# vector of maks

Marks <- c(97, 78, 57, 64, 87)

# find middle number of marks

Result <- median(marks)

Print(result)

# vector of marks

Marks <- c(97, 78, 57, 78, 97, 66, 87, 64, 87, 78)

# define mode() function

Mode = function() (

# calculate mode of marks

Retun(name(sort(-table(mark)))[1])

)
# call mode () function

Mode ()

OUTPUT :

[1] 76.6

[1] 78

[1] “78”

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy