##### 医学統計勉強会25　第3回　連続変数の比較　R code #####

# 一標本問題 #
x <- rnorm(100, mean=2, sd=1)	# 期待値2，標準偏差1の正規分布 #

hist(x)		# histogram #

t.test(x)		# Student's t-test : two-sided test
t.test(x, mu=1.5, alternative="greater") # Student's t-test : one-sided (right-sided) test

wilcox.test(x)	# Wilcoxon signed rank test
wilcox.test(x, mu=1.5, alternative="greater")	# Wilcoxon signed rank test : one-sided (right-sided) test

# 二標本問題 ##
x1 <- c(8, 17, 9, 5, 14, 13, 13, 3, 9, 0)
x2 <- c(19, 13, 13, 18, 21, 18, 11, 21, 20, 11)

boxplot(x1, x2)

t.test(x1, x2)		# Welch's t-test
wilcox.test(x1, x2)	# Mann-Whitney test, Wilcoxon's rank sum test


## 分散分析 ##

# サンプルデータ #
Data <- data.frame(
	z = c(8, 16, 7, 18, 6, 11, 15, 11, 20, 7, 19, 20, 21, 19, 24), 
	group = factor(c(rep(1,5), rep(2,5), rep(3,5)), label=c("placebo", "drugA", "drugB")))
print(Data)
boxplot(z ~ group, data=Data)

# One-way ANOVA #
fit.aov <- aov(z ~ group, data=Data)
summary(fit.aov)

# One-way ANOVA (not assuming equal variances) #
oneway.test(z ~ group, data=Data)

# Kruskal-Wallis rank sum test #
kruskal.test(z ~ group, data=Data)
