-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathassignment2_exercise3_2.Rmd
More file actions
201 lines (165 loc) · 6.47 KB
/
Copy pathassignment2_exercise3_2.Rmd
File metadata and controls
201 lines (165 loc) · 6.47 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
---
title: "Assignment 1"
author: 'Group 67: Jamie Faber, Laurens Prast, Amal Salman'
date: "27 February 2022"
output:
html_document:
df_print: paged
pdf_document: default
highlight: tango
---
```{r include = FALSE}
options(digits=9)
options(knitr.duplicate.label = "allow")
```
## Imports
```{r}
library(ggplot2)
library(car)
```
## Exercise 3
### a)
```{r}
fruitflies=read.table(file="fruitflies.txt",header=TRUE)
# add loglongevity column
fruitflies$loglongevity <- log(fruitflies$longevity)
fruitflies
```
**Informative plot of the data:**
```{r}
ggplot(fruitflies) +
geom_point(aes(x=thorax, y=loglongevity, color=activity))
ggplot(fruitflies) +
geom_boxplot(aes(x = thorax, y=loglongevity, fill = activity))
```
**One-way Anova:**
```{r}
aov=lm(loglongevity~activity,data=fruitflies)
anova(aov)
```
p-value < 0.05 so there is significant differences between the activity groups, i.e., activity influences longevity.
Checking assumptions:
```{r}
par(mfrow=c(1,2))
qqnorm(residuals(aov)); qqline(residuals(aov))
plot(fitted(aov),residuals(aov), main = "Residual Plot")
```
```{r}
leveneTest(loglongevity~activity,data=fruitflies)
shapiro.test(residuals(aov))
```
Levene's test passed, Shapiro didn't.
From the plots and tests, the assumption of equal variances is met, but we cannot assume normality, therefore the requirements for the one-way Anova test aren't met.
Therefore, need to use Kruskal-Wallis test instead.
**Kruskal-Wallis:**
```{r}
kruskal.test(loglongevity~activity,data=fruitflies)
```
However, Kruskal-Wallis test comes at the same conclusion as the one-way Anova test:
p-value < 0.05 so there is significant differences between the activity groups, i.e., activity influences longevity.
**Estimated longevities for the three conditions:**
```{r}
summary(aov)
```
The high activity group has an estimated 3.60 longevity, while the isolated activity group has 0.52, and the low activity group has 0.40. This indicates that the higher the activity, the longer the longevity is.
```{r}
high <- 3.6021243;exp(high)
isolated <- high+0.5172246;exp(isolated)
low <- high+0.3977116;exp(low)
```
### b)
**ANCOVA test:**
```{r}
fruitflies$activity=as.factor(fruitflies$activity)
fruitflies1=lm(loglongevity~thorax+activity,data=fruitflies)
fruitflies1
fruitflies2=lm(loglongevity~thorax,data=fruitflies)
anova(fruitflies2,fruitflies1)
```
p-value < 0.05 so again activity has a significant influence on longevity (now considering thorax but not the interaction between thorax and activity).
```{r}
par(mfrow=c(1,2))
qqnorm(residuals(fruitflies1)); qqline(residuals(fruitflies1))
plot(fitted(fruitflies1),residuals(fruitflies1))
```
```{r}
shapiro.test(residuals(fruitflies1))
```
Passed the normality test so meets all assumptions for ANCOVA.
```{r}
summary(fruitflies1)
```
Does sexual activity increase or decrease longevity?
The estimates for all three types of activity are >0 and increase from low to isolated to high, and so activity increases longevity.
```{r}
fruitflies1=lm(loglongevity~thorax+activity,data=fruitflies)
max_thorax = max(fruitflies$thorax)
min_thorax = min(fruitflies$thorax)
exp(predict(fruitflies1,data.frame(thorax = min_thorax, activity = "high"), interval = "confidence"))
exp(predict(fruitflies1,data.frame(thorax = min_thorax, activity = "isolated"), interval = "confidence"))
exp(predict(fruitflies1,data.frame(thorax = min_thorax, activity = "low"), interval = "confidence"))
exp(predict(fruitflies1,data.frame(thorax = max_thorax, activity = "high"), interval = "confidence"))
exp(predict(fruitflies1,data.frame(thorax = max_thorax, activity = "isolated"), interval = "confidence"))
exp(predict(fruitflies1,data.frame(thorax = max_thorax, activity = "low"), interval = "confidence"))
```
What are the estimated longevities for the three groups, for flies with the minimal and maximal thorax lengths?
For flies with minimal thorax:
high: 17.5, isolated: 37.6, low: 30.6
For flies with maximal thorax:
high: 57.3, isolated: 77.4, low: 70.3
### c)
```{r}
colors <- c("#00AFBB", "#E7B800", "#FC4E07")
colors <- colors[as.numeric(fruitflies$activity)]
plot(loglongevity~thorax, col=colors, pch=16, data=fruitflies)
legend("topleft", legend = levels(fruitflies$activity),
col = c("#00AFBB", "#E7B800", "#FC4E07"), pch = 16, title="activity")
```
*whether this dependence is similar under all three conditions of sexual activity.*
```{r}
plot(longevity~thorax,pch=unclass(activity), data=fruitflies)
for (i in c("high", "isolated", "low")) abline(lm(longevity~thorax,data=fruitflies[fruitflies$activity==i,]))
```
Plot shows no indication that the true lines would not be parallel.
```{r}
fruitflies3=lm(loglongevity~activity*thorax,data=fruitflies)
anova(fruitflies3)
```
p-value=0.15 >0.05 so there is no interaction between factor activity and predictor thorax.
```{r}
summary(fruitflies3)
```
activityisolated:thorax & activitylow:thorax p-value > 0.05, again confirming that there is no interaction/dependence between activity and thorax.
### d)
Both analyses (the Kruskal-Wallis and ANCOVA tests) came to the same conclusion, that activity significantly influences longevity, and the higher the activity the longer the longevity. Neither of the analyses is wrong.
By including thorax length in the analysis, we found out that thorax also significantly influences longevity, and the higher the thorax length, the longer the longevity. So I prefer the analysis with thorax length because it gives a more comprehensive analysis of influences in longevity.
### e)
```{r}
fruitflies$activity=as.factor(fruitflies$activity)
fruitflies1=lm(longevity~thorax+activity,data=fruitflies)
fruitflies2=lm(longevity~thorax,data=fruitflies)
anova(fruitflies2,fruitflies1)
```
p-value still < 0.05
```{r}
par(mfrow=c(1,2))
qqnorm(residuals(fruitflies1)); qqline(residuals(fruitflies1))
plot(fitted(fruitflies1),residuals(fruitflies1))
```
```{r}
shapiro.test(residuals(fruitflies1))
```
```{r}
par(mfrow=c(2,1))
hist(fruitflies$loglongevity)
hist(fruitflies$longevity)
```
```{r}
par(mfrow=c(1,2))
fruitflies1=lm(longevity~thorax+activity,data=fruitflies)
plot(fitted(fruitflies1),residuals(fruitflies1))
fruitflieslog=lm(loglongevity~thorax+activity,data=fruitflies)
plot(fitted(fruitflieslog),residuals(fruitflieslog))
```
The histograms and the Shapiro tests confirm that longevity is more normal than loglongevity, and since the ANCOVA analysis relies on normality, it was not wise to use the logarithm as response.
However the fitted residuals plots show the loglongevity have a beter spread, more homogeneity than the longevity plot