Advertisement
sanket2383

Practicality of life

Apr 24th, 2024
1,186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 9.94 KB | Source Code | 0 0
  1. ## Set A
  2.  
  3. ## 1) Model Sampling from Cauchy and Laplace distribution
  4. > # a)
  5. > # X follows cauchy disribution with parameter theta=10 and lambda=1
  6. > #using inverse Transformation CDF
  7. > n=11 #size of random sample to be drawn from Cauchy distribution
  8. > lambda=1 #Scale Parameter
  9. > theta=10 #Location parameter
  10. > # As we Know that evry distribution cdf follows standard uniform distribution
  11. > y=runif(n) # Generating random sample of size 11 from uniform distribution
  12. > x=lambda*tan(pi* (v-0.5))+theta;x
  13. > # b) Median of the cauchy sample obtain From obove
  14. > median(x)
  15. ## Cauchy= L/2*exp(-L|x-u|)
  16.  
  17.  
  18.  
  19. ## 2) Log normal Cancer
  20. # (1/(x-a)sigrt2pi)exp(-1/2sigsq(logx-a - u)sq)
  21. > # Fiting log normal distribution
  22. > LL=seq(0,60,10);LL #lower Limit of given data
  23. > UL=seq(10,70,10);UL #upper limit of the given data
  24. > Freq=c(20,90,52,11,6,4,1);Freq
  25. > N=sum(Freq)
  26. > x=(UL+LL)/2; #mid point of the gien data
  27. > d=data.frame("lower limit"=LL, "Upper limit"=UL,"mid point"=x, "frequency"=Freq)
  28. > d
  29. > M12=sum(Freq*x/sum(Freq); M12 # Frist sample raw Movement used for estimating mean and Sigma
  30. > M13=sum(Freq*(x^2))/sum(Freq);M13 # Second sample raw moment used for estimating mean sigma
  31. > sigma_est=log(M13,base=exp(1))-(2*log(M12,base =exp (1))); sigma_est ##estimating sigma for calculating Probabilities
  32. > mu_est=(2*log(M12,base=exp(1)))-(log(M13,base=exp(1))/2); mu_est ##estimating mean for calculating probabilities
  33. > a=plnorm(UL,mu_est,sqrt(sigma_est));a #calculating cumulative Probabilities
  34. > px=с()
  35. > px[1]=a[1]
  36. > for (i in 1: (length(a)-1)){
  37. + px[i+1]=ali+1]-a[i]
  38. +}
  39. > ExFreq=round(N*px,2);ExFreq
  40. > sum(ExFreq)
  41. > D1= data.frame(d, "expected"= Exfreq)
  42.  
  43.  
  44.  
  45.  
  46. # 3) literates 2 year Ratio and Regression mthd
  47. #Ratio and regression method of estimation Comparsion with SRSWOR
  48. > x=с(109,101,125,254,559,359,427,481);X
  49. > y=c(99,112,111,278,634,355,399,489); Y
  50. > N=170
  51. #No observation of given data X.
  52. > n=length(y)
  53. > Xt=21288000 #population total of given data X
  54. > Х_bar_N=xt/N;X_bar_N #Population Mean of given data of X
  55. > y_bar_n=mean(y)
  56. > x_bar_n=mean(x)
  57. > #Ratio method.
  58. > Rn=y_bar_n/x_bar_n;Rn #ratio of sample mean of y and x
  59. > Y_bar_N=Rn*X_bar_N;Y_bar_N #Estimate of Population mean of Y
  60. > sy_sq=var(y);sy_sq #sample mean square of y
  61. > sx_sq=var(x);sx_sq #sample mean square of x
  62. > sxy_sq=var(x,y);sxy_sq #sample mean square of xy
  63. > SE_Y_totalhat=N*sqrt(((1/n)-(1/N))*(sy_sq+Rn^2*sx_sq-(2*Rn*sxy_sq)));SE_Y_totalhat #Estimate of SE of population total of Y
  64. > #Regression method:
  65. > byx=sxy_sq/sx_sq;byx #regression coefficient of Y on X
  66. > Y_hat=y_bar_n+(byx*(X_bar_N-x_bar_n));Y_hat #Estimate of Population mean of Y
  67. > Y_hat_total=N*Y_hat;Y_hat_total #Estimate of Population total of Y
  68. > SE_Y_total1=N*sqrt((N-n)/(N*n))*(sy_sq+(byx^2*sx_sq)-(2*byx*sxy_sq)));SE_Y_total1 #Estimate of SE of population total of Y
  69. > #comparison between SRSwor
  70. > var_y_bar=((1/n)-(1/N))*sy_sq;var_y_bar #variance of sample mean under SRSWOR
  71. › SE_Y_hat=N*sqrt(var_y_bar);SE_Y_hat #S.E of sample mean under SRSWOR
  72. > #conclusion- SE(Y_hat)SRSWOR>SE(Y_hat)RATIO>SE(Y_hat)REGRESSION
  73. ># This implies that variation of regression estimator is minimum and use supplementary information increases the precision.
  74.  
  75.  
  76.  
  77.  
  78. ## SET B
  79. ## 1. B
  80. > #que no 4
  81. > mu=5
  82. > sigma=sqrt(4)
  83. > set.seed (8)
  84. > u=runif(8,0,1)
  85. #where u and v follows U(0,1)
  86. > U
  87. > v=runif (8,0,1)
  88. > V
  89. > z=sqrt(-2*log(x=u)) *cos(2*pi*v)
  90. #random sample from N (0,1)
  91. > Z
  92. 1>x=mu+(2* sigma)
  93. #random sample from N(5,4)
  94. > X
  95. > mean (X)
  96. > #conclusion-Hence our sample mean is close to population mean but sample variance shows some deviation but as the sample size tends to infinity it will close to population variance
  97.  
  98.  
  99. #1.C
  100. > #Weibull distribution with alpha=15 and beta=10
  101. > alpha3=3
  102. #parameter values of weibull distribution
  103. > beta3=1
  104. > x=seq (-0.01,15,0.1);x
  105. > px4=dweibull(x,shape=beta3,scale=alpha3);px
  106. > plot(x,px4,col="pink",xlab="variable", ylab="probability", lwd=4,type="1", ylim=c(0,0.92))
  107.  
  108. > #а)
  109. > alpha=1
  110. > beta=2 #parameter values of weibull distribution
  111. > x=seq(-0.01,15,0.1);x
  112. > px=dweibull(x,shape=beta,scale=alpha);px
  113. > lines(x=x,y=px,lty=3,col="yellow", lwd=3)
  114. >#D)
  115. > alpha1=5 #parameter values of weibull distribution
  116. > beta1=1
  117. > px1=dweibull(x,shape=beta1,scale=alpha1);px
  118. > lines(x=x,Y=px1,col="blue", lwd=4,Ity=2)
  119. > #C)
  120. > alpha1=1 #parameter values of weibull distribution
  121. > beta1=1
  122. > px2=dweibull(x,shape=beta1,scale=alphal);px
  123. > lines(x=x,y=px2,col="green", lwd=3,Ity=2)
  124. >
  125. >#d)
  126. > alpha2=10 #parameter values of weibull distribution
  127. > beta2=5
  128. > px3=dweibull(x,shape=beta2,scale=alpha2); px
  129. > lines(x=x,y=px3,col="violet", wd=4,Ity=2)
  130. legend (locator(1), legend=c("W(alpha=3,beta=1)", "W(alpha=1,beta=2)", "W(alpha=5,beta=1)", "W(alpha=1,beta=1)", "W(alpha=10, beta=5)") fill=c("pink", "yellow","blue", "green", "violet"))
  131. > #conclusion-As we we increase the values of both the shape parameter it slowly becomes symmetric from strong negatively skewed.
  132.  
  133.  
  134.  
  135.  
  136. ###proportional and neyman allocation
  137. > N1=80 #population
  138. > N2=60
  139. > N3=40
  140. > N4=100
  141. > N5=100
  142. > Ni=c(80,60,40,100,120); Ni #population of each strata
  143. > N=sum(N1,N2,N3, N4, N5);N
  144. > y_bar_i=c(82.3,161.5,139.2,239.1,200);Y_bar_i #Popn mean of each strata
  145. > Si=c(21.3,17.7,14.7,15.3,18.9);Si
  146. > n=40
  147. > # Obtain the size of the sub sample from each stratum
  148. > # for population allocation
  149. > ni=(n/N)*Ni;ni
  150. > n1=ni[1];n1
  151. > n2=ni|2];n2
  152. > n3=ni[3];n3
  153. > n4=ni[4];n4
  154. > n5=ni[5];n5
  155.  
  156. ># for Nayman's allocation
  157. > ni=(n*Ni*Si)/sum(Ni*Si)
  158. > n1=round(ni(1],0);n1
  159. > n2=round(ni[2],0);n2
  160. > n3=round(ni[3],0);n3
  161. > n4=round(ni[4],0);n4
  162. > n5=round (ni[5],0);n5
  163. > # To obtain S.E of the estimate of population mean and population total
  164. > # under population allocation
  165. > pi=c(N1/N,N2/N,N3/N, N4/N,N5/N);pi
  166. > S_E_est_ Ybar1=sqrt(((1/n)-(1/N))*sum(pi*Si^2));S_E_est_Ybar1
  167. > S.E_est_Yt1=sqrt(N*S_E_est_Ybar1);S.E_est_Yt1
  168. # Under Nayman's allocation
  169. > S.E_est_ Ybar2=sqrt((1/n)*(sum(pi*Si))*2)-((1/N)*sum(pi*Si^2)));S.E_est_Ybar2
  170. > S.E_est_Vt2=sqrt(N*S.E_est_Ybar2);S.E_est_Yt2
  171. > #UNDER SRSWOR
  172. > Y_bar_N=sum(Y_bar_i*Ni)/N;Y_bar_N
  173. > S.E_est_Ybar3=sart((N-n)/(N*n))*(1/(N-1)) *(Isum(Ni-1) *Si^2)+(sum((Y_bar_i-Y_bar _N)*2*Ni));S.E_est_Ybar3
  174. > S.E_est_Yt3=sqrt(N*S.E_est_Ybar3);S.E_est_Yt3
  175. # conclusion:since varience of ybarst of population mean under SRSWOR is more than the varience under population allocation and Neyman's allocation
  176.  
  177.  
  178.  
  179. SET C
  180. Q1. A(i)
  181. > x=sort(c(24,38,61,22,16,57,31,29,35));x
  182. > length(x)
  183. > LL=seq(1,9,1);LL
  184. > UL=sort(seq(1,9,1), decreasing = TRUE); UL
  185. > d=data.frame(LL,UL);a
  186. > d1=subset(d,LL<UL); d1
  187. > d2=data.frame(d1,LB=x[d1$LL],UB=[d1$UL]);d2
  188. > con_Fin=pbinom(d2$UL-1,9,0.5)-pbinom(d2$LL-1,9,0.5)
  189. > data.frame(d2,con_Fin)
  190. > #Median would lie within 24,38 confidence interval with 82% confidence coefficent.
  191.  
  192. Q1. C
  193. > #Q 2: 6 students mark out of 50
  194. > L=c(42,18,25,35,20,30);L
  195. > N=length(L);N #L is total no of sampling unit IN population
  196. > n=2;n
  197. > 0b1=1:6
  198. > 002=1:6
  199. > m=merge(ob1,0b2);m
  200. > sub=subset(m,m$x>m$y);sub #position of observation in SRSWOR sample
  201. > d=data.frame(ob1=L|sub|,11],ob2=L[sub|,2]]);d
  202. > d1=data.frame(d,m=apply(d,1,mean),sv=(apply(d,1,var))*((n-1)/n));d1
  203. > n1= length(d1$sm)
  204. > #verify(E(y_bar)=Y_bar)
  205. > est_y_bar=mean(d15sm);est_y_bar
  206. > #conclusion:population mean is unbiased estimator of sample mean
  207. > ybar=mean(L); ybar
  208. > #veryfy (var(ybar)=((N-n)/Nn)s^2)
  209. > n=length(d1$sv);n
  210. > var_y_bar-var(d15sm)*(n1-1)/(n1);var_y_bar
  211. > RHS=var(L)*((N-n)/(N*n)); RHS
  212.  
  213. > #confidence interval for population mean
  214. > alpha=0.05
  215. > z_alpha_2=gnorm(1-(alpha/2));z_alpha_2
  216. > sigma=sqrt(d1$sv);sigma
  217. > d2-data.frame(d2, L_cl-d15m-(z_alpha_2*sigma), U_CL=15sm+(z_alpha 2*sigma));d2
  218. > d3=data.frame(d2,T_L_cl=N*(d1$sm-(z_alpha_2*sigma)), T_U_CL=N*(d1$sm+(z_alpha_2*sigma)));d3
  219.  
  220. ## list of 3100 , 41 wrong .. Find SE and CI
  221. > N=3100;N # where N is Population size.
  222. > n=200;n #where n is random sample size from population.
  223. > a=41 #No. of names and address which are wrong and need to be corrected in list in sample of 200.
  224. > р=а/п;р #where p is proportion of sample which is unbaised estimator of population proportiona
  225. > q=1-p;9
  226.  
  227. > #To find estimate for standard error for population Proportion
  228. > est_S.E_est_P=sqrt((N-n)/(n*N))*(n/(n-1))*p*q);est_S.E_est_P
  229. > #To find 95% confidence interval for population proprotion
  230. > #for n>30
  231. > alpha=0.05
  232. > Z_alpha=qnorm (1-(alpha/2),0,1);Z_alpha
  233. > L_CI=p-(Z_alpha*est_S.E_est_P);L_CI
  234. > U_CI=p+(Z_alpha*est _S.E_est_P);U_Cl
  235.  
  236. > #To find 95% confidence interval for population Total
  237. > L_CI=N*(p-(Z_alpha*est_S.E_est_P));L_Cl
  238. > U_CI=N*(p+(Z_alpha*est_S.E_est_P));U_Cl
  239.  
  240.  
  241.  
  242. Sale_last_year=c(50,35,12,10, 15,30,9,25, 100,250,50,50,150,100,40);Sal e_last_year
  243. 111 50 35 12 10 15 30 9 25 100 250 50 50 150 100 40
  244. > Sale_this_year=c(56,48,22,14,18,26,11,30,165,409,73,70,95,55,83);Sale _this_year
  245. > N=300
  246. #No observation of given data X
  247. > n=length(y)
  248. > Xt=21300
  249. #population total of given data X
  250. > X_bar_N=Xt/N;_bar_N
  251. [1] 71
  252. #Population Mean of given data of x
  253. > y_bar
  254. _n=mean (y)
  255. > x_
  256. bar_n=mean(x)
  257. #sample mean of y #sample mean of y
  258. > #Ratio method.
  259. > Rn=y_bar_n/x_bar_n;Rn
  260. #ratio of sample mean of y and
  261. sample meanof x
  262. [1] 0.9622642
  263. > Y_bar_N=Rn *X_bar_N;Y_bar_N
  264. #Estimate of Population mean of
  265. Y
  266. [1] 68.32075
  267. > sy_sq=var(y);sy_sq
  268. #sample mean square of y
  269. [1] 88.1
  270. > sx_sq=var(x);sx_sq
  271. #sample mean square of x
  272. [1] 100.6778
  273. > sxy_sq=var(x,y);sxy_sq
  274. #sample mean square of xy
  275. [1] 79.63333
  276. _Y_totalhat=N*sqrt(((1/n)-(1/N))*(sy_sq+Rn^2*sx_sq-
  277. (2*Rn*sxy_sq)));SE_Y.
  278. _totalhat #Estimate of SE of population total of Y
  279. [1] 494.1418
  280. > #Regression method:
  281. > byx=sxy_sq/sx_sq;byx
  282. #regression coefficient of Y on X
  283. [1] 0.7909723
  284. > Y_hat=y_bar_n+(byx*(X_bar_N-x_bar_n));Y__hat
  285. Population mean of Y
  286. [1] 66.14535
  287. > Y_hat_total=N*Y_hat;Y_hat_total
  288. mean of X
  289. [1] 19843.6
  290. #Estimate of Population
  291. > SE_Ytotal1=N*sqrt((N-n)/(N*n))*(sy_sa+(byx^2*sx_sa)-(2*byx*sxy_sq)))
  292. > #The Standard error of estimator from Ratio and regression method is494.1418 and 467.4147
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement