# tCImean confidence intervals for the mean

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