0% found this document useful (0 votes)
113 views31 pages

Experiments Rlab Upto Cat - 1: Lab - 1 Introduction To R - Lab

The document summarizes the results of experiments conducted in RLab up to category 1. It includes assigning values to variables, creating vectors, performing arithmetic operations on vectors, adding vectors of unequal length, using predefined variables, creating sequences, log values, trigonometry, statistics, indexes, replacing values, data frames, and summary statistics.
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)
113 views31 pages

Experiments Rlab Upto Cat - 1: Lab - 1 Introduction To R - Lab

The document summarizes the results of experiments conducted in RLab up to category 1. It includes assigning values to variables, creating vectors, performing arithmetic operations on vectors, adding vectors of unequal length, using predefined variables, creating sequences, log values, trigonometry, statistics, indexes, replacing values, data frames, and summary statistics.
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/ 31

Experiments RLab upto CAT – 1

LAB - 1 Introduction to R – Lab

Name : M.Ramya Sai


RegNo :17MIS1148 Date : 4\12\2018

1) Assigning the value to a variable


> x=5
>x
[1] 5
> print(x)
[1] 5

2) Creating a Vector
> x=c(10,20,30,34)
>x
[1] 10 20 30 34
> y=c(24,29,7,11)
>y
[1] 24 29 7 11

3)Adding Two Vectors


> x+y
[1] 34 49 37 45

4) Subracting Two Vectors


> x-y
[1] -14 -9 23 23
5) Multiplying Two Vectors
> x*y
[1] 240 580 210 374

6) Dividing Two Vectors


> x/y
[1] 0.4166667 0.6896552 4.2857143 [4] 3.0909091

7) Adding Two Vectors of Unequal Length


> x=c(10,20,30,40)
>x
[1] 10 20 30 40
> y=c(50,60,70,80,90)
>y
[1] 50 60 70 80 90
> x+y
[1] 60 80 100 120 100
Warning message:
In x + y : longer object length is not a multiple of shorter object length

8) Predefined Variables
> pi
[1] 3.141593

9) To Show a Series of Sequence


> x=1:10
>x
[1] 1 2 3 4 5 6 7 8 9 10
> y=5:-5
>y
[1] 5 4 3 2 1 0 -1 -2 -3 -4 -5
or
> seq(1,10)
[1] 1 2 3 4 5 6 7 8 9 10
> seq(5,-5)
[1] 5 4 3 2 1 0 -1 -2 -3 -4 -5
> seq(1,10,2)
[1] 1 3 5 7 9

> w=c(1:5,0,-6)
>w
[1] 1 2 3 4 5 0 -6
> w=c(1:5,0:-6)
>w
[1] 1 2 3 4 5 0 -1 -2 -3 -4 -5 -6
> w[1]
[1] 1
> w[0]
integer(0)

10) Multiplying Series


> x=2*5:3*5
>x
[1] 50 40 30

11) Logarithm of any Values


> log(1000,3)
[1] 6.28771

12) Squaring a value


> 5^2
[1] 25

13) Exponential of a value


> exp(7)
[1] 1096.633

14) Logarithm of a value with Base 10


> log(10)
[1] 2.302585
> log10(5)
[1] 0.69897

15) Trignometry
> a=5
> tan(a)
[1] -3.380515
> atan(a)
[1] 1.373401

16) Statistics
> x=c(2,3,5,1,4,4)
>x
[1] 2 3 5 1 4 4
> sum(x)
[1] 19
> mean(x)
[1] 3.166667
> sd(x)
[1] 1.47196

17) Square root of a value


>sqrt(25)
[1] 5

18) For finding index value


> x[c(2,3)]
[1] 0 7
> x[-c(2,3)]
[1] -5 -6 14 27
> x[4]=3
>x
[1] -5 0 7 3 14 27

19) Replacing a value with index value


> x[c(1,4)]=c(2,3)
>x
[1] 2 0 7 3 14 27

20) Predefined variables


> 1/0
[1] Inf
> Inf-Inf
[1] NaN
> sqrt(-1)
[1] NaN
> 1+sin(NaN)
[1] NaN

21) To convert number to character


> x=as.character(4.58)
>x
[1] "4.58"

22) Concatenation of Strings


> paste("First","Second","Third")
[1] "First Second Third"
> paste("First","Second","Third",sep=":")
[1] "First:Second:Third"
> fname="Ramya"
> lname="Mithra"
> paste(fname,lname)
[1] "Ramya Mithra"

23)Creation a table with Data Frames


> x=c(10,20,30)
> y=c("a","b","c")
> z=c(15,25,35)
> w=data.frame(x,y,z)
>w
xy z
1 10 a 15
2 20 b 25
3 30 c 35

or

> w=data.frame(x=c(1,2,3),y=c(4,5,6),z=c(7,8,9))
>w
xyz
1147
2258
3369

> mtcars
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2

> mtcars[1,2]
[1] 6
> mtcars[1,5]
[1] 3.9
LAB - 2 Summary Statistics

Name : M.Ramya Sai


RegNo :17MIS1148 Date : 11\12\2018

1)Creating 6 different vectors


regno=c(1,2,3,4,5,6,7,8,9,10)
sex=c(0,1,0,1,0,1,0,1,0,1)
physics=c(89,67,45,76,23,40,99,56,80,38)
chemistry=c(90,67,34,56,23,89,56,34,13,60)
maths=c(90,89,78,45,34,24,67,15,98,56)
passpercentage=c(2,2,1,2,1,2,1,2,1,2)

2) Creating table for above 6 vectors


class=data.frame(regno,sex,physics,chemistry,maths,passpercentage)
> class
regno sex physics chemistry maths passpercentage
1 1 0 89 90 90 2
2 2 1 67 67 89 2
3 3 0 45 34 78 1
4 4 1 76 56 45 2
5 5 0 23 23 34 1
6 6 1 40 89 24 2
7 7 0 99 56 67 1
8 8 1 56 34 15 2
9 9 0 80 13 98 1
10 10 1 38 60 56 2
>
3) Making sex as male / female and pass percentage as pass /fail
class$sex=factor(class$sex,labels=c("male","female"))
class$passpercentage=factor(class$passpercentage,labels=c("pass","fa
il"))
class
regno sex physics chemistry maths passpercentage
1 1 male 89 90 90 fail
2 2 female 67 67 89 fail
3 3 male 45 34 78 pass
4 4 female 76 56 45 fail
5 5 male 23 23 34 pass
6 6 female 40 89 24 fail
7 7 male 99 56 67 pass
8 8 female 56 34 15 fail
9 9 male 80 13 98 pass
10 10 female 38 60 56 fail

4) Getting only male data


sexm1=subset(class,class$sex=="male")
regno sex physics chemistry maths passpercentage
1 1 male 89 90 90 fail
3 3 male 45 34 78 pass
5 5 male 23 23 34 pass
7 7 male 99 56 67 pass
9 9 male 80 13 98 pass

5) Getting only female data


sexm2=subset(class,class$sex=="female")
regno sex physics chemistry maths passpercentage
2 2 female 67 67 89 fail
4 4 female 76 56 45 fail
6 6 female 40 89 24 fail
8 8 female 56 34 15 fail
10 10 female 38 60 56 fail

6) Getting summary of table


summary(class)
regno sex physics chemistry maths
Min. : 1.00 male :5 Min. :23.00 Min. :13.00 Min. :15.00
1st Qu.: 3.25 female:5 1st Qu.:41.25 1st Qu.:34.00 1st Qu.:36.75
Median : 5.50 Median :61.50 Median :56.00 Median :61.50
Mean : 5.50 Mean :61.30 Mean :52.20 Mean :59.60
3rd Qu.: 7.75 3rd Qu.:79.00 3rd Qu.:65.25 3rd Qu.:86.25
Max. :10.00 Max. :99.00 Max. :90.00 Max. :98.00
passpercentage
pass:4
fail:6

7) Getting summary of only pass percentage


summary(class$passpercentage)
pass fail
4 6

8) Getting no. of male and female in a table


tab1=table(class$sex)
> tab1

male female
5 5

9) Getting no. of male and female with pass / fail in a table


tab2=table(class$sex,class$passpercentage)
> tab2
pass fail
male 4 1
female 0 5

10) Plotting chemistry marks with register number of students


plot(class$chemistry,type="l",main="Chemisrty
Marks",xlab="Regno",ylab="Marks",col="green")
11) Plotting maths marks with register number of students
plot(class$maths,type="l",main="Maths
Marks",xlab="Regno",ylab="Marks",col="blue")

12) Plotting physics marks with register number of students


plot(class$physics,type="l",main="Physics
Marks",xlab="Regno",ylab="Marks",col="red")
LAB - 3 Random Variable and Probability Distribution

Name : M.Ramya Sai


RegNo :17MIS1148 Date : 18\12\2018

Aim :
1) Conducting random experiments with probability concepts
2) Computing and Plotting Binomial and Poisson Distribution

1)Create a vector
> x=c(2,4,7,9,12,15,20)
>x
[1] 2 4 7 9 12 15 20

2) To create 4 numbers at random from x


> x=c(2,4,7,9,12,15,20)
> sample(x,4)
[1] 15 12 4 20
> sample(x,4)
[1] 20 12 15 9

3) To select five numbers at random from the set 1 to 50


> sample(1:10,5)
[1] 4 2 6 3 9
> sample(1:10,11)
Error in sample.int(length(x), size, replace, prob) :
cannot take a sample larger than the population when 'replace =
FALSE'
> sample(1:10,11,replace=TRUE)
[1] 2 8 10 7 1 3 1 2 6 6 8

4) Sampling with replacement suitable


> sample(1:10,5)
[1] 4 2 6 3 9

5) Toss a coin
> sample(c('H','T'),10,replace=TRUE)
[1] "T" "T" "T" "T" "H" "H" "T" "H" "T" "T"

6) Probabilities for the outcomes by using the prob arguments to


sample
> sample(c("success","fail"),10,replace=T,prob=c(0.9,0.1))
[1] "success" "success" "success" "success" "success" "success"
"success" "success" "success" "success"

7) To compute combination
> choose(5,2) # Here n=5, r=2
[1] 10

8) To compute permutation
> n=10
> r=6
> p=factorial(n)/factorial(n-r)
>p
[1] 151200

9) To find the Binomial co-efficient (use choose function)


> choose(10,0:10)
[1] 1 10 45 120 210 252 210 120 45 10 1
10 )To display the Pascal triangle (use for-loop and choose
function)
> for(n in 0:10)
+ print(choose(n,0:n))
[1] 1
[1] 1 1
[1] 1 2 1
[1] 1 3 3 1
[1] 1 4 6 4 1
[1] 1 5 10 10 5 1
[1] 1 6 15 20 15 6 1
[1] 1 7 21 35 35 21 7 1
[1] 1 8 28 56 70 56 28 8 1
[1] 1 9 36 84 126 126 84 36 9 1
[1] 1 10 45 120 210 252 210 120 45 10 1

11) Tossing ‘n’ coins


Go to plot window there we see package ,click on it,help button
there,then type tick on prob package
Or
library(prob)
or go to google, type cran prob package
> prob::tosscoin(4)

toss1 toss2 toss3 toss4


1 H H H H
2 T H H H
3 H T H H
4 T T H H
5 H H T H
6 T H T H
7 H T T H
8 T T T H
9 H H H T
10 T H H T
11 H T H T
12 T T H T
13 H H T T
14 T H T T
15 H T T T
16 T T T T

12) Tossing ‘n’ coins with probabilities displayed


> prob::tosscoin(4,makespace=T)

toss1 toss2 toss3 toss4 probs


1 H H H H 0.0625
2 T H H H 0.0625
3 H T H H 0.0625
4 T T H H 0.0625
5 H H T H 0.0625
6 T H T H 0.0625
7 H T T H 0.0625
8 T T T H 0.0625
9 H H H T 0.0625
10 T H H T 0.0625
11 H T H T 0.0625
12 T T H T 0.0625
13 H H T T 0.0625
14 T H T T 0.0625
15 H T T T 0.0625
16 T T T T 0.0625

13) Rolling ‘n’ dice


> prob::rolldie(3,makespace = T)

X1 X2 X3 probs
1 1 1 1 0.00462963
2 2 1 1 0.00462963
3 3 1 1 0.00462963
4 4 1 1 0.00462963
5 5 1 1 0.00462963
6 6 1 1 0.00462963
7 1 2 1 0.00462963
8 2 2 1 0.00462963
9 3 2 1 0.00462963
10 4 2 1 0.00462963
11 5 2 1 0.00462963
12 6 2 1 0.00462963
13 1 3 1 0.00462963
14 2 3 1 0.00462963
15 3 3 1 0.00462963
16 4 3 1 0.00462963
17 5 3 1 0.00462963
18 6 3 1 0.00462963
19 1 4 1 0.00462963
20 2 4 1 0.00462963
21 3 4 1 0.00462963
22 4 4 1 0.00462963
23 5 4 1 0.00462963
24 6 4 1 0.00462963
25 1 5 1 0.00462963
26 2 5 1 0.00462963
27 3 5 1 0.00462963
28 4 5 1 0.00462963
29 5 5 1 0.00462963
30 6 5 1 0.00462963
31 1 6 1 0.00462963
32 2 6 1 0.00462963
33 3 6 1 0.00462963
34 4 6 1 0.00462963
35 5 6 1 0.00462963
36 6 6 1 0.00462963
37 1 1 2 0.00462963
38 2 1 2 0.00462963
39 3 1 2 0.00462963
40 4 1 2 0.00462963
41 5 1 2 0.00462963
42 6 1 2 0.00462963
43 1 2 2 0.00462963
44 2 2 2 0.00462963
45 3 2 2 0.00462963
46 4 2 2 0.00462963
47 5 2 2 0.00462963
48 6 2 2 0.00462963
49 1 3 2 0.00462963
50 2 3 2 0.00462963
51 3 3 2 0.00462963
52 4 3 2 0.00462963
53 5 3 2 0.00462963
54 6 3 2 0.00462963
55 1 4 2 0.00462963
56 2 4 2 0.00462963
57 3 4 2 0.00462963
58 4 4 2 0.00462963
59 5 4 2 0.00462963
60 6 4 2 0.00462963
61 1 5 2 0.00462963
62 2 5 2 0.00462963
63 3 5 2 0.00462963
64 4 5 2 0.00462963
65 5 5 2 0.00462963
66 6 5 2 0.00462963
67 1 6 2 0.00462963
68 2 6 2 0.00462963
69 3 6 2 0.00462963
70 4 6 2 0.00462963
71 5 6 2 0.00462963
72 6 6 2 0.00462963
73 1 1 3 0.00462963
74 2 1 3 0.00462963
75 3 1 3 0.00462963
76 4 1 3 0.00462963
77 5 1 3 0.00462963
78 6 1 3 0.00462963
79 1 2 3 0.00462963
80 2 2 3 0.00462963
81 3 2 3 0.00462963
82 4 2 3 0.00462963
83 5 2 3 0.00462963
84 6 2 3 0.00462963
85 1 3 3 0.00462963
86 2 3 3 0.00462963
87 3 3 3 0.00462963
88 4 3 3 0.00462963
89 5 3 3 0.00462963
90 6 3 3 0.00462963
91 1 4 3 0.00462963
92 2 4 3 0.00462963
93 3 4 3 0.00462963
94 4 4 3 0.00462963
95 5 4 3 0.00462963
96 6 4 3 0.00462963
97 1 5 3 0.00462963
98 2 5 3 0.00462963
99 3 5 3 0.00462963
100 4 5 3 0.00462963
101 5 5 3 0.00462963
102 6 5 3 0.00462963
103 1 6 3 0.00462963
104 2 6 3 0.00462963
105 3 6 3 0.00462963
106 4 6 3 0.00462963
107 5 6 3 0.00462963
108 6 6 3 0.00462963
109 1 1 4 0.00462963
110 2 1 4 0.00462963
111 3 1 4 0.00462963
112 4 1 4 0.00462963
113 5 1 4 0.00462963
114 6 1 4 0.00462963
115 1 2 4 0.00462963
116 2 2 4 0.00462963
117 3 2 4 0.00462963
118 4 2 4 0.00462963
119 5 2 4 0.00462963
120 6 2 4 0.00462963
121 1 3 4 0.00462963
122 2 3 4 0.00462963
123 3 3 4 0.00462963
124 4 3 4 0.00462963
125 5 3 4 0.00462963
126 6 3 4 0.00462963
127 1 4 4 0.00462963
128 2 4 4 0.00462963
129 3 4 4 0.00462963
130 4 4 4 0.00462963
131 5 4 4 0.00462963
132 6 4 4 0.00462963
133 1 5 4 0.00462963
134 2 5 4 0.00462963
135 3 5 4 0.00462963
136 4 5 4 0.00462963
137 5 5 4 0.00462963
138 6 5 4 0.00462963
139 1 6 4 0.00462963
140 2 6 4 0.00462963
141 3 6 4 0.00462963
142 4 6 4 0.00462963
143 5 6 4 0.00462963
144 6 6 4 0.00462963
145 1 1 5 0.00462963
146 2 1 5 0.00462963
147 3 1 5 0.00462963
148 4 1 5 0.00462963
149 5 1 5 0.00462963
150 6 1 5 0.00462963
151 1 2 5 0.00462963
152 2 2 5 0.00462963
153 3 2 5 0.00462963
154 4 2 5 0.00462963
155 5 2 5 0.00462963
156 6 2 5 0.00462963
157 1 3 5 0.00462963
158 2 3 5 0.00462963
159 3 3 5 0.00462963
160 4 3 5 0.00462963
161 5 3 5 0.00462963
162 6 3 5 0.00462963
163 1 4 5 0.00462963
164 2 4 5 0.00462963
165 3 4 5 0.00462963
166 4 4 5 0.00462963
167 5 4 5 0.00462963
168 6 4 5 0.00462963
169 1 5 5 0.00462963
170 2 5 5 0.00462963
171 3 5 5 0.00462963
172 4 5 5 0.00462963
173 5 5 5 0.00462963
174 6 5 5 0.00462963
175 1 6 5 0.00462963
176 2 6 5 0.00462963
177 3 6 5 0.00462963
178 4 6 5 0.00462963
179 5 6 5 0.00462963
180 6 6 5 0.00462963
181 1 1 6 0.00462963
182 2 1 6 0.00462963
183 3 1 6 0.00462963
184 4 1 6 0.00462963
185 5 1 6 0.00462963
186 6 1 6 0.00462963
187 1 2 6 0.00462963
188 2 2 6 0.00462963
189 3 2 6 0.00462963
190 4 2 6 0.00462963
191 5 2 6 0.00462963
192 6 2 6 0.00462963
193 1 3 6 0.00462963
194 2 3 6 0.00462963
195 3 3 6 0.00462963
196 4 3 6 0.00462963
197 5 3 6 0.00462963
198 6 3 6 0.00462963
199 1 4 6 0.00462963
200 2 4 6 0.00462963
201 3 4 6 0.00462963
202 4 4 6 0.00462963
203 5 4 6 0.00462963
204 6 4 6 0.00462963
205 1 5 6 0.00462963
206 2 5 6 0.00462963
207 3 5 6 0.00462963
208 4 5 6 0.00462963
209 5 5 6 0.00462963
210 6 5 6 0.00462963
211 1 6 6 0.00462963
212 2 6 6 0.00462963
213 3 6 6 0.00462963
214 4 6 6 0.00462963
215 5 6 6 0.00462963
216 6 6 6 0.00462963

14) Rolling ‘n’ dice with restriction on the number of sides of the
dice
> prob::rolldie(3,nsides = 4,makespace = T)

X1 X2 X3 probs
1 1 1 1 0.015625
2 2 1 1 0.015625
3 3 1 1 0.015625
4 4 1 1 0.015625
5 1 2 1 0.015625
6 2 2 1 0.015625
7 3 2 1 0.015625
8 4 2 1 0.015625
9 1 3 1 0.015625
10 2 3 1 0.015625
11 3 3 1 0.015625
12 4 3 1 0.015625
13 1 4 1 0.015625
14 2 4 1 0.015625
15 3 4 1 0.015625
16 4 4 1 0.015625
17 1 1 2 0.015625
18 2 1 2 0.015625
19 3 1 2 0.015625
20 4 1 2 0.015625
21 1 2 2 0.015625
22 2 2 2 0.015625
23 3 2 2 0.015625
24 4 2 2 0.015625
25 1 3 2 0.015625
26 2 3 2 0.015625
27 3 3 2 0.015625
28 4 3 2 0.015625
29 1 4 2 0.015625
30 2 4 2 0.015625
31 3 4 2 0.015625
32 4 4 2 0.015625
33 1 1 3 0.015625
34 2 1 3 0.015625
35 3 1 3 0.015625
36 4 1 3 0.015625
37 1 2 3 0.015625
38 2 2 3 0.015625
39 3 2 3 0.015625
40 4 2 3 0.015625
41 1 3 3 0.015625
42 2 3 3 0.015625
43 3 3 3 0.015625
44 4 3 3 0.015625
45 1 4 3 0.015625
46 2 4 3 0.015625
47 3 4 3 0.015625
48 4 4 3 0.015625
49 1 1 4 0.015625
50 2 1 4 0.015625
51 3 1 4 0.015625
52 4 1 4 0.015625
53 1 2 4 0.015625
54 2 2 4 0.015625
55 3 2 4 0.015625
56 4 2 4 0.015625
57 1 3 4 0.015625
58 2 3 4 0.015625
59 3 3 4 0.015625
60 4 3 4 0.015625
61 1 4 4 0.015625
62 2 4 4 0.015625
63 3 4 4 0.015625
64 4 4 4 0.015625

15) Find mean and variance for a discrete random variable


> x=c(0,1,2,3)
> p=c(1/8,3/8,3/8,1/8)
> mean=sum(x*p)
> mean
[1] 1.5

> variance=sum((x^2 *p))-(mean^2)


> variance
[1] 0.75
LAB - 4 ( Part – 1)
Binomial Distribution

Name : M.Ramya Sai


RegNo :17MIS1148 Date : 8\1\2019

Basics :
N – no of trials
X – no of successor in n trials
p – probability of success
q - probability of failure
p+q=1
Mean µ=n*p
Variance µ=npq
1 ) To find P(X=x), the R function is dbinom(x,n,p) where x
represents the no of success, p represents the probability of success
and n represents the number of trials.
2)To display the probabilities for all possible values of X ,the R
function is
x=0:n
dbinom(x,n,p) or dbinom(0:n,n,p)
3)P(X=x),the cumulative distribution is got by
pbinom(x,n,p,lower.tail=T) Here lower.tail takes true because X
covers the values from 0 to x
4)P(X>x), is got by pbinom(x,n,p,lower.tail=F)
5)Plotting the distribution
x=0:n
y=dbinom(x,n,p)
plot(x,y,type=”h”)

Problem 1 : If 10% of the Screws produced by an automatic


machine are defective , find the probability that are out of 20
screws selected at random , there are
i)Exactly 2 defective
> n=20
> p=0.1
> p(X=2)
> dbinom(2,20,0.1)
[1] 0.2851798

ii)Atleast 2 defective
> pbinom(1,20,0.1,lower.tail=F)
[1] 0.608253

iii)Between 1 and 3defectives


Sum(dbinom(1:3,20,0.1))
[1] 0.74547
iv)Plot the distribution
x=0:20
y=dbinom(x,20,0.1)
plot(x,y,type="h")

v)Compute the probabilities for the whole space.


> y=dbinom(x,20,0.1)
> data.frame(x,y)
x y
1 0 1.215767e-01
2 1 2.701703e-01
3 2 2.851798e-01
4 3 1.901199e-01
5 4 8.977883e-02
6 5 3.192136e-02
7 6 8.867045e-03
8 7 1.970454e-03
9 8 3.557765e-04
10 9 5.270763e-05
11 10 6.442043e-06
12 11 6.507115e-07
13 12 5.422595e-08
14 13 3.707758e-09
15 14 2.059865e-10
16 15 9.154957e-12
17 16 3.178805e-13
18 17 8.310600e-15
19 18 1.539000e-16
20 19 1.800000e-18
21 20 1.000000e-20

Problem 2 : Suppose there are twelve multiple choice question in


an English class quiz.Each question has five possible answers and
only one of them is correct.Find the probability of having four or
less correct answers if a students attempts to answer every
question at random.
> n=12
> p=0.2
> p(X<=4)
> pbinom(4,12,0.2,lower.tail=T)
[1] 0.9274445
LAB - 4 ( Part – 2)
Poisson Distribution

Name : M.Ramya Sai


RegNo :17MIS1148 Date : 22\1\2019

Aim : Computing , Plotting and Visualizing Poisson Distribution


Mean = Variance = ʎ
Problem 1 : i) Compute P(X=5) with Poisson parameter lambda = 7

> dpois(5,7)
[1] 0.1277167
ii) Compute P(x=0),P(x=1),-----P(x=5)

> dpois(0:5,7)
[1] 0.000911882 0.006383174 0.022341108 0.052129252
0.091226192 0.127716668
Or
>x=0:5
>dpois(x,7)

iii) Compute P(x<=5)


> ppois(q=5,lambda=7,lower.tail=T)
[1] 0.3007083
iv) Compute P(x>5)
> ppois(q=5,lambda=7,lower.tail=F)
[1] 0.6992917

Problem 2 : Check the relationship between mean and variance in


poisson distribution with lambda =4 and n=100
> x=0:100
> p=dpois(x,4)
> mean=sum(x*p)
> mean
[1] 4

> var=sum(x^2 *p)-(mean^2)


> var
[1] 4

Problem 3 : Compute the poisson distribution and cumulative


probabilities of the values between 0 to 10 for the parameter 2 in
a table ( rounded upto 4 decimal place )
> p=data.frame(0:10,dpois(0:10,2),ppois(0:10,2))
> round(p,4)
X0.10 dpois.0.10..2. ppois.0.10..2.

1 0 0.1353 0.1353

2 1 0.2707 0.4060
3 2 0.2707 0.6767

4 3 0.1804 0.8571

5 4 0.0902 0.9473

6 5 0.0361 0.9834

7 6 0.0120 0.9955

8 7 0.0034 0.9989

9 8 0.0009 0.9998

10 9 0.0002 1.0000

11 10 0.0000 1.0000

>p
X0.10 dpois.0.10..2. ppois.0.10..2.

1 0 1.353353e-01 0.1353353

2 1 2.706706e-01 0.4060058

3 2 2.706706e-01 0.6766764

4 3 1.804470e-01 0.8571235

5 4 9.022352e-02 0.9473470

6 5 3.608941e-02 0.9834364

7 6 1.202980e-02 0.9954662

8 7 3.437087e-03 0.9989033

9 8 8.592716e-04 0.9997626

10 9 1.909493e-04 0.9999535

11 10 3.818985e-05 0.9999917
Problem 4 : If there are 12 cars crossing the bridge per minute on
a average , find the probability of having 16 or more cars crossing
the bridge ina particular minute.
>ppois(15,lambda=12,lower.tail=F)
[1] 0.1555843
Problem 5 : Compute the Binomial and Poisson distribution using
the data . n=8 , lambda = 2.4 , Also plot the probabilities
Binomial :
> x=0:8
> p=dbinom(x,8,0.3)
> plot(x,p,type="h",main="Pmf for Binomial Distribution",col=2)

Poisson :
x=0:8
> p=dpois(x,2.4)
> plot(x,p,type="h",main="Pmf for Poisson Distribution",col=4)
Problem 6 : Poisson distribution with the parameter 2 ( lambda
=2 )
a)How to obtain a sequence from 0 to 10
> x=dpois(0:10,2)
>x
[1] 1.353353e-01 2.706706e-01 2.706706e-01 1.804470e-01
9.022352e-02 3.608941e-02
[7] 1.202980e-02 3.437087e-03 8.592716e-04 1.909493e-04
3.818985e-05
> round(x,4)
[1] 0.1353 0.2707 0.2707 0.1804 0.0902 0.0361 0.0120 0.0034
0.0009 0.0002 0.0000

b)Calculate P(0),P(1),…….P(10) and make the output prettier.


> dpois(0:10,2)
[1] 1.353353e-01 2.706706e-01 2.706706e-01 1.804470e-01
9.022352e-02 3.608941e-02
[7] 1.202980e-02 3.437087e-03 8.592716e-04 1.909493e-04
3.818985e-05

c)Find P(X<=6)
> sum(dpois(0:6,2))
[1] 0.9954662
>
d) Sum all the probabilities
> sum(dpois(0:6,2))
[1] 0.9954662
>
e) Find P(x>6)
> ppois(q=6,lambda=2,lower.tail=F)
[1] 0.004533806

f) Plot the probabilities. Put some labels on both the axes and a
title
> x=0:10
> p=dpois(x,2)
>
plot(x,p,xlab="Sequence",ylab="Probabilities",type="h",main="Plotti
ng the probabilities",col=2)

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