-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules.Rmd
More file actions
209 lines (176 loc) · 7.54 KB
/
rules.Rmd
File metadata and controls
209 lines (176 loc) · 7.54 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
201
202
203
204
205
206
207
208
209
---
title: "rules"
author: "Ilya"
date: "5/13/2018"
output: github_document
---
#####install packages
```{r}
#install package dplyr if not already installed
list.of.packages <- c("dplyr", "rgdal", "raster", "sp", "data.table", "reshape", "auk", "lubridate", "apriori", "arulesViz")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages, repos = "http://cran.us.r-project.org")
print(new.packages)
library(dplyr)
library(auk)
library(lubridate)
library(reshape)
library(arules)
library(data.table)
library(arulesViz)
```
#####read in shapefile of protected areas and reproject and save as PAD_wgs84.Rdata -- commenting out this part
```{r}
# library("rgdal")
# library("raster")
# library("sp")
#
# #source: https://gapanalysis.usgs.gov/padus/data/download/
# PAD <- shapefile("PADUS1_4Combined.shp")
# #change datum
# PAD_wgs84 <- spTransform(PAD, CRS("+proj=longlat +datum=WGS84"))
#
# #assign cooordinates (package: sp)
# PAD_coordinates = coordinates(PAD_wgs84)
# print(proc.time() - ptm)
#
# PAD_wgs84$X = PAD_coordinates[,1]
# PAD_wgs84$Y = PAD_coordinates[,2]
#
# save(PAD_wgs84, file = "PAD_wgs84.Rdata")
```
#####read in iNaturalist data and assign it to parks -- commenting out this part
```{r}
# rm(list = ls())
# library("raster")
# library("data.table")
# load("PAD_wgs84.Rdata")
#
# #occurrence download available here:
# #https://www.gbif.org/occurrence/download/0016851-180131172636756
# inat = fread("occurrence.txt",blank.lines.skip=TRUE)
# inat = as.data.frame(inat)
# inat$longitude <- inat$decimalLongitude
# inat$latitude <- inat$decimalLatitude
# inat.ok= filter(inat, !is.na(decimalLatitude) & !is.na(decimalLongitude))
#
# #turn it into a SpatialPointsDataFrame
# coordinates(inat.ok) <- c("decimalLongitude","decimalLatitude")#package sp
#
# save(inat.ok,file = "inat.ok.Rdata")
#
# # tell R that inat coordinates are in the same lat/lon reference system
# # as the parks data
# projection(inat.ok) <- projection(PAD_wgs84)
#
# # use 'over' with parks as a SpatialPolygonsDataFrame
# # object, to determine which park (if any) contains each sighting, and
# # store the park name as an attribute of the data
# inat.ok$park <- over(inat.ok, PAD_wgs84)$Unit_Nm#name of park
# inat.ok$X.park <- over(inat.ok, PAD_wgs84)$X
# inat.ok$Y.park <- over(inat.ok, PAD_wgs84)$Y
# inat.ok$park.acres <- over(inat.ok, PAD_wgs84)$GIS_Acres
#
# save(inat.ok, file = "inat.ok.Rdata")
```
#####make small version of data -- comment out this part
```{r}
# load("inat.ok.Rdata")
# inat.ok$date = as.Date(paste(inat.ok$year, inat.ok$month, inat.ok$day, sep='-'))
# inat.ok.sm = inat.ok[, c("park", "recordedBy", "species", "date", "X.park", "Y.park")]
# save(inat.ok.sm, file = "inat.ok.sm.Rdata")
```
#####subset data for NYC
```{r}
load("inat.ok.sm.Rdata")
inat.nyc = subset(inat.ok.sm,X.park >=-74.2589 & X.park< -73.7004 & Y.park <=40.9176 & Y.park>=40.4774)
write.csv(inat.nyc, file = "inat.nyc.csv")
```
#####find association rules for rats in NYC; plot strongly supported associations
```{r}
inat.nyc = read.csv("inat.nyc.csv")
inat.ok = inat.nyc
inat.df = as.data.frame(inat.ok)
#get count by park, date, and species for all data
inat.ct <- inat.df %>%
group_by(park, date, species) %>%
summarize(count = n())
inat.ct$count = 1#make all counts 1
inat.ct = data.frame(inat.ct)
inat.ct$park_date_recordedBy = factor(paste(inat.ct$park, inat.ct$date, inat.ct$recordedBy, sep = "_"))
inat.ct$park_date = factor(paste(inat.ct$park, inat.ct$date, sep = "_"))
i.df = inat.ct[, c("park_date_recordedBy", "park_date", "species", "count")]
RaNo = subset(i.df, species == "Rattus norvegicus")
dim(RaNo)#only seen 5 times
#write and then read back in again as transaction
write.csv(i.df, file = "i.df.csv")
itrans = read.transactions("i.df.csv", format = "single", sep = ",", cols = c("park_date_recordedBy", "species"))
#find all rules involving Norway rats
irulesRaNo <- apriori(itrans, parameter = list(support = 0.0001, confidence = 0.2, maxlen=3), appearance = list(rhs="Rattus norvegicus"))#
subrulesRaNo <- head(irulesRaNo, n = 5, by = "lift")
plot(subrulesRaNo, method = "graph", main = "Norway rat, NYC, N=5 rules")
inspect(subrulesRaNo)
#find all rules for black-capped chickadees:
irulesBlCa <- apriori(itrans, parameter = list(support = 0.0001, confidence = 0.2, maxlen=3), appearance = list(rhs="Poecile atricapillus"))#
subrulesBlCa <- head(irulesBlCa, n = 5, by = "lift")
plot(subrulesBlCa, method = "graph", main = "Black-capped Chickadee, NYC, N=5 rules")
inspect(subrulesBlCa)
```
#####find association rules across all iNaturalist data
```{r}
load("inat.ok.sm.Rdata")
inat.df = inat.ok.sm
inat.df = data.frame(inat.df)
#get count by park, date, and species for all data
inat.ct <- inat.df %>%
group_by(park, date, species) %>%
summarize(count = n())
inat.ct$count = 1#make all counts 1
inat.ct = data.frame(inat.ct)
inat.ct$park_date_recordedBy = factor(paste(inat.ct$park, inat.ct$date, inat.ct$recordedBy, sep = "_"))
inat.ct$park_date = factor(paste(inat.ct$park, inat.ct$date, sep = "_"))
i.df = inat.ct[, c("park_date_recordedBy", "park_date", "species", "count")]
#write and then read back in again as transaction
write.csv(i.df, file = "i.df.csv")
itrans = read.transactions("i.df.csv", format = "single", sep = ",", cols = c("park_date_recordedBy", "species"))
#find all rat observations
rat=subset(i.df, species == "Rattus norvegicus")
#find all rules involving Norway rats
irulesRaNo <- apriori(itrans, parameter = list(support = 0.0001, confidence = 0.05, maxlen=3), appearance = list(rhs="Rattus norvegicus"))#
subrulesRaNo <- head(irulesRaNo, n = 5, by = "lift")
plot(subrulesRaNo, method = "graph", main = "rats, all data, maxlen=3")
print("Norway rat rules")
inspect(subrulesRaNo)
#find all rules for black-capped chickadees:
irulesBlCa <- apriori(itrans, parameter = list(support = 0.0001, confidence = 0.1, maxlen=3), appearance = list(rhs="Poecile atricapillus"))#
subrulesBlCa <- head(irulesBlCa, n = 5, by = "lift")
plot(subrulesBlCa, method = "graph", main = "black-capped chickadees, all data, maxlen=3")
print("Black-capped Chickadee rules")
inspect(subrulesBlCa)
#find all rules, include maxlen of 3
irules <- apriori(itrans, parameter = list(support = 0.001, confidence = 0.1, maxlen=3))#
subrules <- head(irules, n = 10, by = "lift")
plot(subrules, method = "graph", main = "all data, maxlen=3")
```
#####read in basic (not reference) ebird data
```{r}
rm(list = ls())
input_file = "/Users/fischhoff/app_wild/bird_basic_NY_NY/ebd_US-NY-061_relFeb-2018.txt"
output_file = "/Users/fischhoff/app_wild/bird_basic_NY_NY/ebd_US-NY-061_relFeb-2018clean.txt"
auk_clean(input_file, output_file, overwrite=TRUE)
ebd_NYNY = read_ebd(output_file)
```
#####find association rules in eBird NYC data
```{r}
write.csv(ebd_NYNY, file = "ebd_NYNY.csv")
etrans = read.transactions("ebd_NYNY.csv", format = "single", sep = ",", cols = c("checklist_id", "common_name"))
erulesBlCa <- apriori(etrans, parameter = list(support = 0.001, confidence = 0.6, maxlen=3), appearance = list(rhs="Black-capped Chickadee"))#could re-run with
subrulesBlCa <- head(erulesBlCa, n = 5, by = "lift")
plot(subrulesBlCa, method = "graph", main = "black-capped chickadee, eBird NYC, N=10 rules, maxlen=3")
inspect(subrulesBlCa)
erules <- apriori(etrans, parameter = list(support = 0.01, confidence = 0.6, maxlen=3))#could re-run with
subrules <- head(erules, n = 10, by = "lift")
plot(subrules, method = "graph", main = "all species, eBird NYC, N=10 rules, maxlen=3")
plot = plot(subrules, method = "grouped")
inspect(subrules)
```