-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathanova.R
164 lines (121 loc) · 3.5 KB
/
anova.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#ANOVA
## Dataset import
install.packages("palmerpenguins")
library(palmerpenguins)
df <- penguins
colnames(df)
# find unique values
unique(df$species)
unique(df$island)
#t.test laga k dikhayen
t.test(df$bill_length_mm~df$sex)
library(tidyverse)
ggplot(df, aes(sex, bill_length_mm, fill=sex))+geom_boxplot()
data <- df %>%
select(species, flipper_length_mm) %>%
drop_na()
#summary
summary(data)
data %>%
group_by(species) %>%
summarize(mean(flipper_length_mm))
ggplot(data) +
aes(species, flipper_length_mm, col=species)+
geom_jitter() + theme(legend.position = "none")
# normal disrtribution
data %>%
group_by(species) %>%
summarize(shapiro_pvalue = shapiro.test(flipper_length_mm)$p.value)
s_w <- shapiro.test(data$flipper_length_mm)
summary(s_w)
# anova
#normal
res_aov <- aov(flipper_length_mm ~ species, data = data)
summary(res_aov)
hist(res_aov$residuals)
qqnorm(res_aov$residuals)
shapiro.test(res_aov$residuals)
#homogeneous
install.packages("car")
library(car)
leveneTest(flipper_length_mm ~ species, data= data)
install.packages("lattice")
library(lattice)
dotplot(flipper_length_mm ~species, data = data)
ggplot(data) +
aes(species, flipper_length_mm, fill=species)+
geom_boxplot()
# calculate mean and sd for plots and AOV figures
aggregate(flipper_length_mm ~ species, data=data,
function(x) round(c(mean = mean(x), sd =sd(x)), 2))
# another way
df1<- group_by(data, species) %>%
summarise(
mean = mean(flipper_length_mm, na.rm = TRUE),
sd = sd(flipper_length_mm, na.rm = TRUE)
)
df1
# ANOVA aik tareeqa
oneway.test(flipper_length_mm ~ species, data= data, var.equal = TRUE)
oneway.test(flipper_length_mm ~ species, data= data, var.equal = FALSE)
# ANOVA doosra tareeqa
res_aov <- aov(flipper_length_mm ~ species, data = data)
summary(res_aov)
# postHoc Test
install.packages("multcomp", dependencies = TRUE)
library(multcomp)
#TukeyHSD
res_aov <- aov(flipper_length_mm ~ species, data = data)
summary(res_aov)
post_test <- glht(res_aov,
linfct = mcp(species = "Tukey")
)
summary(post_test)
plot(post_test)
plot(TukeyHSD(res_aov))
#Dunnet test
res_aov <- aov(flipper_length_mm ~ species, data = data)
summary(res_aov)
dun <- glht(res_aov,
linfct = mcp(species = "Dunnet"))
summary(dun)
# Two way ANOVA
library(ggthemes)
library(multcompView)
library(tidyverse)
df <- ToothGrowth
df$dose <- as.factor(df$dose)
# calculate two way anova
anova <- aov(len ~ supp*dose, data= df)
summary(anova)
# tukey hsd
tukey <- TukeyHSD(anova)
tukey
# lettering nikaltay
letters <- multcompLetters4(anova, tukey)
letters
# convert them into a df
letters <- data.frame(letters$`supp:dose`$Letters)
letters
# mean and Sd
df_mean <- df %>%
group_by(supp, dose) %>%
summarise(len_mean=mean(len), sd = sd(len)) %>%
arrange(desc(len_mean))
tibble(df_mean)
# letters
df_mean$letters <- letters$letters..supp.dose..Letters
df_mean
#draw
p <- ggplot(df_mean)+
aes(dose, len_mean, group=supp, fill=supp) +
geom_bar(stat = 'identity', position = position_dodge(0.9)) +
geom_errorbar(aes(ymin=len_mean-sd, ymax=len_mean+sd), width =0.1,
position = position_dodge(0.9)) +
geom_text(aes(label=letters, y=len_mean+sd), vjust = -0.2,
position = position_dodge(0.9)) +
scale_fill_manual(values=c("blue", "red"))+
labs(x="Dose", y="Length (mm)",
title="Tooth Growth Publication Ready Plot",
fill= "Supplement");p
ggsave("Plot.pdf", p, width = 10, height = 8, units = "in")