Vous êtes sur la page 1sur 32

R-LAB

M.Vnageswararao
18BEV7021
L49+L50

1. Simple Operations
a) Enter the data {2,5,3,7,1,9,6}directly and store it in a variable x.
Command> x<-c(2,5,3,7,1,9,6)
Output> x
[1] 2 5 3 7 1 9 6

b) Find the number of elements in x, i.e. in the data list.


Command> length(x)
Output> [1] 7

c) Find the last element of x.


Command> x[length(x)]
Output> [1] 6

d) Find the minimum element of x.


Command> min(x)
Output> [1] 1

e) Find the maximum element of x.


Command> max(x)
Output> [1] 9

2. Enter the data {1, 2, ... ,19,20} in a variable x.


Command> x=c(1:20)
>x
Output> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
a) Find the 3rd element in the data list.
Command> x[3]
Output> [1] 3

b) Find 3rd to 5th element in the datalist.


Command> x[3:5]
Output> [1] 3 4 5

c) Find 2nd, 5th, 6th, and 12th element in the list.


Command> x[c(2,5,6,12)]
Output> [1] 2 5 6 12

d) Print the data as {20, 19, …, 2, 1} without again entering the data
Command> rev(x)
Output> [1] 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

3. a) Create a data list (4, 4, 4, 4, 3, 3, 3, 5, 5, 5) using ‘rep’ function.


Command> A<-c(rep(4,4),rep(3,3),rep(5,3))
>A
Output> [1] 4 4 4 4 3 3 3 5 5 5
b) Create a list (4, 6, 3, 4, 6, 3, …, 4, 6, 3) where there 10 occurrences of 4, 6, and 3 in the
given order.
a<-c(4,6,3)
Command> rep(a,10)
Output> [1] 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3 4 6 3
c) Create a list (3, 1, 5, 3, 2, 3, 4, 5, 7,7, 7, 7, 7,7, 6, 5, 4, 3, 2, 1, 34, 21, 54) using one line
command.
Command> k<-c(3,1,5,3,(2:5),rep(7,6),(6:1),34,21,54)
>k
Output> [1] 3 1 5 3 2 3 4 5 7 7 7 7 7 7 6 5 4 3 2 1 34 21 54

d) First create a list (2, 1, 3, 4). Then append this list at the end with another list (5, 7, 12, 6,
-8). Check whether the number of elements in the augmented list is 9.
b<-c(2,1,3,4)
> c<-c(5,7,12,6,8)
Command> r <- append(b ,c ,after=c(length(b)))
>r
Output> [1] 2 1 3 4 5 7 12 6 8

Command> length(r)
Output> [1] 9

4.(a) Print all numbers starting with 3 and ending with 7 with an increment of 0:0.5. Store
these numbers in x.
Command> x<-seq(3,7,0.5)
>x
Output> [1] 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0
(b) Print all even numbers between 2 and 14 (both inclusive)
Command> seq(2,14,by=2)
Output> [1] 2 4 6 8 10 12 14

(c) Type 2*x and see what you get. Each element of x is multiplied by 2.
> x<-seq(3,7,0.5)
Command> 2*x
Output> [1] 6 7 8 9 10 11 12 13 14
Functions in R
M.V.NageswaraRao
18BEV7021
L49+L50

1)

Solution:

a)command> x=c(1:10)
> x
Output> [1] 1 2 3 4 5 6 7 8 9 10

b) command>sum(x)
Output> [1] 55

c) command> mean(x)
Output> [1] 5.5

command> median(x)
Output> [1] 5.5

d) command> p=sum(x^2)
> p
Output> [1] 385
e) command> u=sum((abs(x-mean(x))/length(x)))
> u
Output> [1] 2.5

2) Create a file as follows and store as a :-


a) How many rows are there in this table? How many columns are there?
price=c(52,54.75,57.50,57.50,59.75,62.50,64.75,67.25,67.50,69.75,70,75.50,77.50,78,81.25
,82.50,86.25,87.50,88,92)
>
FloorArea=c(1225,1230,1200,1000,1420,1450,1380,1510,1400,1550,1720,1700,1660,1800,
1830,1790,2010,2000,2100,2240)
> Rooms=c(3,3,3,2,4,3,4,4,5,6,6,5,6,7,6,6,6,6,8,7)
> Age=c(6.2,7.5,4.2,4.8,1.9,5.2,6.5,9.2,0.0,5.7,7.3,4.5,6.8,0.7,5.6,2.3,6.7,3.4,5.6,3.4)
>
CentralHeating=c('YES',rep('NO',3),rep('YES',2),rep('NO',4),'YES','NO',rep('YES',3),'NO','YE
S','NO',rep('YES',2))
command> a=data.frame(price,FloorArea,Rooms,Age,CentralHeating)
> a
Output>
price FloorArea Rooms Age CentralHeating
1 52.00 1225 3 6.2 YES
2 54.75 1230 3 7.5 NO
3 57.50 1200 3 4.2 NO
4 57.50 1000 2 4.8 NO
5 59.75 1420 4 1.9 YES
6 62.50 1450 3 5.2 YES
7 64.75 1380 4 6.5 NO
8 67.25 1510 4 9.2 NO
9 67.50 1400 5 0.0 NO
10 69.75 1550 6 5.7 NO
11 70.00 1720 6 7.3 YES
12 75.50 1700 5 4.5 NO
13 77.50 1660 6 6.8 YES
14 78.00 1800 7 0.7 YES
15 81.25 1830 6 5.6 YES
16 82.50 1790 6 2.3 NO
17 86.25 2010 6 6.7 YES
18 87.50 2000 6 3.4 NO
19 88.00 2100 8 5.6 YES
20 92.00 2240 7 3.4 YES

b) How to find the number of rows and number of columns by a single command?
command> nrow(a)

Output> [1] 20
command> ncol(a)

Output> [1] 5
command> b=c(nrow(a),ncol(a))

> b

Output> [1] 20 5

c) What are the variables in the data file?

command> names(a)

Output> [1] "Price" "FloorArea" "Rooms" "Age"

[5] "CentralHeating"

d) If the file is very large, naturally we cannot simply type `a', because it will cover the entire
screen and we won't be able to understand anything. So how to see the top or bottom few lines in
this file?
command>head(tableinfo)

Output> Price FloorArea Rooms Age CentralHeating

1. 52.00 1225 3 6.2 Yes

2. 54.75 1230 3 7.5 No

3. 57.50 1200 3 4.2 No

4. 57.50 1000 2 4.8 No

5. 59.75 1420 4 1.9 Yes

6. 62.50 1450 3 5.2 Yes

command> tail(k)

Output> Price FloorArea Rooms Age CentralHeating

15. 81.25 1830 6 5.6 Yes


16. 82.50 1790 6 2.3 No

17. 86.25 2010 6 6.7 Yes

18. 87.50 2000 6 3.4 No

19. 88.00 2100 8 5.6 Yes

20. 92.00 2240 7 3.4 Yes

e) If the number of columns is too large, again we may face the same problem. So how to see the
first 5 rows and first 3 columns?

command>k[c(1:5),c(1:3)]

Output>

Price FloorArea Rooms

1. 52.00 1225 3

2. 54.75 1230 3

3. 57.50 1200 3

4. 57.50 1000 2

5. 59.75 1420 4

f) How to get 1st, 3rd, 6th, and 10th row and 2nd, 4th, and 5th column?
command> k[c(1,3,6,10),c(2,4,5)]

Output>

no FloorArea Age CentralHeating

1 1225 6.2 Yes

3 1200 4.2 No

6 1450 5.2 Yes

10 1550 5.7 No

g) How to get values in a specific row or a column?


command> k[2,1]

Output> [1] 54.75

3) Calculate simple statistical measures using the values in the data file.
a) Find means, medians, standard deviations of Price, Floor Area, Rooms, and Age.
b) #for to find mean
c) mean(price)
d) [1] 71.5875
e) mean(floorarea)
f) [1] 1607.75
g) mean(Rooms)
h) [1] 5
i) mean(Age)
j) [1] 4.875
k) #for to find median
l) median(price)
m) [1] 69.875
n) median(floorarea)
o) [1] 1575
p) median(Rooms)
q) [1] 5.5
r) median(Age)
s) [1] 5.4
t) #for to find square deviation
u) sd(price)
v) [1] 12.21094
w) sd(floorarea)
x) [1] 331.7675
y) sd(Rooms)
z) [1] 1.65434
aa) sd(Age)
bb) [1] 2.366182

b) How many houses have central heating and how many don't have?
#a$CentralHeating=factor(a$CentralHeating,labels=c("Yes"))

#y=subset(a,a$CentralHeating=='Yes')

c) Plot Price vs. Floor, Price vs. Age, and Price vs. rooms, in separate graphs.
Plot Price vs. Floorarea

Plot Price vs. Age


Plot Price vs. rooms
d) Draw histograms of Prices, FloorArea, and Age.

d)

sum((abs(price-mean(price))/length(price)))

[1] 10.42125

sum((abs(floorarea-mean(floorarea))/length(floorarea)))

[1] 272.025

sum((abs(Rooms-mean(Rooms))/length(Rooms)))

[1] 1.4

sum((abs(Age-mean(Age))/length(Age)))

[1] 1.8675
e) Draw box plots of Price, FloorArea, and Age.
boxplot(price,floorarea,Age)

f) Draw all the graphs in (c), (d), and (e) in the same graph paper.

c(plot(price,floorarea),plot(price,Age),plot(price,Rooms))
M.V.NageswaraRao
18BEV7021
L49+L50

Q.) Collect at least 20 students and analyse the data by using descriptive statistics
and Interpret the results.
name=c("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T")
age=c(11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30)
maths=c(19,23,42,31,42,50,50,36,23,41,47,25,33,25,35,45,50,32,28,38)

phy=c(10,20,21,22,11,34,33,23,10,29,38,34,55,45,33,32,22,11,19,21)
chy=c(18,19,19,19,19,20,20,20,20,20,21,21,21,21,22,23,24,27,30,36)
Command> data=data.frame(name,age,maths,phy,chy)
Output> data
name age maths phy chy

1 A 11 19 10 18
2 B 12 23 20 19
3 C 13 42 21 19
4 D 14 31 22 19
5 E 15 42 11 19

6 F 16 50 34 20
7 G 17 50 33 20
8 H 18 36 23 20
9 I 19 23 10 20
10 J 20 41 29 20

11 K 21 47 38 21
12 L 22 25 34 21
13 M 23 33 55 21
14 N 24 25 45 21
15 O 25 35 33 22
16 P 26 45 32 23
17 Q 27 50 22 24
18 R 28 32 11 27
19 S 29 28 19 30

20 T 30 38 21 36

> # for all the means


Command> > mean1=mean(age)
> mean1

Output> [1] 20.5


Command> > mean2=mean(maths)
> mean2
Output> [1] 35.75
Command> > mean3=mean(phy)

> mean3
Output> [1] 26.15
Command> > mean4=mean(chy)
> mean4
Output> [1] 22

>
> #for all the medians
Command> > median1=median(age)
> median1
Output> [1] 20.5

Command> > median2=median(maths)


> median2
Output> [1] 35.5
Command> > median3=median(phy)
> median3
Output> [1] 22.5
Command> > median4=median(chy)
> median4
Output> [1] 20.5

>
> #for all the modes of the data
Command> > xr1=table(age)
> mode1=which(xr1==max(xr1))
> mode1

Output> 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Command> > xr2=table(maths)
> mode2=which(xr2==max(xr2))
> mode2

Output> 50 15
Command> > xr3=table(phy)
> mode3=which(xr3==max(xr3))
> mode3
Output> 10 11 21 22 33 34 1 2 5 6 10 11

Command> > xr4=table(chy)


> mode4=which(xr4==max(xr4))
> mode4
Output> 20 3
> #for all the ranges

Command> > range1=(max(age)-min(age))


> range1
Output> [1] 19
Command> > range2=(max(maths)-min(maths))
> range2
Output> [1] 31
Command> > range3=(max(phy)-min(phy))
> range3
Output> [1] 45

Command> > range4=(max(chy)-min(chy))


> range4
Output> [1] 18
> #for all the variance
Command> > variance1=var(age)

> variance1
Output> [1] 35
Command> > variance2=var(maths)
> variance2
Output> [1] 98.61842

Command> > variance3=var(phy)


> variance3
Output> [1] 144.9763
Command> > variance4=var(chy)
> variance4

Output> [1] 19.26316


>
> #for all the standerd deviation
Command> > stand1=sd(age)
> stand1

Output> [1] 5.91608


Command> > stand2=sd(maths)
> stand2
Output> [1] 9.930681
Command> > stand3=sd(phy)
> stand3
Output> [1] 12.04061
Command> > stand4=sd(chy)
> stand4

Output> [1] 4.388981


>
>
> #for all the cummulative frquency
Command> > c.f1=cumsum(age)

> c.f1
Output> [1] 11 23 36 50 65 81 98 116 135 155 176 198 221 245 270 296 323 351 380
[20] 410
Command> > c.f2=cumsum(maths)
> c.f2

Output> [1] 19 42 84 115 157 207 257 293 316 357 404 429 462 487 522 567 617 649 677
[20] 715
Command> > c.f3=cumsum(phy)
> c.f3
Output> [1] 10 30 51 73 84 118 151 174 184 213 251 285 340 385 418 450 472 483 502

[20] 523
Command> > c.f4=cumsum(chy)
> c.f4
Output> [1] 18 37 56 75 94 114 134 154 174 194 215 236 257 278 300 323 347 374 404
[20] 440

>
> # fro all the summaries
Command> > s1=summary(age)
> s1
Output>
Min. 1st Qu. Median Mean 3rd Qu. Max.
11.00 15.75 20.50 20.50 25.25 30.00
Command> > s2=summary(maths)
> s2

Output>
Min. 1st Qu. Median Mean 3rd Qu. Max.
19.00 27.25 35.50 35.75 42.75 50.00
Command> > s3=summary(phy)
> s3

Output>
Min. 1st Qu. Median Mean 3rd Qu. Max.
10.00 19.75 22.50 26.15 33.25 55.00
Command> > s4=summary(chy)
> s4

Output>
Min. 1st Qu. Median Mean 3rd Qu. Max.
18.00 19.75 20.50 22.00 22.25 36.00
M.V.NageswaraRao
18BEV7021
L49+L50
1. Matrices and arrays

a) Matrices and arrays are represented as vectors with dimensions:


Create one matrix x with 1 to12 numbers with 3X4 order.

COMMAND> k<-array(1:12,dim=c(3,4))
OUTPUT> > k<-array(1:12,dim=c(3,4))
>k
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12

> A<-list(c(1,2,3,4),c(5,6,7,8),c(9,10,11,12))
COMMAND> A
OUTPUT> [[1]]
[1] 1 2 3 4

[[2]]
[1] 5 6 7 8

[[3]]
[1] 9 10 11 12

b) Create same matrix with matrix function.

command> y<-matrix(c(1,2,3,4,5,6,7,8,9,10,11,12),nrow=3,ncol=4,byrow=TRUE)
output> y<-matrix(c(1,2,3,4,5,6,7,8,9,10,11,12),nrow=3,ncol=4,byrow=TRUE)
>y
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12

c) Give name of rows of this matrix with A,B,C.

command> z=matrix(c(1,2,3,4,5,6,7,8,9,10,11,12),nrow=3,ncol=4,byrow=TRUE,dimnames=li
st(c("A","B","C")))

output> z=matrix(c(1,2,3,4,5,6,7,8,9,10,11,12),nrow=3,ncol=4,byrow=TRUE,dimnames=list(
c("A","B","C")))
>z
[,1] [,2] [,3] [,4]
A 1 2 3 4
B 5 6 7 8
C 9 10 11 12

d) Transpose of the matrix.

>z=matrix(c(1,2,3,4,5,6,7,8,9,10,11,12),nrow=3,ncol=4,byrow=TRUE,dimnames=list(c("A","B
","C")))

command> t(z)

output> z=matrix(c(1,2,3,4,5,6,7,8,9,10,11,12),nrow=3,ncol=4,byrow=TRUE,dimnames=list(
c("A","B","C")))
> t(z)
AB C
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12

e) Use functions cbind and rbind separately to create different matrices.

> a<-matrix(c(1,2,3,4,5,6,7,8,9,10,11,12),nrow=3,ncol=4,byrow=TRUE)
>a
> b<-matrix(c(1,3,7,8,3,5,2,12,13,15,16,17),nrow=3,ncol=4,byrow=TRUE)
>b
command> cbind(a,b)
output> > cbind(a,b)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 1 2 3 4 1 3 7 8
[2,] 5 6 7 8 3 5 2 12
[3,] 9 10 11 12 13 15 16 17
> a<-matrix(c(1,2,3,4,5,6,7,8,9,10,11,12),nrow=3,ncol=4,byrow=TRUE)
>a
> b<-matrix(c(1,3,7,8,3,5,2,12,13,15,16,17),nrow=3,ncol=4,byrow=TRUE)
>b
command> rbind(a,b)
output> rbind(a,b)
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
[4,] 1 3 7 8
[5,] 3 5 2 12
[6,] 13 15 16 17
f) Use arbitrary numbers to create matrix.

b<-matrix(c(1,3,7,8,3,5,2,12,13,15,16,17),nrow=3,ncol=4,byrow=TRUE)
Command> b
output> [,1] [,2] [,3] [,4]
[1,] 1 3 7 8
[2,] 3 5 2 12
[3,] 13 15 16 17

g) Verify matrix multiplication.


> b<-matrix(c(1,3,7,8,3,5,2,12,13,15,16,17),nrow=3,ncol=4,byrow=TRUE)
Command> b%*% t(b)
Output> [,1] [,2] [,3]
[1,] 123 128 306
[2,] 128 182 350
[3,] 306 350 939
> a<-matrix(c(1,2,3,4,5,6,7,8,9,10,11,12),nrow=3,ncol=4,byrow=TRUE)
>a
> b<-matrix(c(1,3,7,8,3,5,2,12,13,15,16,17),nrow=3,ncol=4,byrow=TRUE)
>b
command>a*b
Output>
[,1] [,2] [,3] [,4]
[1,] 1 6 21 32
[2,] 15 30 14 96
[3,] 117 150 176 204

2. Random sampling
a)In R ,you can simulate these situations with the sample function. pick five numbers
random from the set 1:40
> a<-1:40
> a
Command> sample(a,5)
Output>[1] 1 32 4 10 31

b) Notice that the default behaviour of sample is sampling without replacement. That is, the
samples will not contain the same number twice, and size obviously cannot be bigger than t
he length of the vector to be sampled. If you want sampling with replacement, then you nee
d to add the argument replace=TRUE.Sampling with replacement is suitable for modelling co
in tosses or throws of a die. So, for instance,simulate 10 coin tosses.
(i)Tosses
> p<-c("H","T")
Command> sample(p,10,replace=TRUE)
Output> [1] "T" "H" "H" "H" "H" "H" "T" "H"
[9] "H" "T"

(ii)Die throws
> D<-c(1,2,3,4,5,6)
Command> sample(D,10,replace=TRUE)
Output> [1] 4 6 1 5 2 1 5 3 6 3

c) In fair coin-tossing, the probability of heads should equal the probability of tails, but the i
dea of a random event is not restricted to symmetric cases. It could be equally well applied t
o other cases, such as the successful outcome of a surgical procedure. Hopefully, there woul
d be a better than 50% chance of this. Simulate data with nonequal probabilities for the out
comes (say, a 90% chance of success) by using the prob argument to sample.

q<-c(" HEAD "," TAIL ")


(i)Command> sample(q,5,replace=TRUE,prob=c(0.5,0.5))
Output> [1] " TAIL " " HEAD " " HEAD " " TAIL " " HEAD "

(ii)Command> sample(c(" success "," failure "),5,replace=TRUE ,prob=c(0.9,0.1))


Output> [1] " failure " " success " " success "
[4] " success " " success "

d)
a<-40
b<-5
Command> choose(40,5)
Output> [1] 658008

e)5!
Command> factorial(5)
Output> [1] 120
R-LAB
M.Vnageswararao
18BEV7021
L49+L50

1. Five terminals on an on-line computer system are attached to a communication


line to the central computer system. The probability that any terminal is ready to
transmit is 0.95.
Let X denote the number of ready terminals.
a) Find the probability of getting exactly 3 ready terminals.

Command> x=3
> q=dbinom(x,size=5,prob=0.95)
Output> q
[1] 0.02143438

b) Find all the probabilities.


Command> > x<-seq(0,3,by=1)
> a=dbinom(x,size=5,prob=0.95)
Output> a
[1] 0.0000003125 0.0000296875 0.0011281250 0.0214343750

2. It is known that 20% of integrated circuit chips on a production line are defective.
To maintain and monitor the quality of the chips, a sample of twenty chips is
selected at regular intervals for inspection.
Let X denote the number of defectives found in the sample.
Find the probability of different number of defective found in the sample?

Command> > x<-seq(0,20,by=1)


> c=dbinom(x,size=20,prob=0.2)
Output>
>c
[1] 1.152922e-02 5.764608e-02 1.369094e-01 2.053641e-01 2.181994e-01
[6] 1.745595e-01 1.090997e-01 5.454985e-02 2.216088e-02 7.386959e-03
[11] 2.031414e-03 4.616849e-04 8.656592e-05 1.331783e-05 1.664729e-06
[16] 1.664729e-07 1.300570e-08 7.650410e-10 3.187671e-11 8.388608e-13
[21] 1.048576e-14

3. It is known that 1% of bits transmitted through a digital transmission are


received in error. One hundred bits are transmitted each day. Find the probability of
different number of bits found in error each day.?

Command> z<-seq(0,100,by=1)
d=dbinom(z,size=100,prob=0.01)
d
Output> > d

[1] 3.660323e-01 3.697296e-01 1.848648e-01 6.099917e-02 1.494171e-02


[6] 2.897787e-03 4.634508e-04 6.286346e-05 7.381694e-06 7.621951e-07
[11] 7.006036e-08 5.790112e-09 4.337710e-10 2.965956e-11 1.861747e-12
[16] 1.078184e-13 5.785707e-15 2.887697e-16 1.344999e-17 5.863367e-19
[21] 2.398650e-20 9.230014e-22 3.347893e-23 1.146841e-24 3.716614e-26
[26] 1.141263e-27 3.325359e-29 9.206008e-31 2.424382e-32 6.079954e-34
[31] 1.453457e-35 3.315151e-37 7.220499e-39 1.502889e-40 2.991491e-42
[36] 5.698078e-44 1.039212e-45 1.815713e-47 3.040667e-49 4.882708e-51
[41] 7.521343e-53 1.111802e-54 1.577594e-56 2.149411e-58 2.812590e-60
[46] 3.535467e-62 4.269888e-64 4.955382e-66 5.526836e-68 5.924459e-70
[51] 6.103988e-72 6.044749e-74 5.753549e-76 5.263395e-78 4.627377e-80
[56] 3.909263e-82 3.173103e-84 2.474154e-86 1.852815e-88 1.332276e-90
[61] 9.195842e-93 6.090970e-95 3.870118e-97 2.357936e-99 1.376951e-101
[66] 7.703225e-104 4.126306e-106 2.115098e-108 1.036813e-110 4.856976e-113
[71] 2.172673e-115 9.273039e-118 3.772701e-120 1.461680e-122 5.387028e-125
[76] 1.886367e-127 6.267832e-130 1.973343e-132 5.877609e-135 1.653336e-137
[81] 4.383845e-140 1.093365e-142 2.558996e-145 5.605686e-148 1.145943e-150
[86] 2.178859e-153 3.838722e-156 6.239651e-159 9.310773e-162 1.268066e-164
[91] 1.565513e-167 1.737722e-170 1.717116e-173 1.492009e-176 1.122294e-179
[96] 7.159768e-183 3.766713e-186 1.568973e-189 4.851495e-193 9.900000e-197
[101] 1.000000e-200

4. Plot all of the above problems in a single window for random variable and
respective Probability distribution.

Command> x<-seq(0,3,by=1)
b=dbinom(x,size=5,prob=0.95)
y<-seq(0,20,by=1)
c=dbinom(y,size=20,prob=0.2)
par(mfrow=c(2,2))
z<-seq(0,100,by=1)
d=dbinom(z,size=100,prob=0.01)
plot(x,b,main="1st problem",xlab="no of trails",ylab=" probabilitie")
plot(y,c,main="2nd problem",xlab="no of trails",ylab="probabilitie")
plot(z,d,,main="3rd problem",xlab="no of trails",ylab="probabilitie")

Output>
5. For Q.No. 1 Find P(X ≤ 3) and P(X > 3). For Q. No. 2 Find P(X ≤ 4) and P(X > 4).
Find all the cumulative probabilities and round to 4 decimal places.
P(x≤ 3)
Command> a=round(pbinom(3,size=5,prob=0.95),4)
>a
Output> [1] 0.0226

P(X > 3).


Command> m=1-pbinom(3,size=5,prob=0.95)
> round(m,4)
Output> [1] 0.9774

Q. No. 2: Find P(X ≤ 4)


Command> p<-c(pbinom(4,size=20,prob=0.2))
round(p,4)
Output> [1] 0.6296

P(X > 4).


Command> n=1-c(pbinom(4,size=20,prob=0.2))
> round(n,4)
Output> [1] 0.3704

d) Print the data as {20, 19, …, 2, 1} without again entering the data
Command> rev(x)
Output> [1] 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

6. The probability that a patient recover from a rare blood disease is 0.4. If 15
people are known to have contracted this disease, what is the probability that (a) at
least10 survive, (b) from 3 to 8 survive, and (c) exactly 5 survive?

Command> k=1-pbinom(9,size=15,prob=0.4)
k
Output> [1] 0.0338333
Command> L=pbinom(8,size=15,prob=0.4)-pbinom(3,size=15,prob=0.4)
L
Output> [1] 0.8144507
Command> m=pbinom(5,size=15,prob=0.4)
m
Output> [1] 0.4032156
R-LAB
M.Vnageswararao
18BEV7021
L49+L50

1. During a laboratory experiment, the average number of radioactive particles


passing through a counter in 1 millisecond is 4. What is the probability that 6
particles enter the counter in a given millisecond?
Command> x<-dpois(6,4)
Output> x
[1] 0.1041956

2. In a certain industrial facility, accidents occur infrequently. It is known that the


probability of an accident on any given day is 0.005 and accidents are independent
of each other.
(a) What is the probability that in any given period of 400 days there will be an
accident on one day?
> n=400
> p=0.005
> lambda=n*p
Command>y<-dpois(1,lambda)
>y
Output> [1] 0.2706706

(b) What is the probability that there are at most three days with an accident?
> n=400
> p=0.005
> lambda=n*p
Command> z<-ppois(3,lambda)
z
Output> [1] 0.8571235

3. In a manufacturing process where glass products are made, defects or bubbles


occur, occasionally rendering the piece undesirable for marketing. It is known that,
on average, 1 in every 1000 of these items produced has one or more bubbles. What
is the probability that a random sample of 8000 will yield fewer than 7 items
possessing bubbles?
n=8000
p=0.001
lambda=n*p
Command> z<-ppois(6,lambda)
z
Output> [1] 0.3133743

4. On average, 3 traffic accidents per month occur at a certain intersection. What is


the probability that in any given month at this intersection
(a) exactly 5 accidents will occur?
Command> dpois(5,3)
Output> [1] 0.1008188

(b) fewer than 3 accidents will occur?


Command> ppois(3,3,lower.tail=TRUE)
Output> [1] 0.6472319

(c) at least 2 accidents will occur?


Command> ppois(2,3,lower.tail=FALSE)
Output> [1] 0.5768099

5. The potential buyer of a particular engine requires (among other things) that the
engine start successfully 10 consecutive times. Suppose the probability of a
successful start is 0.990. Let us assume that the outcomes of attempted starts are
independent.
(a) What is the probability that the engine is accepted after only 10 starts?
Command> w=10*0.99
> ppois(10,w,lower.tail=FALSE)
Output> [1] 0.4044513

(b) What is the probability that 12 attempted starts are made during the acceptance
process?
Command> w=10*0.99
> dpois(12,w)
Output> [1] 0.09284745

6. The acceptance scheme for purchasing lots containing a large number of batteries
is to test no more than 75 randomly selected batteries and to reject a lot if a single
battery fails. Suppose the probability of a failure is 0.001.
(a) What is the probability that a lot is accepted?
Command> ppois(75,0.01,lower.tail=FALSE)
Output> [1] 0.9277513
(b) What is the probability that a lot is rejected on the 20th test?
Command> m=75*(1-0.001)
dpois(20,m)
Output> [1] 9.8107528e-4
(c) What is the probability that it is rejected in 10 or fewer trials?
Command> m=75*(1-0.001)
ppois(10,m,lower.tail=TRUE)
Output> [1] 0.0142532

7. Plot the graph for Q. No. 2, 4, 5 and 6 for Random Variable against Probability
Distribution function.
.
Command>

x=seq(1,400,1)
x

b<-dpois(x,2)
b

#3
y=seq(0,3,1)
y
p=3
c<-dpois(y,3)
c
z<-seq(1,20,1)
z

d<-dpois(z,10)
d
q<-seq(1,100,1)
q
m=75*(1-0.001)
e<-dpois(q,m)
e
plot(x,b)
plot(y,c)
plot(z,d)
plot(q,e)

par(mfrow=c(2,2))
plot(x,b,main="1st problem",xlab="random varible",ylab="probabilitie
distribution")
plot(y,c,main="2nd problem",xlab="random varible",ylab="probabilitie
distribution")
plot(z,d,main="3rd problem",xlab="random varible",ylab="probabilitie
distribution")
plot(q,e,main="4rd problem",xlab="random varible",ylab="probabilitie
distribution")

Output

Vous aimerez peut-être aussi