-
Notifications
You must be signed in to change notification settings - Fork 6
/
README.Rmd
127 lines (93 loc) · 3.65 KB
/
README.Rmd
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
---
output:
html_document:
fig_caption: yes
keep_md: yes
---
# pct: Propensity to cycle tool
This repo contains miscellaneous R scipts, documentation and figures related to the Propensity to Cycle Tool.
This repo is not needed to run the PCT on your computer. For that, see [pct-load](https://github.com/npct/pct-load).
## Information on the PCT
The project is funded by the Department for Transport (DfT) so the initial
case studies will be taken from the UK. However, it is expected that the
methods will be of use elsewhere. For that reason, attempts have been made
to make the examples generalisable. All examples presented here
are reproducible using data stored in the [pct-data repository](https://github.com/npct/pct-data/).
## National results
```{r}
la_results = read.csv("../pct-shiny/regions_www/laresults.csv")
sum(la_results$bicycle) / sum(la_results$all) # current level of cycling
sum(la_results$dutch_slc) / sum(la_results$all) # go dutch level
```
## Cycle trip statistics
```{r}
l_nat = readRDS("../pct-bigdata/msoa/l_nat.Rds")
rf_nat = readRDS("../pct-bigdata/msoa/rf_nat.Rds")
l_nat$dist_fast = rf_nat$length / 1000
weighted.mean(l_nat$dist_fast, w = l_nat$bicycle)
weighted.mean(l_nat$dist_fast, w = l_nat$bicycle)
```
## A simple example
If you run the following lines of code on from a local copy of the [pct repository](https://github.com/npct/pct) you will get the same results.
```{r, message=FALSE}
source("set-up.R")
# load some flow data
fleeds <- read.csv("README_files/data/sample-leeds-centre-dists.csv")
# load the zones
leeds <- readOGR("README_files/data", "leeds-central-sample")
```
Now we can estimate propensity to cycle, by using the distance
decay function from [(Iacono et al. 2010)](http://linkinghub.elsevier.com/retrieve/pii/S0966692309000210):
$$
p = \alpha e^{- \beta d}
$$
where $\alpha$, the proportion of made for the shortest distances
and $\beta$, the rate of decay
are parameters to be calculated from empirical evidence.
To implement this understanding in R code we can use the following function:
```{r}
# Distance-dependent mode switch probs
iac <- function(x, a = 0.3, b = 0.2){
a * exp(-b * x)
}
```
Apply this function to openly accessible flow data:
```{r}
fleeds$p_cycle <- iac(fleeds$dist / 1000)
fleeds$n_cycle <- fleeds$p_cycle * fleeds$All.categories..Method.of.travel.to.work
fleeds$pc1 <- fleeds$n_cycle - fleeds$Bicycle
```
Now we can create a simple visualisation of the result:
```{r}
plot(leeds)
for(i in which(fleeds$Area.of.residence == leeds$geo_code[1])){
from <- leeds$geo_code %in% fleeds$Area.of.residence[i]
to <- leeds$geo_code %in% fleeds$Area.of.workplace[i]
x <- coordinates(leeds[from, ])
y <- coordinates(leeds[to, ])
lines(c(x[1], y[1]), c(x[2], y[2]), lwd = fleeds$pc1[i] )
}
```
## Set the `CS_API_KEY` Environment variable
Some of the examples pull data from the
[CycleStreets.net API](http://www.cyclestreets.net/api/).
Once you have a token, you can add it in Ubuntu as
a session variable using the following in your terminal
```{r, engine='bash', eval = FALSE}
echo "export CS_API_KEY='my_token'" >> ~/.profile
```
or system wide variable
```{r, engine='bash', eval = FALSE}
sudo echo "export CS_API_KEY='my_token'" > /etc/profile.d/cyclestreet.sh
```
## Set up rgdal
The version of gdal needs to be newer than 1.11
```{r}
rgdal::getGDALVersionInfo()
# Should return GDAL 1.11.2, released 2015/02/10 (or newer)
```
It is possible to use the following Personal Package Archive (PPA) to get the latest version of gdal on Ubuntu.
```{r, engine='bash', eval = FALSE}
sudo add-apt-repository ppa:ubuntugis/ubuntugis-unstable && sudo apt-get update
sudo apt-get install gdal-bin libgdal-dev
```