In: Statistics and Probability
Develop two functions in R, one that generates s^2 using the formation definition, and one that calculates s^2 based on the hand calculation equation. Generate data from a normal distribution. Use your functions to calculate s^2 for your generated data. Try this for various variances so that your data become closer and closer to the mean (less variance). What do you find, comment.
#Here we create two functions forsample variance S^2 is s.2() and and population variance sigma2()
#The R code is
s.2=function (X){
sumx=0
sumsqx=0
n=length(X)
for(i in 1:n){
sumx=sumx+X[i]
sumsqx=sumsqx+(X[i]*X[i])
}
mx=sumx/n
S.2=((sumsqx/n)-mx^2)*(n/(n-1))
print(S.2)
}
sigma2=function (X){
sumx=0
sumsqx=0
n=length(X)
for(i in 1:n){
sumx=sumx+X[i]
sumsqx=sumsqx+(X[i]*X[i])
}
mx=sumx/n
S.2=((sumsqx/n)-mx^2)
print(S.2)
}
#we take mean is 3 and Variance are
var=seq(0.1,1,0.1)
var
[1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
for( sd in sqrt(var)){
X=rnorm(100,3,sd)
cat("Mean is",mean(X),"S^2is",s.2(X),"Sigma^2",sigma2(X),"\n")
}
] 0.0957932 [1] 0.09483527 Mean is 2.992832 S^2is 0.0957932 Sigma^2 0.09483527 [1] 0.2243562 [1] 0.2221126 Mean is 2.996699 S^2is 0.2243562 Sigma^2 0.2221126 [1] 0.2326334 [1] 0.2303071 Mean is 2.992128 S^2is 0.2326334 Sigma^2 0.2303071 [1] 0.4644581 [1] 0.4598135 Mean is 3.018703 S^2is 0.4644581 Sigma^2 0.4598135 [1] 0.5027912 [1] 0.4977633 Mean is 2.926998 S^2is 0.5027912 Sigma^2 0.4977633 [1] 0.547224 [1] 0.5417517 Mean is 2.819986 S^2is 0.547224 Sigma^2 0.5417517 [1] 0.6746021 [1] 0.6678561 Mean is 2.979211 S^2is 0.6746021 Sigma^2 0.6678561 [1] 0.9897457 [1] 0.9798482 Mean is 2.831677 S^2is 0.9897457 Sigma^2 0.9798482 [1] 1.08549 [1] 1.074635 Mean is 2.920762 S^2is 1.08549 Sigma^2 1.074635 [1] 0.9912502 [1] 0.9813377 Mean is 3.04267 S^2is 0.9912502 Sigma^2 0.9813377
#here the mean is closure to 3 and Variance is closure to various value,for generated sample with parameter mean is 1 and Variance is 1 the sample will show mean is approximately 3 and by using function S^2 i.e. s.2 is approximately 1 than sigma2 function
Here s.2 is a sample mean square which is an unbiased estimator of population variance.The difference from S^2 to Population varaince is minimum than Sigma2 which is biased estimator of population varaince
i.e.sample mean square S^2 show low variation from population variance