-
Notifications
You must be signed in to change notification settings - Fork 7
/
chapter3.R
98 lines (77 loc) · 2.15 KB
/
chapter3.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
#### Program 3-1
pop<-(-4):4
sample(pop, size=4, replace=TRUE)
#### Program 3-2
set.seed(1000)
sam<-sample(pop, size=4, replace=TRUE)
mean(sam)
#### Program 3-3
m<-10
xbar.vec<-rep(NA, m)
for(i in 1:m){
set.seed(1000+i)
xx<-sample(pop, size=4, replace=TRUE)
xbar.vec[i]<-mean(xx)
}
table(xbar.vec)
hist(xbar.vec, main="n=4, m=10", xlab=bquote(bar(X)))
mean(xbar.vec)
sd(xbar.vec)
#### Figure 3-2
m<-100
xbar.vec<-rep(NA, m)
for(i in 1:m){
set.seed(1000+i)
xx<-sample(pop, size=4, replace=TRUE)
xbar.vec[i]<-mean(xx)
}
table(xbar.vec)
hist(xbar.vec, main="n=4, m=100", xlab=bquote(bar(X)))
mean(xbar.vec)
sd(xbar.vec)
m<-10000
xbar.vec<-rep(NA, m)
for(i in 1:m){
set.seed(1000+i)
xx<-sample(pop, size=4, replace=TRUE)
xbar.vec[i]<-mean(xx)
}
table(xbar.vec)
hist(xbar.vec, main="n=4, m=10000", xlab=bquote(bar(X)))
mean(xbar.vec)
sd(xbar.vec)
#### Program 3-4
m<-10000
xbar.vec<-rep(NA, m)
for(i in 1:m){
set.seed(1000+i)
xx<-sample(pop, size=40, replace=TRUE)
xbar.vec[i]<-mean(xx)
}
table(xbar.vec)
hist(xbar.vec, main="n=40, m=10000", xlab=bquote(bar(X)), xlim=c(-4, 4))
mean(xbar.vec)
sd(xbar.vec)
#setwd("C:\\Users\\KNOU_stat\\Dropbox\\KNOU_강의개편\\학부_바이오통계학\\R")
#setwd("C:\\Users\\user\\Dropbox\\KNOU_강의개편\\학부_바이오통계학\\R")
#### Program 3-5
setwd("C:/Users/KNOU_stat/R_codes")
dat0<-read.csv("biostat_ex_data.csv")
library(dplyr)
dat1<-dat0 %>% mutate_at(vars(sex, Recur, stage, smoking, obesity, Recur_1y,
post.CA19.9.binary, post.CA19.9.3grp),
as.factor)
mean(dat1$weight)
t.test(dat1$weight)
t.test(dat1$weight, conf.level=0.99)$conf.int
#### Program 3-6
table(dat1$sex)
prop.test(x=sum(dat1$sex==1), n=nrow(dat1))
binom.test(x=sum(dat1$sex==1), n=nrow(dat1))
#### Program 3-7
t.test(dat1$weight, mu=65)
t.test(dat1$weight, mu=68, alternative = "greater")
#### Program 3-8
prop.test(x=sum(dat1$sex==1), n=nrow(dat1), p=0.6)
binom.test(x=sum(dat1$sex==1), n=nrow(dat1), p=0.6)
binom.test(x=sum(dat1$sex==1), n=nrow(dat1), p=0.6, alternative="greater")