-
Notifications
You must be signed in to change notification settings - Fork 0
/
11. inferential_statistics.R
105 lines (77 loc) · 1.69 KB
/
11. inferential_statistics.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#Normal Distribution
x = rnorm(500,0,1)
plot(density(x))
#Z-Score
#Pria dengan tinggi lebih dari 175cm
#sd = 7cm, mean = 158
z <- (175-158) / 7
p <- pnorm(z, lower.tail=F)
p
#Wanita dengan tinggi antara 142cm dan 152cm
#sd = 5cm, mean = 147
z <- (152-147) / 5
z2 <- (142-147) / 5
p <- pnorm(z) - pnorm(z2)
p
#Confidence intervals
dataset <- read.csv("../datasets/nhanes.csv")
dataset.male <- dataset[dataset$Gender=="male",]
dataset.female <- dataset[dataset$Gender=="female",]
m <- mean(dataset.male$Height)
standard.deviation <- sd(dataset.male$Height)
n <- nrow(dataset.male)
#Confidence interval 95% - alpha = 5%
SE <- qnorm(0.0025) * standard.deviation / sqrt(n)
batas.bawah <- m + SE #174.9cm
batas.atas <- m - SE #175.8cm
#50 samples
dataset.male2 <- dataset.male[1:50,]
m2 <- mean(dataset.male2$Height)
standard.deviation2 <- sd(dataset.male2$Height)
n2 <- nrow(dataset.male2)
#Confidence interval 95% - alpha = 5%
SE2 <- qnorm(0.0025) * standard.deviation2 / sqrt(n2)
batas.bawah2 <- m2 + SE2 #171.9cm
batas.atas2 <- m2 - SE2 #177.3cm
#Hypothesis Testing
mu <- 165
xbar <- 160
sdev <- 7
n3 <- 30
z <- (xbar - mu) / (sdev / sqrt(n3))
z
SE3 <- qnorm(0.025) * sdev / sqrt(n3)
batas.bawah3 <- xbar + SE3
batas.atas3 <- xbar - SE3
qnorm(0.025)
qnorm(0.975)
# z > qnorm(0.975). Null hypothesis ditolak
pnorm(z) * 2
#pvalue 0.015
?t.test
#bohlam
xbar = 9900
mu = 10000
sigma = 120
n = 30
z <- (xbar-mu) / (sigma / sqrt(n))
z
alpha = 0.05
z.alpha = qnorm(1-alpha)
-z.alpha
pval = pnorm(z)
pval
#t-test
xbar = 9900
mu0 = 10000
s = 125
n = 30
t = (xbar-mu0) / (s/sqrt(n))
t
alpha = 0.05
t.alpha = qt(1-alpha, df=n-1)
-t.alpha
pval = pt(t,df=n-1)
pval
#t-test
t.test(dataset.male[1:30,]$Height, mu=172)