-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_codebase.R
More file actions
139 lines (107 loc) · 4.6 KB
/
simple_codebase.R
File metadata and controls
139 lines (107 loc) · 4.6 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
# Abbreviated Code Base For Cleaning Geo-Data
# Michael Fichman & Brad Mattan / Falk Lab / Geoscanning
# 3 / 1/ 2021
# Load and clean geotracking data
# Apply space-time functions and NYU stay events
# Load Retailers and join to observations
# Join observations to census tracts
# Load Libraries
library(tidyverse)
library(sf)
library(tigris)
library(tidycensus)
library(lubridate)
library(jsonlite)
library(devtools)
library(leaflet)
library(leaflet.providers)
library(leaflet.extras)
devtools::install_github("nyu-mhealth/Mobility")
library(Mobility)
# Install functions from R folder in the geoscanning repo
source("R/uploadGeodata.R")
source("R/cleanDates.R")
source("R/spaceTimeLags.R")
source("R/intakeRetailers.R")
source("R/bufferAndJoin.R")
source("R/joinTracts.R")
source("R/indirectMLM.R")
source("R/geotrackingLeaflet.R")
source("R/retailersLeaflet.R")
source("R/intakeSummary.R")
source("R/graphicsFunctions.R")
source("R/exposureLeaflet.R")
source("R/removeDuplicates.R")
# Upload data and add space/time indicators
# Specify the following parameters:
# 1. uploadGeodata - specify the filepath (default is our test data)
# 2. Add a "cleanDates" call for each user with a time window specifying beginning and end times
# using a YYYY-MM-DD HH:MM:SS format
# 3. Specify the stayevent parameters - dist.threshold in meters, time.units in minutes
# 4. Specify the time window for measuring radiusofgyration
# 5. Specify a coordinate system for spaceTimeLags in feet. Default is 2272 for South PA
# Output is an sf object
cleanData <- uploadGeodata("Data/Geotracking/multi_json_test") %>%
cleanDates(., "2019-04-22 21:30:45", "2019-08-05 15:17:47", "file1.json") %>% # parameter
cleanDates(., "2019-04-22 21:30:45", "2019-08-05 15:17:47", "file2.json") %>% # parameter
stayevent(.,
coor = c("lon","lat"),
time = "datetime",
dist.threshold = 100/3.28084, # conversion to feet PARAMETER
time.threshold = 5, # PARAMETER
time.units = "mins",
groupvar = "filename") %>%
mutate(rg_hr = radiusofgyration(.,
coor = c("lon","lat"),
time = "datetime",
time.units = "hour", # PARAMETER
groupvar = "filename")) %>%
spaceTimeLags(., 2272) %>%
intakeSummary(.)
# BM: Warning message from NYU function:
# `group_by_()` is deprecated as of dplyr 0.7.0.
# Please use `group_by()` instead.
# See vignette('programming') for more help
######
# Inspect Data Using A Leaflet map
# Parameters - dataSet, stayEvents
# If stayEvents is TRUE, the map outputs stay events
# otherwise it shows all geotracking observations
# A leaflet map will pop up in your R Studio Viewer
# Toggle the data by user in the app menu at top right
# BM: If you're working with more than 10,000 observations, downsample to ~10,000 before plotting.
geotrackingLeaflet(cleanData %>%
filter(filename == "file1.json") %>% # option to select subject(s) in advance
sample_n(., 10000),
stayEvents = FALSE)
#######
# Output workspace, shp, csv
# Specify a filepath outside the geoscanning repo (on your machine perhaps)
# to write out a workspace and/or csv and shapefile versions of your data
# save.image("your_file_path/fileName.RData")
# write.csv(cleanData %>% as.data.frame(), "yourfile_path/fileName.csv")
#######
# Load Retailers
# Parameters:
# 1. specify the location of the most recent retailer data set
retailers <- intakeRetailers("Data/Retailers/all_Retailers_10_20_20.csv")
# Visualize the retailers using a leaflet map
retailersLeaflet(retailers)
# Associate retailers and Census tract info to geotracking observations
# Parameters:
# bufferAndJoin takes four parameters:
# 1. a retailer database (retailers)
# 2. the geotracking data (here as `.`)
# 3. A crs (coordinate reference system) - keep default 2272 for Philadelphia area (linear unit - feet)
# 4. A buffer size in the linear units of the crs - (defaulted below to 100 feet)
cleanData_Retailers_Tracts <- cleanData %>%
bufferAndJoin(retailers, ., 2272, 100) %>%
joinTracts(.)
# exposureLeaflet visualizes geotracking observations by stay event (size)
# and by exposures under 30mph (color)
exposureLeaflet(cleanData_Retailers_Tracts)
# Output workspace, shp, csv
# Specify a filepath outside the geoscanning repo (on your machine perhaps)
# to write out a workspace and/or csv and shapefile versions of your data
# save.image("your_file_path/fileName.RData")
# write.csv(cleanData_Retailers_Tracts %>% as.data.frame(), "yourfile_path/fileName.csv")