-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraft 1.R
306 lines (243 loc) · 9.95 KB
/
draft 1.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
library(readr)
library(ggplot2)
library(dplyr)
library(tidyverse)
library(knitr)
library(grid)
library(gridExtra)
getwd()
ccbug <- read.csv("~/Downloads/ccbug.csv", stringsAsFactors = FALSE)
#the columns are being read as characters
##if you didnt use 'stringsasfactors = false', convert the column into characters first and then into numeric
str(ccbug)
#converting the required columns into numeric form so as to make them available to plot
ccbug[ ,5:11] <- sapply(ccbug[ ,5:11], as.numeric)
ccbug1 <- ccbug %>% gather( key = "mean",
value = "Densities",
madd, mn1d, mn2d, mn3d, mn4d, mn5d, mned)
ccbug1[ ,13] <- as.numeric(ccbug1[ ,13])
ccbug1 <- subset(ccbug1, Densities < 20)
#Subsetting data to just contain "madd"
ccbug1 <- subset(ccbug1 , mean == "madd")
#Checking the number of entries
#ccbug1$Densities[ccbug1$Densities < 20]
#Function to separate the string in month and assigning a numeric value to each month
date2month <- function(xstr){
months <- c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
ans <- NA
for (i in 1:12){
if (months[i] %in% strsplit(xstr,"-")[[1]]){
ans <- i
break
}
}
ans
}
ccbug1$month <- sapply(ccbug1$date,date2month)
#######################Plotting for all patches to find out if the pattern for madd changes over years
my.year <- 1994
ccbug_my.year <- subset(ccbug1, year == my.year)
plot1 <- ggplot( ccbug_my.year, aes (x = month,
y = Densities,
group = patch,
colour = factor(patch)
)) +
geom_point(size = 0.0001) +
stat_smooth(se = FALSE) +
xlab("months (1994)")
my.year <- 1995
ccbug_my.year <- subset(ccbug1, year == my.year)
plot2 <- ggplot( ccbug_my.year, aes (x = month,
y = Densities,
group = patch,
colour = factor(patch)
)) +
geom_point(size = 0.0001) +
stat_smooth(se = FALSE) +
xlab("months (1995)")
my.year <- 1996
ccbug_my.year <- subset(ccbug1, year == my.year)
plot3 <- ggplot( ccbug_my.year, aes (x = month,
y = Densities,
group = patch,
colour = factor(patch)
)) +
geom_point(size = 0.0001) +
stat_smooth(se = FALSE) +
xlab("months (1996)")
my.year <- 1997
ccbug_my.year <- subset(ccbug1, year == my.year)
plot4 <- ggplot( ccbug_my.year, aes (x = month,
y = Densities,
group = patch,
colour = factor(patch)
)) +
geom_point(size = 0.0001) +
stat_smooth(se = FALSE) +
xlab("months (1997)")
my.year <- 1998
ccbug_my.year <- subset(ccbug1, year == my.year)
plot5 <- ggplot( ccbug_my.year, aes (x = month,
y = Densities,
group = patch,
colour = factor(patch)
)) +
geom_point(size = 0.0001) +
stat_smooth(se = FALSE) +
xlab("months (1998)")
my.year <- 1999
ccbug_my.year <- subset(ccbug1, year == my.year)
plot6 <- ggplot( ccbug_my.year, aes (x = month,
y = Densities,
group = patch,
colour = factor(patch)
)) +
geom_point(size = 0.0001) +
stat_smooth(se = FALSE) +
xlab("months (1999)")
my.year <- 2000
ccbug_my.year <- subset(ccbug1, year == my.year)
plot7 <- ggplot( ccbug_my.year, aes (x = month,
y = Densities,
group = patch,
colour = factor(patch)
)) +
geom_point(size = 0.0001) +
stat_smooth(se = FALSE) +
xlab("months (2000)")
my.year <- 2001
ccbug_my.year <- subset(ccbug1, year == my.year)
plot8 <- ggplot( ccbug_my.year, aes (x = month,
y = Densities,
group = patch,
colour = factor(patch)
)) +
geom_point(size = 0.0001) +
stat_smooth(se = FALSE) +
xlab("months (2001)")
my.year <- 2002
ccbug_my.year <- subset(ccbug1, year == my.year)
plot9 <- ggplot( ccbug_my.year, aes (x = month,
y = Densities,
group = patch,
colour = factor(patch)
)) +
geom_point(size = 0.0001) +
stat_smooth(se = FALSE) +
xlab("months (2002)")
my.year <- 2003
ccbug_my.year <- subset(ccbug1, year == my.year)
plot10 <- ggplot( ccbug_my.year, aes (x = month,
y = Densities,
group = patch,
colour = factor(patch)
)) +
geom_point(size = 0.0001) +
stat_smooth(se = FALSE) +
xlab("months (2003)")
my.year <- 2004
ccbug_my.year <- subset(ccbug1, year == my.year)
plot11 <- ggplot( ccbug_my.year, aes (x = month,
y = Densities,
group = patch,
colour = factor(patch)
)) +
geom_point(size = 0.0001) +
stat_smooth(se = FALSE) +
xlab("months (2004)")
my.year <- 2005
ccbug_my.year <- subset(ccbug1, year == my.year)
plot12 <- ggplot( ccbug_my.year, aes (x = month,
y = Densities,
group = patch,
colour = factor(patch)
)) +
geom_point(size = 0.0001) +
stat_smooth(se = FALSE) +
xlab("months (2005)")
my.year <- 2006
ccbug_my.year <- subset(ccbug1, year == my.year)
plot13 <- ggplot( ccbug_my.year, aes (x = month,
y = Densities,
group = patch,
colour = factor(patch)
)) +
geom_point(size = 0.0001) +
stat_smooth(se = FALSE) +
xlab("months (2006)")
my.year <- 2007
ccbug_my.year <- subset(ccbug1, year == my.year)
plot14 <- ggplot( ccbug_my.year, aes (x = month,
y = Densities,
group = patch,
colour = factor(patch)
)) +
geom_point(size = 0.0001) +
stat_smooth(se = FALSE) +
xlab("months (2007)")
my.year <- 2008
ccbug_my.year <- subset(ccbug1, year == my.year)
plot15 <- ggplot( ccbug_my.year, aes (x = month,
y = Densities,
group = patch,
colour = factor(patch)
)) +
geom_point(size = 0.0001) +
stat_smooth(se = FALSE) +
xlab("months (2008)")
my.year <- 2009
ccbug_my.year <- subset(ccbug1, year == my.year)
plot16 <- ggplot( ccbug_my.year, aes (x = month,
y = Densities,
group = patch,
colour = factor(patch)
)) +
geom_point(size = 0.0001) +
stat_smooth(se = FALSE) +
xlab("months (2009)")
####################Attempt to create a function that simplifies the plot making process
#i <- as.numeric()
#plot <- c()
#for( i in unique(ccbug1$year)){
##KATIE: TRY USING assign()
## as a test say: i <- 1; x1 <- 4; assign(paste0("x",i),7); x1
#plot[i] <- ggplot( subset(ccbug1, year == i), aes (x = month,
# y = Densities,
# group = patch,
# colour = factor(patch)
# )) +
# geom_point(size = 0.0001) +
# stat_smooth(se = FALSE)
# plot <- c(plot, plot[i])
#}
#plot
######################################################################################################
#Arranging the plots into one frame
grid.arrange(plot1, plot2, plot3, plot4, plot5, plot6, plot7, plot8, plot9, plot10,
plot11, plot12, plot13, plot14, plot15, plot16, ncol = 3)
#Metrics:
i <- as.numeric()
b <- as.numeric()
n <- c(unique(ccbug_my.year$patch))
x <- c()
y <- c()
for(i in n) {
b <- mean(ccbug_my.year$Densities[ccbug_my.year$patch == i])
c <- median(ccbug_my.year$Densities[ccbug_my.year$patch == i])
x <- c(x, b)
y <- c(y, c)
}
x
y
mean(x)
median(y)
#Looking at the graphs, one specific patch always seems to resemble the graph that we're looking for.
#Am not sure about what to do next! Should I be focussing on each patch or that one patch thats got a clear curve?
#Should I further divide each year into specific patches?
#Am I doing it right with the metrics?
##KATIE:
# The plots look great. Yes, you are calculating the metrics correctly, x ends up being a vector of all of the patch means for one year.
# It's good to keep an eye on the one patch that seems to have a clear pattern but lets stick with all of the patches for right now. Sometimes the visuals can be tricky and patterns are sometimes revealed only by the statistics.
# Its always good to take a step back and look at the bigger question. We are interested in the way the pattern of adult density (or a metric representing it in someway) is changing over time. So to do that we need a dataset of the metric for each patch in each year. Then we need to estimate the relationship of the metric ~ year, after accounting for random differences between patches.
# So we need a matrix (or data.frame) with a column for patch, one for year, and a column for each metric. So maybe instead of saving just b, you can save (c(i, b)) and loop through all the the ccbug_my.year data.frames? Or you could start with the large data set and have two nested loops, one for year, then one for patch? If you initialize a matrix before the loop (instead of c()) then you can use rbind() to add rows to it as you loop.
# After we make the data set we can use a mixed-effects regression (lmer in package lme4) to estimate the strength of the relationship of density and year