# zCImean confidence interval

Sep 1st, 2014
274
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1.
2.     # Hitung zCImean confidence interval
3.     # Confidence Interval calculation, Normal distribution (Z)
4.     #
6.     # Samarinda, Indonesia, Agustus 2014
7.     #
8.     # Creative Commons Attribution-ShareAlike 4.0
9.     #
10.     # Usage (contoh penggunaan):
11.     # zCImean(95,10,2,33)
12.     #
13.     #
14.     # CI = confidence interval (arbitrary, usually, 90, 95, or 99)
15.     # xbar1 = point estimate of population mean by sample mean
16.     # sigma = population standard deviation
17.     # n = Jumlah sample (sample size)
18.     #
19.     #
20.     #
21.     #
22.
23.     zCImean <- function(CI, xbar, sigma, n){
24.
25.     alpha = CI/100
26.
27.     two.tail.alpha <- alpha/2
28.     two.tail.alpha <- 1-alpha
29.     one.tail.alpha <- two.tail.alpha/2
30.
31.     left.tail.alpha <- one.tail.alpha
32.     right.tail.alpha <- 1-one.tail.alpha
33.
34.     left.z.value <- qnorm(left.tail.alpha)
35.     right.z.value <- qnorm(right.tail.alpha)
36.
37.     left.val <- xbar - ( sqrt(left.z.value^2) * ( sigma/sqrt(n) ) )
38.     right.val <- xbar + ( right.z.value * ( sigma/sqrt(n) ) )
39.     difr <- ( right.z.value * ( sigma/sqrt(n) ) )
40.
41.     cat("\n")
42.     cat( sprintf("CI, xbar, sigma, n" ),fill = TRUE )
43.     cat("\n")
44.
45.
46.     cat( sprintf("%-33s %1s % 5.3f  ", "alpha", ":", alpha ),fill = TRUE )
47.     cat( sprintf("%-33s %1s % 5.3f  ", "two.tail.alpha", ":", two.tail.alpha ),fill = TRUE )
48.     cat( sprintf("%-33s %1s % 5.3f  ", "one.tail.alpha", ":", one.tail.alpha ),fill = TRUE )
49.     cat("\n")
50.     cat( sprintf("%-33s %1s % 5.6g  ", "left.z.value", ":", left.z.value          ),fill = TRUE )
51.     cat( sprintf("%-33s %1s % 5.6g  ", "left.z.value", ":", round(left.z.value,2) ),fill = TRUE )
52.     cat("\n")
53.     cat( sprintf("%-33s %1s % 5.6g  ", "right.z.value", ":", right.z.value         ),fill = TRUE )
54.     cat( sprintf("%-33s %1s % 5.6g  ", "right.z.value", ":", round(right.z.value,2)),fill = TRUE )
55.     cat("\n")
56.     cat( sprintf("%-33s %1s % 4.6g  ", "differ val", ":", round(difr,1)),fill = TRUE )
57.     cat("\n")
58.     cat( sprintf("%-33s %1s % 5.6g %12s %-5.4g %-2s ", "left.val",  ":", left.val,  "   [rounded: ", round(left.val,1), "]"  ),fill = TRUE )
59.     cat( sprintf("%-33s %1s % 5.6g  ", "Point estimate of population mean", ":", xbar ),fill = TRUE )
60.     cat( sprintf("%-33s %1s % 5.6g %12s %-5.4g %-2s ", "right.val", ":", right.val, "   [rounded: ", round(right.val,1),"]" ),fill = TRUE )
61.     cat("\n")
62.     }