Experiments Rlab Upto Cat - 1: Lab - 1 Introduction To R - Lab
Experiments Rlab Upto Cat - 1: Lab - 1 Introduction To R - Lab
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
8) Predefined Variables
> pi
[1] 3.141593
> 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)
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
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
male female
5 5
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
5) Toss a coin
> sample(c('H','T'),10,replace=TRUE)
[1] "T" "T" "T" "T" "H" "H" "T" "H" "T" "T"
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
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
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”)
ii)Atleast 2 defective
> pbinom(1,20,0.1,lower.tail=F)
[1] 0.608253
> 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)
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
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)