Measures of Central Tendency and Variability(1):
1. Problem Statement: Write the R programming code for computing the mean, median,
mode, quartile
deviation, variance, standard deviation, co-e icient of variation, firstfour moments
about the mean for the
following frequency distribution.
2. R Programming Part
2.1. Inputs Given in R Software
• Class height: h=10
• Mid- value of classinterval: x=seq(175,245,10) i.e.{175, 185, 195, 205, 215, 225, 235,
245}
• Given frequencies of class: f=c(52, 68, 85, 92, 100, 95, 70, 28)
2.2. R Programming Code
#ASSESSMENT 1
> h=10
> x=seq(175,245,10)
> f=c(52,68,85,92,100,95,70,28)
> N=sum(f)
> mean=sum(x*f)/N
> mean
[1] 208.9831
> cf=cumsum(f)
> medianclass=min(which(cf>=(N/2)))
> ll=x[medianclass]-(h/2)
> median=ll+((h/f[medianclass])*((N/2)-cf[medianclass-1]))
> median
[1] 209.7826
> modalclass=which(f==max(f))
> f1=f[modalclass]
> f0=f[modalclass-1]
> f2=f[modalclass+1]
> llmodal=x[modalclass]-(h/2)
> mode=llmodal+((h*(f1-f0))/(f1-f0-f2+f1))
> mode
[1] 216.1538
> q1=min(which(cf>=(N/4)))
> q3=min(which(cf>=((3*N)/4)))
> lq1=x[q1]-(h/2)
> lq3=x[q3]-(h/2)
> Q1=lq1+((h/f[q1])*((N/4)-cf[q1-1]))
> Q3=lq3+((h/f[q3])*(((3*N)/4)-cf[q3-1]))
> QD=0.5*(Q3-Q1)
> QD
[1] 15.77709
> var=sum(f*(x-mean)*(x-mean))/N
> SD=sqrt(var)1
> SD
[1] 19.69857
> var
[1] 388.0336
> CV=(SD/mean)*100
> CV
[1] 9.425917
> m1=sum((f*x)-(f*mean))/N
> m2=sum((f*((x-mean)^2)))/N
> m3=sum((f*((x-mean)^3)))/N
> m4=sum((f*((x-mean)^4)))/N
> m1
[1] -1.233213e-14
> m2
[1] 388.0336
> m3
[1]-447.3203
> m4
[1] 306260.9
BINOMIAL, POISSON AND NORMAL DISTRIBUTION(2):
1. Problem Statement: Itis known that probability of an item produced by a certain
machine will be
defective is 0.05. If the produced items are sentto the marketin packets of 20, then write
down the R
code to find the number of packets containing at least, exactly and at most 2 defective
items in a
consignment of 1000 packets.
2. R Programming Part
2.1. Inputs Given in R Software
• Probability : p=0.05
• Itemssentto market : n=20
• Total itemsin consignment : N=1000
• Number of defective items: k=2
2.2. R Programming Code
# binomial distribution
n=20
p=0.05
N=1000
#the number ofpackets containingatleast 2 defective items
k=2
N1=round(N*(1-pbinom(k-1,n,p)))
#the number of packets containing exactly 2 defective items
k=2
N2=round(N*dbinom(k,n,p))
#the number ofpackets containingatmost 2 defective items
k=2
N3=round(N*pbinom(k,n,p))
2.3 Outputs (Expressions or Figures) Obtained in R Software
> N1
[1] 264
> N2
[1] 189
> N3
[1] 925
. Problem Statement: A car hire firm has 2 cars which it hires out day by day. The
number of demands for
a car on each day follows a Poisson distribution with mean 1.5. Write down the R code
to compute the
proportion of days on which (i). neither car is used,(ii). at most one car is used and (iii).
some demand of
car is not fulfilled.
2. R Programming Part
2.1 Inputs Given in R Software
• Mean (Lambda): lam=1.5
2.2 R Programming Code
#QUESTION 2
lam=1.5
#Proportion of days on which neither car is used:
x=0
P1=dpois(x,lam)
#Proportion of days on which at most 1 car is used:
x=1
P2=ppois(x,lam)
#Proportion of days on which some demand of car is not fulfilled:
x=2
P3=1-ppois(x,lam)
2.3 Outputs (Expressions or Figures) Obtained in R Software
> P1
[1] 0.2231302
> P2
[1] 0.5578254
> P3
[1] 0.1911532
1. Problem Statement: The local corporation authorities in a certain city install 10,000
electric lamps in
the streets ofthe city with the assumption thatthe life of lamps is normally distributed.
Ifthese lamps
have an average life of 1,000 burning hours with a standard deviation of 200 hours, then
write down
the R code to calculate the number of lamps might be expected to fail in the first 800
burning hours
and also the number of lamps might be expected to fail between 800 and 1,200 burning
hours.
2. R Programming Part
2.1. Inputs Given in R Software
• Average life of lamps: Mu=1000
• Standard Deviation : SD=200
• Total number of lamps: N=10000
2.2. R Programming Code
#QUESTION 3
#NORMALDISTRIBUTION
Mu=1000
SD=200
N=10000
#1 the number oflamps might be expectedto fail in the first800 burning hours:
x1=800
N1=round(N*pnorm(x1,Mu,SD))
#2 the number oflamps might be expected to fail between 800and 1200 burning hours:
x1=800
x2=1200
N2=round(N*(pnorm(x2,Mu,SD)-pnorm(x1,Mu,SD)))
2.3 (Expressions or Figures) Obtained in R Software
> N1
[1] 1587
> N2
[1] 6827
Program / Problem No.: 1
Write down the R code to compute the coe icient of correlation between X and Y from
the following data:
X : 21 ,23, 30, 54, 57 ,58, 72, 78, 87, 90
Y : 60, 71, 72, 83, 110, 84, 100, 92, 113, 135
Code:
> #CORRELATION
>
> X=c(21,23,30,54,57,58,72,78,87,90)
> Y=c(60,71,72,83,110,84,100,92,113,135)
> Cor1=cor(X,Y)
> Cor1
[1] 0.8775417
Program / Problem No.: 2
Write down the R code to find the rank correlation between the ranks of the variable X
and Y from the following data:
X : 10 15 12 17 13 16 24 14 22
Y : 30 42 45 46 33 34 40 35 39
CODE:
>#RANK CORRELATION
> X=c(10,15,12,17,13,16,24,14,22)
> Y=c(30,42,45,46,33,34,40,35,39)
>
> n=length(X)
> U=rank(X)
> V=rank(Y)
> SumD2=sum((U-V)^2)
> RankCor=1-((6*SumD2)/(n*(n^2-1)))
> RankCor
[1] 0.4
Program / Problem No.:3
Write down the R code to obtain the equation of the regression line of X on Y from the
following data:
X : 4.7 8.2 12.4 15.8 20.7 24.9 31.9 35.0 39.1 38.8
Y : 4.0 8.0 12.5 16.0 20.0 25.0 31.0 36.0 40.0 40.0
CODE:
> #LINEAR REGRESSION
>
> X=c(4.7,8.2,12.4,15.8,20.7,24.9,31.9,35.0,39.1,38.8)
> Y=c(4.0,8.0,12.5,16.0,20.0,25.0,31.0,36.0,40.0,40.0)
>
> Reg=lm(X~Y)
> Reg
Call:
lm(formula = X ~ Y)
Coe icients:
(Intercept) Y
0.7508 0.9634
Program / Problem No.:4
Write down the R code to obtain the equation of the regression plane of Y on X 1 and X 2
from the following data:
X1 : 30 40 20 50 60 40 20 60
X2 : 11 10 7 15 19 12 8 14
Y : 110 80 70 120 150 90 70 120
CODE:
> #MULTI REGRESSION
>
> X1=c(30,40,20,50,60,40,20,60)
> X2=c(11,10,7,15,19,12,8,14)
> Y=c(110,80,70,120,150,90,70,120)
>
> MultiReg=lm(Y~X1+X2)
> MultiReg
Call:
lm(formula = Y ~ X1 + X2)
Coe icients:
(Intercept) X1 X2
16.8314 -0.2442 7.8488
****
ExperimentNo.: 4:
Q.1Experience has shown that 20% of a manufactured product is of top
quality. In one day’s production of 400 articles, only 50 are of top quality.
Write down the R programming code to test whether the production of the
day chosen is a representative sample at 95% confidence level.
Q.2 Before an increase in excise duty on tea, 800 people out of a sample of
1000 were consumers of tea. After the increase in duty, 800 people were
consumers of tea in a sample of 1200 persons. Write down the R
programming code to test whether the significant decrease in the
consumption of tea after the increase in duty at 1 % level of significance.
Q.3 A sample of 900 items is found to have a mean of 3.47 cm. Write down
the R programming code to test whether it can be reasonably regarded as a
simple sample from a population with mean 3.23 cm and SD 2.31 cm at
99% level of confidence.
Q.4 The average mark scored by 32 boys is 72 with a standard deviation of 8, while that
for 36 girls is 70 with a standard deviation of 6. Write down the R programming code to
test whether the boys are performing better than girls on the basis of average mark at 5
% level of significance.
Experiment No.: 5 Correlation, Rank Correlation, Linear Regression and Multiple
Linear Regression:
Program / Problem No.: 1
A random sample of 10 boys with the following IQs:
70, 120, 110, 101, 88, 83, 95, 98, 107, and 100. Write down the
R programming code to test whether the data support the
assumption of a population mean IQ of 100 at 5 % level of
significance.
Code:
> print("h0 := x0=mu")
[1] "h0 := x0=mu"
> print("h1 := x0!=mu")
[1] "h1 := x0!=mu"
> alpha = 0.05
> mu = 100
> n = 10
> x = c(70,120,110,101,88,83,95,98,107,100)
> x0 = mean(x)
> s = sqrt(var(x))
> ttab = qt((1-alpha),(n-1))
> ttab
[1] 1.833113
> tcal = (x0-mu)/(s/sqrt(n-1))
> tcal
[1] -0.5885024
> if(abs(tcal)<abs(ttab)){
+ print("h0 is accepted and h1 is rejected")
+ }else{
+ print("h0 is rejected and h1 is accepted")
+}
[1] "h0 is accepted and h1 is rejected"
Program / Problem No.: 2
The mean height and the standard deviation height of 8 randomly
chosen soldiers are 166.9 cm and 8.29 cm respectively. The
corresponding values of 6 randomly chosen sailors are 170.3 cm and
8.50 cm respectively. Write down the R programming code to test
whether the soldiers are shorter than the sailors on the basis of
average height.
CODE:
> #2
> print("h0 := x1=x2")
[1] "h0 := x1=x2"
> print("h1 := x1!=x2")
[1] "h1 := x1!=x2"
> alpha = 0.01
> n1 = 8
> n2 = 6
> x1 = 166.9
> x2 = 170.3
> s1 = 8.29
> s2 = 8.50
> sigma = sqrt(((n1*s1^2)+(n2*s2^2))/(n1+n2-2))
> ttab = qt((1-alpha),(n1+n2-2))
> ttab
[1] 2.680998
> tcal = (x1-x2)/(sigma*sqrt((1/n1)+(1/n2)))
> tcal
[1] -0.6954801
> if(abs(tcal)<abs(ttab)){
+ print("h0 is accepted and h1 is rejected")
+ }else{
+ print("h0 is rejected and h1 is accepted")
+}
[1] "h0 is accepted and h1 is rejected"
Program / Problem No.:3
The following data relate to the marks obtained by 11 students
in two sets, one held at the beginning of a year and the other at
the end of the year after intensive coaching. Write down the R
programming code to test whether the data indicate that the
students have benefited by coaching at 5 % level of
significance?
CODE:
> #3
> print("h0 := x1=x2")
[1] "h0 := x1=x2"
> print("h1 := x1!=x2")
[1] "h1 := x1!=x2"
> alpha = 0.05
> s1 = c(19,23,16,24,17,18,20,18,21,19,20)
> s2 = c(17,24,20,24,20,22,20,20,18,22,19)
> d = s1-s2
> D = mean(d)
> n = length(d)
> s = sqrt(var(d))
> ttab = qt((1-alpha),(n-1))
> ttab
[1] 1.812461
> tcal = D/(s/sqrt(n-1))
> tcal
[1] -1.313064
> if(abs(tcal)<abs(ttab)){
+ print("h0 is accepted and h1 is rejected")
+ }else{
+ print("h0 is rejected and h1 is accepted")
+}
[1] "h0 is accepted and h1 is rejected
Two random samples drawn from two normal populations with the
following observations.
Write down the R programming code to test whether the two
populations have the same variance at 5 % level of significance.
CODE:
> #4
> print("h0 := s1=s2")
[1] "h0 := s1=s2"
> print("h1 := s1!=s2")
[1] "h1 := s1!=s2"
> alpha = 0.05
> s1 = c(21,24,25,26,27)
> s2 = c(22,27,28,30,31,36)
> n1 = length(s1)
> n2 = length(s2)
> x1 = mean(s1)
> x2 = mean(s2)
> s1 = sqrt(var(s1))
> s2 = sqrt(var(s2))
> var1 = (n1/(n1-1))*(s1^2)
> var2 = (n2/(n2-1))*(s2^2)
> ftab = qf((1-alpha),(n1-1),(n2-1))
> ftab
[1] 5.192168
> if(var1>var2) { fcal = var1/var2) } else { fcal = var2/var1 }
Error: unexpected ')' in "if(var1>var2) { fcal = var1/var2)"
> if(var1>var2) { fcal = var1/var2 } else { fcal = var2/var1 }
> fcal
[1] 3.912453
> if(abs(fcal)<abs(ftab)){
+ print("h0 is accepted and h1 is rejected")
+ }else{
+ print("h0 is rejected and h1 is accepted")
+}