———————————————————————–
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ readr 2.1.5
## ✔ ggplot2 3.5.1 ✔ stringr 1.5.1
## ✔ lubridate 1.9.3 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
library(gridExtra)
##
## Attaching package: 'gridExtra'
##
## The following object is masked from 'package:dplyr':
##
## combine
# Meaning of Probability
prob=c()
trial=seq(10,2000,10)
for (n in trial){
hc=0
for (i in 1:n){
r=sample(0:1,1) #randomly select 1(head) or 0(tail)
if (r==1){
hc=hc+1 #increment the hc variable if head occurs
}
pct=round(hc/n,3) #probability of occurring head after n trials
}
prob=c(prob,pct) # probality of occuring head for different value of n
}
#Create the data frame
df1=data.frame(trial,prob)
# Create the column names
names(df1)=c("n","Prob")
df1
## n Prob
## 1 10 0.700
## 2 20 0.350
## 3 30 0.433
## 4 40 0.550
## 5 50 0.400
## 6 60 0.433
## 7 70 0.443
## 8 80 0.462
## 9 90 0.433
## 10 100 0.380
## 11 110 0.645
## 12 120 0.458
## 13 130 0.554
## 14 140 0.493
## 15 150 0.560
## 16 160 0.506
## 17 170 0.559
## 18 180 0.500
## 19 190 0.521
## 20 200 0.485
## 21 210 0.476
## 22 220 0.477
## 23 230 0.565
## 24 240 0.446
## 25 250 0.492
## 26 260 0.577
## 27 270 0.456
## 28 280 0.489
## 29 290 0.507
## 30 300 0.457
## 31 310 0.481
## 32 320 0.503
## 33 330 0.533
## 34 340 0.485
## 35 350 0.489
## 36 360 0.525
## 37 370 0.514
## 38 380 0.513
## 39 390 0.456
## 40 400 0.527
## 41 410 0.498
## 42 420 0.514
## 43 430 0.467
## 44 440 0.520
## 45 450 0.467
## 46 460 0.520
## 47 470 0.483
## 48 480 0.502
## 49 490 0.504
## 50 500 0.500
## 51 510 0.488
## 52 520 0.477
## 53 530 0.479
## 54 540 0.506
## 55 550 0.522
## 56 560 0.523
## 57 570 0.491
## 58 580 0.490
## 59 590 0.459
## 60 600 0.490
## 61 610 0.498
## 62 620 0.521
## 63 630 0.508
## 64 640 0.498
## 65 650 0.463
## 66 660 0.480
## 67 670 0.463
## 68 680 0.482
## 69 690 0.528
## 70 700 0.507
## 71 710 0.490
## 72 720 0.493
## 73 730 0.501
## 74 740 0.514
## 75 750 0.516
## 76 760 0.496
## 77 770 0.496
## 78 780 0.512
## 79 790 0.530
## 80 800 0.506
## 81 810 0.499
## 82 820 0.498
## 83 830 0.489
## 84 840 0.505
## 85 850 0.518
## 86 860 0.510
## 87 870 0.460
## 88 880 0.502
## 89 890 0.511
## 90 900 0.498
## 91 910 0.510
## 92 920 0.520
## 93 930 0.492
## 94 940 0.520
## 95 950 0.502
## 96 960 0.520
## 97 970 0.515
## 98 980 0.511
## 99 990 0.481
## 100 1000 0.465
## 101 1010 0.515
## 102 1020 0.508
## 103 1030 0.502
## 104 1040 0.488
## 105 1050 0.479
## 106 1060 0.493
## 107 1070 0.480
## 108 1080 0.523
## 109 1090 0.509
## 110 1100 0.512
## 111 1110 0.522
## 112 1120 0.497
## 113 1130 0.487
## 114 1140 0.493
## 115 1150 0.523
## 116 1160 0.470
## 117 1170 0.509
## 118 1180 0.493
## 119 1190 0.504
## 120 1200 0.502
## 121 1210 0.501
## 122 1220 0.503
## 123 1230 0.468
## 124 1240 0.519
## 125 1250 0.495
## 126 1260 0.490
## 127 1270 0.494
## 128 1280 0.498
## 129 1290 0.495
## 130 1300 0.495
## 131 1310 0.505
## 132 1320 0.485
## 133 1330 0.479
## 134 1340 0.505
## 135 1350 0.505
## 136 1360 0.485
## 137 1370 0.502
## 138 1380 0.492
## 139 1390 0.462
## 140 1400 0.503
## 141 1410 0.470
## 142 1420 0.501
## 143 1430 0.497
## 144 1440 0.489
## 145 1450 0.501
## 146 1460 0.498
## 147 1470 0.478
## 148 1480 0.518
## 149 1490 0.485
## 150 1500 0.490
## 151 1510 0.478
## 152 1520 0.505
## 153 1530 0.505
## 154 1540 0.516
## 155 1550 0.508
## 156 1560 0.504
## 157 1570 0.499
## 158 1580 0.487
## 159 1590 0.481
## 160 1600 0.477
## 161 1610 0.486
## 162 1620 0.499
## 163 1630 0.505
## 164 1640 0.491
## 165 1650 0.487
## 166 1660 0.521
## 167 1670 0.515
## 168 1680 0.485
## 169 1690 0.520
## 170 1700 0.500
## 171 1710 0.488
## 172 1720 0.502
## 173 1730 0.499
## 174 1740 0.511
## 175 1750 0.482
## 176 1760 0.506
## 177 1770 0.497
## 178 1780 0.456
## 179 1790 0.494
## 180 1800 0.491
## 181 1810 0.501
## 182 1820 0.493
## 183 1830 0.511
## 184 1840 0.515
## 185 1850 0.511
## 186 1860 0.498
## 187 1870 0.505
## 188 1880 0.484
## 189 1890 0.501
## 190 1900 0.486
## 191 1910 0.481
## 192 1920 0.507
## 193 1930 0.505
## 194 1940 0.514
## 195 1950 0.492
## 196 1960 0.484
## 197 1970 0.485
## 198 1980 0.501
## 199 1990 0.497
## 200 2000 0.488
#Draw the line graph no of trails(n) vs probability
ggplot(df1)+
geom_line(mapping=aes(x=n,y=Prob),color="blue")+
geom_abline(slope=0,intercept = 0.5,colour="red")

#Binomial Distribution
#No of trials=t
# Two outcomes: Success(Occurring head), Failure(Occurring tail)
#no of success=n.
#Probability of success is p = 0.5
n=10
v=rep(0,n) #The vector v stores the no of heads
print(v)
## [1] 0 0 0 0 0 0 0 0 0 0
t=20000
for (j in 1:t){
hn=0 # Initially the no of heads is 0
for (i in 1:n){
x=sample(0:1,1) #random selection of one outcome(head) from two(head and tail) makes the probability of success=0.5
if (x==1){
hn=hn+1 #increase the hc which denotes head count
}
}
v[hn]=v[hn]+1 #Uodating the vector v
}
df=data.frame(1:n,v)
names(df)=c("hc","no")
df
## hc no
## 1 1 159
## 2 2 876
## 3 3 2326
## 4 4 4044
## 5 5 4907
## 6 6 4126
## 7 7 2389
## 8 8 917
## 9 9 211
## 10 10 18
df=df%>%mutate(prob=no/t)
df
## hc no prob
## 1 1 159 0.00795
## 2 2 876 0.04380
## 3 3 2326 0.11630
## 4 4 4044 0.20220
## 5 5 4907 0.24535
## 6 6 4126 0.20630
## 7 7 2389 0.11945
## 8 8 917 0.04585
## 9 9 211 0.01055
## 10 10 18 0.00090
#Draw the bar chart which displays binomial distribution where n=10 and p=0.5
ggplot(df)+
geom_bar(aes(x=hc,y=prob),stat="identity")

#Calculate the parameters of binomial distribution
#mean=no of success(n)*Probability of success(p)
#standard deviation=n*p*(1-p)
meanb=n*0.5
sdb=n*0.5*(1-0.5)
meanb # Note that the graph also shows the mean =5
## [1] 5
sdb
## [1] 2.5
#Binomial distribution in r
x=10 #No of successes
size=100 #No of trials
prob=0.5 #Probability of success)
#dbinom(x,size,prob) gives the probability of
#exactly x no of success in size no of
#trials where the probability of success = prob
dbinom(x,size,prob) #Probability of getting exactly 10 heads out of 100 toss and probability of getting head =0.5
## [1] 1.365543e-17
#pbinom(x,size,prob) gives the probability of
#atleast x no of success in size no of
#trials where the probability of success = prob
pbinom(x,size,prob) #Probability of getting atleast 10 heads out of 100 toss and probability of getting head =0.5
## [1] 1.531645e-17
#rbinom(n,size,prob) gives a sample of n
#items which follows a binomial distribution
#with parameter size and probability p
rbinom(1000,size,prob) # selecting sample of 1000 items
## [1] 46 54 47 40 52 49 53 46 53 49 42 62 51 47 49 45 51 53 53 43 51 48 54 49
## [25] 56 43 43 46 47 48 42 39 44 50 46 52 48 51 54 46 55 49 45 46 49 51 55 49
## [49] 55 56 46 50 47 51 49 52 44 57 60 53 50 55 47 42 54 57 56 54 55 55 51 52
## [73] 53 59 45 55 44 53 52 55 46 55 57 59 60 56 48 53 50 52 55 49 42 56 47 53
## [97] 39 45 54 53 52 52 57 52 54 49 52 39 36 47 44 56 47 50 51 51 48 37 54 56
## [121] 43 53 50 48 48 54 48 55 49 56 55 44 48 45 59 56 58 46 43 54 54 50 54 49
## [145] 52 50 56 52 49 55 51 53 45 50 47 50 43 49 59 58 49 53 49 44 55 47 42 45
## [169] 49 50 54 51 52 40 48 54 45 57 53 51 51 50 55 54 52 51 48 52 46 48 51 49
## [193] 48 50 51 48 50 50 54 48 53 48 47 48 53 44 46 55 51 52 45 48 44 50 45 54
## [217] 48 47 44 53 44 49 52 53 53 54 55 61 48 36 54 57 48 57 54 51 50 51 46 47
## [241] 42 48 50 53 58 46 56 47 56 50 50 51 43 51 56 49 56 49 43 49 52 40 51 47
## [265] 49 45 43 45 53 48 52 45 50 57 45 53 57 57 57 46 54 53 46 44 52 49 43 50
## [289] 50 46 54 47 54 57 41 43 53 51 50 62 52 49 44 48 47 54 50 57 53 53 45 44
## [313] 53 58 55 42 43 48 49 41 52 49 47 58 50 55 50 55 50 53 53 48 51 55 48 55
## [337] 49 50 43 51 48 53 57 62 40 41 45 63 52 46 51 51 47 50 53 54 56 46 46 43
## [361] 53 51 55 45 41 50 54 45 52 40 48 53 53 44 54 49 51 40 45 52 41 49 52 54
## [385] 36 44 59 48 51 58 48 40 43 52 53 49 50 44 54 60 51 44 53 48 51 55 48 57
## [409] 48 45 55 47 50 53 49 54 51 52 50 43 49 53 50 53 51 47 54 50 55 51 55 50
## [433] 45 62 46 48 53 50 59 48 53 41 49 46 50 53 58 47 56 48 50 49 49 56 48 49
## [457] 45 48 52 53 50 52 49 47 49 54 54 50 52 42 52 54 51 55 50 52 47 62 53 51
## [481] 56 52 55 53 42 49 45 47 45 52 46 47 44 47 48 50 43 49 51 46 64 47 46 45
## [505] 50 55 49 52 51 49 57 50 47 56 43 54 43 64 47 46 47 42 59 46 49 42 49 53
## [529] 55 54 53 56 53 54 60 43 54 48 48 56 40 47 48 48 51 50 44 55 55 45 50 50
## [553] 51 44 50 55 58 54 46 54 50 47 45 52 44 49 38 57 53 51 53 50 47 48 47 48
## [577] 55 52 48 45 48 52 53 52 51 54 49 54 48 47 50 42 49 47 48 61 56 47 40 43
## [601] 59 50 48 43 50 55 51 49 54 41 42 55 55 50 55 53 49 50 51 56 50 51 54 54
## [625] 45 52 55 57 55 53 43 58 50 42 57 60 54 52 55 49 48 49 59 47 54 48 49 50
## [649] 57 56 56 52 51 48 56 53 48 52 50 50 32 43 49 52 44 54 57 48 53 53 49 44
## [673] 49 45 59 52 46 61 50 46 43 54 50 51 51 51 51 51 53 44 57 47 53 46 61 45
## [697] 53 48 52 39 49 46 50 43 52 42 42 50 53 39 49 41 49 48 50 52 52 49 45 51
## [721] 51 56 47 43 49 48 46 54 49 46 54 47 54 42 42 53 46 56 60 49 49 58 45 47
## [745] 50 48 45 53 59 48 49 44 53 48 40 50 57 53 47 43 47 44 46 45 54 42 52 52
## [769] 43 54 65 54 48 43 54 49 48 39 46 50 47 49 50 43 46 56 57 42 43 49 50 49
## [793] 53 53 55 50 48 40 45 55 48 46 50 57 51 53 45 49 48 48 48 42 55 48 46 55
## [817] 58 53 43 56 43 50 49 51 55 51 54 51 42 52 46 48 45 57 51 52 54 42 52 48
## [841] 39 49 45 47 35 50 47 48 51 53 47 56 49 38 48 55 51 53 55 46 50 52 47 46
## [865] 58 48 53 45 52 48 45 52 55 48 53 52 40 50 51 47 47 47 45 47 49 43 54 41
## [889] 56 37 51 47 52 52 47 55 50 47 52 59 49 58 57 56 46 46 49 52 48 53 48 38
## [913] 54 46 47 49 55 49 50 47 52 49 46 51 50 55 48 54 45 48 55 48 48 48 51 56
## [937] 61 53 51 50 44 52 46 50 54 49 44 55 53 51 52 50 58 44 50 49 52 46 51 61
## [961] 52 52 46 45 55 58 57 57 47 47 51 50 48 49 58 48 49 49 59 47 49 49 48 54
## [985] 56 45 54 59 54 53 52 53 43 51 58 55 55 54 64 46
#Normal distribution in r
#parameter mean=mu, standard Deviation = sigma
mu=5
sigma=10
x=2.5
#dnorm(x,mu,sigma) gives the probability of
# a random variable x = 2.5 if the variable
# x follows a normal distribution with
# mu=5 and sigma=10
dnorm(x,mu,sigma)
## [1] 0.03866681
#pnorm(x,mu,sigma) gives the probability of
# a random variable x <= 2.5 if the variable
# x follows a normal distribution with
# mu=5 and sigma=10
pnorm(x,mu,sigma)
## [1] 0.4012937
#rnorm(n,mu,sigma) gives the normal(mu,sigma)
#sample of size n
n=1000
rnorm(n,mu,sigma)
## [1] 7.549695092 8.206632003 4.137025152 -0.922599406 4.246365818
## [6] -5.352792633 6.997409865 11.349844364 -0.724360283 -1.564100789
## [11] -2.893526116 5.639493430 9.190005739 14.197119145 15.839520660
## [16] 5.717860069 0.396283526 -4.432868726 -18.351063420 -11.477737295
## [21] -8.172649033 3.568715175 -7.104971915 -9.151977052 9.897900274
## [26] 9.649974582 12.900725554 3.804257559 -6.622192841 6.578694488
## [31] 17.197079073 -0.212141590 4.706312282 7.565265763 6.969021908
## [36] -0.289420457 1.434769983 -8.974927340 11.380872811 1.388410558
## [41] 0.101184787 16.132087128 -2.487985636 21.561286984 15.297718526
## [46] 9.589425422 21.041708586 13.778406446 -0.601067626 2.256088171
## [51] 1.077000224 -10.016782396 6.820480695 10.812173321 21.675927031
## [56] 1.204887433 12.261454376 18.432189579 -1.258403745 4.451596628
## [61] -12.947394356 -2.785042691 -0.115236688 -4.080064421 17.761790985
## [66] 4.560059828 4.709344133 3.003224196 10.277738398 -5.234171228
## [71] 12.192106751 -1.478456045 13.197493393 1.014342183 13.677051185
## [76] 0.220520945 10.040488152 0.594390986 14.798781282 22.935016029
## [81] -6.030675883 4.955076784 13.418555363 13.247965722 10.571548789
## [86] 14.959471874 8.536911899 0.939128139 2.332334380 10.529065051
## [91] 20.911851893 -18.691370813 5.315170457 9.689324424 -12.919438258
## [96] 20.522029335 6.480083359 13.008205544 8.729243142 -5.551168768
## [101] 6.563663857 7.945986057 -0.512423195 -10.654270300 -3.519152441
## [106] 3.338035896 13.128450317 7.153547402 23.317462847 0.701534251
## [111] 0.553213180 0.828527703 -6.411873365 7.144363711 -10.526743597
## [116] -2.735481170 -4.447550119 2.112427669 6.118568201 7.818413796
## [121] 12.858211925 11.266294032 3.201122302 -2.063106766 -4.581107098
## [126] 8.318600038 4.404952350 6.671134036 32.379559448 3.106297677
## [131] 11.144834570 16.646161205 -1.527263284 -2.095325662 2.470368732
## [136] 6.097910645 9.827309682 14.756763843 -17.866932726 5.659329642
## [141] -5.430794135 -10.072555367 14.817580072 -1.833483700 -2.216754120
## [146] -7.655050323 9.635884592 6.092724843 -0.052999926 -14.054090497
## [151] 1.302407454 -5.859335396 2.882466840 0.381373226 0.582636148
## [156] 4.268096244 7.654257456 -1.986680638 13.752996649 3.076910949
## [161] 9.984019970 2.211380504 -7.572017430 17.774407096 17.997856086
## [166] 6.631459845 3.537995695 20.596226534 -9.140315834 3.029868790
## [171] 1.492306975 -9.556147072 15.037223263 -7.648938000 5.536068361
## [176] -1.254594363 -4.250646294 27.454897334 -1.250147005 8.494124551
## [181] 7.884792807 19.476518586 13.514367686 4.773023024 17.114519827
## [186] -1.722135293 -4.853079539 -13.074045558 1.114325379 3.847284765
## [191] 4.076735046 22.025821567 20.589850852 0.538342919 3.655429314
## [196] 1.454317815 8.046572933 -0.119967281 5.804811420 -0.532977814
## [201] 16.291326526 9.325314431 13.501597986 -1.320843222 12.821549981
## [206] 22.647592374 -0.868103986 16.243582128 -0.433225988 0.230100998
## [211] 7.462344389 14.797455434 10.267398994 11.758052617 -4.936431508
## [216] 4.475548372 4.580386180 4.127711867 -0.388787676 -19.038288975
## [221] 18.067643181 -1.235737761 -4.383390927 -4.455898949 0.305652486
## [226] 8.533926195 2.707452704 7.972417777 3.302765037 29.064502119
## [231] -24.700517931 -12.863022898 -6.345992891 -3.542176015 9.597273535
## [236] -18.385187801 -4.107992441 32.958091450 5.354628000 2.379166294
## [241] -15.410415569 2.308485379 6.923309820 -2.342585565 7.324728123
## [246] 0.953075222 0.900738843 16.809042705 5.565413631 1.499567740
## [251] 15.822133617 -1.350408216 8.527654221 -0.802284163 0.186292295
## [256] 12.627046781 -5.454462864 2.843185790 20.116622163 -3.631773680
## [261] 12.451113990 12.626376490 15.447129226 -5.611881416 1.155625919
## [266] 0.878972005 6.993780438 1.774318694 7.452338741 -5.231792481
## [271] 8.387474350 12.276398955 6.286883148 11.293579176 9.448004391
## [276] -2.936975900 3.383202191 5.172055454 15.453026041 8.632799728
## [281] 0.278390279 7.121036855 23.212257180 4.703134409 2.701974695
## [286] 18.997398917 2.810507497 0.985713939 -18.444193419 -8.451768839
## [291] 3.382176476 7.368107720 9.898325756 15.674364587 -5.203711174
## [296] 11.203818006 9.585952436 -4.651092731 15.268853016 1.281223587
## [301] 6.531623222 -7.392667114 -1.673259471 10.374727503 0.955446345
## [306] 7.436794314 -1.648384110 20.498954354 2.476140791 19.068987168
## [311] -3.596412984 -7.560894888 -6.147568677 8.884965649 26.270729981
## [316] 9.545557127 18.445727490 4.219470948 -3.051739015 6.665267816
## [321] 10.652393069 1.146175239 -4.856870397 10.879900630 12.265020362
## [326] 13.057902696 19.366923341 -0.039533124 -2.199837626 -1.081633126
## [331] 0.228648384 25.810627549 22.879709739 -8.743446825 10.179435962
## [336] 2.090449579 17.363081105 16.394445400 3.255877988 4.702040328
## [341] 1.291832886 7.232834054 -1.939031128 -17.661580570 -1.914183472
## [346] 14.792999323 8.642954506 0.339495221 -2.963811805 -8.846296402
## [351] 11.862697414 -1.145413633 22.558383130 6.768184183 5.666678707
## [356] 14.768833454 8.660546195 6.496489002 8.482698234 7.122640497
## [361] -0.460764405 2.916147453 13.212148549 8.234161544 -8.125483593
## [366] 1.028544956 -3.644525633 21.436825153 9.904603230 33.701830793
## [371] 10.420711436 17.494652703 -2.953312591 4.952146702 25.881528293
## [376] -2.479880751 1.417983285 -6.994067168 5.544022593 6.669804165
## [381] 7.999178663 -5.524039766 3.290973002 3.674518871 9.857594829
## [386] 6.214573841 4.715045580 4.508984544 -4.561217550 11.985046101
## [391] 3.420036415 -1.088251988 19.845395994 5.414966982 10.950233761
## [396] 3.709202926 16.286089505 -1.259888717 2.140169069 4.765377460
## [401] 1.225191299 -1.849693245 2.531906966 14.544979433 -7.062993135
## [406] 4.701240503 8.071903996 -3.071565663 7.888486824 -5.593519738
## [411] 0.947166709 -2.481325585 17.799890425 7.762917160 11.577135520
## [416] 0.394161318 11.141043600 -3.542353433 4.922232743 16.631903032
## [421] 14.246321264 10.023666021 -0.351492829 24.520718398 18.656656363
## [426] 5.777574354 17.038259773 15.236321919 3.678386855 3.803571127
## [431] 12.376853265 14.336519739 0.998643474 -9.904338871 -6.033952863
## [436] 10.279974741 17.550917258 -4.923556820 13.514333402 -6.639004539
## [441] 20.650160327 0.952509412 9.687660464 10.860889961 5.072125167
## [446] -5.433421839 30.567921304 -22.947694809 8.199565766 -5.263160827
## [451] 8.181408502 12.809407360 22.237595744 -2.664769362 5.427378766
## [456] 12.528913395 -3.244345047 26.726328856 8.711718534 12.721111108
## [461] 3.459169220 22.116101382 -5.969995310 16.088550968 -8.891379125
## [466] 8.929995369 -1.375897047 4.743614648 5.205366816 0.898474722
## [471] 5.787635415 -9.998322757 11.672862201 -3.316549316 4.483560301
## [476] 3.016892457 -2.871469611 7.411827291 -5.735654424 5.463010088
## [481] 19.938730832 -2.230094520 10.980368610 6.731168051 -10.696918852
## [486] 1.636075216 -8.520685964 -3.749434255 17.182153734 4.345321002
## [491] -0.761650758 10.111998992 11.611244134 19.657859280 22.176703260
## [496] -14.705426237 4.461296375 7.309170600 6.398220268 -9.562286568
## [501] 4.470062260 9.058920272 -11.206449307 15.179360432 -2.684430194
## [506] 6.753615920 -2.672171397 4.710026351 12.839512001 8.261220717
## [511] 14.620654671 18.164776699 3.270819710 18.746836582 13.028106192
## [516] 0.945849918 -9.753571123 0.209503597 11.286693351 11.210107079
## [521] 9.172976420 25.450507874 -5.664536342 -12.680169055 -18.532948276
## [526] 18.563127329 1.367545714 -19.275389722 13.562129312 10.067949041
## [531] 26.036728663 14.689222203 2.651017583 11.887715270 2.001619885
## [536] -13.658396756 12.342969130 -1.277426500 -2.938902938 2.336544478
## [541] 10.338920948 0.599698931 13.344408397 5.845619959 11.281350985
## [546] -1.475462093 -2.375924341 9.851819674 21.437913648 12.128934913
## [551] -11.272726210 1.726824896 6.762104204 -6.925383648 0.360899101
## [556] -1.623796942 12.705063685 10.391402646 12.346931103 1.932486722
## [561] 7.184875467 27.668645583 9.712209477 -5.160699303 13.942057373
## [566] 20.828950032 -11.484938934 2.634671331 0.264445800 -4.445922299
## [571] 1.504726956 2.643254178 -0.743867628 14.100697360 11.943607016
## [576] 18.513088259 7.332730063 6.716952627 9.851644294 -9.433802642
## [581] 0.773708764 3.126725258 12.859147196 -7.202258199 5.648766045
## [586] 13.353521087 9.869210398 8.326573517 7.033888677 -1.704760557
## [591] 11.164923750 -17.500662061 14.113039920 5.675417005 11.562411193
## [596] 23.489630224 13.962414867 17.375079919 6.419375387 0.655553415
## [601] 3.467000484 -9.148061698 -7.702615271 21.153080682 3.991507140
## [606] -4.279266420 3.311287548 3.185345775 6.591311482 10.108710950
## [611] 16.283256579 -7.309094600 13.563743263 -3.567927631 19.888129695
## [616] 12.150064987 2.220154596 4.730996339 -0.995386225 -11.648203454
## [621] 22.926173020 21.263948291 13.388462040 2.254702291 8.086421638
## [626] 15.189106624 3.505791801 -7.243997375 -17.916210048 3.689781451
## [631] 1.927683692 29.756373685 16.454790859 13.249476489 3.083137521
## [636] 18.794753250 -4.565392753 -2.205646441 0.103921463 -4.593588295
## [641] 3.608315493 -2.965348525 -8.641857114 1.368583432 -12.611704058
## [646] 15.324630541 2.354788177 4.425820236 12.405213881 7.863799266
## [651] -10.615821958 1.709573539 11.762134485 0.926785053 21.672286368
## [656] 13.260657164 0.128608490 13.391669803 7.530881916 3.442008853
## [661] 13.405832791 5.682263233 35.396726449 20.275734379 4.996891744
## [666] -10.843788706 -0.833718098 13.821484535 12.193686311 0.877237893
## [671] 1.817965304 17.531381352 6.727161011 5.981702265 15.040241386
## [676] 19.625791674 -0.658055793 -0.969830069 4.432821272 -13.818703602
## [681] 2.816102344 1.712577392 3.464915471 17.913590966 -9.578126574
## [686] -19.601009137 0.397383517 -15.326767117 4.876877145 -8.177648545
## [691] -0.227225515 30.130452386 2.471019725 -9.828043501 14.181634006
## [696] 3.083459052 -1.361240965 9.978122161 -11.050708237 -0.920640342
## [701] 13.999382205 9.165662257 -0.001564392 16.285720463 -7.434367708
## [706] -0.261930191 10.192384464 5.781626141 15.643667454 -12.159193163
## [711] 2.296722516 -10.287932635 7.201755787 -10.212545073 9.129675062
## [716] -3.312121842 -2.983710230 7.576386435 4.880502866 0.830102199
## [721] -2.812393502 22.443866321 15.862036265 4.575041213 -8.746766186
## [726] 13.195138714 -2.851615765 14.520584455 32.724112812 8.789081192
## [731] 5.716602933 12.583398310 6.982002343 5.299595568 8.677374294
## [736] 4.525560016 4.978853047 7.538179413 1.749241723 -16.864800007
## [741] -4.673335223 -15.143838126 1.620420671 3.321806266 -2.887474274
## [746] 1.006221236 -9.317991378 0.196535479 6.622992819 2.757612352
## [751] 19.656187481 1.701700929 -9.015536021 1.839113113 19.276608637
## [756] -10.886312377 0.344090388 -22.564564113 2.442330863 -5.783959564
## [761] 3.126491698 8.587763116 5.863463092 0.613528668 6.538965406
## [766] 26.686355704 7.349588505 6.817558194 2.721624578 16.599392841
## [771] -4.409982527 6.207999391 -28.418872759 -16.241075586 19.108647114
## [776] -2.337415908 -9.187218445 -3.261718703 -3.488826556 16.650718173
## [781] -10.039928081 9.446099153 -5.597932502 -29.104537683 1.713579804
## [786] -7.705042520 1.700067658 9.646958803 1.324264592 5.282303101
## [791] 6.994819158 -1.399080597 11.253947699 17.282165363 -3.007735864
## [796] 23.053407815 -1.371999304 4.153100509 15.498124823 15.175603698
## [801] 9.818114179 -1.734845148 1.182614350 2.511697297 -5.922745573
## [806] -1.505658493 -13.632774424 8.449485218 10.380336466 -5.123109198
## [811] 14.154882028 5.035265798 9.738865230 5.500246229 4.024929051
## [816] 15.455073014 6.429362204 4.355688543 -7.022579608 -16.615808474
## [821] 1.212946049 1.530066746 21.041755867 -4.219429847 -0.278510597
## [826] 0.223367165 6.034492588 11.615597736 9.060212928 13.128334046
## [831] -1.469937359 13.841146150 3.711947948 6.200172962 16.707479651
## [836] -8.185829937 5.229713291 1.452658990 17.607013413 8.806541384
## [841] 2.440211225 3.458199435 2.262527138 32.975484547 -1.656538679
## [846] 2.592616342 6.186487435 9.419495879 -15.711077483 4.050486954
## [851] 8.774669595 -4.055852865 16.314047464 11.362532780 16.142509149
## [856] 15.984026128 7.690355606 6.300507544 -2.761193492 3.353440746
## [861] -2.190896856 -1.595509010 5.273213762 2.592518717 22.870053300
## [866] 1.975378642 -2.044548758 1.020933523 -8.114662498 -1.015209764
## [871] 10.713195790 17.472776817 9.919114456 20.559025628 0.776567066
## [876] -15.542263352 -9.661084375 11.895790162 1.615666963 6.746325451
## [881] 16.373078252 0.328868539 24.678552316 12.141954045 4.923118255
## [886] -0.334531906 23.414929785 10.433774425 0.122654383 16.829368585
## [891] 6.552748510 -0.449787917 1.349481333 8.415045427 10.825734216
## [896] 6.430567092 0.802909737 15.415010623 0.990504656 0.150946099
## [901] 23.463289432 17.077905222 7.007092580 -2.115418947 22.101310302
## [906] 29.260852171 -3.679131792 -2.480801513 -6.149045825 -5.013582500
## [911] -7.505983312 7.670810962 1.770478552 17.408342638 5.880521482
## [916] 9.467498311 21.000651849 -2.999451748 -10.956534328 -12.582525382
## [921] 8.862678099 -1.413144161 2.831975044 -11.345043718 1.060741461
## [926] 14.504713011 12.565042137 17.248556688 13.796330119 15.018793517
## [931] -3.134552259 -12.164311874 15.385688789 11.499611491 -13.093876808
## [936] 3.573359587 15.787266211 4.096747770 -6.044824096 -1.069980240
## [941] 1.128190160 21.622713533 13.190031606 12.525759885 2.400098096
## [946] -6.560691728 -0.151980214 -0.186173149 14.046829949 -15.564275298
## [951] -10.904716962 16.234018035 -4.590276005 24.395351215 -3.421508085
## [956] 5.331276667 22.250834481 25.982216617 -4.431028327 -4.899261805
## [961] -4.765326934 11.852868537 10.990004442 10.880726819 -6.519787836
## [966] 10.124626732 -29.992374499 9.491077144 -2.906419225 -5.533397194
## [971] 13.951336063 4.107373906 -1.976416047 -1.878715447 22.629809571
## [976] 11.960959765 13.937014847 -3.439328194 -4.536267959 -0.045095078
## [981] -2.882397054 7.589761790 13.635447914 -2.502114036 5.878410406
## [986] 15.367381718 21.301610153 17.565533575 13.483165801 -4.628240207
## [991] 0.454936947 15.636641836 6.431610526 12.126949121 0.648996498
## [996] -4.680952220 -0.699993326 4.085625736 7.596503420 4.892732251
#Create a graph for normal distribution.
x=seq(-5,5,.05) #x contains a seq (-5,-4.95,-4.9,....,0,......4.9,4.95,5)
mu=0
sigma=1
prob=dnorm(x,mu,sigma) # calculate probability of random variable x assuming x follows normal distribution.
df=data.frame(x,prob) # create the data frame
names(df)=c("x","Prob")
#Draw the normal distribution curve
ggplot(df)+
geom_line(aes(x=x,y=Prob))+
geom_point(aes(x=x,y=Prob))

#Build the normal population N(mean=10,sd=3)
#with 100000 points
mu=10
sigma=3
npop=100000
pop=rnorm(npop,mu,sigma)
dfp=data.frame(pop)
names(dfp)
## [1] "pop"
plot1=ggplot(dfp)+
geom_histogram(aes(x=pop))
#create sampling distribution for sample size=10
meanv=c() #this vector stores the mean of each sampling distribution
sdv=c() #this vector stores the standard deviation of each sampling distribution
sizev=c() #this vector stores the sample size of each sampling distribution
sampsize=10 #
sampmean=c()
nsamp=2000 # Total no of samples in the sampling distribution
for (i in 1:nsamp) {
x=sample(pop,sampsize,replace = TRUE) # Create sample from normal distribution with replacement
sampmean=c(sampmean,mean(x))
}
meanv=c(meanv,mean(sampmean))
meanv
## [1] 9.983936
sdv=c(sdv,sd(sampmean))
sdv
## [1] 0.9675198
sizev=c(sizev,sampsize)
sizev
## [1] 10
dfsg=data.frame(sampmean)
plots1=ggplot(dfsg)+
geom_histogram(aes(x=sampmean))
#create sampling distribution for sample size=100
sampsize=100 #
sampmean=c()
nsamp=2000 # Total no of samples in the sampling distribution
for (i in 1:nsamp) {
x=sample(pop,sampsize,replace = TRUE) # Create sample from normal distribution with replacement
sampmean=c(sampmean,mean(x))
}
meanv=c(meanv,mean(sampmean))
meanv
## [1] 9.983936 9.988787
sdv=c(sdv,sd(sampmean))
sdv
## [1] 0.9675198 0.2977383
sizev=c(sizev,sampsize)
sizev
## [1] 10 100
dfsg=data.frame(sampmean)
plots2=ggplot(dfsg)+
geom_histogram(aes(x=sampmean))
#create sampling distribution for sample size=1000
sampsize=1000 #
sampmean=c()
nsamp=2000 # Total no of samples in the sampling distribution
for (i in 1:nsamp) {
x=sample(pop,sampsize,replace = TRUE) # Create sample from normal distribution with replacement
sampmean=c(sampmean,mean(x))
}
meanv=c(meanv,mean(sampmean))
meanv
## [1] 9.983936 9.988787 9.987371
sdv=c(sdv,sd(sampmean))
sdv
## [1] 0.96751985 0.29773834 0.09474086
sizev=c(sizev,sampsize)
sizev
## [1] 10 100 1000
dfsg=data.frame(sampmean)
plots3=ggplot(dfsg)+
geom_histogram(aes(x=sampmean))
grid.arrange(plot1,plots1,plots2,plots3,ncol=1)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#Create a dataframe with sample size, mean,sd,sigma/sqrt(n)
dfclt=data.frame(sizev,meanv,sdv,sigma/sqrt(n))
names(dfclt)=c("Sample Size","Mean","SdErr","sigma_by_sqrt(n)")
dfclt
## Sample Size Mean SdErr sigma_by_sqrt(n)
## 1 10 9.983936 0.96751985 0.09486833
## 2 100 9.988787 0.29773834 0.09486833
## 3 1000 9.987371 0.09474086 0.09486833
#Notice that sampling distribution is becoming a normal distribution with mu =10 and the standard error is sigma/root(n). The central limit theorem.