-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththesisblocks.R
304 lines (257 loc) · 10.9 KB
/
thesisblocks.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#load data
care=read.table(file="caredata.csv", header=TRUE, sep=",")
names(care)
str(care)
#Packages used
library(nlme)
library(dplyr)
#packages gotten from last years project
library(lattice)
install.packages("lme4")
library(lme4)
install.packages("geepack")
library(geepack)
install.packages("lsmeans")
library(lsmeans)
install.packages("dplyr")
library(dplyr)
install.packages("MuMIn")
library(MuMIn)
install.packages("ggplot2")
library(ggplot2)
install.packages("nortest")
library(nortest)
#Putting the 4 condition in as a factor
care$cond.f=as.factor(care$cond)
str(care)
#Making a subset only consistent of integers/numbers
xx=care[,sapply(care,is.integer)] #this is to make a subset only of the integers (numbers)
xx
#Adding counts and duration of the 3 behavioral type (positive, negative and neutral)
care$positivect=rowSums(care[,c("foodgrunt", "grunt","lipsmack","pbaboon", "phuman","hgroomct","bgroomct","ghgroomct","gbgroomct","penisd", "playb", "plays","manenrichct","embraceh", "embraceb", "dproxct","dproxout", "movetoobs","playvocal")], na.rm=T)
care$positivect
care$positived=rowSums(care[,c("hgroomd","bgroomd","ghgroomd","gbgroomd","manenrichd")], na.rm=T)
care$positived
care$negativect=rowSums(care[,c("iproxct", "iproxout","threatg", "bark", "headshake", "yawn", "aggb", "pace", "aggdisp", "teeth", "rubgen", "sway", "turn", "scream")], na.rm=T)
care$negativect
care$neutralct=rowSums(care[,c("sgroomct", "eatct","drink","foragect", "restct","schratch", "other")], na.rm=T)
care$neutralct
care$neutrald=rowSums(care[,c("sgroomd","eatd","foraged","restd")], na.rm=T )
care$neutrald
#The numbers are not showing for the positive and negative counts
#Neutral Count behaviors
#######################################################################
#model one without random factor
m1=gls(neutralct~cond.f, data=care, na.action=na.omit, method="ML")
summary(m1)
#model2 - try put individual in as a main factor so cond.f+id
m2=lme(neutralct~cond.f, random=~1|id,data=care, na.action=na.omit, method="ML")
summary(m2)
anova(m1,m2)
#Since the model with the random factor had a lower IAC score we wanna use that
#Plotting residuals for m2 to check if we can use this model
op=par(mfrow=c(2,2), mar=c(5,4,1,2))
plot(m2, add.smooth=FALSE, which=1)
E=resid(m2)
hist(E,xlab="residuals", main="")
plot(care$cond.f, E, xlab="Treatment", ylab="residuals")
plot(care$id, E, xlab="id", ylab="residuals")
#trying the other residuals plot clay showed
E1<-residuals(m2)
plot(filter(care, !is.na(neutralct)) %>% dplyr::select(id),
E1, xlab="id", ylab="Residuals")
plot(filter(care, !is.na(neutralct)) %>% dplyr::select(cond.f),
E1, xlab="id", ylab="Residuals")
qqnorm(residuals(m2))
qqline(residuals(m2))
ad.test(residuals(m2))#this one says error
summary(m2)
lsmeans(m2,pairwise~cond.f)#This one schould be able to show how the behaviors have changes
#There are no significant changes in the overal count of neutral behaviors between the 4 condition.
#making a table that will show the means per individual that can be graphed not sure it works
x1 <- group_by(care, cond.f, id) %>%
summarize(m.neutralct = mean(neutralct, na.rm = TRUE), # na.rm = TRUE to remove missing values
s.neutralct=sd(neutralct, na.rm = TRUE), # na.rm = TRUE to remove missing values
n = sum(!is.na(neutralct)), # of observations, excluding NAs.
se.neutralct=s.neutralct/sqrt(n))
x1
#Trying to plot the amount of neutral counts there are for each individual
ggplot(data=x1,
aes(x=cond.f, y=m.neutralct, fill=id, label=m.neutralct)) +
geom_bar(stat="identity", position=position_dodge(), color = "black") +
geom_errorbar(aes(ymin=m.neutralct, ymax=m.neutralct+se.neutralct), width=0.2,
position=position_dodge(0.9)) +
scale_fill_manual(values=c("black","white", "light grey", "dark grey")) +
xlab("ID") +
ylab("Number of neutral behaviors") +
ylim(0,15) +
labs(fill="id") +
theme_bw() +
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
legend.title=element_text(size=6),
legend.key=element_blank(),
legend.position=c(0.5,0.95),
legend.text=element_text(size=8),
legend.background=element_blank(),
legend.direction="horizontal",
legend.key.size=unit(0.3, "cm"),
axis.title.y=element_text(size=8),
axis.title.x=element_text(size=8),
axis.text.x=element_text(size=8))
"Neutral duration behaviors
#################################################################################
ndm1=gls(neutrald~cond.f, data=care, na.action=na.omit, method="ML")
summary(ndm1)
#model2 - try put individual in as a main factor so cond.f+id
ndm2=lme(neutrald~cond.f, random=~1|id,data=care, na.action=na.omit, method="ML")
summary(ndm2)
anova(ndm1,ndm2)
#Since the model with the random factor had a lower IAC score we wanna use that
#Plotting residuals for ndm2 to check if we can use this model
op=par(mfrow=c(2,2), mar=c(5,4,1,2))
plot(ndm2, add.smooth=FALSE, which=1)
E=resid(ndm2)
hist(E,xlab="residuals", main="")
plot(care$cond.f, E, xlab="Treatment", ylab="residuals")
plot(care$id, E, xlab="id", ylab="residuals")
#trying the other residuals plot clay showed
ndE1<-residuals(ndm2)
plot(filter(care, !is.na(neutrald)) %>% dplyr::select(id),
ndE1, xlab="id", ylab="Residuals")
plot(filter(care, !is.na(neutrald)) %>% dplyr::select(cond.f),
ndE1, xlab="id", ylab="Residuals")
qqnorm(residuals(ndm2))
qqline(residuals(ndm2))
ad.test(residuals(ndm2))#this one says error
summary(ndm1)
lsmeans(ndm2,pairwise~cond.f)#This one schould be able to show how the behaviors have changes
#There are no significant changes in the overal duration of neutral behaviors between the 4 condition.
#making a table that will show the means per individual that can be graphed not sure it works
ndx1 <- group_by(care, cond.f, id) %>%
summarize(m.neutrald = mean(neutrald, na.rm = TRUE), # na.rm = TRUE to remove missing values
s.neutrald=sd(neutrald, na.rm = TRUE), # na.rm = TRUE to remove missing values
n = sum(!is.na(neutrald)), # of observations, excluding NAs.
se.neutrald=s.neutrald/sqrt(n))
ndx1
#Trying to plot the amount of neutral durations there are for each individual it shows up blanc
ggplot(data=ndx1,
aes(x=cond.f, y=m.neutrald, fill=id, label=m.neutrald)) +
geom_bar(stat="identity", position=position_dodge(), color = "black") +
geom_errorbar(aes(ymin=m.neutrald, ymax=m.neutrald+se.neutrald), width=0.2,
position=position_dodge(0.9)) +
scale_fill_manual(values=c("black","white", "light grey", "dark grey")) +
xlab("ID") +
ylab("Duration of neutral behaviors") +
ylim(0,15) +
labs(fill="id") +
theme_bw() +
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
legend.title=element_text(size=6),
legend.key=element_blank(),
legend.position=c(0.5,0.95),
legend.text=element_text(size=8),
legend.background=element_blank(),
legend.direction="horizontal",
legend.key.size=unit(0.3, "cm"),
axis.title.y=element_text(size=8),
axis.title.x=element_text(size=8),
axis.text.x=element_text(size=8))
#positive duration behaviors
#model one without random factor
pdm1=gls(positived~cond.f, data=care, na.action=na.omit, method="ML")
summary(pdm1)
#model2 - try put individual in as a main factor so cond.f+id
pdm2=lme(positived~cond.f, random=~1|id,data=care, na.action=na.omit, method="ML")
summary(pdm2)
anova(pdm1,pdm2)
#Since the model with the random factor had a lower IAC score we wanna use that
#Plotting residuals for m2 to check if we can use this model
op=par(mfrow=c(2,2), mar=c(5,4,1,2))
plot(pdm2, add.smooth=FALSE, which=1)
E=resid(m2)
hist(E,xlab="residuals", main="")
plot(care$cond.f, E, xlab="Treatment", ylab="residuals")
plot(care$id, E, xlab="id", ylab="residuals")
#trying the other residuals plot clay showed
E1<-residuals(pdm2)
plot(filter(care, !is.na(neutrald)) %>% dplyr::select(id),
E1, xlab="id", ylab="Residuals")
plot(filter(care, !is.na(neutrald)) %>% dplyr::select(cond.f),
E1, xlab="id", ylab="Residuals")
#These did not work
qqnorm(residuals(pdm2))
qqline(residuals(m2))
ad.test(residuals(pdm2))#this one says error
summary(pdm2)
lsmeans(pdm2,pairwise~cond.f)#This one schould be able to show how the behaviors have changes
#There are no significant changes in the overal count of neutral behaviors between the 4 condition.
x1 <- group_by(care, cond.f, id) %>%
summarize(m.positived = mean(positived, na.rm = TRUE), # na.rm = TRUE to remove missing values
s.positived=sd(positived, na.rm = TRUE), # na.rm = TRUE to remove missing values
n = sum(!is.na(positived)), # of observations, excluding NAs.
se.positived=s.positived/sqrt(n))
x1
#Trying to plot the amount of neutral counts there are for each individual
ggplot(data=x1,
aes(x=cond.f, y=m.positived, fill=id, label=m.positived)) +
geom_bar(stat="identity", position=position_dodge(), color = "black") +
geom_errorbar(aes(ymin=m.positived, ymax=m.positived+se.positived), width=0.2,
position=position_dodge(0.9)) +
scale_fill_manual(values=c("black","white", "light grey", "dark grey")) +
xlab("ID") +
ylab("Number of neutral behaviors") +
ylim(0,15) +
labs(fill="id") +
theme_bw() +
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
legend.title=element_text(size=6),
legend.key=element_blank(),
legend.position=c(0.5,0.95),
legend.text=element_text(size=8),
legend.background=element_blank(),
legend.direction="horizontal",
legend.key.size=unit(0.3, "cm"),
axis.title.y=element_text(size=8),
axis.title.x=element_text(size=8),
axis.text.x=element_text(size=8))
#also does not show anything helpsfull
###################################################################################################################################
#example
out1=gls(lipsmack~cond.f, data=care, na.action=na.omit, method="ML")
summary(out1)
#example2 - try put individual in as a main factor so cond.f+id
out2=lme(lipsmack~cond.f, random=~1|id,data=care, na.action=na.omit, method="ML")
summary(out2)
anova(out2)
names(xx)
anova(out1,out2) #we wanna use the random factor its better
#notes from meetings with clay
#this line might now be useful
care$test
cor(care, use="pairwise.complete.obs", method="kendall")
cor(care$eatd, y=care$foraged)
cor(care, use="everything")
#adding ct or duration total for negative positive or neutral
care$test=rowSums(care[,c("eatct", "foragect","restct")])
care$test
#looking at residuals
E1<-residuals(out2)
plot(filter(care, !is.na(lipsmack)) %>% dplyr::select(id),
E1, xlab="id", ylab="Residuals")
plot(filter(care, !is.na(lipsmack)) %>% dplyr::select(cond.f),
E1, xlab="id", ylab="Residuals")
#couple bad outliers
qqnorm(residuals(out2))
qqline(residuals(out2))
ad.test(residuals(out2))
plot(out2)
#residuals increase with model fit
x<-care$lipsmack[!is.na(care$lipsmack)]#removes na values from column
E1<-residuals(out2,type="normalized")
plot(x, E1)
care.integer=data.matrix(xx, rownames.force = NA)
cor(xx, use="everything")
#new code